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