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