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