001    /**
002     * Copyright (c) 2000-2010 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.BaseConfigurationAction;
020    import com.liferay.portal.kernel.servlet.SessionMessages;
021    import com.liferay.portal.kernel.util.ParamUtil;
022    import com.liferay.portal.kernel.util.StringPool;
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.PropsValues;
032    import com.liferay.portal.util.WebKeys;
033    import com.liferay.portlet.PortletPreferencesFactoryUtil;
034    import com.liferay.util.UniqueList;
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    import javax.portlet.RenderRequest;
047    import javax.portlet.RenderResponse;
048    
049    /**
050     * @author Jorge Ferrer
051     */
052    public class ConfigurationActionImpl extends BaseConfigurationAction {
053    
054            public void processAction(
055                            PortletConfig portletConfig, ActionRequest actionRequest,
056                            ActionResponse actionResponse)
057                    throws Exception {
058    
059                    String layoutTemplateId = ParamUtil.getString(
060                            actionRequest, "layoutTemplateId");
061                    String portletSetupShowBorders = ParamUtil.getString(
062                            actionRequest, "portletSetupShowBorders");
063    
064                    String portletResource = ParamUtil.getString(
065                            actionRequest, "portletResource");
066    
067                    PortletPreferences preferences =
068                            PortletPreferencesFactoryUtil.getPortletSetup(
069                                    actionRequest, portletResource);
070    
071                    String oldLayoutTemplateId = preferences.getValue(
072                            "layout-template-id",
073                            PropsValues.NESTED_PORTLETS_LAYOUT_TEMPLATE_DEFAULT);
074    
075                    if (!oldLayoutTemplateId.equals(layoutTemplateId)) {
076                            reorganizeNestedColumns(
077                                    actionRequest, portletResource, layoutTemplateId,
078                                    oldLayoutTemplateId);
079                    }
080    
081                    preferences.setValue("layout-template-id", layoutTemplateId);
082                    preferences.setValue(
083                            "portlet-setup-show-borders", portletSetupShowBorders);
084    
085                    preferences.store();
086    
087                    SessionMessages.add(
088                            actionRequest, portletConfig.getPortletName() + ".doConfigure");
089            }
090    
091            public String render(
092                            PortletConfig portletConfig, RenderRequest renderRequest,
093                            RenderResponse renderResponse)
094                    throws Exception {
095    
096                    return "/html/portlet/nested_portlets/configuration.jsp";
097            }
098    
099            protected List<String> getColumnNames(String content, String portletId) {
100                    Matcher matcher = _pattern.matcher(content);
101    
102                    Set<String> columnIds = new HashSet<String>();
103    
104                    while (matcher.find()) {
105                            if (Validator.isNotNull(matcher.group(1))) {
106                                    columnIds.add(matcher.group(1));
107                            }
108                    }
109    
110                    List<String> columnNames = new UniqueList<String>();
111    
112                    for (String columnId : columnIds) {
113                            if (columnId.indexOf(portletId) == -1) {
114                                    columnNames.add(portletId + StringPool.UNDERLINE + columnId);
115                            }
116                    }
117    
118                    return columnNames;
119            }
120    
121            protected void reorganizeNestedColumns(
122                            ActionRequest actionRequest, String portletResource,
123                            String newLayoutTemplateId, String oldLayoutTemplateId)
124                    throws PortalException, SystemException {
125    
126                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
127                            WebKeys.THEME_DISPLAY);
128    
129                    Layout layout = themeDisplay.getLayout();
130                    LayoutTypePortlet layoutTypePortlet =
131                            themeDisplay.getLayoutTypePortlet();
132                    Theme theme = themeDisplay.getTheme();
133    
134                    LayoutTemplate newLayoutTemplate =
135                            LayoutTemplateLocalServiceUtil.getLayoutTemplate(
136                                    newLayoutTemplateId, false, theme.getThemeId());
137    
138                    List<String> newColumns = getColumnNames(
139                            newLayoutTemplate.getContent(), portletResource);
140    
141                    LayoutTemplate oldLayoutTemplate =
142                            LayoutTemplateLocalServiceUtil.getLayoutTemplate(
143                                    oldLayoutTemplateId, false, theme.getThemeId());
144    
145                    List<String> oldColumns = getColumnNames(
146                            oldLayoutTemplate.getContent(), portletResource);
147    
148                    layoutTypePortlet.reorganizePortlets(newColumns, oldColumns);
149    
150                    layoutTypePortlet.setStateMax(StringPool.BLANK);
151    
152                    LayoutLocalServiceUtil.updateLayout(
153                            layout.getGroupId(), layout.isPrivateLayout(),
154                            layout.getLayoutId(), layout.getTypeSettings());
155            }
156    
157            private static Pattern _pattern = Pattern.compile(
158                    "processColumn[(]\"(.*?)\"(?:, *\"(?:.*?)\")?[)]", Pattern.DOTALL);
159    
160    }