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