001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet.nestedportlets.action;
022:
023: import com.liferay.portal.PortalException;
024: import com.liferay.portal.SystemException;
025: import com.liferay.portal.kernel.portlet.ConfigurationAction;
026: import com.liferay.portal.kernel.util.ParamUtil;
027: import com.liferay.portal.kernel.util.StringPool;
028: import com.liferay.portal.kernel.util.Validator;
029: import com.liferay.portal.model.Layout;
030: import com.liferay.portal.model.LayoutTemplate;
031: import com.liferay.portal.model.LayoutTypePortlet;
032: import com.liferay.portal.model.Theme;
033: import com.liferay.portal.service.LayoutLocalServiceUtil;
034: import com.liferay.portal.service.impl.LayoutTemplateLocalUtil;
035: import com.liferay.portal.theme.ThemeDisplay;
036: import com.liferay.portal.util.PropsValues;
037: import com.liferay.portal.util.WebKeys;
038: import com.liferay.portlet.PortletPreferencesFactoryUtil;
039: import com.liferay.util.UniqueList;
040: import com.liferay.util.servlet.SessionMessages;
041:
042: import java.util.HashSet;
043: import java.util.Iterator;
044: import java.util.List;
045: import java.util.Set;
046: import java.util.regex.Matcher;
047: import java.util.regex.Pattern;
048:
049: import javax.portlet.ActionRequest;
050: import javax.portlet.ActionResponse;
051: import javax.portlet.PortletConfig;
052: import javax.portlet.PortletPreferences;
053: import javax.portlet.RenderRequest;
054: import javax.portlet.RenderResponse;
055:
056: /**
057: * <a href="ConfigurationActionImpl.java.html"><b><i>View Source</i></b></a>
058: *
059: * @author Jorge Ferrer
060: *
061: */
062: public class ConfigurationActionImpl implements ConfigurationAction {
063:
064: public void processAction(PortletConfig config, ActionRequest req,
065: ActionResponse res) throws Exception {
066:
067: String layoutTemplateId = ParamUtil.getString(req,
068: "layoutTemplateId");
069: String portletSetupShowBorders = ParamUtil.getString(req,
070: "portletSetupShowBorders");
071:
072: String portletResource = ParamUtil.getString(req,
073: "portletResource");
074:
075: PortletPreferences prefs = PortletPreferencesFactoryUtil
076: .getPortletSetup(req, portletResource, true, true);
077:
078: String oldLayoutTemplateId = prefs.getValue(
079: "layout-template-id",
080: PropsValues.NESTED_PORTLETS_LAYOUT_TEMPLATE_DEFAULT);
081:
082: if (!oldLayoutTemplateId.equals(layoutTemplateId)) {
083: reorganizeNestedColumns(req, portletResource,
084: layoutTemplateId, oldLayoutTemplateId);
085: }
086:
087: prefs.setValue("layout-template-id", layoutTemplateId);
088: prefs.setValue("portlet-setup-show-borders",
089: portletSetupShowBorders);
090:
091: prefs.store();
092:
093: SessionMessages.add(req, config.getPortletName()
094: + ".doConfigure");
095: }
096:
097: public String render(PortletConfig config, RenderRequest req,
098: RenderResponse res) throws Exception {
099:
100: return "/html/portlet/nested_portlets/configuration.jsp";
101: }
102:
103: protected List getColumnNames(String content, String portletId) {
104: Matcher m = _searchColumnsPattern.matcher(content);
105:
106: Set columnIds = new HashSet();
107:
108: while (m.find()) {
109: if (Validator.isNotNull(m.group(1))) {
110: columnIds.add(m.group(1));
111: }
112: }
113:
114: List columnNames = new UniqueList();
115:
116: Iterator itr = columnIds.iterator();
117:
118: while (itr.hasNext()) {
119: String columnId = (String) itr.next();
120:
121: if (columnId.indexOf(portletId) == -1) {
122: columnNames.add(portletId + StringPool.UNDERLINE
123: + columnId);
124: }
125: }
126:
127: return columnNames;
128: }
129:
130: protected void reorganizeNestedColumns(ActionRequest req,
131: String portletResource, String newLayoutTemplateId,
132: String oldLayoutTemplateId) throws PortalException,
133: SystemException {
134:
135: ThemeDisplay themeDisplay = (ThemeDisplay) req
136: .getAttribute(WebKeys.THEME_DISPLAY);
137:
138: Layout layout = themeDisplay.getLayout();
139: LayoutTypePortlet layoutTypePortlet = themeDisplay
140: .getLayoutTypePortlet();
141: Theme theme = themeDisplay.getTheme();
142:
143: LayoutTemplate newLayoutTemplate = LayoutTemplateLocalUtil
144: .getLayoutTemplate(newLayoutTemplateId, false, theme
145: .getThemeId());
146:
147: List newColumns = getColumnNames(
148: newLayoutTemplate.getContent(), portletResource);
149:
150: LayoutTemplate oldLayoutTemplate = LayoutTemplateLocalUtil
151: .getLayoutTemplate(oldLayoutTemplateId, false, theme
152: .getThemeId());
153:
154: List oldColumns = getColumnNames(
155: oldLayoutTemplate.getContent(), portletResource);
156:
157: layoutTypePortlet.reorganizeNestedColumns(portletResource,
158: newColumns, oldColumns);
159:
160: LayoutLocalServiceUtil.updateLayout(layout.getGroupId(), layout
161: .isPrivateLayout(), layout.getLayoutId(), layout
162: .getTypeSettings());
163: }
164:
165: private static final Pattern _searchColumnsPattern = Pattern
166: .compile("processColumn[(]\"(.*?)\"[)]", Pattern.DOTALL);
167:
168: }
|