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.util.PortalUtil;
033    import com.liferay.portlet.sites.util.SitesUtil;
034    
035    import java.util.Locale;
036    import java.util.Map;
037    
038    import javax.portlet.ActionRequest;
039    import javax.portlet.ActionResponse;
040    import javax.portlet.PortletConfig;
041    import javax.portlet.RenderRequest;
042    import javax.portlet.RenderResponse;
043    
044    import org.apache.struts.action.ActionForm;
045    import org.apache.struts.action.ActionForward;
046    import org.apache.struts.action.ActionMapping;
047    
048    /**
049     * @author Brian Wing Shun Chan
050     * @author Ryan Park
051     * @author Máté Thurzó
052     * @author Josef Sustacek
053     */
054    public class EditLayoutSetPrototypeAction extends PortletAction {
055    
056            @Override
057            public void processAction(
058                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
059                            ActionRequest actionRequest, ActionResponse actionResponse)
060                    throws Exception {
061    
062                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
063    
064                    try {
065                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
066                                    updateLayoutSetPrototype(actionRequest);
067                            }
068                            else if (cmd.equals(Constants.DELETE)) {
069                                    deleteLayoutSetPrototypes(actionRequest);
070                            }
071                            else if (cmd.equals("reset_merge_fail_count")) {
072                                    resetMergeFailCount(actionRequest);
073                            }
074    
075                            sendRedirect(actionRequest, actionResponse);
076                    }
077                    catch (Exception e) {
078                            if (e instanceof PrincipalException) {
079                                    SessionErrors.add(actionRequest, e.getClass());
080    
081                                    setForward(
082                                            actionRequest, "portlet.layout_set_prototypes.error");
083                            }
084                            else if (e instanceof RequiredLayoutSetPrototypeException) {
085                                    SessionErrors.add(actionRequest, e.getClass());
086    
087                                    String redirect = PortalUtil.escapeRedirect(
088                                            ParamUtil.getString(actionRequest, "redirect"));
089    
090                                    if (Validator.isNotNull(redirect)) {
091                                            actionResponse.sendRedirect(redirect);
092                                    }
093                            }
094                            else {
095                                    throw e;
096                            }
097                    }
098            }
099    
100            @Override
101            public ActionForward render(
102                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
103                            RenderRequest renderRequest, RenderResponse renderResponse)
104                    throws Exception {
105    
106                    try {
107                            ActionUtil.getLayoutSetPrototype(renderRequest);
108                    }
109                    catch (Exception e) {
110                            if (e instanceof NoSuchLayoutSetPrototypeException ||
111                                    e instanceof PrincipalException) {
112    
113                                    SessionErrors.add(renderRequest, e.getClass());
114    
115                                    return mapping.findForward(
116                                            "portlet.layout_set_prototypes.error");
117                            }
118                            else {
119                                    throw e;
120                            }
121                    }
122    
123                    return mapping.findForward(
124                            getForward(
125                                    renderRequest,
126                                    "portlet.layout_set_prototypes.edit_layout_set_prototype"));
127            }
128    
129            protected void deleteLayoutSetPrototypes(ActionRequest actionRequest)
130                    throws Exception {
131    
132                    long[] layoutSetPrototypeIds = StringUtil.split(
133                            ParamUtil.getString(actionRequest, "layoutSetPrototypeIds"), 0L);
134    
135                    for (long layoutSetPrototypeId : layoutSetPrototypeIds) {
136                            LayoutSetPrototypeServiceUtil.deleteLayoutSetPrototype(
137                                    layoutSetPrototypeId);
138                    }
139            }
140    
141            protected void resetMergeFailCount(ActionRequest actionRequest)
142                    throws Exception {
143    
144                    long layoutSetPrototypeId = ParamUtil.getLong(
145                            actionRequest, "layoutSetPrototypeId");
146    
147                    LayoutSetPrototype layoutSetPrototype =
148                            LayoutSetPrototypeServiceUtil.getLayoutSetPrototype(
149                                    layoutSetPrototypeId);
150    
151                    SitesUtil.setMergeFailCount(layoutSetPrototype, 0);
152            }
153    
154            protected void updateLayoutSetPrototype(ActionRequest actionRequest)
155                    throws Exception {
156    
157                    long layoutSetPrototypeId = ParamUtil.getLong(
158                            actionRequest, "layoutSetPrototypeId");
159    
160                    Map<Locale, String> nameMap = LocalizationUtil.getLocalizationMap(
161                            actionRequest, "name");
162                    String description = ParamUtil.getString(actionRequest, "description");
163                    boolean active = ParamUtil.getBoolean(actionRequest, "active");
164                    boolean layoutsUpdateable = ParamUtil.getBoolean(
165                            actionRequest, "layoutsUpdateable");
166    
167                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
168                            actionRequest);
169    
170                    LayoutSetPrototype layoutSetPrototype = null;
171    
172                    if (layoutSetPrototypeId <= 0) {
173    
174                            // Add layout prototoype
175    
176                            layoutSetPrototype =
177                                    LayoutSetPrototypeServiceUtil.addLayoutSetPrototype(
178                                            nameMap, description, active, layoutsUpdateable,
179                                            serviceContext);
180                    }
181                    else {
182    
183                            // Update layout prototoype
184    
185                            layoutSetPrototype =
186                                    LayoutSetPrototypeServiceUtil.updateLayoutSetPrototype(
187                                            layoutSetPrototypeId, nameMap, description, active,
188                                            layoutsUpdateable, serviceContext);
189                    }
190    
191                    // Custom JSPs
192    
193                    String customJspServletContextName = ParamUtil.getString(
194                            actionRequest, "customJspServletContextName");
195    
196                    UnicodeProperties settingsProperties =
197                            layoutSetPrototype.getSettingsProperties();
198    
199                    settingsProperties.setProperty(
200                            "customJspServletContextName", customJspServletContextName);
201    
202                    LayoutSetPrototypeServiceUtil.updateLayoutSetPrototype(
203                            layoutSetPrototype.getLayoutSetPrototypeId(),
204                            settingsProperties.toString());
205            }
206    
207    }