1   /**
2    * Copyright (c) 2000-2008 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.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.taggedcontent.util.AssetPublisherUtil;
35  import com.liferay.portlet.tags.service.TagsEntryLocalServiceUtil;
36  
37  import javax.portlet.ActionRequest;
38  import javax.portlet.ActionResponse;
39  import javax.portlet.PortletConfig;
40  import javax.portlet.PortletPreferences;
41  import javax.portlet.RenderRequest;
42  import javax.portlet.RenderResponse;
43  
44  /**
45   * <a href="ConfigurationActionImpl.java.html"><b><i>View Source</i></b></a>
46   *
47   * @author Brian Wing Shun Chan
48   *
49   */
50  public class ConfigurationActionImpl implements ConfigurationAction {
51  
52      public void processAction(
53              PortletConfig portletConfig, ActionRequest actionRequest,
54              ActionResponse actionResponse)
55          throws Exception {
56  
57          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
58  
59          String portletResource = ParamUtil.getString(
60              actionRequest, "portletResource");
61  
62          PortletPreferences prefs =
63              PortletPreferencesFactoryUtil.getPortletSetup(
64                  actionRequest, portletResource);
65  
66          if (cmd.equals("add-selection")) {
67              AssetPublisherUtil.addSelection(actionRequest, prefs);
68          }
69          else if (cmd.equals("move-selection-down")) {
70              moveSelectionDown(actionRequest, prefs);
71          }
72          else if (cmd.equals("move-selection-up")) {
73              moveSelectionUp(actionRequest, prefs);
74          }
75          else if (cmd.equals("remove-selection")) {
76              removeSelection(actionRequest, prefs);
77          }
78          else if (cmd.equals("selection-style")) {
79              setSelectionStyle(actionRequest, prefs);
80          }
81          else if (cmd.equals(Constants.UPDATE)) {
82              String selectionStyle = prefs.getValue(
83                  "selection-style", "dynamic");
84  
85              if (selectionStyle.equals("dynamic")) {
86                  updateDynamicSettings(actionRequest, prefs);
87              }
88              else if (selectionStyle.equals("manual")) {
89                  updateManualSettings(actionRequest, prefs);
90              }
91          }
92  
93          if (SessionErrors.isEmpty(actionRequest)) {
94              prefs.store();
95  
96              SessionMessages.add(
97                  actionRequest, portletConfig.getPortletName() + ".doConfigure");
98          }
99      }
100 
101     public String render(
102             PortletConfig portletConfig, RenderRequest renderRequest,
103             RenderResponse renderResponse)
104         throws Exception {
105 
106         return "/html/portlet/tagged_content/configuration.jsp";
107     }
108 
109     protected void moveSelectionDown(
110             ActionRequest actionRequest, PortletPreferences prefs)
111         throws Exception {
112 
113         int assetOrder = ParamUtil.getInteger(actionRequest, "assetOrder");
114 
115         String[] manualEntries = prefs.getValues(
116             "manual-entries", new String[0]);
117 
118         if ((assetOrder >= (manualEntries.length - 1)) || (assetOrder < 0)) {
119             return;
120         }
121 
122         String temp = manualEntries[assetOrder + 1];
123 
124         manualEntries[assetOrder + 1] = manualEntries[assetOrder];
125         manualEntries[assetOrder] = temp;
126 
127         prefs.setValues("manual-entries", manualEntries);
128     }
129 
130     protected void moveSelectionUp(
131             ActionRequest actionRequest, PortletPreferences prefs)
132         throws Exception {
133 
134         int assetOrder = ParamUtil.getInteger(actionRequest, "assetOrder");
135 
136         String[] manualEntries = prefs.getValues(
137             "manual-entries", new String[0]);
138 
139         if ((assetOrder >= manualEntries.length) || (assetOrder <= 0)) {
140             return;
141         }
142 
143         String temp = manualEntries[assetOrder - 1];
144 
145         manualEntries[assetOrder - 1] = manualEntries[assetOrder];
146         manualEntries[assetOrder] = temp;
147 
148         prefs.setValues("manual-entries", manualEntries);
149     }
150 
151     protected void removeSelection(
152             ActionRequest actionRequest, PortletPreferences prefs)
153         throws Exception {
154 
155         int assetOrder = ParamUtil.getInteger(actionRequest, "assetOrder");
156 
157         String[] manualEntries = prefs.getValues(
158             "manual-entries", new String[0]);
159 
160         if (assetOrder >= manualEntries.length) {
161             return;
162         }
163 
164         String[] newEntries = new String[manualEntries.length -1];
165 
166         int i = 0;
167         int j = 0;
168 
169         for (; i < manualEntries.length; i++) {
170             if (i != assetOrder) {
171                 newEntries[j++] = manualEntries[i];
172             }
173         }
174 
175         prefs.setValues("manual-entries", newEntries);
176     }
177 
178     protected void setSelectionStyle(
179             ActionRequest actionRequest, PortletPreferences prefs)
180         throws Exception {
181 
182         String selectionStyle = ParamUtil.getString(
183             actionRequest, "selectionStyle");
184         String displayStyle = ParamUtil.getString(
185             actionRequest, "displayStyle");
186 
187         prefs.setValue("selection-style", selectionStyle);
188 
189         if (selectionStyle.equals("manual") ||
190             selectionStyle.equals("view-count")) {
191 
192             prefs.setValue("show-query-logic", String.valueOf(false));
193         }
194 
195         if (!selectionStyle.equals("view-count") &&
196             displayStyle.equals("view-count-details")) {
197 
198             prefs.setValue("display-style", "full-content");
199         }
200     }
201 
202     protected void updateDynamicSettings(
203             ActionRequest actionRequest, PortletPreferences prefs)
204         throws Exception {
205 
206         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
207             WebKeys.THEME_DISPLAY);
208 
209         long userId = themeDisplay.getUserId();
210 
211         String[] entries = StringUtil.split(
212             ParamUtil.getString(actionRequest, "entries"));
213         String[] notEntries = StringUtil.split(
214             ParamUtil.getString(actionRequest, "notEntries"));
215         boolean mergeUrlTags = ParamUtil.getBoolean(
216             actionRequest, "mergeUrlTags");
217         boolean andOperator = ParamUtil.getBoolean(
218             actionRequest, "andOperator");
219 
220         long classNameId = ParamUtil.getLong(actionRequest, "classNameId");
221         String category = ParamUtil.getString(actionRequest, "category");
222         String displayStyle = ParamUtil.getString(
223             actionRequest, "displayStyle");
224         String orderByColumn1 = ParamUtil.getString(
225             actionRequest, "orderByColumn1");
226         String orderByColumn2 = ParamUtil.getString(
227             actionRequest, "orderByColumn2");
228         String orderByType1 = ParamUtil.getString(
229             actionRequest, "orderByType1");
230         String orderByType2 = ParamUtil.getString(
231             actionRequest, "orderByType2");
232         boolean excludeZeroViewCount = ParamUtil.getBoolean(
233             actionRequest, "excludeZeroViewCount");
234         boolean showQueryLogic = ParamUtil.getBoolean(
235             actionRequest, "showQueryLogic");
236         int delta = ParamUtil.getInteger(actionRequest, "delta");
237         String paginationType = ParamUtil.getString(
238             actionRequest, "paginationType");
239         boolean showAvailableLocales = ParamUtil.getBoolean(
240             actionRequest, "showAvailableLocales");
241         boolean enableComments = ParamUtil.getBoolean(
242             actionRequest, "enableComments");
243         boolean enableCommentRatings = ParamUtil.getBoolean(
244             actionRequest, "enableCommentRatings");
245         boolean enableRatings = ParamUtil.getBoolean(
246             actionRequest, "enableRatings");
247         String medatadaFields = ParamUtil.getString(
248             actionRequest, "metadataFields");
249 
250         prefs.setValues("entries", entries);
251         prefs.setValues("not-entries", notEntries);
252         prefs.setValue("merge-url-tags", String.valueOf(mergeUrlTags));
253         prefs.setValue("and-operator", String.valueOf(andOperator));
254 
255         prefs.setValue("class-name-id", String.valueOf(classNameId));
256         prefs.setValue("category", category);
257         prefs.setValue("display-style", displayStyle);
258         prefs.setValue("order-by-column-1", orderByColumn1);
259         prefs.setValue("order-by-column-2", orderByColumn2);
260         prefs.setValue("order-by-type-1", orderByType1);
261         prefs.setValue("order-by-type-2", orderByType2);
262         prefs.setValue(
263             "exclude-zero-view-count", String.valueOf(excludeZeroViewCount));
264         prefs.setValue("show-query-logic", String.valueOf(showQueryLogic));
265         prefs.setValue("delta", String.valueOf(delta));
266         prefs.setValue("pagination-type", paginationType);
267         prefs.setValue(
268             "show-available-locales", String.valueOf(showAvailableLocales));
269         prefs.setValue("enable-ratings", String.valueOf(enableRatings));
270         prefs.setValue("enable-comments", String.valueOf(enableComments));
271         prefs.setValue(
272             "enable-comment-ratings", String.valueOf(enableCommentRatings));
273         prefs.setValue("metadata-fields", medatadaFields);
274 
275         TagsEntryLocalServiceUtil.checkEntries(userId, entries);
276         TagsEntryLocalServiceUtil.checkEntries(userId, notEntries);
277     }
278 
279     protected void updateManualSettings(
280             ActionRequest actionRequest, PortletPreferences prefs)
281         throws Exception {
282 
283         String displayStyle = ParamUtil.getString(
284             actionRequest, "displayStyle");
285         boolean showAvailableLocales = ParamUtil.getBoolean(
286             actionRequest, "showAvailableLocales");
287         boolean enableComments = ParamUtil.getBoolean(
288             actionRequest, "enableComments");
289         boolean enableCommentRatings = ParamUtil.getBoolean(
290             actionRequest, "enableCommentRatings");
291         boolean enableRatings = ParamUtil.getBoolean(
292             actionRequest, "enableRatings");
293 
294         prefs.setValue("display-style", displayStyle);
295         prefs.setValue(
296             "show-available-locales", String.valueOf(showAvailableLocales));
297         prefs.setValue("enable-comments", String.valueOf(enableComments));
298         prefs.setValue(
299             "enable-comment-ratings", String.valueOf(enableCommentRatings));
300         prefs.setValue("enable-ratings", String.valueOf(enableRatings));
301     }
302 
303 }