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.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            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                                    updateLayoutPrototype(actionRequest);
055                            }
056                            else if (cmd.equals(Constants.DELETE)) {
057                                    deleteLayoutPrototypes(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(actionRequest, "portlet.layout_prototypes.error");
067                            }
068                            else {
069                                    throw e;
070                            }
071                    }
072            }
073    
074            public ActionForward render(
075                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
076                            RenderRequest renderRequest, RenderResponse renderResponse)
077                    throws Exception {
078    
079                    try {
080                            ActionUtil.getLayoutPrototype(renderRequest);
081                    }
082                    catch (Exception e) {
083                            if (e instanceof NoSuchLayoutPrototypeException ||
084                                    e instanceof PrincipalException) {
085    
086                                    SessionErrors.add(renderRequest, e.getClass().getName());
087    
088                                    return mapping.findForward("portlet.layout_prototypes.error");
089                            }
090                            else {
091                                    throw e;
092                            }
093                    }
094    
095                    return mapping.findForward(getForward(
096                            renderRequest, "portlet.layout_prototypes.edit_layout_prototype"));
097            }
098    
099            protected void deleteLayoutPrototypes(ActionRequest actionRequest)
100                    throws Exception {
101    
102                    long[] layoutPrototypeIds = StringUtil.split(
103                            ParamUtil.getString(actionRequest, "layoutPrototypeIds"), 0L);
104    
105                    for (long layoutPrototypeId : layoutPrototypeIds) {
106                            LayoutPrototypeServiceUtil.deleteLayoutPrototype(layoutPrototypeId);
107                    }
108            }
109    
110            protected void updateLayoutPrototype(ActionRequest actionRequest)
111                    throws Exception {
112    
113                    long layoutPrototypeId = ParamUtil.getLong(
114                            actionRequest, "layoutPrototypeId");
115    
116                    Map<Locale, String> nameMap = LocalizationUtil.getLocalizationMap(
117                            actionRequest, "name");
118                    String description = ParamUtil.getString(actionRequest, "description");
119                    boolean active = ParamUtil.getBoolean(actionRequest, "active");
120    
121                    if (layoutPrototypeId <= 0) {
122    
123                            // Add layout prototoype
124    
125                            LayoutPrototypeServiceUtil.addLayoutPrototype(
126                                    nameMap, description, active);
127                    }
128                    else {
129    
130                            // Update layout prototoype
131    
132                            LayoutPrototypeServiceUtil.updateLayoutPrototype(
133                                    layoutPrototypeId, nameMap, description, active);
134                    }
135            }
136    
137    }