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.layoutprototypes.action;
016    
017    import com.liferay.portal.NoSuchLayoutPrototypeException;
018    import com.liferay.portal.RequiredLayoutPrototypeException;
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.Validator;
025    import com.liferay.portal.model.LayoutPrototype;
026    import com.liferay.portal.security.auth.PrincipalException;
027    import com.liferay.portal.service.LayoutPrototypeServiceUtil;
028    import com.liferay.portal.service.ServiceContext;
029    import com.liferay.portal.service.ServiceContextFactory;
030    import com.liferay.portal.struts.PortletAction;
031    import com.liferay.portal.util.PortalUtil;
032    import com.liferay.portlet.sites.util.SitesUtil;
033    
034    import java.util.Locale;
035    import java.util.Map;
036    
037    import javax.portlet.ActionRequest;
038    import javax.portlet.ActionResponse;
039    import javax.portlet.PortletConfig;
040    import javax.portlet.RenderRequest;
041    import javax.portlet.RenderResponse;
042    
043    import org.apache.struts.action.ActionForm;
044    import org.apache.struts.action.ActionForward;
045    import org.apache.struts.action.ActionMapping;
046    
047    /**
048     * @author Jorge Ferrer
049     * @author Vilmos Papp
050     * @author Josef Sustacek
051     */
052    public class EditLayoutPrototypeAction extends PortletAction {
053    
054            @Override
055            public void processAction(
056                            ActionMapping actionMapping, ActionForm actionForm,
057                            PortletConfig portletConfig, ActionRequest actionRequest,
058                            ActionResponse actionResponse)
059                    throws Exception {
060    
061                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
062    
063                    try {
064                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
065                                    updateLayoutPrototype(actionRequest);
066                            }
067                            else if (cmd.equals(Constants.DELETE)) {
068                                    deleteLayoutPrototypes(actionRequest);
069                            }
070                            else if (cmd.equals("reset_merge_fail_count")) {
071                                    resetMergeFailCount(actionRequest);
072                            }
073    
074                            sendRedirect(actionRequest, actionResponse);
075                    }
076                    catch (Exception e) {
077                            if (e instanceof PrincipalException) {
078                                    SessionErrors.add(actionRequest, e.getClass());
079    
080                                    setForward(actionRequest, "portlet.layout_prototypes.error");
081                            }
082                            else if (e instanceof RequiredLayoutPrototypeException) {
083                                    SessionErrors.add(actionRequest, e.getClass());
084    
085                                    String redirect = PortalUtil.escapeRedirect(
086                                            ParamUtil.getString(actionRequest, "redirect"));
087    
088                                    if (Validator.isNotNull(redirect)) {
089                                            actionResponse.sendRedirect(redirect);
090                                    }
091                            }
092                            else {
093                                    throw e;
094                            }
095                    }
096            }
097    
098            @Override
099            public ActionForward render(
100                            ActionMapping actionMapping, ActionForm actionForm,
101                            PortletConfig portletConfig, RenderRequest renderRequest,
102                            RenderResponse renderResponse)
103                    throws Exception {
104    
105                    try {
106                            ActionUtil.getLayoutPrototype(renderRequest);
107                    }
108                    catch (Exception e) {
109                            if (e instanceof NoSuchLayoutPrototypeException ||
110                                    e instanceof PrincipalException) {
111    
112                                    SessionErrors.add(renderRequest, e.getClass());
113    
114                                    return actionMapping.findForward(
115                                            "portlet.layout_prototypes.error");
116                            }
117                            else {
118                                    throw e;
119                            }
120                    }
121    
122                    return actionMapping.findForward(
123                            getForward(
124                                    renderRequest,
125                                    "portlet.layout_prototypes.edit_layout_prototype"));
126            }
127    
128            protected void deleteLayoutPrototypes(ActionRequest actionRequest)
129                    throws Exception {
130    
131                    long[] layoutPrototypeIds = StringUtil.split(
132                            ParamUtil.getString(actionRequest, "layoutPrototypeIds"), 0L);
133    
134                    for (long layoutPrototypeId : layoutPrototypeIds) {
135                            LayoutPrototypeServiceUtil.deleteLayoutPrototype(layoutPrototypeId);
136                    }
137            }
138    
139            /**
140             * Resets the number of failed merge attempts for the page template, which
141             * is accessed from the action request's <code>layoutPrototypeId</code>
142             * param.
143             *
144             * <p>
145             * No merge from the page template to the actual page(s) is performed at
146             * this point.
147             * </p>
148             *
149             * @param  actionRequest the action request
150             * @throws Exception if an exception occurred
151             */
152            protected void resetMergeFailCount(ActionRequest actionRequest)
153                    throws Exception {
154    
155                    long layoutPrototypeId = ParamUtil.getLong(
156                            actionRequest, "layoutPrototypeId");
157    
158                    LayoutPrototype layoutPrototype =
159                            LayoutPrototypeServiceUtil.getLayoutPrototype(layoutPrototypeId);
160    
161                    SitesUtil.setMergeFailCount(layoutPrototype, 0);
162            }
163    
164            protected void updateLayoutPrototype(ActionRequest actionRequest)
165                    throws Exception {
166    
167                    long layoutPrototypeId = ParamUtil.getLong(
168                            actionRequest, "layoutPrototypeId");
169    
170                    Map<Locale, String> nameMap = LocalizationUtil.getLocalizationMap(
171                            actionRequest, "name");
172                    String description = ParamUtil.getString(actionRequest, "description");
173                    boolean active = ParamUtil.getBoolean(actionRequest, "active");
174    
175                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
176                            LayoutPrototype.class.getName(), actionRequest);
177    
178                    if (layoutPrototypeId <= 0) {
179    
180                            // Add layout prototoype
181    
182                            LayoutPrototypeServiceUtil.addLayoutPrototype(
183                                    nameMap, description, active, serviceContext);
184                    }
185                    else {
186    
187                            // Update layout prototoype
188    
189                            LayoutPrototypeServiceUtil.updateLayoutPrototype(
190                                    layoutPrototypeId, nameMap, description, active,
191                                    serviceContext);
192                    }
193            }
194    
195    }