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                                            layout.isPrivateLayout());
166    
167                            if (!scopeLayout.hasScopeGroup()) {
168                                    String name = String.valueOf(scopeLayout.getPlid());
169    
170                                    GroupLocalServiceUtil.addGroup(
171                                            themeDisplay.getUserId(),
172                                            GroupConstants.DEFAULT_PARENT_GROUP_ID,
173                                            Layout.class.getName(), scopeLayout.getPlid(),
174                                            GroupConstants.DEFAULT_LIVE_GROUP_ID, name, null, 0, null,
175                                            false, true, null);
176                            }
177    
178                            scopeGroupId = scopeLayout.getGroupId();
179                            scopeName = scopeLayout.getName(themeDisplay.getLocale());
180                    }
181                    else {
182                            throw new IllegalArgumentException(
183                                    "Scope type " + scopeType + " is invalid");
184                    }
185    
186                    return new Tuple(scopeGroupId, scopeName);
187            }
188    
189            protected String getOldScopeName(
190                            ActionRequest actionRequest, Portlet portlet)
191                    throws Exception {
192    
193                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
194                            WebKeys.THEME_DISPLAY);
195    
196                    Layout layout = themeDisplay.getLayout();
197    
198                    PortletPreferences preferences =
199                            PortletPreferencesFactoryUtil.getLayoutPortletSetup(
200                                    layout, portlet.getPortletId());
201    
202                    String scopeType = GetterUtil.getString(
203                            preferences.getValue("lfrScopeType", null));
204    
205                    if (Validator.isNull(scopeType)) {
206                            return null;
207                    }
208    
209                    String scopeName = null;
210    
211                    if (scopeType.equals("company")) {
212                            scopeName = themeDisplay.translate("global");
213                    }
214                    else if (scopeType.equals("layout")) {
215                            String scopeLayoutUuid = GetterUtil.getString(
216                                    preferences.getValue("lfrScopeLayoutUuid", null));
217    
218                            try {
219                                    Layout scopeLayout =
220                                            LayoutLocalServiceUtil.getLayoutByUuidAndGroupId(
221                                                    scopeLayoutUuid, layout.getGroupId(),
222                                                    layout.isPrivateLayout());
223    
224                                    scopeName = scopeLayout.getName(themeDisplay.getLocale());
225                            }
226                            catch (NoSuchLayoutException nsle) {
227                            }
228                    }
229                    else {
230                            throw new IllegalArgumentException(
231                                    "Scope type " + scopeType + " is invalid");
232                    }
233    
234                    return scopeName;
235            }
236    
237            protected String getPortletTitle(
238                    PortletRequest portletRequest, Portlet portlet,
239                    PortletPreferences preferences) {
240    
241                    ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
242                            WebKeys.THEME_DISPLAY);
243    
244                    String portletTitle = PortletConfigurationUtil.getPortletTitle(
245                            preferences, themeDisplay.getLanguageId());
246    
247                    if (Validator.isNull(portletTitle)) {
248                            ServletContext servletContext =
249                                    (ServletContext)portletRequest.getAttribute(WebKeys.CTX);
250    
251                            PortletConfig portletConfig = PortletConfigFactoryUtil.create(
252                                    portlet, servletContext);
253    
254                            ResourceBundle resourceBundle = portletConfig.getResourceBundle(
255                                    themeDisplay.getLocale());
256    
257                            portletTitle = resourceBundle.getString(
258                                    JavaConstants.JAVAX_PORTLET_TITLE);
259                    }
260    
261                    return portletTitle;
262            }
263    
264            protected void updateScope(ActionRequest actionRequest, Portlet portlet)
265                    throws Exception {
266    
267                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
268                            WebKeys.THEME_DISPLAY);
269    
270                    Layout layout = themeDisplay.getLayout();
271    
272                    PortletPreferences preferences =
273                            PortletPreferencesFactoryUtil.getLayoutPortletSetup(
274                                    layout, portlet.getPortletId());
275    
276                    String scopeType = ParamUtil.getString(actionRequest, "scopeType");
277    
278                    preferences.setValue("lfrScopeType", scopeType);
279    
280                    String scopeLayoutUuid = ParamUtil.getString(
281                            actionRequest, "scopeLayoutUuid");
282    
283                    if (!scopeType.equals("layout")) {
284                            scopeLayoutUuid = StringPool.BLANK;
285                    }
286    
287                    preferences.setValue("lfrScopeLayoutUuid", scopeLayoutUuid);
288    
289                    String portletTitle = getPortletTitle(
290                            actionRequest, portlet, preferences);
291    
292                    Tuple newScopeTuple = getNewScope(actionRequest);
293    
294                    long newScopeGroupId = (Long)newScopeTuple.getObject(0);
295    
296                    preferences.setValue("groupId", String.valueOf(newScopeGroupId));
297    
298                    String oldScopeName = getOldScopeName(actionRequest, portlet);
299                    String newScopeName = (String)newScopeTuple.getObject(1);
300    
301                    String newPortletTitle = PortalUtil.getNewPortletTitle(
302                            portletTitle, oldScopeName, newScopeName);
303    
304                    if (!newPortletTitle.equals(portletTitle)) {
305                            preferences.setValue(
306                                    "portletSetupTitle_" + themeDisplay.getLanguageId(),
307                                    newPortletTitle);
308                            preferences.setValue(
309                                    "portletSetupUseCustomTitle", Boolean.TRUE.toString());
310                    }
311    
312                    preferences.store();
313            }
314    
315    }