1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.nestedportlets.action;
16  
17  import com.liferay.portal.kernel.exception.PortalException;
18  import com.liferay.portal.kernel.exception.SystemException;
19  import com.liferay.portal.kernel.portlet.BaseConfigurationAction;
20  import com.liferay.portal.kernel.servlet.SessionMessages;
21  import com.liferay.portal.kernel.util.ParamUtil;
22  import com.liferay.portal.kernel.util.StringPool;
23  import com.liferay.portal.kernel.util.Validator;
24  import com.liferay.portal.model.Layout;
25  import com.liferay.portal.model.LayoutTemplate;
26  import com.liferay.portal.model.LayoutTypePortlet;
27  import com.liferay.portal.model.Theme;
28  import com.liferay.portal.service.LayoutLocalServiceUtil;
29  import com.liferay.portal.service.LayoutTemplateLocalServiceUtil;
30  import com.liferay.portal.theme.ThemeDisplay;
31  import com.liferay.portal.util.PropsValues;
32  import com.liferay.portal.util.WebKeys;
33  import com.liferay.portlet.PortletPreferencesFactoryUtil;
34  import com.liferay.util.UniqueList;
35  
36  import java.util.HashSet;
37  import java.util.List;
38  import java.util.Set;
39  import java.util.regex.Matcher;
40  import java.util.regex.Pattern;
41  
42  import javax.portlet.ActionRequest;
43  import javax.portlet.ActionResponse;
44  import javax.portlet.PortletConfig;
45  import javax.portlet.PortletPreferences;
46  import javax.portlet.RenderRequest;
47  import javax.portlet.RenderResponse;
48  
49  /**
50   * <a href="ConfigurationActionImpl.java.html"><b><i>View Source</i></b></a>
51   *
52   * @author Jorge Ferrer
53   */
54  public class ConfigurationActionImpl extends BaseConfigurationAction {
55  
56      public void processAction(
57              PortletConfig portletConfig, ActionRequest actionRequest,
58              ActionResponse actionResponse)
59          throws Exception {
60  
61          String layoutTemplateId = ParamUtil.getString(
62              actionRequest, "layoutTemplateId");
63          String portletSetupShowBorders = ParamUtil.getString(
64              actionRequest, "portletSetupShowBorders");
65  
66          String portletResource = ParamUtil.getString(
67              actionRequest, "portletResource");
68  
69          PortletPreferences preferences =
70              PortletPreferencesFactoryUtil.getPortletSetup(
71                  actionRequest, portletResource);
72  
73          String oldLayoutTemplateId = preferences.getValue(
74              "layout-template-id",
75              PropsValues.NESTED_PORTLETS_LAYOUT_TEMPLATE_DEFAULT);
76  
77          if (!oldLayoutTemplateId.equals(layoutTemplateId)) {
78              reorganizeNestedColumns(
79                  actionRequest, portletResource, layoutTemplateId,
80                  oldLayoutTemplateId);
81          }
82  
83          preferences.setValue("layout-template-id", layoutTemplateId);
84          preferences.setValue(
85              "portlet-setup-show-borders", portletSetupShowBorders);
86  
87          preferences.store();
88  
89          SessionMessages.add(
90              actionRequest, portletConfig.getPortletName() + ".doConfigure");
91      }
92  
93      public String render(
94              PortletConfig portletConfig, RenderRequest renderRequest,
95              RenderResponse renderResponse)
96          throws Exception {
97  
98          return "/html/portlet/nested_portlets/configuration.jsp";
99      }
100 
101     protected List<String> getColumnNames(String content, String portletId) {
102         Matcher matcher = _pattern.matcher(content);
103 
104         Set<String> columnIds = new HashSet<String>();
105 
106         while (matcher.find()) {
107             if (Validator.isNotNull(matcher.group(1))) {
108                 columnIds.add(matcher.group(1));
109             }
110         }
111 
112         List<String> columnNames = new UniqueList<String>();
113 
114         for (String columnId : columnIds) {
115             if (columnId.indexOf(portletId) == -1) {
116                 columnNames.add(portletId + StringPool.UNDERLINE + columnId);
117             }
118         }
119 
120         return columnNames;
121     }
122 
123     protected void reorganizeNestedColumns(
124             ActionRequest actionRequest, String portletResource,
125             String newLayoutTemplateId, String oldLayoutTemplateId)
126         throws PortalException, SystemException {
127 
128         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
129             WebKeys.THEME_DISPLAY);
130 
131         Layout layout = themeDisplay.getLayout();
132         LayoutTypePortlet layoutTypePortlet =
133             themeDisplay.getLayoutTypePortlet();
134         Theme theme = themeDisplay.getTheme();
135 
136         LayoutTemplate newLayoutTemplate =
137             LayoutTemplateLocalServiceUtil.getLayoutTemplate(
138                 newLayoutTemplateId, false, theme.getThemeId());
139 
140         List<String> newColumns = getColumnNames(
141             newLayoutTemplate.getContent(), portletResource);
142 
143         LayoutTemplate oldLayoutTemplate =
144             LayoutTemplateLocalServiceUtil.getLayoutTemplate(
145                 oldLayoutTemplateId, false, theme.getThemeId());
146 
147         List<String> oldColumns = getColumnNames(
148             oldLayoutTemplate.getContent(), portletResource);
149 
150         layoutTypePortlet.reorganizePortlets(newColumns, oldColumns);
151 
152         layoutTypePortlet.setStateMax(StringPool.BLANK);
153 
154         LayoutLocalServiceUtil.updateLayout(
155             layout.getGroupId(), layout.isPrivateLayout(),
156             layout.getLayoutId(), layout.getTypeSettings());
157     }
158 
159     private static Pattern _pattern = Pattern.compile(
160         "processColumn[(]\"(.*?)\"[)]", Pattern.DOTALL);
161 
162 }