1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.assetpublisher.action;
16  
17  import com.liferay.portal.kernel.portlet.BaseConfigurationAction;
18  import com.liferay.portal.kernel.servlet.SessionErrors;
19  import com.liferay.portal.kernel.servlet.SessionMessages;
20  import com.liferay.portal.kernel.util.ArrayUtil;
21  import com.liferay.portal.kernel.util.Constants;
22  import com.liferay.portal.kernel.util.ParamUtil;
23  import com.liferay.portal.kernel.util.StringPool;
24  import com.liferay.portal.kernel.util.StringUtil;
25  import com.liferay.portal.theme.ThemeDisplay;
26  import com.liferay.portal.util.WebKeys;
27  import com.liferay.portlet.PortletPreferencesFactoryUtil;
28  import com.liferay.portlet.asset.AssetTagException;
29  import com.liferay.portlet.asset.service.AssetTagLocalServiceUtil;
30  import com.liferay.portlet.assetpublisher.util.AssetPublisherUtil;
31  
32  import javax.portlet.ActionRequest;
33  import javax.portlet.ActionResponse;
34  import javax.portlet.PortletConfig;
35  import javax.portlet.PortletPreferences;
36  import javax.portlet.RenderRequest;
37  import javax.portlet.RenderResponse;
38  
39  /**
40   * <a href="ConfigurationActionImpl.java.html"><b><i>View Source</i></b></a>
41   *
42   * @author Brian Wing Shun Chan
43   */
44  public class ConfigurationActionImpl extends BaseConfigurationAction {
45  
46      public void processAction(
47              PortletConfig portletConfig, ActionRequest actionRequest,
48              ActionResponse actionResponse)
49          throws Exception {
50  
51          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
52  
53          try {
54              String portletResource = ParamUtil.getString(
55                  actionRequest, "portletResource");
56  
57              PortletPreferences preferences =
58                  PortletPreferencesFactoryUtil.getPortletSetup(
59                      actionRequest, portletResource);
60  
61              if (cmd.equals("add-selection")) {
62                  AssetPublisherUtil.addSelection(actionRequest, preferences);
63              }
64              else if (cmd.equals("move-selection-down")) {
65                  moveSelectionDown(actionRequest, preferences);
66              }
67              else if (cmd.equals("move-selection-up")) {
68                  moveSelectionUp(actionRequest, preferences);
69              }
70              else if (cmd.equals("remove-selection")) {
71                  removeSelection(actionRequest, preferences);
72              }
73              else if (cmd.equals("selection-style")) {
74                  setSelectionStyle(actionRequest, preferences);
75              }
76              else if (cmd.equals(Constants.UPDATE)) {
77                  String selectionStyle = preferences.getValue(
78                      "selection-style", "dynamic");
79  
80                  if (selectionStyle.equals("dynamic")) {
81                      updateDynamicSettings(actionRequest, preferences);
82                  }
83                  else if (selectionStyle.equals("manual")) {
84                      updateManualSettings(actionRequest, preferences);
85                  }
86              }
87  
88              if (SessionErrors.isEmpty(actionRequest)) {
89                  preferences.store();
90  
91                  SessionMessages.add(
92                      actionRequest,
93                      portletConfig.getPortletName() + ".doConfigure");
94              }
95  
96              actionResponse.sendRedirect(
97                  ParamUtil.getString(actionRequest, "redirect"));
98          }
99          catch (Exception e) {
100             if (e instanceof AssetTagException) {
101                 SessionErrors.add(actionRequest, e.getClass().getName(), e);
102             }
103             else {
104                 throw e;
105             }
106         }
107     }
108 
109     public String render(
110             PortletConfig portletConfig, RenderRequest renderRequest,
111             RenderResponse renderResponse)
112         throws Exception {
113 
114         return "/html/portlet/asset_publisher/configuration.jsp";
115     }
116 
117     protected void moveSelectionDown(
118             ActionRequest actionRequest, PortletPreferences preferences)
119         throws Exception {
120 
121         int assetEntryOrder = ParamUtil.getInteger(
122             actionRequest, "assetEntryOrder");
123 
124         String[] manualEntries = preferences.getValues(
125             "asset-entry-xml", new String[0]);
126 
127         if ((assetEntryOrder >= (manualEntries.length - 1)) ||
128             (assetEntryOrder < 0)) {
129 
130             return;
131         }
132 
133         String temp = manualEntries[assetEntryOrder + 1];
134 
135         manualEntries[assetEntryOrder + 1] = manualEntries[assetEntryOrder];
136         manualEntries[assetEntryOrder] = temp;
137 
138         preferences.setValues("asset-entry-xml", manualEntries);
139     }
140 
141     protected void moveSelectionUp(
142             ActionRequest actionRequest, PortletPreferences preferences)
143         throws Exception {
144 
145         int assetEntryOrder = ParamUtil.getInteger(
146             actionRequest, "assetEntryOrder");
147 
148         String[] manualEntries = preferences.getValues(
149             "asset-entry-xml", new String[0]);
150 
151         if ((assetEntryOrder >= manualEntries.length) ||
152             (assetEntryOrder <= 0)) {
153 
154             return;
155         }
156 
157         String temp = manualEntries[assetEntryOrder - 1];
158 
159         manualEntries[assetEntryOrder - 1] = manualEntries[assetEntryOrder];
160         manualEntries[assetEntryOrder] = temp;
161 
162         preferences.setValues("asset-entry-xml", manualEntries);
163     }
164 
165     protected void removeSelection(
166             ActionRequest actionRequest, PortletPreferences preferences)
167         throws Exception {
168 
169         int assetEntryOrder = ParamUtil.getInteger(
170             actionRequest, "assetEntryOrder");
171 
172         String[] manualEntries = preferences.getValues(
173             "asset-entry-xml", new String[0]);
174 
175         if (assetEntryOrder >= manualEntries.length) {
176             return;
177         }
178 
179         String[] newEntries = new String[manualEntries.length -1];
180 
181         int i = 0;
182         int j = 0;
183 
184         for (; i < manualEntries.length; i++) {
185             if (i != assetEntryOrder) {
186                 newEntries[j++] = manualEntries[i];
187             }
188         }
189 
190         preferences.setValues("asset-entry-xml", newEntries);
191     }
192 
193     protected void setSelectionStyle(
194             ActionRequest actionRequest, PortletPreferences preferences)
195         throws Exception {
196 
197         String selectionStyle = ParamUtil.getString(
198             actionRequest, "selectionStyle");
199         String displayStyle = ParamUtil.getString(
200             actionRequest, "displayStyle");
201 
202         preferences.setValue("selection-style", selectionStyle);
203 
204         if (selectionStyle.equals("manual") ||
205             selectionStyle.equals("view-count")) {
206 
207             preferences.setValue("show-query-logic", String.valueOf(false));
208         }
209 
210         if (!selectionStyle.equals("view-count") &&
211             displayStyle.equals("view-count-details")) {
212 
213             preferences.setValue("display-style", "full-content");
214         }
215     }
216 
217     protected void updateDynamicSettings(
218             ActionRequest actionRequest, PortletPreferences preferences)
219         throws Exception {
220 
221         updateQueryLogic(actionRequest, preferences);
222 
223         boolean mergeUrlTags = ParamUtil.getBoolean(
224             actionRequest, "mergeUrlTags");
225 
226         boolean defaultScope = ParamUtil.getBoolean(
227             actionRequest, "defaultScope");
228         String[] scopeIds = StringUtil.split(
229             ParamUtil.getString(actionRequest, "scopeIds"));
230         boolean anyAssetType = ParamUtil.getBoolean(
231             actionRequest, "anyAssetType");
232         long[] classNameIds = StringUtil.split(
233             ParamUtil.getString(actionRequest, "classNameIds"), 0L);
234         long assetVocabularyId = ParamUtil.getLong(
235             actionRequest, "assetVocabularyId");
236         String displayStyle = ParamUtil.getString(
237             actionRequest, "displayStyle");
238         boolean showAssetTitle = ParamUtil.getBoolean(
239             actionRequest, "showAssetTitle");
240         boolean showContextLink = ParamUtil.getBoolean(
241             actionRequest, "showContextLink");
242         int abstractLength = ParamUtil.getInteger(
243             actionRequest, "abstractLength");
244         String assetLinkBehaviour = ParamUtil.getString(
245             actionRequest, "assetLinkBehaviour");
246         String orderByColumn1 = ParamUtil.getString(
247             actionRequest, "orderByColumn1");
248         String orderByColumn2 = ParamUtil.getString(
249             actionRequest, "orderByColumn2");
250         String orderByType1 = ParamUtil.getString(
251             actionRequest, "orderByType1");
252         String orderByType2 = ParamUtil.getString(
253             actionRequest, "orderByType2");
254         boolean excludeZeroViewCount = ParamUtil.getBoolean(
255             actionRequest, "excludeZeroViewCount");
256         boolean showQueryLogic = ParamUtil.getBoolean(
257             actionRequest, "showQueryLogic");
258         int delta = ParamUtil.getInteger(actionRequest, "delta");
259         String paginationType = ParamUtil.getString(
260             actionRequest, "paginationType");
261         boolean showAvailableLocales = ParamUtil.getBoolean(
262             actionRequest, "showAvailableLocales");
263         String[] extensions = actionRequest.getParameterValues("extensions");
264         boolean enablePrint = ParamUtil.getBoolean(
265             actionRequest, "enablePrint");
266         boolean enableFlags = ParamUtil.getBoolean(
267             actionRequest, "enableFlags");
268         boolean enableRatings = ParamUtil.getBoolean(
269             actionRequest, "enableRatings");
270         boolean enableComments = ParamUtil.getBoolean(
271             actionRequest, "enableComments");
272         boolean enableCommentRatings = ParamUtil.getBoolean(
273             actionRequest, "enableCommentRatings");
274         String medatadaFields = ParamUtil.getString(
275             actionRequest, "metadataFields");
276 
277         preferences.setValue("selection-style", "dynamic");
278         preferences.setValue("merge-url-tags", String.valueOf(mergeUrlTags));
279         preferences.setValue("default-scope", String.valueOf(defaultScope));
280         preferences.setValues("scope-ids", ArrayUtil.toStringArray(scopeIds));
281         preferences.setValue("any-asset-type", String.valueOf(anyAssetType));
282         preferences.setValues(
283             "class-name-ids", ArrayUtil.toStringArray(classNameIds));
284         preferences.setValue(
285             "asset-vocabulary-id", String.valueOf(assetVocabularyId));
286         preferences.setValue("display-style", displayStyle);
287         preferences.setValue(
288             "show-asset-title", String.valueOf(showAssetTitle));
289         preferences.setValue(
290             "show-context-link", String.valueOf(showContextLink));
291         preferences.setValue("abstract-length", String.valueOf(abstractLength));
292         preferences.setValue("asset-link-behaviour", assetLinkBehaviour);
293         preferences.setValue("order-by-column-1", orderByColumn1);
294         preferences.setValue("order-by-column-2", orderByColumn2);
295         preferences.setValue("order-by-type-1", orderByType1);
296         preferences.setValue("order-by-type-2", orderByType2);
297         preferences.setValue(
298             "exclude-zero-view-count", String.valueOf(excludeZeroViewCount));
299         preferences.setValue(
300             "show-query-logic", String.valueOf(showQueryLogic));
301         preferences.setValue("delta", String.valueOf(delta));
302         preferences.setValue("pagination-type", paginationType);
303         preferences.setValue(
304             "show-available-locales", String.valueOf(showAvailableLocales));
305         preferences.setValues("extensions", extensions);
306         preferences.setValue("enable-print", String.valueOf(enablePrint));
307         preferences.setValue("enable-flags", String.valueOf(enableFlags));
308         preferences.setValue("enable-ratings", String.valueOf(enableRatings));
309         preferences.setValue("enable-comments", String.valueOf(enableComments));
310         preferences.setValue(
311             "enable-comment-ratings", String.valueOf(enableCommentRatings));
312         preferences.setValue("metadata-fields", medatadaFields);
313     }
314 
315     protected void updateManualSettings(
316             ActionRequest actionRequest, PortletPreferences preferences)
317         throws Exception {
318 
319         String displayStyle = ParamUtil.getString(
320             actionRequest, "displayStyle");
321         boolean showAssetTitle = ParamUtil.getBoolean(
322             actionRequest, "showAssetTitle");
323         boolean showContextLink = ParamUtil.getBoolean(
324             actionRequest, "showContextLink");
325         int abstractLength = ParamUtil.getInteger(
326             actionRequest, "abstractLength");
327         String assetLinkBehaviour = ParamUtil.getString(
328             actionRequest, "assetLinkBehaviour");
329         boolean showAvailableLocales = ParamUtil.getBoolean(
330             actionRequest, "showAvailableLocales");
331         String[] extensions = actionRequest.getParameterValues("extensions");
332         boolean enablePrint = ParamUtil.getBoolean(
333             actionRequest, "enablePrint");
334         boolean enableFlags = ParamUtil.getBoolean(
335             actionRequest, "enableFlags");
336         boolean enableRatings = ParamUtil.getBoolean(
337             actionRequest, "enableRatings");
338         boolean enableComments = ParamUtil.getBoolean(
339             actionRequest, "enableComments");
340         boolean enableCommentRatings = ParamUtil.getBoolean(
341             actionRequest, "enableCommentRatings");
342         boolean enableTagBasedNavigation = ParamUtil.getBoolean(
343             actionRequest, "enableTagBasedNavigation");
344         String medatadaFields = ParamUtil.getString(
345             actionRequest, "metadataFields");
346 
347         preferences.setValue("selection-style", "manual");
348         preferences.setValue("display-style", displayStyle);
349         preferences.setValue(
350             "show-asset-title", String.valueOf(showAssetTitle));
351         preferences.setValue(
352             "show-context-link", String.valueOf(showContextLink));
353         preferences.setValue("abstract-length", String.valueOf(abstractLength));
354         preferences.setValue("asset-link-behaviour", assetLinkBehaviour);
355         preferences.setValue(
356             "show-available-locales", String.valueOf(showAvailableLocales));
357         preferences.setValues("extensions", extensions);
358         preferences.setValue("enable-print", String.valueOf(enablePrint));
359         preferences.setValue("enable-flags", String.valueOf(enableFlags));
360         preferences.setValue("enable-ratings", String.valueOf(enableRatings));
361         preferences.setValue("enable-comments", String.valueOf(enableComments));
362         preferences.setValue(
363             "enable-comment-ratings", String.valueOf(enableCommentRatings));
364         preferences.setValue(
365             "enable-tag-based-navigation",
366             String.valueOf(enableTagBasedNavigation));
367         preferences.setValue("metadata-fields", medatadaFields);
368     }
369 
370     protected void updateQueryLogic(
371             ActionRequest actionRequest, PortletPreferences preferences)
372         throws Exception {
373 
374         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
375             WebKeys.THEME_DISPLAY);
376 
377         long userId = themeDisplay.getUserId();
378         long groupId = themeDisplay.getScopeGroupId();
379 
380         int[] queryRulesIndexes = StringUtil.split(
381             ParamUtil.getString(actionRequest, "queryLogicIndexes"), 0);
382 
383         int i = 0;
384 
385         for (int queryRulesIndex : queryRulesIndexes) {
386             boolean contains = ParamUtil.getBoolean(
387                 actionRequest, "queryContains" + queryRulesIndex);
388             boolean andOperator = ParamUtil.getBoolean(
389                 actionRequest, "queryAndOperator" + queryRulesIndex);
390             String name = ParamUtil.getString(
391                 actionRequest, "queryName" + queryRulesIndex);
392 
393             String[] values = null;
394 
395             if (name.equals("assetTags")) {
396                 values = StringUtil.split(ParamUtil.getString(
397                     actionRequest, "queryTagNames" + queryRulesIndex));
398 
399                 AssetTagLocalServiceUtil.checkTags(userId, groupId, values);
400             }
401             else {
402                 values = StringUtil.split(ParamUtil.getString(
403                     actionRequest, "queryCategoryIds" + queryRulesIndex));
404             }
405 
406             preferences.setValue("queryContains" + i, String.valueOf(contains));
407             preferences.setValue(
408                 "queryAndOperator" + i, String.valueOf(andOperator));
409             preferences.setValue("queryName" + i, name);
410             preferences.setValues("queryValues" + i, values);
411 
412             i++;
413         }
414 
415         // Clear previous preferences that are now blank
416 
417         String[] values = preferences.getValues(
418             "queryValues" + i, new String[0]);
419 
420         while (values.length > 0) {
421             preferences.setValue("queryContains" + i, StringPool.BLANK);
422             preferences.setValue("queryAndOperator" + i, StringPool.BLANK);
423             preferences.setValue("queryName" + i, StringPool.BLANK);
424             preferences.setValues("queryValues" + i, new String[0]);
425 
426             i++;
427 
428             values = preferences.getValues("queryValues" + i, new String[0]);
429         }
430     }
431 
432 }