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