1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.assetpublisher.action;
24  
25  import com.liferay.portal.kernel.portlet.BaseConfigurationAction;
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.assetpublisher.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  /**
46   * <a href="ConfigurationActionImpl.java.html"><b><i>View Source</i></b></a>
47   *
48   * @author Brian Wing Shun Chan
49   *
50   */
51  public class ConfigurationActionImpl extends BaseConfigurationAction {
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 preferences =
65                  PortletPreferencesFactoryUtil.getPortletSetup(
66                      actionRequest, portletResource);
67  
68              if (cmd.equals("add-selection")) {
69                  AssetPublisherUtil.addSelection(actionRequest, preferences);
70              }
71              else if (cmd.equals("move-selection-down")) {
72                  moveSelectionDown(actionRequest, preferences);
73              }
74              else if (cmd.equals("move-selection-up")) {
75                  moveSelectionUp(actionRequest, preferences);
76              }
77              else if (cmd.equals("remove-selection")) {
78                  removeSelection(actionRequest, preferences);
79              }
80              else if (cmd.equals("selection-style")) {
81                  setSelectionStyle(actionRequest, preferences);
82              }
83              else if (cmd.equals(Constants.UPDATE)) {
84                  String selectionStyle = preferences.getValue(
85                      "selection-style", "dynamic");
86  
87                  if (selectionStyle.equals("dynamic")) {
88                      updateDynamicSettings(actionRequest, preferences);
89                  }
90                  else if (selectionStyle.equals("manual")) {
91                      updateManualSettings(actionRequest, preferences);
92                  }
93              }
94  
95              if (SessionErrors.isEmpty(actionRequest)) {
96                  preferences.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/asset_publisher/configuration.jsp";
119     }
120 
121     protected void moveSelectionDown(
122             ActionRequest actionRequest, PortletPreferences preferences)
123         throws Exception {
124 
125         int assetOrder = ParamUtil.getInteger(actionRequest, "assetOrder");
126 
127         String[] manualEntries = preferences.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         preferences.setValues("manual-entries", manualEntries);
140     }
141 
142     protected void moveSelectionUp(
143             ActionRequest actionRequest, PortletPreferences preferences)
144         throws Exception {
145 
146         int assetOrder = ParamUtil.getInteger(actionRequest, "assetOrder");
147 
148         String[] manualEntries = preferences.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         preferences.setValues("manual-entries", manualEntries);
161     }
162 
163     protected void removeSelection(
164             ActionRequest actionRequest, PortletPreferences preferences)
165         throws Exception {
166 
167         int assetOrder = ParamUtil.getInteger(actionRequest, "assetOrder");
168 
169         String[] manualEntries = preferences.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         preferences.setValues("manual-entries", newEntries);
188     }
189 
190     protected void setSelectionStyle(
191             ActionRequest actionRequest, PortletPreferences preferences)
192         throws Exception {
193 
194         String selectionStyle = ParamUtil.getString(
195             actionRequest, "selectionStyle");
196         String displayStyle = ParamUtil.getString(
197             actionRequest, "displayStyle");
198 
199         preferences.setValue("selection-style", selectionStyle);
200 
201         if (selectionStyle.equals("manual") ||
202             selectionStyle.equals("view-count")) {
203 
204             preferences.setValue("show-query-logic", String.valueOf(false));
205         }
206 
207         if (!selectionStyle.equals("view-count") &&
208             displayStyle.equals("view-count-details")) {
209 
210             preferences.setValue("display-style", "full-content");
211         }
212     }
213 
214     protected void updateDynamicSettings(
215             ActionRequest actionRequest, PortletPreferences preferences)
216         throws Exception {
217 
218         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
219             WebKeys.THEME_DISPLAY);
220 
221         long userId = themeDisplay.getUserId();
222         long groupId = themeDisplay.getScopeGroupId();
223 
224         String[] entries = StringUtil.split(
225             ParamUtil.getString(actionRequest, "entries"));
226         String[] notEntries = StringUtil.split(
227             ParamUtil.getString(actionRequest, "notEntries"));
228         boolean mergeUrlTags = ParamUtil.getBoolean(
229             actionRequest, "mergeUrlTags");
230         boolean andOperator = ParamUtil.getBoolean(
231             actionRequest, "andOperator");
232 
233         long classNameId = ParamUtil.getLong(actionRequest, "classNameId");
234         String category = ParamUtil.getString(actionRequest, "category");
235         String displayStyle = ParamUtil.getString(
236             actionRequest, "displayStyle");
237         boolean showAssetTitle = ParamUtil.getBoolean(
238             actionRequest, "showAssetTitle");
239         boolean showContextLink = ParamUtil.getBoolean(
240             actionRequest, "showContextLink");
241         int abstractLength = ParamUtil.getInteger(
242             actionRequest, "abstractLength");
243         String assetLinkBehaviour = ParamUtil.getString(
244             actionRequest, "assetLinkBehaviour");
245         String orderByColumn1 = ParamUtil.getString(
246             actionRequest, "orderByColumn1");
247         String orderByColumn2 = ParamUtil.getString(
248             actionRequest, "orderByColumn2");
249         String orderByType1 = ParamUtil.getString(
250             actionRequest, "orderByType1");
251         String orderByType2 = ParamUtil.getString(
252             actionRequest, "orderByType2");
253         boolean excludeZeroViewCount = ParamUtil.getBoolean(
254             actionRequest, "excludeZeroViewCount");
255         boolean showQueryLogic = ParamUtil.getBoolean(
256             actionRequest, "showQueryLogic");
257         int delta = ParamUtil.getInteger(actionRequest, "delta");
258         String paginationType = ParamUtil.getString(
259             actionRequest, "paginationType");
260         boolean showAvailableLocales = ParamUtil.getBoolean(
261             actionRequest, "showAvailableLocales");
262         boolean enableComments = ParamUtil.getBoolean(
263             actionRequest, "enableComments");
264         boolean enableCommentRatings = ParamUtil.getBoolean(
265             actionRequest, "enableCommentRatings");
266         boolean enableRatings = ParamUtil.getBoolean(
267             actionRequest, "enableRatings");
268         String medatadaFields = ParamUtil.getString(
269             actionRequest, "metadataFields");
270 
271         preferences.setValue("selection-style", "dynamic");
272 
273         preferences.setValues("entries", entries);
274         preferences.setValues("not-entries", notEntries);
275         preferences.setValue("merge-url-tags", String.valueOf(mergeUrlTags));
276         preferences.setValue("and-operator", String.valueOf(andOperator));
277 
278         preferences.setValue("class-name-id", String.valueOf(classNameId));
279         preferences.setValue("category", category);
280         preferences.setValue("display-style", displayStyle);
281         preferences.setValue(
282             "show-asset-title", String.valueOf(showAssetTitle));
283         preferences.setValue(
284             "show-context-link", String.valueOf(showContextLink));
285         preferences.setValue("abstract-length", String.valueOf(abstractLength));
286         preferences.setValue("asset-link-behaviour", assetLinkBehaviour);
287         preferences.setValue("order-by-column-1", orderByColumn1);
288         preferences.setValue("order-by-column-2", orderByColumn2);
289         preferences.setValue("order-by-type-1", orderByType1);
290         preferences.setValue("order-by-type-2", orderByType2);
291         preferences.setValue(
292             "exclude-zero-view-count", String.valueOf(excludeZeroViewCount));
293         preferences.setValue(
294             "show-query-logic", String.valueOf(showQueryLogic));
295         preferences.setValue("delta", String.valueOf(delta));
296         preferences.setValue("pagination-type", paginationType);
297         preferences.setValue(
298             "show-available-locales", String.valueOf(showAvailableLocales));
299         preferences.setValue("enable-ratings", String.valueOf(enableRatings));
300         preferences.setValue("enable-comments", String.valueOf(enableComments));
301         preferences.setValue(
302             "enable-comment-ratings", String.valueOf(enableCommentRatings));
303         preferences.setValue("metadata-fields", medatadaFields);
304 
305         TagsEntryLocalServiceUtil.checkEntries(userId, groupId, entries);
306         TagsEntryLocalServiceUtil.checkEntries(userId, groupId, notEntries);
307     }
308 
309     protected void updateManualSettings(
310             ActionRequest actionRequest, PortletPreferences preferences)
311         throws Exception {
312 
313         String displayStyle = ParamUtil.getString(
314             actionRequest, "displayStyle");
315         boolean showAssetTitle = ParamUtil.getBoolean(
316             actionRequest, "showAssetTitle");
317         boolean showContextLink = ParamUtil.getBoolean(
318             actionRequest, "showContextLink");
319         int abstractLength = ParamUtil.getInteger(
320             actionRequest, "abstractLength");
321         String assetLinkBehaviour = ParamUtil.getString(
322             actionRequest, "assetLinkBehaviour");
323         boolean showAvailableLocales = ParamUtil.getBoolean(
324             actionRequest, "showAvailableLocales");
325         boolean enableComments = ParamUtil.getBoolean(
326             actionRequest, "enableComments");
327         boolean enableCommentRatings = ParamUtil.getBoolean(
328             actionRequest, "enableCommentRatings");
329         boolean enableRatings = ParamUtil.getBoolean(
330             actionRequest, "enableRatings");
331         boolean enableTagBasedNavigation = ParamUtil.getBoolean(
332             actionRequest, "enableTagBasedNavigation");
333         String medatadaFields = ParamUtil.getString(
334             actionRequest, "metadataFields");
335 
336         preferences.setValue("selection-style", "manual");
337         preferences.setValue("display-style", displayStyle);
338         preferences.setValue(
339             "show-asset-title", String.valueOf(showAssetTitle));
340         preferences.setValue(
341             "show-context-link", String.valueOf(showContextLink));
342         preferences.setValue("abstract-length", String.valueOf(abstractLength));
343         preferences.setValue("asset-link-behaviour", assetLinkBehaviour);
344         preferences.setValue(
345             "show-available-locales", String.valueOf(showAvailableLocales));
346         preferences.setValue("enable-comments", String.valueOf(enableComments));
347         preferences.setValue(
348             "enable-comment-ratings", String.valueOf(enableCommentRatings));
349         preferences.setValue("enable-ratings", String.valueOf(enableRatings));
350         preferences.setValue(
351             "enable-tag-based-navigation",
352             String.valueOf(enableTagBasedNavigation));
353         preferences.setValue("metadata-fields", medatadaFields);
354     }
355 
356 }