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