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