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