001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.socialequityadmin.action;
016    
017    import com.liferay.portal.kernel.bean.BeanPropertiesUtil;
018    import com.liferay.portal.kernel.util.ParamUtil;
019    import com.liferay.portal.struts.PortletAction;
020    import com.liferay.portal.theme.ThemeDisplay;
021    import com.liferay.portal.util.PortalUtil;
022    import com.liferay.portal.util.WebKeys;
023    import com.liferay.portlet.social.model.SocialEquityActionMapping;
024    import com.liferay.portlet.social.model.SocialEquitySetting;
025    import com.liferay.portlet.social.model.SocialEquitySettingConstants;
026    import com.liferay.portlet.social.service.SocialEquitySettingLocalServiceUtil;
027    
028    import java.util.ArrayList;
029    import java.util.HashMap;
030    import java.util.List;
031    import java.util.Map;
032    
033    import javax.portlet.ActionRequest;
034    import javax.portlet.ActionResponse;
035    import javax.portlet.PortletConfig;
036    import javax.portlet.RenderRequest;
037    import javax.portlet.RenderResponse;
038    
039    import org.apache.struts.action.ActionForm;
040    import org.apache.struts.action.ActionForward;
041    import org.apache.struts.action.ActionMapping;
042    
043    /**
044     * @author Zsolt Balogh
045     * @author Zsolt Berentey
046     * @author Brian Wing Shun Chan
047     */
048    public class ViewAction extends PortletAction {
049    
050            public void processAction(
051                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
052                            ActionRequest actionRequest, ActionResponse actionResponse)
053                    throws Exception {
054    
055                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
056                            WebKeys.THEME_DISPLAY);
057    
058                    String[] classNames = PortalUtil.getSocialEquityClassNames();
059    
060                    for (String className : classNames) {
061                            List<SocialEquityActionMapping> mergedEquityActionMappings =
062                                    getMergedEquityActionMappings(actionRequest, className);
063    
064                            SocialEquitySettingLocalServiceUtil.updateSocialEquitySettings(
065                                    themeDisplay.getScopeGroupId(), className,
066                                    mergedEquityActionMappings);
067                    }
068    
069                    sendRedirect(actionRequest, actionResponse);
070            }
071    
072            public ActionForward render(
073                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
074                            RenderRequest renderRequest, RenderResponse renderResponse)
075                    throws Exception {
076    
077                    ThemeDisplay themeDisplay = (ThemeDisplay)renderRequest.getAttribute(
078                            WebKeys.THEME_DISPLAY);
079    
080                    Map<String, List<SocialEquityActionMapping>> equityActionMappingsMap =
081                            new HashMap<String, List<SocialEquityActionMapping>>();
082    
083                    String[] classNames = PortalUtil.getSocialEquityClassNames();
084    
085                    for (String className : classNames) {
086                            List<SocialEquityActionMapping> mergedEquityActionMappings =
087                                    getMergedEquityActionMappings(
088                                            themeDisplay.getScopeGroupId(), className);
089    
090                            equityActionMappingsMap.put(className, mergedEquityActionMappings);
091                    }
092    
093                    renderRequest.setAttribute(
094                            WebKeys.SOCIAL_EQUITY_ACTION_MAPPINGS_MAP, equityActionMappingsMap);
095    
096                    return mapping.findForward("portlet.social_equity_admin.view");
097            }
098    
099            protected SocialEquityActionMapping getMergedEquityActionMapping(
100                            ActionRequest actionRequest,
101                            SocialEquityActionMapping equityActionMapping)
102                    throws Exception {
103    
104                    SocialEquityActionMapping mergedMapping = equityActionMapping.clone();
105    
106                    updateModel(actionRequest, mergedMapping, "informationDailyLimit");
107                    updateModel(actionRequest, mergedMapping, "informationLifespan");
108                    updateModel(actionRequest, mergedMapping, "informationValue");
109                    updateModel(actionRequest, mergedMapping, "participationDailyLimit");
110                    updateModel(actionRequest, mergedMapping, "participationLifespan");
111                    updateModel(actionRequest, mergedMapping, "participationValue");
112    
113                    return mergedMapping;
114            }
115    
116            protected SocialEquityActionMapping getMergedEquityActionMapping(
117                            long groupId, SocialEquityActionMapping equityActionMapping)
118                    throws Exception {
119    
120                    SocialEquityActionMapping mergedEquityActionMapping =
121                            equityActionMapping.clone();
122    
123                    List<SocialEquitySetting> equitySettings =
124                            SocialEquitySettingLocalServiceUtil.getEquitySettings(
125                                    groupId, equityActionMapping.getClassName(),
126                                    equityActionMapping.getActionId());
127    
128                    for (SocialEquitySetting equitySetting : equitySettings) {
129                            if (equitySetting.getType() ==
130                                            SocialEquitySettingConstants.TYPE_INFORMATION) {
131    
132                                    mergedEquityActionMapping.setInformationDailyLimit(
133                                            equitySetting.getDailyLimit());
134                                    mergedEquityActionMapping.setInformationLifespan(
135                                            equitySetting.getLifespan());
136                                    mergedEquityActionMapping.setInformationValue(
137                                            equitySetting.getValue());
138                            }
139                            else {
140                                    mergedEquityActionMapping.setParticipationDailyLimit(
141                                            equitySetting.getDailyLimit());
142                                    mergedEquityActionMapping.setParticipationLifespan(
143                                            equitySetting.getLifespan());
144                                    mergedEquityActionMapping.setParticipationValue(
145                                            equitySetting.getValue());
146                            }
147                    }
148    
149                    return mergedEquityActionMapping;
150            }
151    
152            protected List<SocialEquityActionMapping> getMergedEquityActionMappings(
153                            ActionRequest actionRequest, String className)
154                    throws Exception {
155    
156                    List<SocialEquityActionMapping> mergedEquityActionMappings =
157                            new ArrayList<SocialEquityActionMapping>();
158    
159                    List<SocialEquityActionMapping> equityActionMappings =
160                            PortalUtil.getSocialEquityActionMappings(className);
161    
162                    for (SocialEquityActionMapping equityActionMapping :
163                                    equityActionMappings) {
164    
165                            SocialEquityActionMapping mergedEquityActionMapping =
166                                    getMergedEquityActionMapping(
167                                            actionRequest, equityActionMapping);
168    
169                            mergedEquityActionMappings.add(mergedEquityActionMapping);
170                    }
171    
172                    return mergedEquityActionMappings;
173            }
174    
175            protected List<SocialEquityActionMapping> getMergedEquityActionMappings(
176                            long groupId, String className)
177                    throws Exception {
178    
179                    List<SocialEquityActionMapping> mergedEquityActionMappings =
180                            new ArrayList<SocialEquityActionMapping>();
181    
182                    List<SocialEquityActionMapping> equityActionMappings =
183                            PortalUtil.getSocialEquityActionMappings(className);
184    
185                    for (SocialEquityActionMapping equityActionMapping :
186                                    equityActionMappings) {
187    
188                            SocialEquityActionMapping mergedEquityActionMapping =
189                                    getMergedEquityActionMapping(groupId, equityActionMapping);
190    
191                            mergedEquityActionMappings.add(mergedEquityActionMapping);
192                    }
193    
194                    return mergedEquityActionMappings;
195            }
196    
197            protected void updateModel(
198                            ActionRequest actionRequest,
199                            SocialEquityActionMapping equityActionMapping, String param)
200                    throws Exception {
201    
202                    String className = equityActionMapping.getClassName();
203    
204                    int value = ParamUtil.getInteger(
205                            actionRequest,
206                            className + "." + equityActionMapping.getActionId() + "." + param,
207                            -1);
208    
209                    if (value >= 0) {
210                            BeanPropertiesUtil.setProperty(equityActionMapping, param, value);
211                    }
212            }
213    
214    }