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.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.Validator;
023    import com.liferay.portal.model.Layout;
024    import com.liferay.portal.model.LayoutTemplate;
025    import com.liferay.portal.model.LayoutTypePortlet;
026    import com.liferay.portal.model.Theme;
027    import com.liferay.portal.service.LayoutLocalServiceUtil;
028    import com.liferay.portal.service.LayoutTemplateLocalServiceUtil;
029    import com.liferay.portal.theme.ThemeDisplay;
030    import com.liferay.portal.util.PropsValues;
031    import com.liferay.portal.util.WebKeys;
032    import com.liferay.portlet.PortletPreferencesFactoryUtil;
033    import com.liferay.util.UniqueList;
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 =
064                            PortletPreferencesFactoryUtil.getPortletSetup(
065                                    actionRequest, portletResource);
066    
067                    String oldLayoutTemplateId = preferences.getValue(
068                            "layoutTemplateId",
069                            PropsValues.NESTED_PORTLETS_LAYOUT_TEMPLATE_DEFAULT);
070    
071                    if (!oldLayoutTemplateId.equals(layoutTemplateId)) {
072                            reorganizeNestedColumns(
073                                    actionRequest, portletResource, layoutTemplateId,
074                                    oldLayoutTemplateId);
075                    }
076    
077                    super.processAction(portletConfig, actionRequest, actionResponse);
078            }
079    
080            protected List<String> getColumnNames(String content, String portletId) {
081                    Matcher matcher = _pattern.matcher(content);
082    
083                    Set<String> columnIds = new HashSet<String>();
084    
085                    while (matcher.find()) {
086                            if (Validator.isNotNull(matcher.group(1))) {
087                                    columnIds.add(matcher.group(1));
088                            }
089                    }
090    
091                    List<String> columnNames = new UniqueList<String>();
092    
093                    for (String columnId : columnIds) {
094                            if (columnId.indexOf(portletId) == -1) {
095                                    columnNames.add(portletId + 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    }