001    /**
002     * Copyright (c) 2000-2010 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.layoutsetprototypes.action;
016    
017    import com.liferay.portal.NoSuchLayoutSetPrototypeException;
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.LayoutSetPrototypeServiceUtil;
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 Brian Wing Shun Chan
042     */
043    public class EditLayoutSetPrototypeAction extends PortletAction {
044    
045            public void processAction(
046                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
047                            ActionRequest actionRequest, ActionResponse actionResponse)
048                    throws Exception {
049    
050                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
051    
052                    try {
053                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
054                                    updateLayoutSetPrototype(actionRequest);
055                            }
056                            else if (cmd.equals(Constants.DELETE)) {
057                                    deleteLayoutSetPrototypes(actionRequest);
058                            }
059    
060                            sendRedirect(actionRequest, actionResponse);
061                    }
062                    catch (Exception e) {
063                            if (e instanceof PrincipalException) {
064                                    SessionErrors.add(actionRequest, e.getClass().getName());
065    
066                                    setForward(
067                                            actionRequest, "portlet.layout_set_prototypes.error");
068                            }
069                            else {
070                                    throw e;
071                            }
072                    }
073            }
074    
075            public ActionForward render(
076                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
077                            RenderRequest renderRequest, RenderResponse renderResponse)
078                    throws Exception {
079    
080                    try {
081                            ActionUtil.getLayoutSetPrototype(renderRequest);
082                    }
083                    catch (Exception e) {
084                            if (e instanceof NoSuchLayoutSetPrototypeException ||
085                                    e instanceof PrincipalException) {
086    
087                                    SessionErrors.add(renderRequest, e.getClass().getName());
088    
089                                    return mapping.findForward(
090                                            "portlet.layout_set_prototypes.error");
091                            }
092                            else {
093                                    throw e;
094                            }
095                    }
096    
097                    return mapping.findForward(getForward(
098                            renderRequest,
099                            "portlet.layout_set_prototypes.edit_layout_set_prototype"));
100            }
101    
102            protected void deleteLayoutSetPrototypes(ActionRequest actionRequest)
103                    throws Exception {
104    
105                    long[] layoutSetPrototypeIds = StringUtil.split(
106                            ParamUtil.getString(actionRequest, "layoutSetPrototypeIds"), 0L);
107    
108                    for (long layoutSetPrototypeId : layoutSetPrototypeIds) {
109                            LayoutSetPrototypeServiceUtil.deleteLayoutSetPrototype(
110                                    layoutSetPrototypeId);
111                    }
112            }
113    
114            protected void updateLayoutSetPrototype(ActionRequest actionRequest)
115                    throws Exception {
116    
117                    long layoutSetPrototypeId = ParamUtil.getLong(
118                            actionRequest, "layoutSetPrototypeId");
119    
120                    Map<Locale, String> nameMap = LocalizationUtil.getLocalizationMap(
121                            actionRequest, "name");
122                    String description = ParamUtil.getString(actionRequest, "description");
123                    boolean active = ParamUtil.getBoolean(actionRequest, "active");
124    
125                    if (layoutSetPrototypeId <= 0) {
126    
127                            // Add layout prototoype
128    
129                            LayoutSetPrototypeServiceUtil.addLayoutSetPrototype(
130                                    nameMap, description, active);
131                    }
132                    else {
133    
134                            // Update layout prototoype
135    
136                            LayoutSetPrototypeServiceUtil.updateLayoutSetPrototype(
137                                    layoutSetPrototypeId, nameMap, description, active);
138                    }
139            }
140    
141    }