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