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