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