001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.assetpublisher.action;
016    
017    import com.liferay.portal.kernel.portlet.BaseConfigurationAction;
018    import com.liferay.portal.kernel.servlet.SessionErrors;
019    import com.liferay.portal.kernel.servlet.SessionMessages;
020    import com.liferay.portal.kernel.util.ArrayUtil;
021    import com.liferay.portal.kernel.util.Constants;
022    import com.liferay.portal.kernel.util.ParamUtil;
023    import com.liferay.portal.kernel.util.StringPool;
024    import com.liferay.portal.kernel.util.StringUtil;
025    import com.liferay.portal.theme.ThemeDisplay;
026    import com.liferay.portal.util.WebKeys;
027    import com.liferay.portlet.PortletPreferencesFactoryUtil;
028    import com.liferay.portlet.asset.AssetTagException;
029    import com.liferay.portlet.asset.service.AssetTagLocalServiceUtil;
030    import com.liferay.portlet.assetpublisher.util.AssetPublisherUtil;
031    
032    import javax.portlet.ActionRequest;
033    import javax.portlet.ActionResponse;
034    import javax.portlet.PortletConfig;
035    import javax.portlet.PortletPreferences;
036    import javax.portlet.RenderRequest;
037    import javax.portlet.RenderResponse;
038    
039    /**
040     * @author Brian Wing Shun Chan
041     */
042    public class ConfigurationActionImpl extends BaseConfigurationAction {
043    
044            public void processAction(
045                            PortletConfig portletConfig, ActionRequest actionRequest,
046                            ActionResponse actionResponse)
047                    throws Exception {
048    
049                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
050    
051                    try {
052                            String portletResource = ParamUtil.getString(
053                                    actionRequest, "portletResource");
054    
055                            PortletPreferences preferences =
056                                    PortletPreferencesFactoryUtil.getPortletSetup(
057                                            actionRequest, portletResource);
058    
059                            if (cmd.equals("add-selection")) {
060                                    AssetPublisherUtil.addSelection(actionRequest, preferences);
061                            }
062                            else if (cmd.equals("move-selection-down")) {
063                                    moveSelectionDown(actionRequest, preferences);
064                            }
065                            else if (cmd.equals("move-selection-up")) {
066                                    moveSelectionUp(actionRequest, preferences);
067                            }
068                            else if (cmd.equals("remove-selection")) {
069                                    removeSelection(actionRequest, preferences);
070                            }
071                            else if (cmd.equals("selection-style")) {
072                                    setSelectionStyle(actionRequest, preferences);
073                            }
074                            else if (cmd.equals(Constants.UPDATE)) {
075                                    String selectionStyle = preferences.getValue(
076                                            "selection-style", "dynamic");
077    
078                                    if (selectionStyle.equals("dynamic")) {
079                                            updateDynamicSettings(actionRequest, preferences);
080                                    }
081                                    else if (selectionStyle.equals("manual")) {
082                                            updateManualSettings(actionRequest, preferences);
083                                    }
084                            }
085    
086                            if (SessionErrors.isEmpty(actionRequest)) {
087                                    preferences.store();
088    
089                                    SessionMessages.add(
090                                            actionRequest,
091                                            portletConfig.getPortletName() + ".doConfigure");
092                            }
093    
094                            actionResponse.sendRedirect(
095                                    ParamUtil.getString(actionRequest, "redirect"));
096                    }
097                    catch (Exception e) {
098                            if (e instanceof AssetTagException) {
099                                    SessionErrors.add(actionRequest, e.getClass().getName(), e);
100                            }
101                            else {
102                                    throw e;
103                            }
104                    }
105            }
106    
107            public String render(
108                            PortletConfig portletConfig, RenderRequest renderRequest,
109                            RenderResponse renderResponse)
110                    throws Exception {
111    
112                    return "/html/portlet/asset_publisher/configuration.jsp";
113            }
114    
115            protected void moveSelectionDown(
116                            ActionRequest actionRequest, PortletPreferences preferences)
117                    throws Exception {
118    
119                    int assetEntryOrder = ParamUtil.getInteger(
120                            actionRequest, "assetEntryOrder");
121    
122                    String[] manualEntries = preferences.getValues(
123                            "asset-entry-xml", new String[0]);
124    
125                    if ((assetEntryOrder >= (manualEntries.length - 1)) ||
126                            (assetEntryOrder < 0)) {
127    
128                            return;
129                    }
130    
131                    String temp = manualEntries[assetEntryOrder + 1];
132    
133                    manualEntries[assetEntryOrder + 1] = manualEntries[assetEntryOrder];
134                    manualEntries[assetEntryOrder] = temp;
135    
136                    preferences.setValues("asset-entry-xml", manualEntries);
137            }
138    
139            protected void moveSelectionUp(
140                            ActionRequest actionRequest, PortletPreferences preferences)
141                    throws Exception {
142    
143                    int assetEntryOrder = ParamUtil.getInteger(
144                            actionRequest, "assetEntryOrder");
145    
146                    String[] manualEntries = preferences.getValues(
147                            "asset-entry-xml", new String[0]);
148    
149                    if ((assetEntryOrder >= manualEntries.length) ||
150                            (assetEntryOrder <= 0)) {
151    
152                            return;
153                    }
154    
155                    String temp = manualEntries[assetEntryOrder - 1];
156    
157                    manualEntries[assetEntryOrder - 1] = manualEntries[assetEntryOrder];
158                    manualEntries[assetEntryOrder] = temp;
159    
160                    preferences.setValues("asset-entry-xml", manualEntries);
161            }
162    
163            protected void removeSelection(
164                            ActionRequest actionRequest, PortletPreferences preferences)
165                    throws Exception {
166    
167                    int assetEntryOrder = ParamUtil.getInteger(
168                            actionRequest, "assetEntryOrder");
169    
170                    String[] manualEntries = preferences.getValues(
171                            "asset-entry-xml", new String[0]);
172    
173                    if (assetEntryOrder >= manualEntries.length) {
174                            return;
175                    }
176    
177                    String[] newEntries = new String[manualEntries.length -1];
178    
179                    int i = 0;
180                    int j = 0;
181    
182                    for (; i < manualEntries.length; i++) {
183                            if (i != assetEntryOrder) {
184                                    newEntries[j++] = manualEntries[i];
185                            }
186                    }
187    
188                    preferences.setValues("asset-entry-xml", newEntries);
189            }
190    
191            protected void setSelectionStyle(
192                            ActionRequest actionRequest, PortletPreferences preferences)
193                    throws Exception {
194    
195                    String selectionStyle = ParamUtil.getString(
196                            actionRequest, "selectionStyle");
197                    String displayStyle = ParamUtil.getString(
198                            actionRequest, "displayStyle");
199    
200                    preferences.setValue("selection-style", selectionStyle);
201    
202                    if (selectionStyle.equals("manual") ||
203                            selectionStyle.equals("view-count")) {
204    
205                            preferences.setValue("show-query-logic", String.valueOf(false));
206                    }
207    
208                    if (!selectionStyle.equals("view-count") &&
209                            displayStyle.equals("view-count-details")) {
210    
211                            preferences.setValue("display-style", "full-content");
212                    }
213            }
214    
215            protected void updateDynamicSettings(
216                            ActionRequest actionRequest, PortletPreferences preferences)
217                    throws Exception {
218    
219                    updateDisplaySettings(actionRequest, preferences);
220                    updateQueryLogic(actionRequest, preferences);
221                    updateRssSettings(actionRequest, preferences);
222    
223                    boolean mergeUrlTags = ParamUtil.getBoolean(
224                            actionRequest, "mergeUrlTags");
225                    boolean defaultScope = ParamUtil.getBoolean(
226                            actionRequest, "defaultScope");
227                    String[] scopeIds = StringUtil.split(
228                            ParamUtil.getString(actionRequest, "scopeIds"));
229                    long assetVocabularyId = ParamUtil.getLong(
230                            actionRequest, "assetVocabularyId");
231                    String orderByColumn1 = ParamUtil.getString(
232                            actionRequest, "orderByColumn1");
233                    String orderByColumn2 = ParamUtil.getString(
234                            actionRequest, "orderByColumn2");
235                    String orderByType1 = ParamUtil.getString(
236                            actionRequest, "orderByType1");
237                    String orderByType2 = ParamUtil.getString(
238                            actionRequest, "orderByType2");
239                    boolean excludeZeroViewCount = ParamUtil.getBoolean(
240                            actionRequest, "excludeZeroViewCount");
241                    boolean showQueryLogic = ParamUtil.getBoolean(
242                            actionRequest, "showQueryLogic");
243                    int delta = ParamUtil.getInteger(actionRequest, "delta");
244                    String paginationType = ParamUtil.getString(
245                            actionRequest, "paginationType");
246                    String[] extensions = actionRequest.getParameterValues("extensions");
247    
248                    preferences.setValue("selection-style", "dynamic");
249                    preferences.setValue("merge-url-tags", String.valueOf(mergeUrlTags));
250                    preferences.setValue("default-scope", String.valueOf(defaultScope));
251                    preferences.setValues("scope-ids", ArrayUtil.toStringArray(scopeIds));
252                    preferences.setValue(
253                            "asset-vocabulary-id", String.valueOf(assetVocabularyId));
254                    preferences.setValue("order-by-column-1", orderByColumn1);
255                    preferences.setValue("order-by-column-2", orderByColumn2);
256                    preferences.setValue("order-by-type-1", orderByType1);
257                    preferences.setValue("order-by-type-2", orderByType2);
258                    preferences.setValue(
259                            "exclude-zero-view-count", String.valueOf(excludeZeroViewCount));
260                    preferences.setValue(
261                            "show-query-logic", String.valueOf(showQueryLogic));
262                    preferences.setValue("delta", String.valueOf(delta));
263                    preferences.setValue("pagination-type", paginationType);
264                    preferences.setValues("extensions", extensions);
265            }
266    
267            protected void updateManualSettings(
268                            ActionRequest actionRequest, PortletPreferences preferences)
269                    throws Exception {
270    
271                    updateDisplaySettings(actionRequest, preferences);
272                    updateRssSettings(actionRequest, preferences);
273            }
274    
275            protected void updateDisplaySettings(
276                            ActionRequest actionRequest, PortletPreferences preferences)
277                    throws Exception {
278    
279                    String displayStyle = ParamUtil.getString(
280                            actionRequest, "displayStyle");
281                    boolean anyAssetType = ParamUtil.getBoolean(
282                            actionRequest, "anyAssetType");
283                    long[] classNameIds = StringUtil.split(
284                            ParamUtil.getString(actionRequest, "classNameIds"), 0L);
285                    boolean showAssetTitle = ParamUtil.getBoolean(
286                            actionRequest, "showAssetTitle");
287                    boolean showContextLink = ParamUtil.getBoolean(
288                            actionRequest, "showContextLink");
289                    int abstractLength = ParamUtil.getInteger(
290                            actionRequest, "abstractLength");
291                    String assetLinkBehaviour = ParamUtil.getString(
292                            actionRequest, "assetLinkBehaviour");
293                    boolean showAvailableLocales = ParamUtil.getBoolean(
294                            actionRequest, "showAvailableLocales");
295                    String[] extensions = actionRequest.getParameterValues("extensions");
296                    boolean enablePrint = ParamUtil.getBoolean(
297                            actionRequest, "enablePrint");
298                    boolean enableFlags = ParamUtil.getBoolean(
299                            actionRequest, "enableFlags");
300                    boolean enableRatings = ParamUtil.getBoolean(
301                            actionRequest, "enableRatings");
302                    boolean enableComments = ParamUtil.getBoolean(
303                            actionRequest, "enableComments");
304                    boolean enableCommentRatings = ParamUtil.getBoolean(
305                            actionRequest, "enableCommentRatings");
306                    boolean enableTagBasedNavigation = ParamUtil.getBoolean(
307                            actionRequest, "enableTagBasedNavigation");
308                    String medatadaFields = ParamUtil.getString(
309                            actionRequest, "metadataFields");
310    
311                    preferences.setValue("selection-style", "manual");
312                    preferences.setValue("display-style", displayStyle);
313                    preferences.setValue("any-asset-type", String.valueOf(anyAssetType));
314                    preferences.setValues(
315                            "class-name-ids", ArrayUtil.toStringArray(classNameIds));
316                    preferences.setValue(
317                            "show-asset-title", String.valueOf(showAssetTitle));
318                    preferences.setValue(
319                            "show-context-link", String.valueOf(showContextLink));
320                    preferences.setValue("abstract-length", String.valueOf(abstractLength));
321                    preferences.setValue("asset-link-behaviour", assetLinkBehaviour);
322                    preferences.setValue(
323                            "show-available-locales", String.valueOf(showAvailableLocales));
324                    preferences.setValues("extensions", extensions);
325                    preferences.setValue("enable-print", String.valueOf(enablePrint));
326                    preferences.setValue("enable-flags", String.valueOf(enableFlags));
327                    preferences.setValue("enable-ratings", String.valueOf(enableRatings));
328                    preferences.setValue("enable-comments", String.valueOf(enableComments));
329                    preferences.setValue(
330                            "enable-comment-ratings", String.valueOf(enableCommentRatings));
331                    preferences.setValue(
332                            "enable-tag-based-navigation",
333                            String.valueOf(enableTagBasedNavigation));
334                    preferences.setValue("metadata-fields", medatadaFields);
335            }
336    
337            protected void updateQueryLogic(
338                            ActionRequest actionRequest, PortletPreferences preferences)
339                    throws Exception {
340    
341                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
342                            WebKeys.THEME_DISPLAY);
343    
344                    long userId = themeDisplay.getUserId();
345                    long groupId = themeDisplay.getScopeGroupId();
346    
347                    int[] queryRulesIndexes = StringUtil.split(
348                            ParamUtil.getString(actionRequest, "queryLogicIndexes"), 0);
349    
350                    int i = 0;
351    
352                    for (int queryRulesIndex : queryRulesIndexes) {
353                            boolean contains = ParamUtil.getBoolean(
354                                    actionRequest, "queryContains" + queryRulesIndex);
355                            boolean andOperator = ParamUtil.getBoolean(
356                                    actionRequest, "queryAndOperator" + queryRulesIndex);
357                            String name = ParamUtil.getString(
358                                    actionRequest, "queryName" + queryRulesIndex);
359    
360                            String[] values = null;
361    
362                            if (name.equals("assetTags")) {
363                                    values = StringUtil.split(ParamUtil.getString(
364                                            actionRequest, "queryTagNames" + queryRulesIndex));
365    
366                                    AssetTagLocalServiceUtil.checkTags(userId, groupId, values);
367                            }
368                            else {
369                                    values = StringUtil.split(ParamUtil.getString(
370                                            actionRequest, "queryCategoryIds" + queryRulesIndex));
371                            }
372    
373                            preferences.setValue("queryContains" + i, String.valueOf(contains));
374                            preferences.setValue(
375                                    "queryAndOperator" + i, String.valueOf(andOperator));
376                            preferences.setValue("queryName" + i, name);
377                            preferences.setValues("queryValues" + i, values);
378    
379                            i++;
380                    }
381    
382                    // Clear previous preferences that are now blank
383    
384                    String[] values = preferences.getValues(
385                            "queryValues" + i, new String[0]);
386    
387                    while (values.length > 0) {
388                            preferences.setValue("queryContains" + i, StringPool.BLANK);
389                            preferences.setValue("queryAndOperator" + i, StringPool.BLANK);
390                            preferences.setValue("queryName" + i, StringPool.BLANK);
391                            preferences.setValues("queryValues" + i, new String[0]);
392    
393                            i++;
394    
395                            values = preferences.getValues("queryValues" + i, new String[0]);
396                    }
397            }
398    
399            protected void updateRssSettings(
400                            ActionRequest actionRequest, PortletPreferences preferences)
401                    throws Exception {
402    
403                    boolean enableRSS = ParamUtil.getBoolean(
404                            actionRequest, "enableRSS");
405                    int rssDelta = ParamUtil.getInteger(actionRequest, "rssDelta");
406                    String rssDisplayStyle = ParamUtil.getString(
407                            actionRequest, "rssDisplayStyle");
408                    String rssFormat = ParamUtil.getString(actionRequest, "rssFormat");
409                    String rssName = ParamUtil.getString(actionRequest, "rssName");
410    
411                    preferences.setValue("enable-rss", String.valueOf(enableRSS));
412                    preferences.setValue("rss-delta", String.valueOf(rssDelta));
413                    preferences.setValue("rss-display-style", rssDisplayStyle);
414                    preferences.setValue("rss-format", rssFormat);
415                    preferences.setValue("rss-name", rssName);
416            }
417    
418    }