1   /**
2    * Copyright (c) 2000-2008 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 prefs =
79              PortletPreferencesFactoryUtil.getPortletSetup(
80                  actionRequest, portletResource);
81  
82          String oldLayoutTemplateId = prefs.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          prefs.setValue("layout-template-id", layoutTemplateId);
93          prefs.setValue("portlet-setup-show-borders", portletSetupShowBorders);
94  
95          prefs.store();
96  
97          SessionMessages.add(
98              actionRequest, portletConfig.getPortletName() + ".doConfigure");
99      }
100 
101     public String render(
102             PortletConfig portletConfig, RenderRequest renderRequest,
103             RenderResponse renderResponse)
104         throws Exception {
105 
106         return "/html/portlet/nested_portlets/configuration.jsp";
107     }
108 
109     protected List<String> getColumnNames(String content, String portletId) {
110         Matcher matcher = _pattern.matcher(content);
111 
112         Set<String> columnIds = new HashSet<String>();
113 
114         while (matcher.find()) {
115             if (Validator.isNotNull(matcher.group(1))) {
116                 columnIds.add(matcher.group(1));
117             }
118         }
119 
120         List<String> columnNames = new UniqueList<String>();
121 
122         for (String columnId : columnIds) {
123             if (columnId.indexOf(portletId) == -1) {
124                 columnNames.add(portletId + StringPool.UNDERLINE + columnId);
125             }
126         }
127 
128         return columnNames;
129     }
130 
131     protected void reorganizeNestedColumns(
132             ActionRequest actionRequest, String portletResource,
133             String newLayoutTemplateId, String oldLayoutTemplateId)
134         throws PortalException, SystemException {
135 
136         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
137             WebKeys.THEME_DISPLAY);
138 
139         Layout layout = themeDisplay.getLayout();
140         LayoutTypePortlet layoutTypePortlet =
141             themeDisplay.getLayoutTypePortlet();
142         Theme theme = themeDisplay.getTheme();
143 
144         LayoutTemplate newLayoutTemplate =
145             LayoutTemplateLocalServiceUtil.getLayoutTemplate(
146                 newLayoutTemplateId, false, theme.getThemeId());
147 
148         List<String> newColumns = getColumnNames(
149             newLayoutTemplate.getContent(), portletResource);
150 
151         LayoutTemplate oldLayoutTemplate =
152             LayoutTemplateLocalServiceUtil.getLayoutTemplate(
153                 oldLayoutTemplateId, false, theme.getThemeId());
154 
155         List<String> oldColumns = getColumnNames(
156             oldLayoutTemplate.getContent(), portletResource);
157 
158         layoutTypePortlet.reorganizePortlets(newColumns, oldColumns);
159 
160         layoutTypePortlet.setStateMax(StringPool.BLANK);
161 
162         LayoutLocalServiceUtil.updateLayout(
163             layout.getGroupId(), layout.isPrivateLayout(),
164             layout.getLayoutId(), layout.getTypeSettings());
165     }
166 
167     private static Pattern _pattern = Pattern.compile(
168         "processColumn[(]\"(.*?)\"[)]", Pattern.DOTALL);
169 
170 }