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