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