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.security.auth.PrincipalException;
026    import com.liferay.portal.service.LayoutPrototypeServiceUtil;
027    import com.liferay.portal.struts.PortletAction;
028    import com.liferay.portal.util.PortalUtil;
029    
030    import java.util.Locale;
031    import java.util.Map;
032    
033    import javax.portlet.ActionRequest;
034    import javax.portlet.ActionResponse;
035    import javax.portlet.PortletConfig;
036    import javax.portlet.RenderRequest;
037    import javax.portlet.RenderResponse;
038    
039    import org.apache.struts.action.ActionForm;
040    import org.apache.struts.action.ActionForward;
041    import org.apache.struts.action.ActionMapping;
042    
043    /**
044     * @author Jorge Ferrer
045     * @author Vilmos Papp
046     */
047    public class EditLayoutPrototypeAction extends PortletAction {
048    
049            @Override
050            public void processAction(
051                            ActionMapping actionMapping, ActionForm actionForm,
052                            PortletConfig portletConfig, ActionRequest actionRequest,
053                            ActionResponse actionResponse)
054                    throws Exception {
055    
056                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
057    
058                    try {
059                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
060                                    updateLayoutPrototype(actionRequest);
061                            }
062                            else if (cmd.equals(Constants.DELETE)) {
063                                    deleteLayoutPrototypes(actionRequest);
064                            }
065    
066                            sendRedirect(actionRequest, actionResponse);
067                    }
068                    catch (Exception e) {
069                            if (e instanceof PrincipalException) {
070                                    SessionErrors.add(actionRequest, e.getClass());
071    
072                                    setForward(actionRequest, "portlet.layout_prototypes.error");
073                            }
074                            else if (e instanceof RequiredLayoutPrototypeException) {
075                                    SessionErrors.add(actionRequest, e.getClass());
076    
077                                    String redirect = PortalUtil.escapeRedirect(
078                                            ParamUtil.getString(actionRequest, "redirect"));
079    
080                                    if (Validator.isNotNull(redirect)) {
081                                            actionResponse.sendRedirect(redirect);
082                                    }
083                            }
084                            else {
085                                    throw e;
086                            }
087                    }
088            }
089    
090            @Override
091            public ActionForward render(
092                            ActionMapping actionMapping, ActionForm actionForm,
093                            PortletConfig portletConfig, RenderRequest renderRequest,
094                            RenderResponse renderResponse)
095                    throws Exception {
096    
097                    try {
098                            ActionUtil.getLayoutPrototype(renderRequest);
099                    }
100                    catch (Exception e) {
101                            if (e instanceof NoSuchLayoutPrototypeException ||
102                                    e instanceof PrincipalException) {
103    
104                                    SessionErrors.add(renderRequest, e.getClass());
105    
106                                    return actionMapping.findForward(
107                                            "portlet.layout_prototypes.error");
108                            }
109                            else {
110                                    throw e;
111                            }
112                    }
113    
114                    return actionMapping.findForward(
115                            getForward(
116                                    renderRequest,
117                                    "portlet.layout_prototypes.edit_layout_prototype"));
118            }
119    
120            protected void deleteLayoutPrototypes(ActionRequest actionRequest)
121                    throws Exception {
122    
123                    long[] layoutPrototypeIds = StringUtil.split(
124                            ParamUtil.getString(actionRequest, "layoutPrototypeIds"), 0L);
125    
126                    for (long layoutPrototypeId : layoutPrototypeIds) {
127                            LayoutPrototypeServiceUtil.deleteLayoutPrototype(layoutPrototypeId);
128                    }
129            }
130    
131            protected void updateLayoutPrototype(ActionRequest actionRequest)
132                    throws Exception {
133    
134                    long layoutPrototypeId = ParamUtil.getLong(
135                            actionRequest, "layoutPrototypeId");
136    
137                    Map<Locale, String> nameMap = LocalizationUtil.getLocalizationMap(
138                            actionRequest, "name");
139                    String description = ParamUtil.getString(actionRequest, "description");
140                    boolean active = ParamUtil.getBoolean(actionRequest, "active");
141    
142                    if (layoutPrototypeId <= 0) {
143    
144                            // Add layout prototoype
145    
146                            LayoutPrototypeServiceUtil.addLayoutPrototype(
147                                    nameMap, description, active);
148                    }
149                    else {
150    
151                            // Update layout prototoype
152    
153                            LayoutPrototypeServiceUtil.updateLayoutPrototype(
154                                    layoutPrototypeId, nameMap, description, active);
155                    }
156            }
157    
158    }