1
22
23 package com.liferay.portlet.taggedcontent.action;
24
25 import com.liferay.portal.kernel.portlet.ConfigurationAction;
26 import com.liferay.portal.kernel.servlet.SessionErrors;
27 import com.liferay.portal.kernel.servlet.SessionMessages;
28 import com.liferay.portal.kernel.util.Constants;
29 import com.liferay.portal.kernel.util.ParamUtil;
30 import com.liferay.portal.kernel.util.StringUtil;
31 import com.liferay.portal.theme.ThemeDisplay;
32 import com.liferay.portal.util.WebKeys;
33 import com.liferay.portlet.PortletPreferencesFactoryUtil;
34 import com.liferay.portlet.taggedcontent.util.AssetPublisherUtil;
35 import com.liferay.portlet.tags.TagsEntryException;
36 import com.liferay.portlet.tags.service.TagsEntryLocalServiceUtil;
37
38 import javax.portlet.ActionRequest;
39 import javax.portlet.ActionResponse;
40 import javax.portlet.PortletConfig;
41 import javax.portlet.PortletPreferences;
42 import javax.portlet.RenderRequest;
43 import javax.portlet.RenderResponse;
44
45
51 public class ConfigurationActionImpl implements ConfigurationAction {
52
53 public void processAction(
54 PortletConfig portletConfig, ActionRequest actionRequest,
55 ActionResponse actionResponse)
56 throws Exception {
57
58 String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
59
60 try {
61 String portletResource = ParamUtil.getString(
62 actionRequest, "portletResource");
63
64 PortletPreferences prefs =
65 PortletPreferencesFactoryUtil.getPortletSetup(
66 actionRequest, portletResource);
67
68 if (cmd.equals("add-selection")) {
69 AssetPublisherUtil.addSelection(actionRequest, prefs);
70 }
71 else if (cmd.equals("move-selection-down")) {
72 moveSelectionDown(actionRequest, prefs);
73 }
74 else if (cmd.equals("move-selection-up")) {
75 moveSelectionUp(actionRequest, prefs);
76 }
77 else if (cmd.equals("remove-selection")) {
78 removeSelection(actionRequest, prefs);
79 }
80 else if (cmd.equals("selection-style")) {
81 setSelectionStyle(actionRequest, prefs);
82 }
83 else if (cmd.equals(Constants.UPDATE)) {
84 String selectionStyle = prefs.getValue(
85 "selection-style", "dynamic");
86
87 if (selectionStyle.equals("dynamic")) {
88 updateDynamicSettings(actionRequest, prefs);
89 }
90 else if (selectionStyle.equals("manual")) {
91 updateManualSettings(actionRequest, prefs);
92 }
93 }
94
95 if (SessionErrors.isEmpty(actionRequest)) {
96 prefs.store();
97
98 SessionMessages.add(
99 actionRequest,
100 portletConfig.getPortletName() + ".doConfigure");
101 }
102 }
103 catch (Exception e) {
104 if (e instanceof TagsEntryException) {
105 SessionErrors.add(actionRequest, e.getClass().getName(), e);
106 }
107 else {
108 throw e;
109 }
110 }
111 }
112
113 public String render(
114 PortletConfig portletConfig, RenderRequest renderRequest,
115 RenderResponse renderResponse)
116 throws Exception {
117
118 return "/html/portlet/tagged_content/configuration.jsp";
119 }
120
121 protected void moveSelectionDown(
122 ActionRequest actionRequest, PortletPreferences prefs)
123 throws Exception {
124
125 int assetOrder = ParamUtil.getInteger(actionRequest, "assetOrder");
126
127 String[] manualEntries = prefs.getValues(
128 "manual-entries", new String[0]);
129
130 if ((assetOrder >= (manualEntries.length - 1)) || (assetOrder < 0)) {
131 return;
132 }
133
134 String temp = manualEntries[assetOrder + 1];
135
136 manualEntries[assetOrder + 1] = manualEntries[assetOrder];
137 manualEntries[assetOrder] = temp;
138
139 prefs.setValues("manual-entries", manualEntries);
140 }
141
142 protected void moveSelectionUp(
143 ActionRequest actionRequest, PortletPreferences prefs)
144 throws Exception {
145
146 int assetOrder = ParamUtil.getInteger(actionRequest, "assetOrder");
147
148 String[] manualEntries = prefs.getValues(
149 "manual-entries", new String[0]);
150
151 if ((assetOrder >= manualEntries.length) || (assetOrder <= 0)) {
152 return;
153 }
154
155 String temp = manualEntries[assetOrder - 1];
156
157 manualEntries[assetOrder - 1] = manualEntries[assetOrder];
158 manualEntries[assetOrder] = temp;
159
160 prefs.setValues("manual-entries", manualEntries);
161 }
162
163 protected void removeSelection(
164 ActionRequest actionRequest, PortletPreferences prefs)
165 throws Exception {
166
167 int assetOrder = ParamUtil.getInteger(actionRequest, "assetOrder");
168
169 String[] manualEntries = prefs.getValues(
170 "manual-entries", new String[0]);
171
172 if (assetOrder >= manualEntries.length) {
173 return;
174 }
175
176 String[] newEntries = new String[manualEntries.length -1];
177
178 int i = 0;
179 int j = 0;
180
181 for (; i < manualEntries.length; i++) {
182 if (i != assetOrder) {
183 newEntries[j++] = manualEntries[i];
184 }
185 }
186
187 prefs.setValues("manual-entries", newEntries);
188 }
189
190 protected void setSelectionStyle(
191 ActionRequest actionRequest, PortletPreferences prefs)
192 throws Exception {
193
194 String selectionStyle = ParamUtil.getString(
195 actionRequest, "selectionStyle");
196 String displayStyle = ParamUtil.getString(
197 actionRequest, "displayStyle");
198
199 prefs.setValue("selection-style", selectionStyle);
200
201 if (selectionStyle.equals("manual") ||
202 selectionStyle.equals("view-count")) {
203
204 prefs.setValue("show-query-logic", String.valueOf(false));
205 }
206
207 if (!selectionStyle.equals("view-count") &&
208 displayStyle.equals("view-count-details")) {
209
210 prefs.setValue("display-style", "full-content");
211 }
212 }
213
214 protected void updateDynamicSettings(
215 ActionRequest actionRequest, PortletPreferences prefs)
216 throws Exception {
217
218 ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
219 WebKeys.THEME_DISPLAY);
220
221 long userId = themeDisplay.getUserId();
222
223 String[] entries = StringUtil.split(
224 ParamUtil.getString(actionRequest, "entries"));
225 String[] notEntries = StringUtil.split(
226 ParamUtil.getString(actionRequest, "notEntries"));
227 boolean mergeUrlTags = ParamUtil.getBoolean(
228 actionRequest, "mergeUrlTags");
229 boolean andOperator = ParamUtil.getBoolean(
230 actionRequest, "andOperator");
231
232 long classNameId = ParamUtil.getLong(actionRequest, "classNameId");
233 String category = ParamUtil.getString(actionRequest, "category");
234 String displayStyle = ParamUtil.getString(
235 actionRequest, "displayStyle");
236 String orderByColumn1 = ParamUtil.getString(
237 actionRequest, "orderByColumn1");
238 String orderByColumn2 = ParamUtil.getString(
239 actionRequest, "orderByColumn2");
240 String orderByType1 = ParamUtil.getString(
241 actionRequest, "orderByType1");
242 String orderByType2 = ParamUtil.getString(
243 actionRequest, "orderByType2");
244 boolean excludeZeroViewCount = ParamUtil.getBoolean(
245 actionRequest, "excludeZeroViewCount");
246 boolean showQueryLogic = ParamUtil.getBoolean(
247 actionRequest, "showQueryLogic");
248 int delta = ParamUtil.getInteger(actionRequest, "delta");
249 String paginationType = ParamUtil.getString(
250 actionRequest, "paginationType");
251 boolean showAvailableLocales = ParamUtil.getBoolean(
252 actionRequest, "showAvailableLocales");
253 boolean enableComments = ParamUtil.getBoolean(
254 actionRequest, "enableComments");
255 boolean enableCommentRatings = ParamUtil.getBoolean(
256 actionRequest, "enableCommentRatings");
257 boolean enableRatings = ParamUtil.getBoolean(
258 actionRequest, "enableRatings");
259 String medatadaFields = ParamUtil.getString(
260 actionRequest, "metadataFields");
261
262 prefs.setValues("entries", entries);
263 prefs.setValues("not-entries", notEntries);
264 prefs.setValue("merge-url-tags", String.valueOf(mergeUrlTags));
265 prefs.setValue("and-operator", String.valueOf(andOperator));
266
267 prefs.setValue("class-name-id", String.valueOf(classNameId));
268 prefs.setValue("category", category);
269 prefs.setValue("display-style", displayStyle);
270 prefs.setValue("order-by-column-1", orderByColumn1);
271 prefs.setValue("order-by-column-2", orderByColumn2);
272 prefs.setValue("order-by-type-1", orderByType1);
273 prefs.setValue("order-by-type-2", orderByType2);
274 prefs.setValue(
275 "exclude-zero-view-count", String.valueOf(excludeZeroViewCount));
276 prefs.setValue("show-query-logic", String.valueOf(showQueryLogic));
277 prefs.setValue("delta", String.valueOf(delta));
278 prefs.setValue("pagination-type", paginationType);
279 prefs.setValue(
280 "show-available-locales", String.valueOf(showAvailableLocales));
281 prefs.setValue("enable-ratings", String.valueOf(enableRatings));
282 prefs.setValue("enable-comments", String.valueOf(enableComments));
283 prefs.setValue(
284 "enable-comment-ratings", String.valueOf(enableCommentRatings));
285 prefs.setValue("metadata-fields", medatadaFields);
286
287 TagsEntryLocalServiceUtil.checkEntries(userId, entries);
288 TagsEntryLocalServiceUtil.checkEntries(userId, notEntries);
289 }
290
291 protected void updateManualSettings(
292 ActionRequest actionRequest, PortletPreferences prefs)
293 throws Exception {
294
295 String displayStyle = ParamUtil.getString(
296 actionRequest, "displayStyle");
297 boolean showAvailableLocales = ParamUtil.getBoolean(
298 actionRequest, "showAvailableLocales");
299 boolean enableComments = ParamUtil.getBoolean(
300 actionRequest, "enableComments");
301 boolean enableCommentRatings = ParamUtil.getBoolean(
302 actionRequest, "enableCommentRatings");
303 boolean enableRatings = ParamUtil.getBoolean(
304 actionRequest, "enableRatings");
305
306 prefs.setValue("display-style", displayStyle);
307 prefs.setValue(
308 "show-available-locales", String.valueOf(showAvailableLocales));
309 prefs.setValue("enable-comments", String.valueOf(enableComments));
310 prefs.setValue(
311 "enable-comment-ratings", String.valueOf(enableCommentRatings));
312 prefs.setValue("enable-ratings", String.valueOf(enableRatings));
313 }
314
315 }