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.assetpublisher.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.assetpublisher.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/asset_publisher/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         long groupId = themeDisplay.getScopeGroupId();
222 
223         String[] entries = StringUtil.split(
224             ParamUtil.getString(actionRequest, "entries"));
225         String[] notEntries = StringUtil.split(
226             ParamUtil.getString(actionRequest, "notEntries"));
227         boolean mergeUrlTags = ParamUtil.getBoolean(
228             actionRequest, "mergeUrlTags");
229         boolean andOperator = ParamUtil.getBoolean(
230             actionRequest, "andOperator");
231 
232         long classNameId = ParamUtil.getLong(actionRequest, "classNameId");
233         String category = ParamUtil.getString(actionRequest, "category");
234         String displayStyle = ParamUtil.getString(
235             actionRequest, "displayStyle");
236         boolean showAssetTitle = ParamUtil.getBoolean(
237             actionRequest, "showAssetTitle");
238         boolean showContextLink = ParamUtil.getBoolean(
239             actionRequest, "showContextLink");
240         int abstractLength = ParamUtil.getInteger(
241             actionRequest, "abstractLength");
242         String assetLinkBehaviour = ParamUtil.getString(
243             actionRequest, "assetLinkBehaviour");
244         String orderByColumn1 = ParamUtil.getString(
245             actionRequest, "orderByColumn1");
246         String orderByColumn2 = ParamUtil.getString(
247             actionRequest, "orderByColumn2");
248         String orderByType1 = ParamUtil.getString(
249             actionRequest, "orderByType1");
250         String orderByType2 = ParamUtil.getString(
251             actionRequest, "orderByType2");
252         boolean excludeZeroViewCount = ParamUtil.getBoolean(
253             actionRequest, "excludeZeroViewCount");
254         boolean showQueryLogic = ParamUtil.getBoolean(
255             actionRequest, "showQueryLogic");
256         int delta = ParamUtil.getInteger(actionRequest, "delta");
257         String paginationType = ParamUtil.getString(
258             actionRequest, "paginationType");
259         boolean showAvailableLocales = ParamUtil.getBoolean(
260             actionRequest, "showAvailableLocales");
261         boolean enableComments = ParamUtil.getBoolean(
262             actionRequest, "enableComments");
263         boolean enableCommentRatings = ParamUtil.getBoolean(
264             actionRequest, "enableCommentRatings");
265         boolean enableRatings = ParamUtil.getBoolean(
266             actionRequest, "enableRatings");
267         String medatadaFields = ParamUtil.getString(
268             actionRequest, "metadataFields");
269 
270         preferences.setValue("selection-style", "dynamic");
271 
272         preferences.setValues("entries", entries);
273         preferences.setValues("not-entries", notEntries);
274         preferences.setValue("merge-url-tags", String.valueOf(mergeUrlTags));
275         preferences.setValue("and-operator", String.valueOf(andOperator));
276 
277         preferences.setValue("class-name-id", String.valueOf(classNameId));
278         preferences.setValue("category", category);
279         preferences.setValue("display-style", displayStyle);
280         preferences.setValue(
281             "show-asset-title", String.valueOf(showAssetTitle));
282         preferences.setValue(
283             "show-context-link", String.valueOf(showContextLink));
284         preferences.setValue("abstract-length", String.valueOf(abstractLength));
285         preferences.setValue("asset-link-behaviour", assetLinkBehaviour);
286         preferences.setValue("order-by-column-1", orderByColumn1);
287         preferences.setValue("order-by-column-2", orderByColumn2);
288         preferences.setValue("order-by-type-1", orderByType1);
289         preferences.setValue("order-by-type-2", orderByType2);
290         preferences.setValue(
291             "exclude-zero-view-count", String.valueOf(excludeZeroViewCount));
292         preferences.setValue(
293             "show-query-logic", String.valueOf(showQueryLogic));
294         preferences.setValue("delta", String.valueOf(delta));
295         preferences.setValue("pagination-type", paginationType);
296         preferences.setValue(
297             "show-available-locales", String.valueOf(showAvailableLocales));
298         preferences.setValue("enable-ratings", String.valueOf(enableRatings));
299         preferences.setValue("enable-comments", String.valueOf(enableComments));
300         preferences.setValue(
301             "enable-comment-ratings", String.valueOf(enableCommentRatings));
302         preferences.setValue("metadata-fields", medatadaFields);
303 
304         TagsEntryLocalServiceUtil.checkEntries(userId, groupId, entries);
305         TagsEntryLocalServiceUtil.checkEntries(userId, groupId, notEntries);
306     }
307 
308     protected void updateManualSettings(
309             ActionRequest actionRequest, PortletPreferences preferences)
310         throws Exception {
311 
312         String displayStyle = ParamUtil.getString(
313             actionRequest, "displayStyle");
314         boolean showAssetTitle = ParamUtil.getBoolean(
315             actionRequest, "showAssetTitle");
316         boolean showContextLink = ParamUtil.getBoolean(
317             actionRequest, "showContextLink");
318         int abstractLength = ParamUtil.getInteger(
319             actionRequest, "abstractLength");
320         String assetLinkBehaviour = ParamUtil.getString(
321             actionRequest, "assetLinkBehaviour");
322         boolean showAvailableLocales = ParamUtil.getBoolean(
323             actionRequest, "showAvailableLocales");
324         boolean enableComments = ParamUtil.getBoolean(
325             actionRequest, "enableComments");
326         boolean enableCommentRatings = ParamUtil.getBoolean(
327             actionRequest, "enableCommentRatings");
328         boolean enableRatings = ParamUtil.getBoolean(
329             actionRequest, "enableRatings");
330         boolean enableTagBasedNavigation = ParamUtil.getBoolean(
331             actionRequest, "enableTagBasedNavigation");
332         String medatadaFields = ParamUtil.getString(
333             actionRequest, "metadataFields");
334 
335         preferences.setValue("selection-style", "manual");
336         preferences.setValue("display-style", displayStyle);
337         preferences.setValue(
338             "show-asset-title", String.valueOf(showAssetTitle));
339         preferences.setValue(
340             "show-context-link", String.valueOf(showContextLink));
341         preferences.setValue("abstract-length", String.valueOf(abstractLength));
342         preferences.setValue("asset-link-behaviour", assetLinkBehaviour);
343         preferences.setValue(
344             "show-available-locales", String.valueOf(showAvailableLocales));
345         preferences.setValue("enable-comments", String.valueOf(enableComments));
346         preferences.setValue(
347             "enable-comment-ratings", String.valueOf(enableCommentRatings));
348         preferences.setValue("enable-ratings", String.valueOf(enableRatings));
349         preferences.setValue(
350             "enable-tag-based-navigation",
351             String.valueOf(enableTagBasedNavigation));
352         preferences.setValue("metadata-fields", medatadaFields);
353     }
354 
355 }