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.assetpublisher.action;
21  
22  import com.liferay.portal.kernel.portlet.BaseConfigurationAction;
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.assetpublisher.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 extends BaseConfigurationAction {
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 preferences =
62                  PortletPreferencesFactoryUtil.getPortletSetup(
63                      actionRequest, portletResource);
64  
65              if (cmd.equals("add-selection")) {
66                  AssetPublisherUtil.addSelection(actionRequest, preferences);
67              }
68              else if (cmd.equals("move-selection-down")) {
69                  moveSelectionDown(actionRequest, preferences);
70              }
71              else if (cmd.equals("move-selection-up")) {
72                  moveSelectionUp(actionRequest, preferences);
73              }
74              else if (cmd.equals("remove-selection")) {
75                  removeSelection(actionRequest, preferences);
76              }
77              else if (cmd.equals("selection-style")) {
78                  setSelectionStyle(actionRequest, preferences);
79              }
80              else if (cmd.equals(Constants.UPDATE)) {
81                  String selectionStyle = preferences.getValue(
82                      "selection-style", "dynamic");
83  
84                  if (selectionStyle.equals("dynamic")) {
85                      updateDynamicSettings(actionRequest, preferences);
86                  }
87                  else if (selectionStyle.equals("manual")) {
88                      updateManualSettings(actionRequest, preferences);
89                  }
90              }
91  
92              if (SessionErrors.isEmpty(actionRequest)) {
93                  preferences.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/asset_publisher/configuration.jsp";
116     }
117 
118     protected void moveSelectionDown(
119             ActionRequest actionRequest, PortletPreferences preferences)
120         throws Exception {
121 
122         int assetOrder = ParamUtil.getInteger(actionRequest, "assetOrder");
123 
124         String[] manualEntries = preferences.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         preferences.setValues("manual-entries", manualEntries);
137     }
138 
139     protected void moveSelectionUp(
140             ActionRequest actionRequest, PortletPreferences preferences)
141         throws Exception {
142 
143         int assetOrder = ParamUtil.getInteger(actionRequest, "assetOrder");
144 
145         String[] manualEntries = preferences.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         preferences.setValues("manual-entries", manualEntries);
158     }
159 
160     protected void removeSelection(
161             ActionRequest actionRequest, PortletPreferences preferences)
162         throws Exception {
163 
164         int assetOrder = ParamUtil.getInteger(actionRequest, "assetOrder");
165 
166         String[] manualEntries = preferences.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         preferences.setValues("manual-entries", newEntries);
185     }
186 
187     protected void setSelectionStyle(
188             ActionRequest actionRequest, PortletPreferences preferences)
189         throws Exception {
190 
191         String selectionStyle = ParamUtil.getString(
192             actionRequest, "selectionStyle");
193         String displayStyle = ParamUtil.getString(
194             actionRequest, "displayStyle");
195 
196         preferences.setValue("selection-style", selectionStyle);
197 
198         if (selectionStyle.equals("manual") ||
199             selectionStyle.equals("view-count")) {
200 
201             preferences.setValue("show-query-logic", String.valueOf(false));
202         }
203 
204         if (!selectionStyle.equals("view-count") &&
205             displayStyle.equals("view-count-details")) {
206 
207             preferences.setValue("display-style", "full-content");
208         }
209     }
210 
211     protected void updateDynamicSettings(
212             ActionRequest actionRequest, PortletPreferences preferences)
213         throws Exception {
214 
215         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
216             WebKeys.THEME_DISPLAY);
217 
218         long userId = themeDisplay.getUserId();
219         long groupId = themeDisplay.getScopeGroupId();
220 
221         String[] entries = StringUtil.split(
222             ParamUtil.getString(actionRequest, "entries"));
223         String[] notEntries = StringUtil.split(
224             ParamUtil.getString(actionRequest, "notEntries"));
225         boolean mergeUrlTags = ParamUtil.getBoolean(
226             actionRequest, "mergeUrlTags");
227         boolean andOperator = ParamUtil.getBoolean(
228             actionRequest, "andOperator");
229 
230         long classNameId = ParamUtil.getLong(actionRequest, "classNameId");
231         String category = ParamUtil.getString(actionRequest, "category");
232         String displayStyle = ParamUtil.getString(
233             actionRequest, "displayStyle");
234         boolean showAssetTitle = ParamUtil.getBoolean(
235             actionRequest, "showAssetTitle");
236         boolean showContextLink = ParamUtil.getBoolean(
237             actionRequest, "showContextLink");
238         int abstractLength = ParamUtil.getInteger(
239             actionRequest, "abstractLength");
240         String assetLinkBehaviour = ParamUtil.getString(
241             actionRequest, "assetLinkBehaviour");
242         String orderByColumn1 = ParamUtil.getString(
243             actionRequest, "orderByColumn1");
244         String orderByColumn2 = ParamUtil.getString(
245             actionRequest, "orderByColumn2");
246         String orderByType1 = ParamUtil.getString(
247             actionRequest, "orderByType1");
248         String orderByType2 = ParamUtil.getString(
249             actionRequest, "orderByType2");
250         boolean excludeZeroViewCount = ParamUtil.getBoolean(
251             actionRequest, "excludeZeroViewCount");
252         boolean showQueryLogic = ParamUtil.getBoolean(
253             actionRequest, "showQueryLogic");
254         int delta = ParamUtil.getInteger(actionRequest, "delta");
255         String paginationType = ParamUtil.getString(
256             actionRequest, "paginationType");
257         boolean showAvailableLocales = ParamUtil.getBoolean(
258             actionRequest, "showAvailableLocales");
259         boolean enableComments = ParamUtil.getBoolean(
260             actionRequest, "enableComments");
261         boolean enableCommentRatings = ParamUtil.getBoolean(
262             actionRequest, "enableCommentRatings");
263         boolean enableRatings = ParamUtil.getBoolean(
264             actionRequest, "enableRatings");
265         String medatadaFields = ParamUtil.getString(
266             actionRequest, "metadataFields");
267 
268         preferences.setValue("selection-style", "dynamic");
269 
270         preferences.setValues("entries", entries);
271         preferences.setValues("not-entries", notEntries);
272         preferences.setValue("merge-url-tags", String.valueOf(mergeUrlTags));
273         preferences.setValue("and-operator", String.valueOf(andOperator));
274 
275         preferences.setValue("class-name-id", String.valueOf(classNameId));
276         preferences.setValue("category", category);
277         preferences.setValue("display-style", displayStyle);
278         preferences.setValue(
279             "show-asset-title", String.valueOf(showAssetTitle));
280         preferences.setValue(
281             "show-context-link", String.valueOf(showContextLink));
282         preferences.setValue("abstract-length", String.valueOf(abstractLength));
283         preferences.setValue("asset-link-behaviour", assetLinkBehaviour);
284         preferences.setValue("order-by-column-1", orderByColumn1);
285         preferences.setValue("order-by-column-2", orderByColumn2);
286         preferences.setValue("order-by-type-1", orderByType1);
287         preferences.setValue("order-by-type-2", orderByType2);
288         preferences.setValue(
289             "exclude-zero-view-count", String.valueOf(excludeZeroViewCount));
290         preferences.setValue(
291             "show-query-logic", String.valueOf(showQueryLogic));
292         preferences.setValue("delta", String.valueOf(delta));
293         preferences.setValue("pagination-type", paginationType);
294         preferences.setValue(
295             "show-available-locales", String.valueOf(showAvailableLocales));
296         preferences.setValue("enable-ratings", String.valueOf(enableRatings));
297         preferences.setValue("enable-comments", String.valueOf(enableComments));
298         preferences.setValue(
299             "enable-comment-ratings", String.valueOf(enableCommentRatings));
300         preferences.setValue("metadata-fields", medatadaFields);
301 
302         TagsEntryLocalServiceUtil.checkEntries(userId, groupId, entries);
303         TagsEntryLocalServiceUtil.checkEntries(userId, groupId, notEntries);
304     }
305 
306     protected void updateManualSettings(
307             ActionRequest actionRequest, PortletPreferences preferences)
308         throws Exception {
309 
310         String displayStyle = ParamUtil.getString(
311             actionRequest, "displayStyle");
312         boolean showAssetTitle = ParamUtil.getBoolean(
313             actionRequest, "showAssetTitle");
314         boolean showContextLink = ParamUtil.getBoolean(
315             actionRequest, "showContextLink");
316         int abstractLength = ParamUtil.getInteger(
317             actionRequest, "abstractLength");
318         String assetLinkBehaviour = ParamUtil.getString(
319             actionRequest, "assetLinkBehaviour");
320         boolean showAvailableLocales = ParamUtil.getBoolean(
321             actionRequest, "showAvailableLocales");
322         boolean enableComments = ParamUtil.getBoolean(
323             actionRequest, "enableComments");
324         boolean enableCommentRatings = ParamUtil.getBoolean(
325             actionRequest, "enableCommentRatings");
326         boolean enableRatings = ParamUtil.getBoolean(
327             actionRequest, "enableRatings");
328         boolean enableTagBasedNavigation = ParamUtil.getBoolean(
329             actionRequest, "enableTagBasedNavigation");
330         String medatadaFields = ParamUtil.getString(
331             actionRequest, "metadataFields");
332 
333         preferences.setValue("selection-style", "manual");
334         preferences.setValue("display-style", displayStyle);
335         preferences.setValue(
336             "show-asset-title", String.valueOf(showAssetTitle));
337         preferences.setValue(
338             "show-context-link", String.valueOf(showContextLink));
339         preferences.setValue("abstract-length", String.valueOf(abstractLength));
340         preferences.setValue("asset-link-behaviour", assetLinkBehaviour);
341         preferences.setValue(
342             "show-available-locales", String.valueOf(showAvailableLocales));
343         preferences.setValue("enable-comments", String.valueOf(enableComments));
344         preferences.setValue(
345             "enable-comment-ratings", String.valueOf(enableCommentRatings));
346         preferences.setValue("enable-ratings", String.valueOf(enableRatings));
347         preferences.setValue(
348             "enable-tag-based-navigation",
349             String.valueOf(enableTagBasedNavigation));
350         preferences.setValue("metadata-fields", medatadaFields);
351     }
352 
353 }