001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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(getForward(
112                            renderRequest, "portlet.layout_prototypes.edit_layout_prototype"));
113            }
114    
115            protected void deleteLayoutPrototypes(ActionRequest actionRequest)
116                    throws Exception {
117    
118                    long[] layoutPrototypeIds = StringUtil.split(
119                            ParamUtil.getString(actionRequest, "layoutPrototypeIds"), 0L);
120    
121                    for (long layoutPrototypeId : layoutPrototypeIds) {
122                            LayoutPrototypeServiceUtil.deleteLayoutPrototype(layoutPrototypeId);
123                    }
124            }
125    
126            protected void updateLayoutPrototype(ActionRequest actionRequest)
127                    throws Exception {
128    
129                    long layoutPrototypeId = ParamUtil.getLong(
130                            actionRequest, "layoutPrototypeId");
131    
132                    Map<Locale, String> nameMap = LocalizationUtil.getLocalizationMap(
133                            actionRequest, "name");
134                    String description = ParamUtil.getString(actionRequest, "description");
135                    boolean active = ParamUtil.getBoolean(actionRequest, "active");
136    
137                    if (layoutPrototypeId <= 0) {
138    
139                            // Add layout prototoype
140    
141                            LayoutPrototypeServiceUtil.addLayoutPrototype(
142                                    nameMap, description, active);
143                    }
144                    else {
145    
146                            // Update layout prototoype
147    
148                            LayoutPrototypeServiceUtil.updateLayoutPrototype(
149                                    layoutPrototypeId, nameMap, description, active);
150                    }
151            }
152    
153    }