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         updateDisplaySettings(actionRequest, preferences);
222         updateQueryLogic(actionRequest, preferences);
223         updateRssSettings(actionRequest, preferences);
224 
225         boolean mergeUrlTags = ParamUtil.getBoolean(
226             actionRequest, "mergeUrlTags");
227         boolean defaultScope = ParamUtil.getBoolean(
228             actionRequest, "defaultScope");
229         String[] scopeIds = StringUtil.split(
230             ParamUtil.getString(actionRequest, "scopeIds"));
231         long assetVocabularyId = ParamUtil.getLong(
232             actionRequest, "assetVocabularyId");
233         String orderByColumn1 = ParamUtil.getString(
234             actionRequest, "orderByColumn1");
235         String orderByColumn2 = ParamUtil.getString(
236             actionRequest, "orderByColumn2");
237         String orderByType1 = ParamUtil.getString(
238             actionRequest, "orderByType1");
239         String orderByType2 = ParamUtil.getString(
240             actionRequest, "orderByType2");
241         boolean excludeZeroViewCount = ParamUtil.getBoolean(
242             actionRequest, "excludeZeroViewCount");
243         boolean showQueryLogic = ParamUtil.getBoolean(
244             actionRequest, "showQueryLogic");
245         int delta = ParamUtil.getInteger(actionRequest, "delta");
246         String paginationType = ParamUtil.getString(
247             actionRequest, "paginationType");
248         String[] extensions = actionRequest.getParameterValues("extensions");
249 
250         preferences.setValue("selection-style", "dynamic");
251         preferences.setValue("merge-url-tags", String.valueOf(mergeUrlTags));
252         preferences.setValue("default-scope", String.valueOf(defaultScope));
253         preferences.setValues("scope-ids", ArrayUtil.toStringArray(scopeIds));
254         preferences.setValue(
255             "asset-vocabulary-id", String.valueOf(assetVocabularyId));
256         preferences.setValue("order-by-column-1", orderByColumn1);
257         preferences.setValue("order-by-column-2", orderByColumn2);
258         preferences.setValue("order-by-type-1", orderByType1);
259         preferences.setValue("order-by-type-2", orderByType2);
260         preferences.setValue(
261             "exclude-zero-view-count", String.valueOf(excludeZeroViewCount));
262         preferences.setValue(
263             "show-query-logic", String.valueOf(showQueryLogic));
264         preferences.setValue("delta", String.valueOf(delta));
265         preferences.setValue("pagination-type", paginationType);
266         preferences.setValues("extensions", extensions);
267     }
268 
269     protected void updateManualSettings(
270             ActionRequest actionRequest, PortletPreferences preferences)
271         throws Exception {
272 
273         updateDisplaySettings(actionRequest, preferences);
274         updateRssSettings(actionRequest, preferences);
275     }
276 
277     protected void updateDisplaySettings(
278             ActionRequest actionRequest, PortletPreferences preferences)
279         throws Exception {
280 
281         String displayStyle = ParamUtil.getString(
282             actionRequest, "displayStyle");
283         boolean anyAssetType = ParamUtil.getBoolean(
284             actionRequest, "anyAssetType");
285         long[] classNameIds = StringUtil.split(
286             ParamUtil.getString(actionRequest, "classNameIds"), 0L);
287         boolean showAssetTitle = ParamUtil.getBoolean(
288             actionRequest, "showAssetTitle");
289         boolean showContextLink = ParamUtil.getBoolean(
290             actionRequest, "showContextLink");
291         int abstractLength = ParamUtil.getInteger(
292             actionRequest, "abstractLength");
293         String assetLinkBehaviour = ParamUtil.getString(
294             actionRequest, "assetLinkBehaviour");
295         boolean showAvailableLocales = ParamUtil.getBoolean(
296             actionRequest, "showAvailableLocales");
297         String[] extensions = actionRequest.getParameterValues("extensions");
298         boolean enablePrint = ParamUtil.getBoolean(
299             actionRequest, "enablePrint");
300         boolean enableFlags = ParamUtil.getBoolean(
301             actionRequest, "enableFlags");
302         boolean enableRatings = ParamUtil.getBoolean(
303             actionRequest, "enableRatings");
304         boolean enableComments = ParamUtil.getBoolean(
305             actionRequest, "enableComments");
306         boolean enableCommentRatings = ParamUtil.getBoolean(
307             actionRequest, "enableCommentRatings");
308         boolean enableTagBasedNavigation = ParamUtil.getBoolean(
309             actionRequest, "enableTagBasedNavigation");
310         String medatadaFields = ParamUtil.getString(
311             actionRequest, "metadataFields");
312 
313         preferences.setValue("selection-style", "manual");
314         preferences.setValue("display-style", displayStyle);
315         preferences.setValue("any-asset-type", String.valueOf(anyAssetType));
316         preferences.setValues(
317             "class-name-ids", ArrayUtil.toStringArray(classNameIds));
318         preferences.setValue(
319             "show-asset-title", String.valueOf(showAssetTitle));
320         preferences.setValue(
321             "show-context-link", String.valueOf(showContextLink));
322         preferences.setValue("abstract-length", String.valueOf(abstractLength));
323         preferences.setValue("asset-link-behaviour", assetLinkBehaviour);
324         preferences.setValue(
325             "show-available-locales", String.valueOf(showAvailableLocales));
326         preferences.setValues("extensions", extensions);
327         preferences.setValue("enable-print", String.valueOf(enablePrint));
328         preferences.setValue("enable-flags", String.valueOf(enableFlags));
329         preferences.setValue("enable-ratings", String.valueOf(enableRatings));
330         preferences.setValue("enable-comments", String.valueOf(enableComments));
331         preferences.setValue(
332             "enable-comment-ratings", String.valueOf(enableCommentRatings));
333         preferences.setValue(
334             "enable-tag-based-navigation",
335             String.valueOf(enableTagBasedNavigation));
336         preferences.setValue("metadata-fields", medatadaFields);
337     }
338 
339     protected void updateQueryLogic(
340             ActionRequest actionRequest, PortletPreferences preferences)
341         throws Exception {
342 
343         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
344             WebKeys.THEME_DISPLAY);
345 
346         long userId = themeDisplay.getUserId();
347         long groupId = themeDisplay.getScopeGroupId();
348 
349         int[] queryRulesIndexes = StringUtil.split(
350             ParamUtil.getString(actionRequest, "queryLogicIndexes"), 0);
351 
352         int i = 0;
353 
354         for (int queryRulesIndex : queryRulesIndexes) {
355             boolean contains = ParamUtil.getBoolean(
356                 actionRequest, "queryContains" + queryRulesIndex);
357             boolean andOperator = ParamUtil.getBoolean(
358                 actionRequest, "queryAndOperator" + queryRulesIndex);
359             String name = ParamUtil.getString(
360                 actionRequest, "queryName" + queryRulesIndex);
361 
362             String[] values = null;
363 
364             if (name.equals("assetTags")) {
365                 values = StringUtil.split(ParamUtil.getString(
366                     actionRequest, "queryTagNames" + queryRulesIndex));
367 
368                 AssetTagLocalServiceUtil.checkTags(userId, groupId, values);
369             }
370             else {
371                 values = StringUtil.split(ParamUtil.getString(
372                     actionRequest, "queryCategoryIds" + queryRulesIndex));
373             }
374 
375             preferences.setValue("queryContains" + i, String.valueOf(contains));
376             preferences.setValue(
377                 "queryAndOperator" + i, String.valueOf(andOperator));
378             preferences.setValue("queryName" + i, name);
379             preferences.setValues("queryValues" + i, values);
380 
381             i++;
382         }
383 
384         // Clear previous preferences that are now blank
385 
386         String[] values = preferences.getValues(
387             "queryValues" + i, new String[0]);
388 
389         while (values.length > 0) {
390             preferences.setValue("queryContains" + i, StringPool.BLANK);
391             preferences.setValue("queryAndOperator" + i, StringPool.BLANK);
392             preferences.setValue("queryName" + i, StringPool.BLANK);
393             preferences.setValues("queryValues" + i, new String[0]);
394 
395             i++;
396 
397             values = preferences.getValues("queryValues" + i, new String[0]);
398         }
399     }
400 
401     protected void updateRssSettings(
402             ActionRequest actionRequest, PortletPreferences preferences)
403         throws Exception {
404 
405         boolean enableRSS = ParamUtil.getBoolean(
406             actionRequest, "enableRSS");
407         int rssDelta = ParamUtil.getInteger(actionRequest, "rssDelta");
408         String rssDisplayStyle = ParamUtil.getString(
409             actionRequest, "rssDisplayStyle");
410         String rssFormat = ParamUtil.getString(actionRequest, "rssFormat");
411         String rssName = ParamUtil.getString(actionRequest, "rssName");
412 
413         preferences.setValue("enable-rss", String.valueOf(enableRSS));
414         preferences.setValue("rss-delta", String.valueOf(rssDelta));
415         preferences.setValue("rss-display-style", rssDisplayStyle);
416         preferences.setValue("rss-format", rssFormat);
417         preferences.setValue("rss-name", rssName);
418     }
419 
420 }