1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.nestedportlets.action;
24  
25  import com.liferay.portal.PortalException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.kernel.portlet.ConfigurationAction;
28  import com.liferay.portal.kernel.servlet.SessionMessages;
29  import com.liferay.portal.kernel.util.ParamUtil;
30  import com.liferay.portal.kernel.util.StringPool;
31  import com.liferay.portal.kernel.util.Validator;
32  import com.liferay.portal.model.Layout;
33  import com.liferay.portal.model.LayoutTemplate;
34  import com.liferay.portal.model.LayoutTypePortlet;
35  import com.liferay.portal.model.Theme;
36  import com.liferay.portal.service.LayoutLocalServiceUtil;
37  import com.liferay.portal.service.LayoutTemplateLocalServiceUtil;
38  import com.liferay.portal.theme.ThemeDisplay;
39  import com.liferay.portal.util.PropsValues;
40  import com.liferay.portal.util.WebKeys;
41  import com.liferay.portlet.PortletPreferencesFactoryUtil;
42  import com.liferay.util.UniqueList;
43  
44  import java.util.HashSet;
45  import java.util.List;
46  import java.util.Set;
47  import java.util.regex.Matcher;
48  import java.util.regex.Pattern;
49  
50  import javax.portlet.ActionRequest;
51  import javax.portlet.ActionResponse;
52  import javax.portlet.PortletConfig;
53  import javax.portlet.PortletPreferences;
54  import javax.portlet.RenderRequest;
55  import javax.portlet.RenderResponse;
56  
57  /**
58   * <a href="ConfigurationActionImpl.java.html"><b><i>View Source</i></b></a>
59   *
60   * @author Jorge Ferrer
61   *
62   */
63  public class ConfigurationActionImpl implements ConfigurationAction {
64  
65      public void processAction(
66              PortletConfig portletConfig, ActionRequest actionRequest,
67              ActionResponse actionResponse)
68          throws Exception {
69  
70          String layoutTemplateId = ParamUtil.getString(
71              actionRequest, "layoutTemplateId");
72          String portletSetupShowBorders = ParamUtil.getString(
73              actionRequest, "portletSetupShowBorders");
74  
75          String portletResource = ParamUtil.getString(
76              actionRequest, "portletResource");
77  
78          PortletPreferences preferences =
79              PortletPreferencesFactoryUtil.getPortletSetup(
80                  actionRequest, portletResource);
81  
82          String oldLayoutTemplateId = preferences.getValue(
83              "layout-template-id",
84              PropsValues.NESTED_PORTLETS_LAYOUT_TEMPLATE_DEFAULT);
85  
86          if (!oldLayoutTemplateId.equals(layoutTemplateId)) {
87              reorganizeNestedColumns(
88                  actionRequest, portletResource, layoutTemplateId,
89                  oldLayoutTemplateId);
90          }
91  
92          preferences.setValue("layout-template-id", layoutTemplateId);
93          preferences.setValue(
94              "portlet-setup-show-borders", portletSetupShowBorders);
95  
96          preferences.store();
97  
98          SessionMessages.add(
99              actionRequest, portletConfig.getPortletName() + ".doConfigure");
100     }
101 
102     public String render(
103             PortletConfig portletConfig, RenderRequest renderRequest,
104             RenderResponse renderResponse)
105         throws Exception {
106 
107         return "/html/portlet/nested_portlets/configuration.jsp";
108     }
109 
110     protected List<String> getColumnNames(String content, String portletId) {
111         Matcher matcher = _pattern.matcher(content);
112 
113         Set<String> columnIds = new HashSet<String>();
114 
115         while (matcher.find()) {
116             if (Validator.isNotNull(matcher.group(1))) {
117                 columnIds.add(matcher.group(1));
118             }
119         }
120 
121         List<String> columnNames = new UniqueList<String>();
122 
123         for (String columnId : columnIds) {
124             if (columnId.indexOf(portletId) == -1) {
125                 columnNames.add(portletId + StringPool.UNDERLINE + columnId);
126             }
127         }
128 
129         return columnNames;
130     }
131 
132     protected void reorganizeNestedColumns(
133             ActionRequest actionRequest, String portletResource,
134             String newLayoutTemplateId, String oldLayoutTemplateId)
135         throws PortalException, SystemException {
136 
137         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
138             WebKeys.THEME_DISPLAY);
139 
140         Layout layout = themeDisplay.getLayout();
141         LayoutTypePortlet layoutTypePortlet =
142             themeDisplay.getLayoutTypePortlet();
143         Theme theme = themeDisplay.getTheme();
144 
145         LayoutTemplate newLayoutTemplate =
146             LayoutTemplateLocalServiceUtil.getLayoutTemplate(
147                 newLayoutTemplateId, false, theme.getThemeId());
148 
149         List<String> newColumns = getColumnNames(
150             newLayoutTemplate.getContent(), portletResource);
151 
152         LayoutTemplate oldLayoutTemplate =
153             LayoutTemplateLocalServiceUtil.getLayoutTemplate(
154                 oldLayoutTemplateId, false, theme.getThemeId());
155 
156         List<String> oldColumns = getColumnNames(
157             oldLayoutTemplate.getContent(), portletResource);
158 
159         layoutTypePortlet.reorganizePortlets(newColumns, oldColumns);
160 
161         layoutTypePortlet.setStateMax(StringPool.BLANK);
162 
163         LayoutLocalServiceUtil.updateLayout(
164             layout.getGroupId(), layout.isPrivateLayout(),
165             layout.getLayoutId(), layout.getTypeSettings());
166     }
167 
168     private static Pattern _pattern = Pattern.compile(
169         "processColumn[(]\"(.*?)\"[)]", Pattern.DOTALL);
170 
171 }