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