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.Iterator;
46  import java.util.List;
47  import java.util.Set;
48  import java.util.regex.Matcher;
49  import java.util.regex.Pattern;
50  
51  import javax.portlet.ActionRequest;
52  import javax.portlet.ActionResponse;
53  import javax.portlet.PortletConfig;
54  import javax.portlet.PortletPreferences;
55  import javax.portlet.RenderRequest;
56  import javax.portlet.RenderResponse;
57  
58  /**
59   * <a href="ConfigurationActionImpl.java.html"><b><i>View Source</i></b></a>
60   *
61   * @author Jorge Ferrer
62   *
63   */
64  public class ConfigurationActionImpl implements ConfigurationAction {
65  
66      public void processAction(
67              PortletConfig config, ActionRequest req, ActionResponse res)
68          throws Exception {
69  
70          String layoutTemplateId = ParamUtil.getString(req, "layoutTemplateId");
71          String portletSetupShowBorders = ParamUtil.getString(
72              req, "portletSetupShowBorders");
73  
74          String portletResource = ParamUtil.getString(req, "portletResource");
75  
76          PortletPreferences prefs =
77              PortletPreferencesFactoryUtil.getPortletSetup(
78                  req, portletResource, true, true);
79  
80          String oldLayoutTemplateId = prefs.getValue(
81              "layout-template-id",
82              PropsValues.NESTED_PORTLETS_LAYOUT_TEMPLATE_DEFAULT);
83  
84          if (!oldLayoutTemplateId.equals(layoutTemplateId)) {
85              reorganizeNestedColumns(
86                  req, portletResource, layoutTemplateId, oldLayoutTemplateId);
87          }
88  
89          prefs.setValue("layout-template-id", layoutTemplateId);
90          prefs.setValue("portlet-setup-show-borders", portletSetupShowBorders);
91  
92          prefs.store();
93  
94          SessionMessages.add(req, config.getPortletName() + ".doConfigure");
95      }
96  
97      public String render(
98              PortletConfig config, RenderRequest req, RenderResponse res)
99          throws Exception {
100 
101         return "/html/portlet/nested_portlets/configuration.jsp";
102     }
103 
104     protected List getColumnNames(String content, String portletId) {
105         Matcher m = _searchColumnsPattern.matcher(content);
106 
107         Set columnIds = new HashSet();
108 
109         while (m.find()) {
110             if (Validator.isNotNull(m.group(1))) {
111                 columnIds.add(m.group(1));
112             }
113         }
114 
115         List columnNames = new UniqueList();
116 
117         Iterator itr = columnIds.iterator();
118 
119         while (itr.hasNext()) {
120             String columnId = (String)itr.next();
121 
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 req, String portletResource,
132             String newLayoutTemplateId, String oldLayoutTemplateId)
133         throws PortalException, SystemException {
134 
135         ThemeDisplay themeDisplay = (ThemeDisplay)req.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             LayoutTemplateLocalUtil.getLayoutTemplate(
145                 newLayoutTemplateId, false, theme.getThemeId());
146 
147         List newColumns = getColumnNames(
148             newLayoutTemplate.getContent(), portletResource);
149 
150         LayoutTemplate oldLayoutTemplate =
151             LayoutTemplateLocalUtil.getLayoutTemplate(
152                 oldLayoutTemplateId, false, theme.getThemeId());
153 
154         List oldColumns = getColumnNames(
155             oldLayoutTemplate.getContent(), portletResource);
156 
157         layoutTypePortlet.reorganizeNestedColumns(
158             portletResource, newColumns, oldColumns);
159 
160         LayoutLocalServiceUtil.updateLayout(
161             layout.getGroupId(), layout.isPrivateLayout(),
162             layout.getLayoutId(), layout.getTypeSettings());
163     }
164 
165     private static final Pattern _searchColumnsPattern = Pattern.compile(
166         "processColumn[(]\"(.*?)\"[)]", Pattern.DOTALL);
167 
168 }