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.nestedportlets.action;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.portlet.DefaultConfigurationAction;
020    import com.liferay.portal.kernel.util.ParamUtil;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.kernel.util.UniqueList;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.portal.model.Layout;
025    import com.liferay.portal.model.LayoutTemplate;
026    import com.liferay.portal.model.LayoutTypePortlet;
027    import com.liferay.portal.model.Theme;
028    import com.liferay.portal.service.LayoutLocalServiceUtil;
029    import com.liferay.portal.service.LayoutTemplateLocalServiceUtil;
030    import com.liferay.portal.theme.ThemeDisplay;
031    import com.liferay.portal.util.PortalUtil;
032    import com.liferay.portal.util.PropsValues;
033    import com.liferay.portal.util.WebKeys;
034    
035    import java.util.HashSet;
036    import java.util.List;
037    import java.util.Set;
038    import java.util.regex.Matcher;
039    import java.util.regex.Pattern;
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 Jorge Ferrer
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 layoutTemplateId = getParameter(
058                            actionRequest, "layoutTemplateId");
059    
060                    String portletResource = ParamUtil.getString(
061                            actionRequest, "portletResource");
062    
063                    PortletPreferences preferences = actionRequest.getPreferences();
064    
065                    String oldLayoutTemplateId = preferences.getValue(
066                            "layoutTemplateId",
067                            PropsValues.NESTED_PORTLETS_LAYOUT_TEMPLATE_DEFAULT);
068    
069                    if (!oldLayoutTemplateId.equals(layoutTemplateId)) {
070                            reorganizeNestedColumns(
071                                    actionRequest, portletResource, layoutTemplateId,
072                                    oldLayoutTemplateId);
073                    }
074    
075                    super.processAction(portletConfig, actionRequest, actionResponse);
076            }
077    
078            protected List<String> getColumnNames(String content, String portletId) {
079                    Matcher matcher = _pattern.matcher(content);
080    
081                    Set<String> columnIds = new HashSet<String>();
082    
083                    while (matcher.find()) {
084                            if (Validator.isNotNull(matcher.group(1))) {
085                                    columnIds.add(matcher.group(1));
086                            }
087                    }
088    
089                    List<String> columnNames = new UniqueList<String>();
090    
091                    for (String columnId : columnIds) {
092                            if (!columnId.contains(portletId)) {
093                                    columnNames.add(
094                                            PortalUtil.getPortletNamespace(portletId) +
095                                                    StringPool.UNDERLINE + columnId);
096                            }
097                    }
098    
099                    return columnNames;
100            }
101    
102            protected void reorganizeNestedColumns(
103                            ActionRequest actionRequest, String portletResource,
104                            String newLayoutTemplateId, String oldLayoutTemplateId)
105                    throws PortalException, SystemException {
106    
107                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
108                            WebKeys.THEME_DISPLAY);
109    
110                    Layout layout = themeDisplay.getLayout();
111                    LayoutTypePortlet layoutTypePortlet =
112                            themeDisplay.getLayoutTypePortlet();
113                    Theme theme = themeDisplay.getTheme();
114    
115                    LayoutTemplate newLayoutTemplate =
116                            LayoutTemplateLocalServiceUtil.getLayoutTemplate(
117                                    newLayoutTemplateId, false, theme.getThemeId());
118    
119                    List<String> newColumns = getColumnNames(
120                            newLayoutTemplate.getContent(), portletResource);
121    
122                    LayoutTemplate oldLayoutTemplate =
123                            LayoutTemplateLocalServiceUtil.getLayoutTemplate(
124                                    oldLayoutTemplateId, false, theme.getThemeId());
125    
126                    List<String> oldColumns = getColumnNames(
127                            oldLayoutTemplate.getContent(), portletResource);
128    
129                    layoutTypePortlet.reorganizePortlets(newColumns, oldColumns);
130    
131                    layoutTypePortlet.setStateMax(StringPool.BLANK);
132    
133                    LayoutLocalServiceUtil.updateLayout(
134                            layout.getGroupId(), layout.isPrivateLayout(), layout.getLayoutId(),
135                            layout.getTypeSettings());
136            }
137    
138            private static Pattern _pattern = Pattern.compile(
139                    "processColumn[(]\"(.*?)\"(?:, *\"(?:.*?)\")?[)]", Pattern.DOTALL);
140    
141    }