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