001    /**
002     * Copyright (c) 2000-2012 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.StringPool;
025    import com.liferay.portal.kernel.util.Tuple;
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.PortalUtil;
034    import com.liferay.portal.util.WebKeys;
035    import com.liferay.portlet.PortletConfigFactoryUtil;
036    import com.liferay.portlet.PortletPreferencesFactoryUtil;
037    import com.liferay.portlet.portletconfiguration.util.PortletConfigurationUtil;
038    
039    import java.util.ResourceBundle;
040    
041    import javax.portlet.ActionRequest;
042    import javax.portlet.ActionResponse;
043    import javax.portlet.PortletConfig;
044    import javax.portlet.PortletPreferences;
045    import javax.portlet.PortletRequest;
046    import javax.portlet.RenderRequest;
047    import javax.portlet.RenderResponse;
048    
049    import javax.servlet.ServletContext;
050    
051    import org.apache.struts.action.ActionForm;
052    import org.apache.struts.action.ActionForward;
053    import org.apache.struts.action.ActionMapping;
054    
055    /**
056     * @author Jesper Weissglas
057     * @author Jorge Ferrer
058     * @author Hugo Huijser
059     */
060    public class EditScopeAction extends EditConfigurationAction {
061    
062            @Override
063            public void processAction(
064                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
065                            ActionRequest actionRequest, ActionResponse actionResponse)
066                    throws Exception {
067    
068                    Portlet portlet = null;
069    
070                    try {
071                            portlet = getPortlet(actionRequest);
072                    }
073                    catch (PrincipalException pe) {
074                            SessionErrors.add(
075                                    actionRequest, PrincipalException.class.getName());
076    
077                            setForward(actionRequest, "portlet.portlet_configuration.error");
078                    }
079    
080                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
081    
082                    if (cmd.equals(Constants.SAVE)) {
083                            updateScope(actionRequest, portlet);
084                    }
085    
086                    if (SessionErrors.isEmpty(actionRequest)) {
087                            String portletResource = ParamUtil.getString(
088                                    actionRequest, "portletResource");
089    
090                            SessionMessages.add(
091                                    actionRequest,
092                                    portletConfig.getPortletName() +
093                                            SessionMessages.KEY_SUFFIX_REFRESH_PORTLET,
094                                    portletResource);
095    
096                            SessionMessages.add(
097                                    actionRequest,
098                                    portletConfig.getPortletName() +
099                                            SessionMessages.KEY_SUFFIX_UPDATED_CONFIGURATION);
100    
101                            String redirect = PortalUtil.escapeRedirect(
102                                    ParamUtil.getString(actionRequest, "redirect"));
103    
104                            if (Validator.isNotNull(redirect)) {
105                                    actionResponse.sendRedirect(redirect);
106                            }
107                    }
108            }
109    
110            @Override
111            public ActionForward render(
112                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
113                            RenderRequest renderRequest, RenderResponse renderResponse)
114                    throws Exception {
115    
116                    Portlet portlet = null;
117    
118                    try {
119                            portlet = getPortlet(renderRequest);
120                    }
121                    catch (PrincipalException pe) {
122                            SessionErrors.add(
123                                    renderRequest, PrincipalException.class.getName());
124    
125                            return mapping.findForward("portlet.portlet_configuration.error");
126                    }
127    
128                    renderResponse.setTitle(getTitle(portlet, renderRequest));
129    
130                    return mapping.findForward(getForward(
131                            renderRequest, "portlet.portlet_configuration.edit_scope"));
132            }
133    
134            protected Tuple getNewScope(ActionRequest actionRequest)
135                    throws Exception {
136    
137                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
138                            WebKeys.THEME_DISPLAY);
139    
140                    Layout layout = themeDisplay.getLayout();
141    
142                    String scopeType = ParamUtil.getString(actionRequest, "scopeType");
143    
144                    long scopeGroupId = 0;
145                    String scopeName = null;
146    
147                    if (Validator.isNull(scopeType)) {
148                            scopeGroupId = layout.getGroupId();
149                    }
150                    else if (scopeType.equals("company")) {
151                            scopeGroupId = themeDisplay.getCompanyGroupId();
152                            scopeName = themeDisplay.translate("global");
153                    }
154                    else if (scopeType.equals("layout")) {
155                            String scopeLayoutUuid = ParamUtil.getString(
156                                    actionRequest, "scopeLayoutUuid");
157    
158                            Layout scopeLayout =
159                                    LayoutLocalServiceUtil.getLayoutByUuidAndGroupId(
160                                            scopeLayoutUuid, layout.getGroupId());
161    
162                            if (!scopeLayout.hasScopeGroup()) {
163                                    String name = String.valueOf(scopeLayout.getPlid());
164    
165                                    GroupLocalServiceUtil.addGroup(
166                                            themeDisplay.getUserId(), Layout.class.getName(),
167                                            scopeLayout.getPlid(), name, null, 0, null, false, true,
168                                            null);
169                            }
170    
171                            scopeGroupId = scopeLayout.getGroupId();
172                            scopeName = scopeLayout.getName(themeDisplay.getLocale());
173                    }
174                    else {
175                            throw new IllegalArgumentException(
176                                    "Scope type " + scopeType + " is invalid");
177                    }
178    
179                    return new Tuple(scopeGroupId, scopeName);
180            }
181    
182            protected String getOldScopeName(
183                            ActionRequest actionRequest, Portlet portlet)
184                    throws Exception {
185    
186                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
187                            WebKeys.THEME_DISPLAY);
188    
189                    Layout layout = themeDisplay.getLayout();
190    
191                    PortletPreferences preferences =
192                            PortletPreferencesFactoryUtil.getLayoutPortletSetup(
193                                    layout, portlet.getPortletId());
194    
195                    String scopeType = GetterUtil.getString(
196                            preferences.getValue("lfrScopeType", null));
197    
198                    if (Validator.isNull(scopeType)) {
199                            return null;
200                    }
201    
202                    String scopeName = null;
203    
204                    if (scopeType.equals("company")) {
205                            scopeName = themeDisplay.translate("global");
206                    }
207                    else if (scopeType.equals("layout")) {
208                            String scopeLayoutUuid = GetterUtil.getString(
209                                    preferences.getValue("lfrScopeLayoutUuid", null));
210    
211                            try {
212                                    Layout scopeLayout =
213                                            LayoutLocalServiceUtil.getLayoutByUuidAndGroupId(
214                                                    scopeLayoutUuid, layout.getGroupId());
215    
216                                    scopeName = scopeLayout.getName(themeDisplay.getLocale());
217                            }
218                            catch (NoSuchLayoutException nsle) {
219                            }
220                    }
221                    else {
222                            throw new IllegalArgumentException(
223                                    "Scope type " + scopeType + " is invalid");
224                    }
225    
226                    return scopeName;
227            }
228    
229            protected String getPortletTitle(
230                    PortletRequest portletRequest, Portlet portlet,
231                    PortletPreferences preferences) {
232    
233                    ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
234                            WebKeys.THEME_DISPLAY);
235    
236                    String portletTitle = PortletConfigurationUtil.getPortletTitle(
237                            preferences, themeDisplay.getLanguageId());
238    
239                    if (Validator.isNull(portletTitle)) {
240                            ServletContext servletContext =
241                                    (ServletContext)portletRequest.getAttribute(WebKeys.CTX);
242    
243                            PortletConfig portletConfig = PortletConfigFactoryUtil.create(
244                                    portlet, servletContext);
245    
246                            ResourceBundle resourceBundle = portletConfig.getResourceBundle(
247                                    themeDisplay.getLocale());
248    
249                            portletTitle = resourceBundle.getString(
250                                    JavaConstants.JAVAX_PORTLET_TITLE);
251                    }
252    
253                    return portletTitle;
254            }
255    
256            protected void updateScope(ActionRequest actionRequest, Portlet portlet)
257                    throws Exception {
258    
259                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
260                            WebKeys.THEME_DISPLAY);
261    
262                    Layout layout = themeDisplay.getLayout();
263    
264                    PortletPreferences preferences =
265                            PortletPreferencesFactoryUtil.getLayoutPortletSetup(
266                                    layout, portlet.getPortletId());
267    
268                    String scopeType = ParamUtil.getString(actionRequest, "scopeType");
269    
270                    preferences.setValue("lfrScopeType", scopeType);
271    
272                    String scopeLayoutUuid = ParamUtil.getString(
273                            actionRequest, "scopeLayoutUuid");
274    
275                    if (!scopeType.equals("layout")) {
276                            scopeLayoutUuid = StringPool.BLANK;
277                    }
278    
279                    preferences.setValue("lfrScopeLayoutUuid", scopeLayoutUuid);
280    
281                    String portletTitle = getPortletTitle(
282                            actionRequest, portlet, preferences);
283    
284                    Tuple newScopeTuple = getNewScope(actionRequest);
285    
286                    long newScopeGroupId = (Long)newScopeTuple.getObject(0);
287    
288                    preferences.setValue("groupId", String.valueOf(newScopeGroupId));
289    
290                    String oldScopeName = getOldScopeName(actionRequest, portlet);
291                    String newScopeName = (String)newScopeTuple.getObject(1);
292    
293                    String newPortletTitle = PortalUtil.getNewPortletTitle(
294                            portletTitle, oldScopeName, newScopeName);
295    
296                    if (!newPortletTitle.equals(portletTitle)) {
297                            preferences.setValue(
298                                    "portletSetupTitle_" + themeDisplay.getLanguageId(),
299                                    newPortletTitle);
300                            preferences.setValue(
301                                    "portletSetupUseCustomTitle", Boolean.TRUE.toString());
302                    }
303    
304                    preferences.store();
305            }
306    
307    }