001    /**
002     * Copyright (c) 2000-present 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.layoutsetprototypes.action;
016    
017    import com.liferay.portal.NoSuchLayoutSetPrototypeException;
018    import com.liferay.portal.RequiredLayoutSetPrototypeException;
019    import com.liferay.portal.kernel.servlet.MultiSessionMessages;
020    import com.liferay.portal.kernel.servlet.SessionErrors;
021    import com.liferay.portal.kernel.util.Constants;
022    import com.liferay.portal.kernel.util.HttpUtil;
023    import com.liferay.portal.kernel.util.LocalizationUtil;
024    import com.liferay.portal.kernel.util.ParamUtil;
025    import com.liferay.portal.kernel.util.StringUtil;
026    import com.liferay.portal.kernel.util.UnicodeProperties;
027    import com.liferay.portal.kernel.util.Validator;
028    import com.liferay.portal.model.LayoutSetPrototype;
029    import com.liferay.portal.security.auth.PrincipalException;
030    import com.liferay.portal.service.LayoutSetPrototypeServiceUtil;
031    import com.liferay.portal.service.ServiceContext;
032    import com.liferay.portal.service.ServiceContextFactory;
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.PortletKeys;
037    import com.liferay.portal.util.WebKeys;
038    import com.liferay.portlet.sites.util.SitesUtil;
039    
040    import java.util.Locale;
041    import java.util.Map;
042    
043    import javax.portlet.ActionRequest;
044    import javax.portlet.ActionResponse;
045    import javax.portlet.PortletConfig;
046    import javax.portlet.PortletURL;
047    import javax.portlet.RenderRequest;
048    import javax.portlet.RenderResponse;
049    
050    import org.apache.struts.action.ActionForm;
051    import org.apache.struts.action.ActionForward;
052    import org.apache.struts.action.ActionMapping;
053    
054    /**
055     * @author Brian Wing Shun Chan
056     * @author Ryan Park
057     * @author M??t?? Thurz??
058     * @author Josef Sustacek
059     */
060    public class EditLayoutSetPrototypeAction extends PortletAction {
061    
062            @Override
063            public void processAction(
064                            ActionMapping actionMapping, ActionForm actionForm,
065                            PortletConfig portletConfig, ActionRequest actionRequest,
066                            ActionResponse actionResponse)
067                    throws Exception {
068    
069                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
070    
071                    String redirect = ParamUtil.getString(actionRequest, "redirect");
072    
073                    try {
074                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
075                                    LayoutSetPrototype layoutSetPrototype =
076                                            updateLayoutSetPrototype(actionRequest);
077    
078                                    ThemeDisplay themeDisplay =
079                                            (ThemeDisplay)actionRequest.getAttribute(
080                                                    WebKeys.THEME_DISPLAY);
081    
082                                    ThemeDisplay siteThemeDisplay =
083                                            (ThemeDisplay)themeDisplay.clone();
084    
085                                    siteThemeDisplay.setScopeGroupId(
086                                            layoutSetPrototype.getGroupId());
087    
088                                    PortletURL siteAdministrationURL =
089                                            PortalUtil.getSiteAdministrationURL(
090                                                    actionResponse, siteThemeDisplay,
091                                                    PortletKeys.SITE_TEMPLATE_SETTINGS);
092    
093                                    String controlPanelURL = HttpUtil.setParameter(
094                                            themeDisplay.getURLControlPanel(), "p_p_id",
095                                            PortletKeys.LAYOUT_SET_PROTOTYPE);
096    
097                                    controlPanelURL = HttpUtil.setParameter(
098                                            controlPanelURL, "controlPanelCategory",
099                                            themeDisplay.getControlPanelCategory());
100    
101                                    siteAdministrationURL.setParameter("redirect", controlPanelURL);
102    
103                                    redirect = siteAdministrationURL.toString();
104    
105                                    if (cmd.equals(Constants.ADD)) {
106                                            hideDefaultSuccessMessage(actionRequest);
107    
108                                            MultiSessionMessages.add(
109                                                    actionRequest,
110                                                    PortletKeys.SITE_TEMPLATE_SETTINGS +
111                                                            "requestProcessed");
112                                    }
113                            }
114                            else if (cmd.equals(Constants.DELETE)) {
115                                    deleteLayoutSetPrototypes(actionRequest);
116                            }
117                            else if (cmd.equals("reset_merge_fail_count")) {
118                                    resetMergeFailCount(actionRequest);
119                            }
120    
121                            sendRedirect(actionRequest, actionResponse, redirect);
122                    }
123                    catch (Exception e) {
124                            if (e instanceof PrincipalException) {
125                                    SessionErrors.add(actionRequest, e.getClass());
126    
127                                    setForward(
128                                            actionRequest, "portlet.layout_set_prototypes.error");
129                            }
130                            else if (e instanceof RequiredLayoutSetPrototypeException) {
131                                    SessionErrors.add(actionRequest, e.getClass());
132    
133                                    redirect = PortalUtil.escapeRedirect(redirect);
134    
135                                    if (Validator.isNotNull(redirect)) {
136                                            actionResponse.sendRedirect(redirect);
137                                    }
138                            }
139                            else {
140                                    throw e;
141                            }
142                    }
143            }
144    
145            @Override
146            public ActionForward render(
147                            ActionMapping actionMapping, ActionForm actionForm,
148                            PortletConfig portletConfig, RenderRequest renderRequest,
149                            RenderResponse renderResponse)
150                    throws Exception {
151    
152                    try {
153                            ActionUtil.getLayoutSetPrototype(renderRequest);
154                    }
155                    catch (Exception e) {
156                            if (e instanceof NoSuchLayoutSetPrototypeException ||
157                                    e instanceof PrincipalException) {
158    
159                                    SessionErrors.add(renderRequest, e.getClass());
160    
161                                    return actionMapping.findForward(
162                                            "portlet.layout_set_prototypes.error");
163                            }
164                            else {
165                                    throw e;
166                            }
167                    }
168    
169                    return actionMapping.findForward(
170                            getForward(
171                                    renderRequest,
172                                    "portlet.layout_set_prototypes.edit_layout_set_prototype"));
173            }
174    
175            protected void deleteLayoutSetPrototypes(ActionRequest actionRequest)
176                    throws Exception {
177    
178                    long[] layoutSetPrototypeIds = StringUtil.split(
179                            ParamUtil.getString(actionRequest, "layoutSetPrototypeIds"), 0L);
180    
181                    for (long layoutSetPrototypeId : layoutSetPrototypeIds) {
182                            LayoutSetPrototypeServiceUtil.deleteLayoutSetPrototype(
183                                    layoutSetPrototypeId);
184                    }
185            }
186    
187            @Override
188            protected boolean isCheckMethodOnProcessAction() {
189                    return _CHECK_METHOD_ON_PROCESS_ACTION;
190            }
191    
192            protected void resetMergeFailCount(ActionRequest actionRequest)
193                    throws Exception {
194    
195                    long layoutSetPrototypeId = ParamUtil.getLong(
196                            actionRequest, "layoutSetPrototypeId");
197    
198                    LayoutSetPrototype layoutSetPrototype =
199                            LayoutSetPrototypeServiceUtil.getLayoutSetPrototype(
200                                    layoutSetPrototypeId);
201    
202                    SitesUtil.setMergeFailCount(layoutSetPrototype, 0);
203            }
204    
205            protected LayoutSetPrototype updateLayoutSetPrototype(
206                            ActionRequest actionRequest)
207                    throws Exception {
208    
209                    long layoutSetPrototypeId = ParamUtil.getLong(
210                            actionRequest, "layoutSetPrototypeId");
211    
212                    Map<Locale, String> nameMap = LocalizationUtil.getLocalizationMap(
213                            actionRequest, "name");
214                    Map<Locale, String> descriptionMap =
215                            LocalizationUtil.getLocalizationMap(actionRequest, "description");
216                    boolean active = ParamUtil.getBoolean(actionRequest, "active");
217                    boolean layoutsUpdateable = ParamUtil.getBoolean(
218                            actionRequest, "layoutsUpdateable");
219    
220                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
221                            actionRequest);
222    
223                    LayoutSetPrototype layoutSetPrototype = null;
224    
225                    if (layoutSetPrototypeId <= 0) {
226    
227                            // Add layout prototoype
228    
229                            layoutSetPrototype =
230                                    LayoutSetPrototypeServiceUtil.addLayoutSetPrototype(
231                                            nameMap, descriptionMap, active, layoutsUpdateable,
232                                            serviceContext);
233                    }
234                    else {
235    
236                            // Update layout prototoype
237    
238                            layoutSetPrototype =
239                                    LayoutSetPrototypeServiceUtil.updateLayoutSetPrototype(
240                                            layoutSetPrototypeId, nameMap, descriptionMap, active,
241                                            layoutsUpdateable, serviceContext);
242                    }
243    
244                    // Custom JSPs
245    
246                    String customJspServletContextName = ParamUtil.getString(
247                            actionRequest, "customJspServletContextName");
248    
249                    UnicodeProperties settingsProperties =
250                            layoutSetPrototype.getSettingsProperties();
251    
252                    settingsProperties.setProperty(
253                            "customJspServletContextName", customJspServletContextName);
254    
255                    return LayoutSetPrototypeServiceUtil.updateLayoutSetPrototype(
256                            layoutSetPrototype.getLayoutSetPrototypeId(),
257                            settingsProperties.toString());
258            }
259    
260            private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
261    
262    }