001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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() + ".doConfigure");
104    
105                                            SessionMessages.add(
106                                                    actionRequest,
107                                                    portletConfig.getPortletName() + ".doRefresh",
108                                                    portletResource);
109                                    }
110    
111                                    String redirect = PortalUtil.escapeRedirect(
112                                            ParamUtil.getString(actionRequest, "redirect"));
113    
114                                    if (Validator.isNotNull(redirect)) {
115                                            actionResponse.sendRedirect(redirect);
116                                    }
117                            }
118                            catch (Exception e) {
119                                    if (e instanceof AssetTagException) {
120                                            SessionErrors.add(actionRequest, e.getClass().getName(), e);
121                                    }
122                                    else {
123                                            throw e;
124                                    }
125                            }
126                    }
127            }
128    
129            protected String[] getClassTypeIds(
130                    ActionRequest actionRequest, String[] classNameIds) throws Exception {
131    
132                    String anyAssetTypeString = getParameter(
133                            actionRequest, "anyAssetType");
134    
135                    boolean anyAssetType = GetterUtil.getBoolean(anyAssetTypeString);
136    
137                    if (anyAssetType) {
138                            return null;
139                    }
140    
141                    long defaultAssetTypeId = GetterUtil.getLong(anyAssetTypeString);
142    
143                    if ((defaultAssetTypeId == 0) && (classNameIds.length == 1)) {
144                            defaultAssetTypeId = GetterUtil.getLong(classNameIds[0]);
145                    }
146    
147                    if (defaultAssetTypeId <= 0 ) {
148                            return null;
149                    }
150    
151                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
152                            WebKeys.THEME_DISPLAY);
153    
154                    String className = PortalUtil.getClassName(defaultAssetTypeId);
155    
156                    AssetRendererFactory assetRendererFactory =
157                            AssetRendererFactoryRegistryUtil.getAssetRendererFactoryByClassName(
158                                    className);
159    
160                    long[] groupdIds = {
161                            themeDisplay.getCompanyGroupId(), themeDisplay.getScopeGroupId()
162                    };
163    
164                    if (assetRendererFactory.getClassTypes(groupdIds) == null) {
165                            return null;
166                    }
167    
168                    String assetClassName = AssetPublisherUtil.getClassName(
169                            assetRendererFactory);
170    
171                    String anyAssetClassTypeString = getParameter(
172                            actionRequest, "anyClassType" + assetClassName);
173    
174                    boolean anyAssetClassType = GetterUtil.getBoolean(
175                            anyAssetClassTypeString);
176    
177                    if (anyAssetClassType) {
178                            return null;
179                    }
180    
181                    long defaultAssetClassTypeId = GetterUtil.getLong(
182                            anyAssetClassTypeString);
183    
184                    if (defaultAssetClassTypeId > 0) {
185                            return new String[] {String.valueOf(defaultAssetClassTypeId)};
186                    }
187                    else {
188                            return StringUtil.split(
189                                    getParameter(actionRequest, "classTypeIds" + assetClassName));
190                    }
191            }
192    
193            protected void moveSelectionDown(
194                            ActionRequest actionRequest, PortletPreferences preferences)
195                    throws Exception {
196    
197                    int assetEntryOrder = ParamUtil.getInteger(
198                            actionRequest, "assetEntryOrder");
199    
200                    String[] manualEntries = preferences.getValues(
201                            "assetEntryXml", new String[0]);
202    
203                    if ((assetEntryOrder >= (manualEntries.length - 1)) ||
204                            (assetEntryOrder < 0)) {
205    
206                            return;
207                    }
208    
209                    String temp = manualEntries[assetEntryOrder + 1];
210    
211                    manualEntries[assetEntryOrder + 1] = manualEntries[assetEntryOrder];
212                    manualEntries[assetEntryOrder] = temp;
213    
214                    preferences.setValues("assetEntryXml", manualEntries);
215            }
216    
217            protected void moveSelectionUp(
218                            ActionRequest actionRequest, PortletPreferences preferences)
219                    throws Exception {
220    
221                    int assetEntryOrder = ParamUtil.getInteger(
222                            actionRequest, "assetEntryOrder");
223    
224                    String[] manualEntries = preferences.getValues(
225                            "assetEntryXml", new String[0]);
226    
227                    if ((assetEntryOrder >= manualEntries.length) ||
228                            (assetEntryOrder <= 0)) {
229    
230                            return;
231                    }
232    
233                    String temp = manualEntries[assetEntryOrder - 1];
234    
235                    manualEntries[assetEntryOrder - 1] = manualEntries[assetEntryOrder];
236                    manualEntries[assetEntryOrder] = temp;
237    
238                    preferences.setValues("assetEntryXml", manualEntries);
239            }
240    
241            protected void removeSelection(
242                            ActionRequest actionRequest, PortletPreferences preferences)
243                    throws Exception {
244    
245                    int assetEntryOrder = ParamUtil.getInteger(
246                            actionRequest, "assetEntryOrder");
247    
248                    String[] manualEntries = preferences.getValues(
249                            "assetEntryXml", new String[0]);
250    
251                    if (assetEntryOrder >= manualEntries.length) {
252                            return;
253                    }
254    
255                    String[] newEntries = new String[manualEntries.length -1];
256    
257                    int i = 0;
258                    int j = 0;
259    
260                    for (; i < manualEntries.length; i++) {
261                            if (i != assetEntryOrder) {
262                                    newEntries[j++] = manualEntries[i];
263                            }
264                    }
265    
266                    preferences.setValues("assetEntryXml", newEntries);
267            }
268    
269            protected void setSelectionStyle(
270                            ActionRequest actionRequest, PortletPreferences preferences)
271                    throws Exception {
272    
273                    String selectionStyle = getParameter(actionRequest, "selectionStyle");
274                    String displayStyle = getParameter(actionRequest, "displayStyle");
275    
276                    preferences.setValue("selectionStyle", selectionStyle);
277    
278                    if (selectionStyle.equals("manual") ||
279                            selectionStyle.equals("view-count")) {
280    
281                            preferences.setValue("showQueryLogic", String.valueOf(false));
282                    }
283    
284                    if (!selectionStyle.equals("view-count") &&
285                            displayStyle.equals("view-count-details")) {
286    
287                            preferences.setValue("displayStyle", "full-content");
288                    }
289            }
290    
291            protected void updateDefaultAssetPublisher(ActionRequest actionRequest)
292                    throws Exception {
293    
294                    boolean defaultAssetPublisher = ParamUtil.getBoolean(
295                            actionRequest, "defaultAssetPublisher");
296    
297                    Layout layout = (Layout)actionRequest.getAttribute(WebKeys.LAYOUT);
298    
299                    String portletResource = ParamUtil.getString(
300                            actionRequest, "portletResource");
301    
302                    UnicodeProperties typeSettingsProperties =
303                            layout.getTypeSettingsProperties();
304    
305                    if (defaultAssetPublisher) {
306                            typeSettingsProperties.setProperty(
307                                    LayoutTypePortletConstants.DEFAULT_ASSET_PUBLISHER_PORTLET_ID,
308                                    portletResource);
309                    }
310                    else {
311                            String defaultAssetPublisherPortletId =
312                                    typeSettingsProperties.getProperty(
313                                            LayoutTypePortletConstants.
314                                                    DEFAULT_ASSET_PUBLISHER_PORTLET_ID);
315    
316                            if (Validator.isNotNull(defaultAssetPublisherPortletId) &&
317                                    defaultAssetPublisherPortletId.equals(portletResource)) {
318    
319                                    typeSettingsProperties.setProperty(
320                                            LayoutTypePortletConstants.
321                                                    DEFAULT_ASSET_PUBLISHER_PORTLET_ID,
322                                            StringPool.BLANK);
323                            }
324                    }
325    
326                    layout = LayoutServiceUtil.updateLayout(
327                            layout.getGroupId(), layout.isPrivateLayout(),
328                            layout.getLayoutId(), layout.getTypeSettings());
329            }
330    
331            protected void updateDisplaySettings(ActionRequest actionRequest)
332                    throws Exception {
333    
334                    String[] classNameIds = StringUtil.split(
335                            getParameter(actionRequest, "classNameIds"));
336                    String[] classTypeIds = getClassTypeIds(actionRequest, classNameIds);
337                    String[] extensions = actionRequest.getParameterValues("extensions");
338                    String[] scopeIds = StringUtil.split(
339                            getParameter(actionRequest, "scopeIds"));
340    
341                    setPreference(actionRequest, "classNameIds", classNameIds);
342                    setPreference(actionRequest, "classTypeIds", classTypeIds);
343                    setPreference(actionRequest, "extensions", extensions);
344                    setPreference(actionRequest, "scopeIds", scopeIds);
345            }
346    
347            protected void updateQueryLogic(
348                            ActionRequest actionRequest, PortletPreferences preferences)
349                    throws Exception {
350    
351                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
352                            WebKeys.THEME_DISPLAY);
353    
354                    long userId = themeDisplay.getUserId();
355                    long groupId = themeDisplay.getScopeGroupId();
356    
357                    int[] queryRulesIndexes = StringUtil.split(
358                            ParamUtil.getString(actionRequest, "queryLogicIndexes"), 0);
359    
360                    int i = 0;
361    
362                    for (int queryRulesIndex : queryRulesIndexes) {
363                            boolean contains = ParamUtil.getBoolean(
364                                    actionRequest, "queryContains" + queryRulesIndex);
365                            boolean andOperator = ParamUtil.getBoolean(
366                                    actionRequest, "queryAndOperator" + queryRulesIndex);
367                            String name = ParamUtil.getString(
368                                    actionRequest, "queryName" + queryRulesIndex);
369    
370                            String[] values = null;
371    
372                            if (name.equals("assetTags")) {
373                                    values = StringUtil.split(ParamUtil.getString(
374                                            actionRequest, "queryTagNames" + queryRulesIndex));
375    
376                                    AssetTagLocalServiceUtil.checkTags(userId, groupId, values);
377                            }
378                            else {
379                                    values = StringUtil.split(ParamUtil.getString(
380                                            actionRequest, "queryCategoryIds" + queryRulesIndex));
381                            }
382    
383                            setPreference(
384                                    actionRequest, "queryContains" + i, String.valueOf(contains));
385                            setPreference(
386                                    actionRequest, "queryAndOperator" + i,
387                                    String.valueOf(andOperator));
388                            setPreference(actionRequest, "queryName" + i, name);
389                            setPreference(actionRequest, "queryValues" + i, values);
390    
391                            i++;
392                    }
393    
394                    // Clear previous preferences that are now blank
395    
396                    String[] values = preferences.getValues(
397                            "queryValues" + i, new String[0]);
398    
399                    while (values.length > 0) {
400                            setPreference(actionRequest, "queryContains" + i, StringPool.BLANK);
401                            setPreference(
402                                    actionRequest, "queryAndOperator" + i, StringPool.BLANK);
403                            setPreference(actionRequest, "queryName" + i, StringPool.BLANK);
404                            setPreference(actionRequest, "queryValues" + i, new String[0]);
405    
406                            i++;
407    
408                            values = preferences.getValues("queryValues" + i, new String[0]);
409                    }
410            }
411    
412    }