001
014
015 package com.liferay.portlet.socialactivity.action;
016
017 import com.liferay.portal.kernel.json.JSONArray;
018 import com.liferay.portal.kernel.json.JSONFactoryUtil;
019 import com.liferay.portal.kernel.json.JSONObject;
020 import com.liferay.portal.kernel.util.Constants;
021 import com.liferay.portal.kernel.util.GetterUtil;
022 import com.liferay.portal.kernel.util.ParamUtil;
023 import com.liferay.portal.security.permission.comparator.ModelResourceComparator;
024 import com.liferay.portal.struts.PortletAction;
025 import com.liferay.portal.theme.ThemeDisplay;
026 import com.liferay.portal.util.WebKeys;
027 import com.liferay.portlet.social.model.SocialActivityCounterConstants;
028 import com.liferay.portlet.social.model.SocialActivityCounterDefinition;
029 import com.liferay.portlet.social.model.SocialActivityDefinition;
030 import com.liferay.portlet.social.model.SocialActivitySetting;
031 import com.liferay.portlet.social.service.SocialActivitySettingLocalServiceUtil;
032 import com.liferay.portlet.social.service.SocialActivitySettingServiceUtil;
033 import com.liferay.portlet.social.util.SocialConfigurationUtil;
034
035 import java.util.ArrayList;
036 import java.util.Arrays;
037 import java.util.Comparator;
038 import java.util.LinkedHashMap;
039 import java.util.List;
040 import java.util.Map;
041
042 import javax.portlet.ActionRequest;
043 import javax.portlet.ActionResponse;
044 import javax.portlet.PortletConfig;
045 import javax.portlet.RenderRequest;
046 import javax.portlet.RenderResponse;
047
048 import org.apache.struts.action.ActionForm;
049 import org.apache.struts.action.ActionForward;
050 import org.apache.struts.action.ActionMapping;
051
052
055 public class ViewAction extends PortletAction {
056
057 @Override
058 public void processAction(
059 ActionMapping actionMapping, ActionForm actionForm,
060 PortletConfig portletConfig, ActionRequest actionRequest,
061 ActionResponse actionResponse)
062 throws Exception {
063
064 String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
065
066 if (cmd.equals(Constants.UPDATE)) {
067 updateActivitySettings(actionRequest);
068 }
069
070 sendRedirect(actionRequest, actionResponse);
071 }
072
073 @Override
074 public ActionForward render(
075 ActionMapping actionMapping, ActionForm actionForm,
076 PortletConfig portletConfig, RenderRequest renderRequest,
077 RenderResponse renderResponse)
078 throws Exception {
079
080 ThemeDisplay themeDisplay = (ThemeDisplay)renderRequest.getAttribute(
081 WebKeys.THEME_DISPLAY);
082
083 Map<String, Boolean> activitySettingsMap =
084 new LinkedHashMap<String, Boolean>();
085
086 List<SocialActivitySetting> activitySettings =
087 SocialActivitySettingLocalServiceUtil.getActivitySettings(
088 themeDisplay.getScopeGroupIdOrLiveGroupId());
089
090 String[] modelNames = SocialConfigurationUtil.getActivityModelNames();
091
092 Comparator<String> comparator = new ModelResourceComparator(
093 themeDisplay.getLocale());
094
095 Arrays.sort(modelNames, comparator);
096
097 for (String modelName : modelNames) {
098 activitySettingsMap.put(modelName, false);
099 }
100
101 for (SocialActivitySetting activitySetting : activitySettings) {
102 String name = activitySetting.getName();
103
104 if (name.equals("enabled") &&
105 activitySettingsMap.containsKey(
106 activitySetting.getClassName())) {
107
108 activitySettingsMap.put(
109 activitySetting.getClassName(),
110 GetterUtil.getBoolean(activitySetting.getValue()));
111 }
112 }
113
114 renderRequest.setAttribute(
115 WebKeys.SOCIAL_ACTIVITY_SETTINGS_MAP, activitySettingsMap);
116
117 return actionMapping.findForward("portlet.social_activity.view");
118 }
119
120 protected SocialActivityCounterDefinition updateActivityCounterDefinition(
121 JSONObject actionJSONObject,
122 SocialActivityDefinition activityDefinition,
123 String activityCounterName) {
124
125 SocialActivityCounterDefinition activityCounterDefinition =
126 activityDefinition.getActivityCounterDefinition(
127 activityCounterName);
128
129 if (activityCounterDefinition == null) {
130 activityCounterDefinition = new SocialActivityCounterDefinition();
131
132 activityCounterDefinition.setName(activityCounterName);
133 }
134
135 if (activityCounterName.equals(
136 SocialActivityCounterConstants.NAME_CONTRIBUTION)) {
137
138 activityCounterDefinition.setOwnerType(
139 SocialActivityCounterConstants.TYPE_CREATOR);
140 }
141 else if (activityCounterName.equals(
142 SocialActivityCounterConstants.NAME_PARTICIPATION)) {
143
144 activityCounterDefinition.setOwnerType(
145 SocialActivityCounterConstants.TYPE_ACTOR);
146 }
147 else if (activityCounterName.equals(
148 SocialActivityCounterConstants.NAME_POPULARITY)) {
149
150 activityCounterDefinition.setOwnerType(
151 SocialActivityCounterConstants.TYPE_ASSET);
152
153 activityCounterName =
154 SocialActivityCounterConstants.NAME_CONTRIBUTION;
155 }
156 else {
157 throw new IllegalArgumentException();
158 }
159
160 activityCounterDefinition.setEnabled(
161 actionJSONObject.getBoolean("active"));
162 activityCounterDefinition.setIncrement(
163 actionJSONObject.getInt(activityCounterName + "Increment"));
164 activityCounterDefinition.setLimitPeriod(
165 actionJSONObject.getInt(activityCounterName + "LimitPeriod"));
166 activityCounterDefinition.setLimitValue(
167 actionJSONObject.getInt(activityCounterName + "LimitValue"));
168
169 return activityCounterDefinition;
170 }
171
172 protected void updateActivitySettings(ActionRequest actionRequest)
173 throws Exception {
174
175 ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
176 WebKeys.THEME_DISPLAY);
177
178 String settingsJSON = ParamUtil.getString(
179 actionRequest, "settingsJSON");
180
181 JSONObject settingsJSONObject = JSONFactoryUtil.createJSONObject(
182 settingsJSON);
183
184 JSONArray actionsJSONArray = settingsJSONObject.getJSONArray("actions");
185
186 String modelName = settingsJSONObject.getString("modelName");
187
188 for (int i = 0; i < actionsJSONArray.length(); i++) {
189 JSONObject actionJSONObject = actionsJSONArray.getJSONObject(i);
190
191 int activityType = actionJSONObject.getInt("activityType");
192
193 SocialActivityDefinition activityDefinition =
194 SocialActivitySettingLocalServiceUtil.getActivityDefinition(
195 themeDisplay.getScopeGroupIdOrLiveGroupId(), modelName,
196 activityType);
197
198 if (activityDefinition == null) {
199 continue;
200 }
201
202 List<SocialActivityCounterDefinition> activityCounterDefinitions =
203 new ArrayList<SocialActivityCounterDefinition>();
204
205 activityCounterDefinitions.add(
206 updateActivityCounterDefinition(
207 actionJSONObject, activityDefinition,
208 SocialActivityCounterConstants.NAME_CONTRIBUTION));
209
210 activityCounterDefinitions.add(
211 updateActivityCounterDefinition(
212 actionJSONObject, activityDefinition,
213 SocialActivityCounterConstants.NAME_PARTICIPATION));
214
215 activityCounterDefinitions.add(
216 updateActivityCounterDefinition(
217 actionJSONObject, activityDefinition,
218 SocialActivityCounterConstants.NAME_POPULARITY));
219
220 SocialActivitySettingServiceUtil.updateActivitySettings(
221 themeDisplay.getScopeGroupIdOrLiveGroupId(), modelName,
222 activityType, activityCounterDefinitions);
223 }
224 }
225
226 }