001    /**
002     * Copyright (c) 2000-2012 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 mapping, ActionForm form, PortletConfig portletConfig,
052                            ActionRequest actionRequest, ActionResponse actionResponse)
053                    throws Exception {
054    
055                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
056    
057                    try {
058                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
059                                    updateLayoutPrototype(actionRequest);
060                            }
061                            else if (cmd.equals(Constants.DELETE)) {
062                                    deleteLayoutPrototypes(actionRequest);
063                            }
064    
065                            sendRedirect(actionRequest, actionResponse);
066                    }
067                    catch (Exception e) {
068                            if (e instanceof PrincipalException) {
069                                    SessionErrors.add(actionRequest, e.getClass());
070    
071                                    setForward(actionRequest, "portlet.layout_prototypes.error");
072                            }
073                            else if (e instanceof RequiredLayoutPrototypeException) {
074                                    SessionErrors.add(actionRequest, e.getClass());
075    
076                                    String redirect = PortalUtil.escapeRedirect(
077                                            ParamUtil.getString(actionRequest, "redirect"));
078    
079                                    if (Validator.isNotNull(redirect)) {
080                                            actionResponse.sendRedirect(redirect);
081                                    }
082                            }
083                            else {
084                                    throw e;
085                            }
086                    }
087            }
088    
089            @Override
090            public ActionForward render(
091                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
092                            RenderRequest renderRequest, RenderResponse renderResponse)
093                    throws Exception {
094    
095                    try {
096                            ActionUtil.getLayoutPrototype(renderRequest);
097                    }
098                    catch (Exception e) {
099                            if (e instanceof NoSuchLayoutPrototypeException ||
100                                    e instanceof PrincipalException) {
101    
102                                    SessionErrors.add(renderRequest, e.getClass());
103    
104                                    return mapping.findForward("portlet.layout_prototypes.error");
105                            }
106                            else {
107                                    throw e;
108                            }
109                    }
110    
111                    return mapping.findForward(
112                            getForward(
113                                    renderRequest,
114                                    "portlet.layout_prototypes.edit_layout_prototype"));
115            }
116    
117            protected void deleteLayoutPrototypes(ActionRequest actionRequest)
118                    throws Exception {
119    
120                    long[] layoutPrototypeIds = StringUtil.split(
121                            ParamUtil.getString(actionRequest, "layoutPrototypeIds"), 0L);
122    
123                    for (long layoutPrototypeId : layoutPrototypeIds) {
124                            LayoutPrototypeServiceUtil.deleteLayoutPrototype(layoutPrototypeId);
125                    }
126            }
127    
128            protected void updateLayoutPrototype(ActionRequest actionRequest)
129                    throws Exception {
130    
131                    long layoutPrototypeId = ParamUtil.getLong(
132                            actionRequest, "layoutPrototypeId");
133    
134                    Map<Locale, String> nameMap = LocalizationUtil.getLocalizationMap(
135                            actionRequest, "name");
136                    String description = ParamUtil.getString(actionRequest, "description");
137                    boolean active = ParamUtil.getBoolean(actionRequest, "active");
138    
139                    if (layoutPrototypeId <= 0) {
140    
141                            // Add layout prototoype
142    
143                            LayoutPrototypeServiceUtil.addLayoutPrototype(
144                                    nameMap, description, active);
145                    }
146                    else {
147    
148                            // Update layout prototoype
149    
150                            LayoutPrototypeServiceUtil.updateLayoutPrototype(
151                                    layoutPrototypeId, nameMap, description, active);
152                    }
153            }
154    
155    }