1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
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.Constants;
21  import com.liferay.portal.kernel.util.ParamUtil;
22  import com.liferay.portal.kernel.util.StringUtil;
23  import com.liferay.portal.theme.ThemeDisplay;
24  import com.liferay.portal.util.WebKeys;
25  import com.liferay.portlet.PortletPreferencesFactoryUtil;
26  import com.liferay.portlet.assetpublisher.util.AssetPublisherUtil;
27  import com.liferay.portlet.tags.TagsEntryException;
28  import com.liferay.portlet.tags.service.TagsEntryLocalServiceUtil;
29  
30  import javax.portlet.ActionRequest;
31  import javax.portlet.ActionResponse;
32  import javax.portlet.PortletConfig;
33  import javax.portlet.PortletPreferences;
34  import javax.portlet.RenderRequest;
35  import javax.portlet.RenderResponse;
36  
37  /**
38   * <a href="ConfigurationActionImpl.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Brian Wing Shun Chan
41   */
42  public class ConfigurationActionImpl extends BaseConfigurationAction {
43  
44      public void processAction(
45              PortletConfig portletConfig, ActionRequest actionRequest,
46              ActionResponse actionResponse)
47          throws Exception {
48  
49          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
50  
51          try {
52              String portletResource = ParamUtil.getString(
53                  actionRequest, "portletResource");
54  
55              PortletPreferences preferences =
56                  PortletPreferencesFactoryUtil.getPortletSetup(
57                      actionRequest, portletResource);
58  
59              if (cmd.equals("add-selection")) {
60                  AssetPublisherUtil.addSelection(actionRequest, preferences);
61              }
62              else if (cmd.equals("move-selection-down")) {
63                  moveSelectionDown(actionRequest, preferences);
64              }
65              else if (cmd.equals("move-selection-up")) {
66                  moveSelectionUp(actionRequest, preferences);
67              }
68              else if (cmd.equals("remove-selection")) {
69                  removeSelection(actionRequest, preferences);
70              }
71              else if (cmd.equals("selection-style")) {
72                  setSelectionStyle(actionRequest, preferences);
73              }
74              else if (cmd.equals(Constants.UPDATE)) {
75                  String selectionStyle = preferences.getValue(
76                      "selection-style", "dynamic");
77  
78                  if (selectionStyle.equals("dynamic")) {
79                      updateDynamicSettings(actionRequest, preferences);
80                  }
81                  else if (selectionStyle.equals("manual")) {
82                      updateManualSettings(actionRequest, preferences);
83                  }
84              }
85  
86              if (SessionErrors.isEmpty(actionRequest)) {
87                  preferences.store();
88  
89                  SessionMessages.add(
90                      actionRequest,
91                      portletConfig.getPortletName() + ".doConfigure");
92              }
93  
94              actionResponse.sendRedirect(
95                  ParamUtil.getString(actionRequest, "redirect"));
96          }
97          catch (Exception e) {
98              if (e instanceof TagsEntryException) {
99                  SessionErrors.add(actionRequest, e.getClass().getName(), e);
100             }
101             else {
102                 throw e;
103             }
104         }
105     }
106 
107     public String render(
108             PortletConfig portletConfig, RenderRequest renderRequest,
109             RenderResponse renderResponse)
110         throws Exception {
111 
112         return "/html/portlet/asset_publisher/configuration.jsp";
113     }
114 
115     protected void moveSelectionDown(
116             ActionRequest actionRequest, PortletPreferences preferences)
117         throws Exception {
118 
119         int assetOrder = ParamUtil.getInteger(actionRequest, "assetOrder");
120 
121         String[] manualEntries = preferences.getValues(
122             "manual-entries", new String[0]);
123 
124         if ((assetOrder >= (manualEntries.length - 1)) || (assetOrder < 0)) {
125             return;
126         }
127 
128         String temp = manualEntries[assetOrder + 1];
129 
130         manualEntries[assetOrder + 1] = manualEntries[assetOrder];
131         manualEntries[assetOrder] = temp;
132 
133         preferences.setValues("manual-entries", manualEntries);
134     }
135 
136     protected void moveSelectionUp(
137             ActionRequest actionRequest, PortletPreferences preferences)
138         throws Exception {
139 
140         int assetOrder = ParamUtil.getInteger(actionRequest, "assetOrder");
141 
142         String[] manualEntries = preferences.getValues(
143             "manual-entries", new String[0]);
144 
145         if ((assetOrder >= manualEntries.length) || (assetOrder <= 0)) {
146             return;
147         }
148 
149         String temp = manualEntries[assetOrder - 1];
150 
151         manualEntries[assetOrder - 1] = manualEntries[assetOrder];
152         manualEntries[assetOrder] = temp;
153 
154         preferences.setValues("manual-entries", manualEntries);
155     }
156 
157     protected void removeSelection(
158             ActionRequest actionRequest, PortletPreferences preferences)
159         throws Exception {
160 
161         int assetOrder = ParamUtil.getInteger(actionRequest, "assetOrder");
162 
163         String[] manualEntries = preferences.getValues(
164             "manual-entries", new String[0]);
165 
166         if (assetOrder >= manualEntries.length) {
167             return;
168         }
169 
170         String[] newEntries = new String[manualEntries.length -1];
171 
172         int i = 0;
173         int j = 0;
174 
175         for (; i < manualEntries.length; i++) {
176             if (i != assetOrder) {
177                 newEntries[j++] = manualEntries[i];
178             }
179         }
180 
181         preferences.setValues("manual-entries", newEntries);
182     }
183 
184     protected void setSelectionStyle(
185             ActionRequest actionRequest, PortletPreferences preferences)
186         throws Exception {
187 
188         String selectionStyle = ParamUtil.getString(
189             actionRequest, "selectionStyle");
190         String displayStyle = ParamUtil.getString(
191             actionRequest, "displayStyle");
192 
193         preferences.setValue("selection-style", selectionStyle);
194 
195         if (selectionStyle.equals("manual") ||
196             selectionStyle.equals("view-count")) {
197 
198             preferences.setValue("show-query-logic", String.valueOf(false));
199         }
200 
201         if (!selectionStyle.equals("view-count") &&
202             displayStyle.equals("view-count-details")) {
203 
204             preferences.setValue("display-style", "full-content");
205         }
206     }
207 
208     protected void updateDynamicSettings(
209             ActionRequest actionRequest, PortletPreferences preferences)
210         throws Exception {
211 
212         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
213             WebKeys.THEME_DISPLAY);
214 
215         long userId = themeDisplay.getUserId();
216         long groupId = themeDisplay.getScopeGroupId();
217 
218         String[] entries = StringUtil.split(
219             ParamUtil.getString(actionRequest, "entries"));
220         String[] notEntries = StringUtil.split(
221             ParamUtil.getString(actionRequest, "notEntries"));
222         boolean mergeUrlTags = ParamUtil.getBoolean(
223             actionRequest, "mergeUrlTags");
224         boolean andOperator = ParamUtil.getBoolean(
225             actionRequest, "andOperator");
226 
227         long classNameId = ParamUtil.getLong(actionRequest, "classNameId");
228         String category = ParamUtil.getString(actionRequest, "category");
229         String displayStyle = ParamUtil.getString(
230             actionRequest, "displayStyle");
231         boolean showAssetTitle = ParamUtil.getBoolean(
232             actionRequest, "showAssetTitle");
233         boolean showContextLink = ParamUtil.getBoolean(
234             actionRequest, "showContextLink");
235         int abstractLength = ParamUtil.getInteger(
236             actionRequest, "abstractLength");
237         String assetLinkBehaviour = ParamUtil.getString(
238             actionRequest, "assetLinkBehaviour");
239         String orderByColumn1 = ParamUtil.getString(
240             actionRequest, "orderByColumn1");
241         String orderByColumn2 = ParamUtil.getString(
242             actionRequest, "orderByColumn2");
243         String orderByType1 = ParamUtil.getString(
244             actionRequest, "orderByType1");
245         String orderByType2 = ParamUtil.getString(
246             actionRequest, "orderByType2");
247         boolean excludeZeroViewCount = ParamUtil.getBoolean(
248             actionRequest, "excludeZeroViewCount");
249         boolean showQueryLogic = ParamUtil.getBoolean(
250             actionRequest, "showQueryLogic");
251         int delta = ParamUtil.getInteger(actionRequest, "delta");
252         String paginationType = ParamUtil.getString(
253             actionRequest, "paginationType");
254         boolean showAvailableLocales = ParamUtil.getBoolean(
255             actionRequest, "showAvailableLocales");
256         boolean enableComments = ParamUtil.getBoolean(
257             actionRequest, "enableComments");
258         boolean enableCommentRatings = ParamUtil.getBoolean(
259             actionRequest, "enableCommentRatings");
260         boolean enableRatings = ParamUtil.getBoolean(
261             actionRequest, "enableRatings");
262         String medatadaFields = ParamUtil.getString(
263             actionRequest, "metadataFields");
264 
265         preferences.setValue("selection-style", "dynamic");
266 
267         preferences.setValues("entries", entries);
268         preferences.setValues("not-entries", notEntries);
269         preferences.setValue("merge-url-tags", String.valueOf(mergeUrlTags));
270         preferences.setValue("and-operator", String.valueOf(andOperator));
271 
272         preferences.setValue("class-name-id", String.valueOf(classNameId));
273         preferences.setValue("category", category);
274         preferences.setValue("display-style", displayStyle);
275         preferences.setValue(
276             "show-asset-title", String.valueOf(showAssetTitle));
277         preferences.setValue(
278             "show-context-link", String.valueOf(showContextLink));
279         preferences.setValue("abstract-length", String.valueOf(abstractLength));
280         preferences.setValue("asset-link-behaviour", assetLinkBehaviour);
281         preferences.setValue("order-by-column-1", orderByColumn1);
282         preferences.setValue("order-by-column-2", orderByColumn2);
283         preferences.setValue("order-by-type-1", orderByType1);
284         preferences.setValue("order-by-type-2", orderByType2);
285         preferences.setValue(
286             "exclude-zero-view-count", String.valueOf(excludeZeroViewCount));
287         preferences.setValue(
288             "show-query-logic", String.valueOf(showQueryLogic));
289         preferences.setValue("delta", String.valueOf(delta));
290         preferences.setValue("pagination-type", paginationType);
291         preferences.setValue(
292             "show-available-locales", String.valueOf(showAvailableLocales));
293         preferences.setValue("enable-ratings", String.valueOf(enableRatings));
294         preferences.setValue("enable-comments", String.valueOf(enableComments));
295         preferences.setValue(
296             "enable-comment-ratings", String.valueOf(enableCommentRatings));
297         preferences.setValue("metadata-fields", medatadaFields);
298 
299         TagsEntryLocalServiceUtil.checkEntries(userId, groupId, entries);
300         TagsEntryLocalServiceUtil.checkEntries(userId, groupId, notEntries);
301     }
302 
303     protected void updateManualSettings(
304             ActionRequest actionRequest, PortletPreferences preferences)
305         throws Exception {
306 
307         String displayStyle = ParamUtil.getString(
308             actionRequest, "displayStyle");
309         boolean showAssetTitle = ParamUtil.getBoolean(
310             actionRequest, "showAssetTitle");
311         boolean showContextLink = ParamUtil.getBoolean(
312             actionRequest, "showContextLink");
313         int abstractLength = ParamUtil.getInteger(
314             actionRequest, "abstractLength");
315         String assetLinkBehaviour = ParamUtil.getString(
316             actionRequest, "assetLinkBehaviour");
317         boolean showAvailableLocales = ParamUtil.getBoolean(
318             actionRequest, "showAvailableLocales");
319         boolean enableComments = ParamUtil.getBoolean(
320             actionRequest, "enableComments");
321         boolean enableCommentRatings = ParamUtil.getBoolean(
322             actionRequest, "enableCommentRatings");
323         boolean enableRatings = ParamUtil.getBoolean(
324             actionRequest, "enableRatings");
325         boolean enableTagBasedNavigation = ParamUtil.getBoolean(
326             actionRequest, "enableTagBasedNavigation");
327         String medatadaFields = ParamUtil.getString(
328             actionRequest, "metadataFields");
329 
330         preferences.setValue("selection-style", "manual");
331         preferences.setValue("display-style", displayStyle);
332         preferences.setValue(
333             "show-asset-title", String.valueOf(showAssetTitle));
334         preferences.setValue(
335             "show-context-link", String.valueOf(showContextLink));
336         preferences.setValue("abstract-length", String.valueOf(abstractLength));
337         preferences.setValue("asset-link-behaviour", assetLinkBehaviour);
338         preferences.setValue(
339             "show-available-locales", String.valueOf(showAvailableLocales));
340         preferences.setValue("enable-comments", String.valueOf(enableComments));
341         preferences.setValue(
342             "enable-comment-ratings", String.valueOf(enableCommentRatings));
343         preferences.setValue("enable-ratings", String.valueOf(enableRatings));
344         preferences.setValue(
345             "enable-tag-based-navigation",
346             String.valueOf(enableTagBasedNavigation));
347         preferences.setValue("metadata-fields", medatadaFields);
348     }
349 
350 }