1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class ConfigurationActionImpl extends BaseConfigurationAction {
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          try {
60              String portletResource = ParamUtil.getString(
61                  actionRequest, "portletResource");
62  
63              PortletPreferences preferences =
64                  PortletPreferencesFactoryUtil.getPortletSetup(
65                      actionRequest, portletResource);
66  
67              if (cmd.equals("add-selection")) {
68                  AssetPublisherUtil.addSelection(actionRequest, preferences);
69              }
70              else if (cmd.equals("move-selection-down")) {
71                  moveSelectionDown(actionRequest, preferences);
72              }
73              else if (cmd.equals("move-selection-up")) {
74                  moveSelectionUp(actionRequest, preferences);
75              }
76              else if (cmd.equals("remove-selection")) {
77                  removeSelection(actionRequest, preferences);
78              }
79              else if (cmd.equals("selection-style")) {
80                  setSelectionStyle(actionRequest, preferences);
81              }
82              else if (cmd.equals(Constants.UPDATE)) {
83                  String selectionStyle = preferences.getValue(
84                      "selection-style", "dynamic");
85  
86                  if (selectionStyle.equals("dynamic")) {
87                      updateDynamicSettings(actionRequest, preferences);
88                  }
89                  else if (selectionStyle.equals("manual")) {
90                      updateManualSettings(actionRequest, preferences);
91                  }
92              }
93  
94              if (SessionErrors.isEmpty(actionRequest)) {
95                  preferences.store();
96  
97                  SessionMessages.add(
98                      actionRequest,
99                      portletConfig.getPortletName() + ".doConfigure");
100             }
101         }
102         catch (Exception e) {
103             if (e instanceof TagsEntryException) {
104                 SessionErrors.add(actionRequest, e.getClass().getName(), e);
105             }
106             else {
107                 throw e;
108             }
109         }
110     }
111 
112     public String render(
113             PortletConfig portletConfig, RenderRequest renderRequest,
114             RenderResponse renderResponse)
115         throws Exception {
116 
117         return "/html/portlet/tagged_content/configuration.jsp";
118     }
119 
120     protected void moveSelectionDown(
121             ActionRequest actionRequest, PortletPreferences preferences)
122         throws Exception {
123 
124         int assetOrder = ParamUtil.getInteger(actionRequest, "assetOrder");
125 
126         String[] manualEntries = preferences.getValues(
127             "manual-entries", new String[0]);
128 
129         if ((assetOrder >= (manualEntries.length - 1)) || (assetOrder < 0)) {
130             return;
131         }
132 
133         String temp = manualEntries[assetOrder + 1];
134 
135         manualEntries[assetOrder + 1] = manualEntries[assetOrder];
136         manualEntries[assetOrder] = temp;
137 
138         preferences.setValues("manual-entries", manualEntries);
139     }
140 
141     protected void moveSelectionUp(
142             ActionRequest actionRequest, PortletPreferences preferences)
143         throws Exception {
144 
145         int assetOrder = ParamUtil.getInteger(actionRequest, "assetOrder");
146 
147         String[] manualEntries = preferences.getValues(
148             "manual-entries", new String[0]);
149 
150         if ((assetOrder >= manualEntries.length) || (assetOrder <= 0)) {
151             return;
152         }
153 
154         String temp = manualEntries[assetOrder - 1];
155 
156         manualEntries[assetOrder - 1] = manualEntries[assetOrder];
157         manualEntries[assetOrder] = temp;
158 
159         preferences.setValues("manual-entries", manualEntries);
160     }
161 
162     protected void removeSelection(
163             ActionRequest actionRequest, PortletPreferences preferences)
164         throws Exception {
165 
166         int assetOrder = ParamUtil.getInteger(actionRequest, "assetOrder");
167 
168         String[] manualEntries = preferences.getValues(
169             "manual-entries", new String[0]);
170 
171         if (assetOrder >= manualEntries.length) {
172             return;
173         }
174 
175         String[] newEntries = new String[manualEntries.length -1];
176 
177         int i = 0;
178         int j = 0;
179 
180         for (; i < manualEntries.length; i++) {
181             if (i != assetOrder) {
182                 newEntries[j++] = manualEntries[i];
183             }
184         }
185 
186         preferences.setValues("manual-entries", newEntries);
187     }
188 
189     protected void setSelectionStyle(
190             ActionRequest actionRequest, PortletPreferences preferences)
191         throws Exception {
192 
193         String selectionStyle = ParamUtil.getString(
194             actionRequest, "selectionStyle");
195         String displayStyle = ParamUtil.getString(
196             actionRequest, "displayStyle");
197 
198         preferences.setValue("selection-style", selectionStyle);
199 
200         if (selectionStyle.equals("manual") ||
201             selectionStyle.equals("view-count")) {
202 
203             preferences.setValue("show-query-logic", String.valueOf(false));
204         }
205 
206         if (!selectionStyle.equals("view-count") &&
207             displayStyle.equals("view-count-details")) {
208 
209             preferences.setValue("display-style", "full-content");
210         }
211     }
212 
213     protected void updateDynamicSettings(
214             ActionRequest actionRequest, PortletPreferences preferences)
215         throws Exception {
216 
217         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
218             WebKeys.THEME_DISPLAY);
219 
220         long userId = themeDisplay.getUserId();
221 
222         String[] entries = StringUtil.split(
223             ParamUtil.getString(actionRequest, "entries"));
224         String[] notEntries = StringUtil.split(
225             ParamUtil.getString(actionRequest, "notEntries"));
226         boolean mergeUrlTags = ParamUtil.getBoolean(
227             actionRequest, "mergeUrlTags");
228         boolean andOperator = ParamUtil.getBoolean(
229             actionRequest, "andOperator");
230 
231         long classNameId = ParamUtil.getLong(actionRequest, "classNameId");
232         String category = ParamUtil.getString(actionRequest, "category");
233         String displayStyle = ParamUtil.getString(
234             actionRequest, "displayStyle");
235         String orderByColumn1 = ParamUtil.getString(
236             actionRequest, "orderByColumn1");
237         String orderByColumn2 = ParamUtil.getString(
238             actionRequest, "orderByColumn2");
239         String orderByType1 = ParamUtil.getString(
240             actionRequest, "orderByType1");
241         String orderByType2 = ParamUtil.getString(
242             actionRequest, "orderByType2");
243         boolean excludeZeroViewCount = ParamUtil.getBoolean(
244             actionRequest, "excludeZeroViewCount");
245         boolean showQueryLogic = ParamUtil.getBoolean(
246             actionRequest, "showQueryLogic");
247         int delta = ParamUtil.getInteger(actionRequest, "delta");
248         String paginationType = ParamUtil.getString(
249             actionRequest, "paginationType");
250         boolean showAvailableLocales = ParamUtil.getBoolean(
251             actionRequest, "showAvailableLocales");
252         boolean enableComments = ParamUtil.getBoolean(
253             actionRequest, "enableComments");
254         boolean enableCommentRatings = ParamUtil.getBoolean(
255             actionRequest, "enableCommentRatings");
256         boolean enableRatings = ParamUtil.getBoolean(
257             actionRequest, "enableRatings");
258         String medatadaFields = ParamUtil.getString(
259             actionRequest, "metadataFields");
260 
261         preferences.setValue("selection-style", "dynamic");
262 
263         preferences.setValues("entries", entries);
264         preferences.setValues("not-entries", notEntries);
265         preferences.setValue("merge-url-tags", String.valueOf(mergeUrlTags));
266         preferences.setValue("and-operator", String.valueOf(andOperator));
267 
268         preferences.setValue("class-name-id", String.valueOf(classNameId));
269         preferences.setValue("category", category);
270         preferences.setValue("display-style", displayStyle);
271         preferences.setValue("order-by-column-1", orderByColumn1);
272         preferences.setValue("order-by-column-2", orderByColumn2);
273         preferences.setValue("order-by-type-1", orderByType1);
274         preferences.setValue("order-by-type-2", orderByType2);
275         preferences.setValue(
276             "exclude-zero-view-count", String.valueOf(excludeZeroViewCount));
277         preferences.setValue(
278             "show-query-logic", String.valueOf(showQueryLogic));
279         preferences.setValue("delta", String.valueOf(delta));
280         preferences.setValue("pagination-type", paginationType);
281         preferences.setValue(
282             "show-available-locales", String.valueOf(showAvailableLocales));
283         preferences.setValue("enable-ratings", String.valueOf(enableRatings));
284         preferences.setValue("enable-comments", String.valueOf(enableComments));
285         preferences.setValue(
286             "enable-comment-ratings", String.valueOf(enableCommentRatings));
287         preferences.setValue("metadata-fields", medatadaFields);
288 
289         TagsEntryLocalServiceUtil.checkEntries(userId, entries);
290         TagsEntryLocalServiceUtil.checkEntries(userId, notEntries);
291     }
292 
293     protected void updateManualSettings(
294             ActionRequest actionRequest, PortletPreferences preferences)
295         throws Exception {
296 
297         String displayStyle = ParamUtil.getString(
298             actionRequest, "displayStyle");
299         boolean showAvailableLocales = ParamUtil.getBoolean(
300             actionRequest, "showAvailableLocales");
301         boolean enableComments = ParamUtil.getBoolean(
302             actionRequest, "enableComments");
303         boolean enableCommentRatings = ParamUtil.getBoolean(
304             actionRequest, "enableCommentRatings");
305         boolean enableRatings = ParamUtil.getBoolean(
306             actionRequest, "enableRatings");
307         String medatadaFields = ParamUtil.getString(
308             actionRequest, "metadataFields");
309 
310         preferences.setValue("display-style", displayStyle);
311         preferences.setValue(
312             "show-available-locales", String.valueOf(showAvailableLocales));
313         preferences.setValue("enable-comments", String.valueOf(enableComments));
314         preferences.setValue(
315             "enable-comment-ratings", String.valueOf(enableCommentRatings));
316         preferences.setValue("enable-ratings", String.valueOf(enableRatings));
317         preferences.setValue("metadata-fields", medatadaFields);
318     }
319 
320 }