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