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