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