001    /**
002     * Copyright (c) 2000-2013 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.DefaultConfigurationAction;
018    import com.liferay.portal.kernel.portlet.LiferayPortletConfig;
019    import com.liferay.portal.kernel.servlet.SessionErrors;
020    import com.liferay.portal.kernel.servlet.SessionMessages;
021    import com.liferay.portal.kernel.util.Constants;
022    import com.liferay.portal.kernel.util.GetterUtil;
023    import com.liferay.portal.kernel.util.ParamUtil;
024    import com.liferay.portal.kernel.util.StringPool;
025    import com.liferay.portal.kernel.util.StringUtil;
026    import com.liferay.portal.kernel.util.UnicodeProperties;
027    import com.liferay.portal.kernel.util.Validator;
028    import com.liferay.portal.model.Layout;
029    import com.liferay.portal.model.LayoutTypePortletConstants;
030    import com.liferay.portal.service.LayoutLocalServiceUtil;
031    import com.liferay.portal.theme.ThemeDisplay;
032    import com.liferay.portal.util.PortalUtil;
033    import com.liferay.portal.util.WebKeys;
034    import com.liferay.portlet.asset.AssetRendererFactoryRegistryUtil;
035    import com.liferay.portlet.asset.AssetTagException;
036    import com.liferay.portlet.asset.model.AssetRendererFactory;
037    import com.liferay.portlet.asset.service.AssetTagLocalServiceUtil;
038    import com.liferay.portlet.assetpublisher.util.AssetPublisherUtil;
039    
040    import javax.portlet.ActionRequest;
041    import javax.portlet.ActionResponse;
042    import javax.portlet.PortletConfig;
043    import javax.portlet.PortletPreferences;
044    
045    /**
046     * @author Brian Wing Shun Chan
047     * @author Juan Fern??ndez
048     */
049    public class ConfigurationActionImpl extends DefaultConfigurationAction {
050    
051            @Override
052            public void processAction(
053                            PortletConfig portletConfig, ActionRequest actionRequest,
054                            ActionResponse actionResponse)
055                    throws Exception {
056    
057                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
058    
059                    String portletResource = ParamUtil.getString(
060                            actionRequest, "portletResource");
061    
062                    PortletPreferences preferences = actionRequest.getPreferences();
063    
064                    if (cmd.equals(Constants.UPDATE)) {
065                            updateDisplaySettings(actionRequest);
066    
067                            String selectionStyle = getParameter(
068                                    actionRequest, "selectionStyle");
069    
070                            if (selectionStyle.equals("dynamic")) {
071                                    updateQueryLogic(actionRequest, preferences);
072                            }
073    
074                            updateDefaultAssetPublisher(actionRequest);
075    
076                            super.processAction(portletConfig, actionRequest, actionResponse);
077                    }
078                    else {
079                            try {
080                                    if (cmd.equals("add-selection")) {
081                                            AssetPublisherUtil.addSelection(actionRequest, preferences);
082                                    }
083                                    else if (cmd.equals("move-selection-down")) {
084                                            moveSelectionDown(actionRequest, preferences);
085                                    }
086                                    else if (cmd.equals("move-selection-up")) {
087                                            moveSelectionUp(actionRequest, preferences);
088                                    }
089                                    else if (cmd.equals("remove-selection")) {
090                                            removeSelection(actionRequest, preferences);
091                                    }
092                                    else if (cmd.equals("select-scope")) {
093                                            setScopes(actionRequest, preferences);
094                                    }
095                                    else if (cmd.equals("selection-style")) {
096                                            setSelectionStyle(actionRequest, preferences);
097                                    }
098    
099                                    if (SessionErrors.isEmpty(actionRequest)) {
100                                            preferences.store();
101    
102                                            LiferayPortletConfig liferayPortletConfig =
103                                                    (LiferayPortletConfig)portletConfig;
104    
105                                            SessionMessages.add(
106                                                    actionRequest,
107                                                    liferayPortletConfig.getPortletId() +
108                                                            SessionMessages.KEY_SUFFIX_REFRESH_PORTLET,
109                                                    portletResource);
110    
111                                            SessionMessages.add(
112                                                    actionRequest,
113                                                    liferayPortletConfig.getPortletId() +
114                                                            SessionMessages.KEY_SUFFIX_UPDATED_CONFIGURATION);
115                                    }
116    
117                                    String redirect = PortalUtil.escapeRedirect(
118                                            ParamUtil.getString(actionRequest, "redirect"));
119    
120                                    if (Validator.isNotNull(redirect)) {
121                                            actionResponse.sendRedirect(redirect);
122                                    }
123                            }
124                            catch (Exception e) {
125                                    if (e instanceof AssetTagException) {
126                                            SessionErrors.add(actionRequest, e.getClass(), e);
127                                    }
128                                    else {
129                                            throw e;
130                                    }
131                            }
132                    }
133            }
134    
135            protected String[] getClassTypeIds(
136                            ActionRequest actionRequest, String[] classNameIds)
137                    throws Exception {
138    
139                    String anyAssetTypeString = getParameter(actionRequest, "anyAssetType");
140    
141                    boolean anyAssetType = GetterUtil.getBoolean(anyAssetTypeString);
142    
143                    if (anyAssetType) {
144                            return null;
145                    }
146    
147                    long defaultAssetTypeId = GetterUtil.getLong(anyAssetTypeString);
148    
149                    if ((defaultAssetTypeId == 0) && (classNameIds.length == 1)) {
150                            defaultAssetTypeId = GetterUtil.getLong(classNameIds[0]);
151                    }
152    
153                    if (defaultAssetTypeId <= 0 ) {
154                            return null;
155                    }
156    
157                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
158                            WebKeys.THEME_DISPLAY);
159    
160                    String className = PortalUtil.getClassName(defaultAssetTypeId);
161    
162                    AssetRendererFactory assetRendererFactory =
163                            AssetRendererFactoryRegistryUtil.getAssetRendererFactoryByClassName(
164                                    className);
165    
166                    long[] groupIds = {
167                            themeDisplay.getCompanyGroupId(), themeDisplay.getScopeGroupId()
168                    };
169    
170                    if (assetRendererFactory.getClassTypes(
171                                    groupIds, themeDisplay.getLocale()) == null) {
172    
173                            return null;
174                    }
175    
176                    String assetClassName = AssetPublisherUtil.getClassName(
177                            assetRendererFactory);
178    
179                    String anyAssetClassTypeString = getParameter(
180                            actionRequest, "anyClassType" + assetClassName);
181    
182                    boolean anyAssetClassType = GetterUtil.getBoolean(
183                            anyAssetClassTypeString);
184    
185                    if (anyAssetClassType) {
186                            return null;
187                    }
188    
189                    long defaultAssetClassTypeId = GetterUtil.getLong(
190                            anyAssetClassTypeString);
191    
192                    if (defaultAssetClassTypeId > 0) {
193                            return new String[] {String.valueOf(defaultAssetClassTypeId)};
194                    }
195                    else {
196                            return StringUtil.split(
197                                    getParameter(actionRequest, "classTypeIds" + assetClassName));
198                    }
199            }
200    
201            protected void moveSelectionDown(
202                            ActionRequest actionRequest, PortletPreferences preferences)
203                    throws Exception {
204    
205                    int assetEntryOrder = ParamUtil.getInteger(
206                            actionRequest, "assetEntryOrder");
207    
208                    String[] manualEntries = preferences.getValues(
209                            "assetEntryXml", new String[0]);
210    
211                    if ((assetEntryOrder >= (manualEntries.length - 1)) ||
212                            (assetEntryOrder < 0)) {
213    
214                            return;
215                    }
216    
217                    String temp = manualEntries[assetEntryOrder + 1];
218    
219                    manualEntries[assetEntryOrder + 1] = manualEntries[assetEntryOrder];
220                    manualEntries[assetEntryOrder] = temp;
221    
222                    preferences.setValues("assetEntryXml", manualEntries);
223            }
224    
225            protected void moveSelectionUp(
226                            ActionRequest actionRequest, PortletPreferences preferences)
227                    throws Exception {
228    
229                    int assetEntryOrder = ParamUtil.getInteger(
230                            actionRequest, "assetEntryOrder");
231    
232                    String[] manualEntries = preferences.getValues(
233                            "assetEntryXml", new String[0]);
234    
235                    if ((assetEntryOrder >= manualEntries.length) ||
236                            (assetEntryOrder <= 0)) {
237    
238                            return;
239                    }
240    
241                    String temp = manualEntries[assetEntryOrder - 1];
242    
243                    manualEntries[assetEntryOrder - 1] = manualEntries[assetEntryOrder];
244                    manualEntries[assetEntryOrder] = temp;
245    
246                    preferences.setValues("assetEntryXml", manualEntries);
247            }
248    
249            protected void removeSelection(
250                            ActionRequest actionRequest, PortletPreferences preferences)
251                    throws Exception {
252    
253                    int assetEntryOrder = ParamUtil.getInteger(
254                            actionRequest, "assetEntryOrder");
255    
256                    String[] manualEntries = preferences.getValues(
257                            "assetEntryXml", new String[0]);
258    
259                    if (assetEntryOrder >= manualEntries.length) {
260                            return;
261                    }
262    
263                    String[] newEntries = new String[manualEntries.length -1];
264    
265                    int i = 0;
266                    int j = 0;
267    
268                    for (; i < manualEntries.length; i++) {
269                            if (i != assetEntryOrder) {
270                                    newEntries[j++] = manualEntries[i];
271                            }
272                    }
273    
274                    preferences.setValues("assetEntryXml", newEntries);
275            }
276    
277            protected void setScopes(
278                            ActionRequest actionRequest, PortletPreferences preferences)
279                    throws Exception {
280    
281                    String defaultScope = getParameter(actionRequest, "defaultScope");
282                    String[] scopeIds = StringUtil.split(
283                            getParameter(actionRequest, "scopeIds"));
284    
285                    preferences.setValue("defaultScope", defaultScope);
286                    preferences.setValues("scopeIds", scopeIds);
287            }
288    
289            protected void setSelectionStyle(
290                            ActionRequest actionRequest, PortletPreferences preferences)
291                    throws Exception {
292    
293                    String selectionStyle = getParameter(actionRequest, "selectionStyle");
294                    String displayStyle = getParameter(actionRequest, "displayStyle");
295    
296                    preferences.setValue("selectionStyle", selectionStyle);
297    
298                    if (selectionStyle.equals("manual") ||
299                            selectionStyle.equals("view-count")) {
300    
301                            preferences.setValue("enableRss", String.valueOf(false));
302                            preferences.setValue("showQueryLogic", Boolean.FALSE.toString());
303    
304                            preferences.reset("rssDelta");
305                            preferences.reset("rssDisplayStyle");
306                            preferences.reset("rssFormat");
307                            preferences.reset("rssName");
308                    }
309    
310                    if (!selectionStyle.equals("view-count") &&
311                            displayStyle.equals("view-count-details")) {
312    
313                            preferences.setValue("displayStyle", "full-content");
314                    }
315            }
316    
317            protected void updateDefaultAssetPublisher(ActionRequest actionRequest)
318                    throws Exception {
319    
320                    boolean defaultAssetPublisher = ParamUtil.getBoolean(
321                            actionRequest, "defaultAssetPublisher");
322    
323                    Layout layout = (Layout)actionRequest.getAttribute(WebKeys.LAYOUT);
324    
325                    String portletResource = ParamUtil.getString(
326                            actionRequest, "portletResource");
327    
328                    UnicodeProperties typeSettingsProperties =
329                            layout.getTypeSettingsProperties();
330    
331                    if (defaultAssetPublisher) {
332                            typeSettingsProperties.setProperty(
333                                    LayoutTypePortletConstants.DEFAULT_ASSET_PUBLISHER_PORTLET_ID,
334                                    portletResource);
335                    }
336                    else {
337                            String defaultAssetPublisherPortletId =
338                                    typeSettingsProperties.getProperty(
339                                            LayoutTypePortletConstants.
340                                                    DEFAULT_ASSET_PUBLISHER_PORTLET_ID);
341    
342                            if (Validator.isNotNull(defaultAssetPublisherPortletId) &&
343                                    defaultAssetPublisherPortletId.equals(portletResource)) {
344    
345                                    typeSettingsProperties.setProperty(
346                                            LayoutTypePortletConstants.
347                                                    DEFAULT_ASSET_PUBLISHER_PORTLET_ID,
348                                            StringPool.BLANK);
349                            }
350                    }
351    
352                    layout = LayoutLocalServiceUtil.updateLayout(
353                            layout.getGroupId(), layout.isPrivateLayout(), layout.getLayoutId(),
354                            layout.getTypeSettings());
355            }
356    
357            protected void updateDisplaySettings(ActionRequest actionRequest)
358                    throws Exception {
359    
360                    String[] classNameIds = StringUtil.split(
361                            getParameter(actionRequest, "classNameIds"));
362                    String[] classTypeIds = getClassTypeIds(actionRequest, classNameIds);
363                    String[] extensions = actionRequest.getParameterValues("extensions");
364                    String[] scopeIds = StringUtil.split(
365                            getParameter(actionRequest, "scopeIds"));
366    
367                    setPreference(actionRequest, "classNameIds", classNameIds);
368                    setPreference(actionRequest, "classTypeIds", classTypeIds);
369                    setPreference(actionRequest, "extensions", extensions);
370                    setPreference(actionRequest, "scopeIds", scopeIds);
371            }
372    
373            protected void updateQueryLogic(
374                            ActionRequest actionRequest, PortletPreferences preferences)
375                    throws Exception {
376    
377                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
378                            WebKeys.THEME_DISPLAY);
379    
380                    long userId = themeDisplay.getUserId();
381                    long groupId = themeDisplay.getScopeGroupId();
382    
383                    int[] queryRulesIndexes = StringUtil.split(
384                            ParamUtil.getString(actionRequest, "queryLogicIndexes"), 0);
385    
386                    int i = 0;
387    
388                    for (int queryRulesIndex : queryRulesIndexes) {
389                            boolean contains = ParamUtil.getBoolean(
390                                    actionRequest, "queryContains" + queryRulesIndex);
391                            boolean andOperator = ParamUtil.getBoolean(
392                                    actionRequest, "queryAndOperator" + queryRulesIndex);
393                            String name = ParamUtil.getString(
394                                    actionRequest, "queryName" + queryRulesIndex);
395    
396                            String[] values = null;
397    
398                            if (name.equals("assetTags")) {
399                                    values = StringUtil.split(
400                                            ParamUtil.getString(
401                                                    actionRequest, "queryTagNames" + queryRulesIndex));
402    
403                                    AssetTagLocalServiceUtil.checkTags(userId, groupId, values);
404                            }
405                            else {
406                                    values = StringUtil.split(
407                                            ParamUtil.getString(
408                                                    actionRequest, "queryCategoryIds" + queryRulesIndex));
409                            }
410    
411                            setPreference(
412                                    actionRequest, "queryContains" + i, String.valueOf(contains));
413                            setPreference(
414                                    actionRequest, "queryAndOperator" + i,
415                                    String.valueOf(andOperator));
416                            setPreference(actionRequest, "queryName" + i, name);
417                            setPreference(actionRequest, "queryValues" + i, values);
418    
419                            i++;
420                    }
421    
422                    // Clear previous preferences that are now blank
423    
424                    String[] values = preferences.getValues(
425                            "queryValues" + i, new String[0]);
426    
427                    while (values.length > 0) {
428                            setPreference(actionRequest, "queryContains" + i, StringPool.BLANK);
429                            setPreference(
430                                    actionRequest, "queryAndOperator" + i, StringPool.BLANK);
431                            setPreference(actionRequest, "queryName" + i, StringPool.BLANK);
432                            setPreference(actionRequest, "queryValues" + i, new String[0]);
433    
434                            i++;
435    
436                            values = preferences.getValues("queryValues" + i, new String[0]);
437                    }
438            }
439    
440    }