001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.portletconfiguration.action;
016    
017    import com.liferay.portal.NoSuchLayoutException;
018    import com.liferay.portal.kernel.servlet.SessionErrors;
019    import com.liferay.portal.kernel.servlet.SessionMessages;
020    import com.liferay.portal.kernel.util.Constants;
021    import com.liferay.portal.kernel.util.GetterUtil;
022    import com.liferay.portal.kernel.util.JavaConstants;
023    import com.liferay.portal.kernel.util.ParamUtil;
024    import com.liferay.portal.kernel.util.StringBundler;
025    import com.liferay.portal.kernel.util.StringPool;
026    import com.liferay.portal.kernel.util.Validator;
027    import com.liferay.portal.model.Layout;
028    import com.liferay.portal.model.Portlet;
029    import com.liferay.portal.security.auth.PrincipalException;
030    import com.liferay.portal.service.GroupLocalServiceUtil;
031    import com.liferay.portal.service.LayoutLocalServiceUtil;
032    import com.liferay.portal.theme.ThemeDisplay;
033    import com.liferay.portal.util.WebKeys;
034    import com.liferay.portlet.PortletConfigFactoryUtil;
035    import com.liferay.portlet.PortletPreferencesFactoryUtil;
036    import com.liferay.portlet.portletconfiguration.util.PortletConfigurationUtil;
037    
038    import java.util.ResourceBundle;
039    
040    import javax.portlet.ActionRequest;
041    import javax.portlet.ActionResponse;
042    import javax.portlet.PortletConfig;
043    import javax.portlet.PortletPreferences;
044    import javax.portlet.PortletRequest;
045    import javax.portlet.RenderRequest;
046    import javax.portlet.RenderResponse;
047    
048    import javax.servlet.ServletContext;
049    
050    import org.apache.struts.action.ActionForm;
051    import org.apache.struts.action.ActionForward;
052    import org.apache.struts.action.ActionMapping;
053    
054    /**
055     * @author Jesper Weissglas
056     * @author Jorge Ferrer
057     */
058    public class EditScopeAction extends EditConfigurationAction {
059    
060            public void processAction(
061                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
062                            ActionRequest actionRequest, ActionResponse actionResponse)
063                    throws Exception {
064    
065                    Portlet portlet = null;
066    
067                    try {
068                            portlet = getPortlet(actionRequest);
069                    }
070                    catch (PrincipalException pe) {
071                            SessionErrors.add(
072                                    actionRequest, PrincipalException.class.getName());
073    
074                            setForward(actionRequest, "portlet.portlet_configuration.error");
075                    }
076    
077                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
078    
079                    if (cmd.equals(Constants.SAVE)) {
080                            updateScope(actionRequest, portlet);
081                    }
082    
083                    if (SessionErrors.isEmpty(actionRequest)) {
084                            SessionMessages.add(
085                                    actionRequest,
086                                    portletConfig.getPortletName() + ".doConfigure");
087    
088                            String redirect = ParamUtil.getString(actionRequest, "redirect");
089    
090                            actionResponse.sendRedirect(redirect);
091                    }
092            }
093    
094            public ActionForward render(
095                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
096                            RenderRequest renderRequest, RenderResponse renderResponse)
097                    throws Exception {
098    
099                    Portlet portlet = null;
100    
101                    try {
102                            portlet = getPortlet(renderRequest);
103                    }
104                    catch (PrincipalException pe) {
105                            SessionErrors.add(
106                                    renderRequest, PrincipalException.class.getName());
107    
108                            return mapping.findForward("portlet.portlet_configuration.error");
109                    }
110    
111                    renderResponse.setTitle(getTitle(portlet, renderRequest));
112    
113                    return mapping.findForward(getForward(
114                            renderRequest, "portlet.portlet_configuration.edit_scope"));
115            }
116    
117            protected String getPortletTitle(
118                    PortletRequest portletRequest, Portlet portlet,
119                    PortletPreferences preferences) {
120    
121                    ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
122                            WebKeys.THEME_DISPLAY);
123    
124                    String portletTitle = PortletConfigurationUtil.getPortletTitle(
125                            preferences, themeDisplay.getLanguageId());
126    
127                    if (Validator.isNull(portletTitle)) {
128                            ServletContext servletContext =
129                                    (ServletContext)portletRequest.getAttribute(WebKeys.CTX);
130    
131                            PortletConfig portletConfig = PortletConfigFactoryUtil.create(
132                                    portlet, servletContext);
133    
134                            ResourceBundle resourceBundle = portletConfig.getResourceBundle(
135                                    themeDisplay.getLocale());
136    
137                            portletTitle = resourceBundle.getString(
138                                    JavaConstants.JAVAX_PORTLET_TITLE);
139                    }
140    
141                    return portletTitle;
142            }
143    
144            protected void updateScope(ActionRequest actionRequest, Portlet portlet)
145                    throws Exception {
146    
147                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
148                            WebKeys.THEME_DISPLAY);
149    
150                    Layout layout = themeDisplay.getLayout();
151    
152                    PortletPreferences preferences =
153                            PortletPreferencesFactoryUtil.getLayoutPortletSetup(
154                                    layout, portlet.getPortletId());
155    
156                    String scopeLayoutUuid = ParamUtil.getString(
157                            actionRequest, "scopeLayoutUuid");
158                    String oldScopeLayoutUuid = GetterUtil.getString(
159                            preferences.getValue("lfr-scope-layout-uuid", null));
160                    String title = getPortletTitle(actionRequest, portlet, preferences);
161                    String newTitle = title;
162    
163                    // Remove old scope suffix from the title if present
164    
165                    if (Validator.isNotNull(oldScopeLayoutUuid)) {
166                            try {
167                                    Layout oldScopeLayout =
168                                            LayoutLocalServiceUtil.getLayoutByUuidAndGroupId(
169                                                    oldScopeLayoutUuid, layout.getGroupId());
170    
171                                    StringBundler sb = new StringBundler(4);
172    
173                                    sb.append(StringPool.SPACE);
174                                    sb.append(StringPool.OPEN_PARENTHESIS);
175                                    sb.append(oldScopeLayout.getName(themeDisplay.getLocale()));
176                                    sb.append(StringPool.CLOSE_PARENTHESIS);
177    
178                                    String suffix = sb.toString();
179    
180                                    if (newTitle.endsWith(suffix)) {
181                                            newTitle = newTitle.substring(
182                                                    0, title.length() - suffix.length());
183                                    }
184                            }
185                            catch (NoSuchLayoutException nsle) {
186                            }
187                    }
188    
189                    // Add new scope suffix to the title
190    
191                    if (Validator.isNotNull(scopeLayoutUuid)) {
192                            Layout scopeLayout =
193                                    LayoutLocalServiceUtil.getLayoutByUuidAndGroupId(
194                                            scopeLayoutUuid, layout.getGroupId());
195    
196                            if (!scopeLayout.hasScopeGroup()) {
197                                    String name = String.valueOf(scopeLayout.getPlid());
198    
199                                    GroupLocalServiceUtil.addGroup(
200                                            themeDisplay.getUserId(), Layout.class.getName(),
201                                            scopeLayout.getPlid(), name, null, 0, null, true, null);
202                            }
203    
204                            StringBundler sb = new StringBundler(5);
205    
206                            sb.append(newTitle);
207                            sb.append(StringPool.SPACE);
208                            sb.append(StringPool.OPEN_PARENTHESIS);
209                            sb.append(scopeLayout.getName(themeDisplay.getLocale()));
210                            sb.append(StringPool.CLOSE_PARENTHESIS);
211    
212                            newTitle = sb.toString();
213                    }
214    
215                    preferences.setValue("lfr-scope-layout-uuid", scopeLayoutUuid);
216    
217                    if (!newTitle.equals(title)) {
218                            preferences.setValue(
219                                    "portlet-setup-title-" + themeDisplay.getLanguageId(),
220                                    newTitle);
221                            preferences.setValue("portlet-setup-use-custom-title", "true");
222                    }
223    
224                    preferences.store();
225            }
226    
227    }