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.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.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 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 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         String orderByColumn1 = ParamUtil.getString(
238             actionRequest, "orderByColumn1");
239         String orderByColumn2 = ParamUtil.getString(
240             actionRequest, "orderByColumn2");
241         String orderByType1 = ParamUtil.getString(
242             actionRequest, "orderByType1");
243         String orderByType2 = ParamUtil.getString(
244             actionRequest, "orderByType2");
245         boolean excludeZeroViewCount = ParamUtil.getBoolean(
246             actionRequest, "excludeZeroViewCount");
247         boolean showQueryLogic = ParamUtil.getBoolean(
248             actionRequest, "showQueryLogic");
249         int delta = ParamUtil.getInteger(actionRequest, "delta");
250         String paginationType = ParamUtil.getString(
251             actionRequest, "paginationType");
252         boolean showAvailableLocales = ParamUtil.getBoolean(
253             actionRequest, "showAvailableLocales");
254         boolean enableComments = ParamUtil.getBoolean(
255             actionRequest, "enableComments");
256         boolean enableCommentRatings = ParamUtil.getBoolean(
257             actionRequest, "enableCommentRatings");
258         boolean enableRatings = ParamUtil.getBoolean(
259             actionRequest, "enableRatings");
260         String medatadaFields = ParamUtil.getString(
261             actionRequest, "metadataFields");
262 
263         preferences.setValue("selection-style", "dynamic");
264 
265         preferences.setValues("entries", entries);
266         preferences.setValues("not-entries", notEntries);
267         preferences.setValue("merge-url-tags", String.valueOf(mergeUrlTags));
268         preferences.setValue("and-operator", String.valueOf(andOperator));
269 
270         preferences.setValue("class-name-id", String.valueOf(classNameId));
271         preferences.setValue("category", category);
272         preferences.setValue("display-style", displayStyle);
273         preferences.setValue("order-by-column-1", orderByColumn1);
274         preferences.setValue("order-by-column-2", orderByColumn2);
275         preferences.setValue("order-by-type-1", orderByType1);
276         preferences.setValue("order-by-type-2", orderByType2);
277         preferences.setValue(
278             "exclude-zero-view-count", String.valueOf(excludeZeroViewCount));
279         preferences.setValue(
280             "show-query-logic", String.valueOf(showQueryLogic));
281         preferences.setValue("delta", String.valueOf(delta));
282         preferences.setValue("pagination-type", paginationType);
283         preferences.setValue(
284             "show-available-locales", String.valueOf(showAvailableLocales));
285         preferences.setValue("enable-ratings", String.valueOf(enableRatings));
286         preferences.setValue("enable-comments", String.valueOf(enableComments));
287         preferences.setValue(
288             "enable-comment-ratings", String.valueOf(enableCommentRatings));
289         preferences.setValue("metadata-fields", medatadaFields);
290 
291         TagsEntryLocalServiceUtil.checkEntries(userId, groupId, entries);
292         TagsEntryLocalServiceUtil.checkEntries(userId, groupId, notEntries);
293     }
294 
295     protected void updateManualSettings(
296             ActionRequest actionRequest, PortletPreferences preferences)
297         throws Exception {
298 
299         String displayStyle = ParamUtil.getString(
300             actionRequest, "displayStyle");
301         boolean showAvailableLocales = ParamUtil.getBoolean(
302             actionRequest, "showAvailableLocales");
303         boolean enableComments = ParamUtil.getBoolean(
304             actionRequest, "enableComments");
305         boolean enableCommentRatings = ParamUtil.getBoolean(
306             actionRequest, "enableCommentRatings");
307         boolean enableRatings = ParamUtil.getBoolean(
308             actionRequest, "enableRatings");
309 
310         preferences.setValue("selection-style", "manual");
311         preferences.setValue("display-style", displayStyle);
312         preferences.setValue(
313             "show-available-locales", String.valueOf(showAvailableLocales));
314         preferences.setValue("enable-comments", String.valueOf(enableComments));
315         preferences.setValue(
316             "enable-comment-ratings", String.valueOf(enableCommentRatings));
317         preferences.setValue("enable-ratings", String.valueOf(enableRatings));
318     }
319 
320 }