001    /**
002     * Copyright (c) 2000-2011 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.kernel.util.UnicodeProperties;
024    import com.liferay.portal.model.LayoutSetPrototype;
025    import com.liferay.portal.security.auth.PrincipalException;
026    import com.liferay.portal.service.LayoutSetPrototypeServiceUtil;
027    import com.liferay.portal.service.ServiceContext;
028    import com.liferay.portal.service.ServiceContextFactory;
029    import com.liferay.portal.struts.PortletAction;
030    
031    import java.util.Locale;
032    import java.util.Map;
033    
034    import javax.portlet.ActionRequest;
035    import javax.portlet.ActionResponse;
036    import javax.portlet.PortletConfig;
037    import javax.portlet.RenderRequest;
038    import javax.portlet.RenderResponse;
039    
040    import org.apache.struts.action.ActionForm;
041    import org.apache.struts.action.ActionForward;
042    import org.apache.struts.action.ActionMapping;
043    
044    /**
045     * @author Brian Wing Shun Chan
046     * @author Ryan Park
047     * @author Máté Thurzó
048     */
049    public class EditLayoutSetPrototypeAction extends PortletAction {
050    
051            @Override
052            public void processAction(
053                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
054                            ActionRequest actionRequest, ActionResponse actionResponse)
055                    throws Exception {
056    
057                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
058    
059                    try {
060                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
061                                    updateLayoutSetPrototype(actionRequest);
062                            }
063                            else if (cmd.equals(Constants.DELETE)) {
064                                    deleteLayoutSetPrototypes(actionRequest);
065                            }
066    
067                            sendRedirect(actionRequest, actionResponse);
068                    }
069                    catch (Exception e) {
070                            if (e instanceof PrincipalException) {
071                                    SessionErrors.add(actionRequest, e.getClass().getName());
072    
073                                    setForward(
074                                            actionRequest, "portlet.layout_set_prototypes.error");
075                            }
076                            else {
077                                    throw e;
078                            }
079                    }
080            }
081    
082            @Override
083            public ActionForward render(
084                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
085                            RenderRequest renderRequest, RenderResponse renderResponse)
086                    throws Exception {
087    
088                    try {
089                            ActionUtil.getLayoutSetPrototype(renderRequest);
090                    }
091                    catch (Exception e) {
092                            if (e instanceof NoSuchLayoutSetPrototypeException ||
093                                    e instanceof PrincipalException) {
094    
095                                    SessionErrors.add(renderRequest, e.getClass().getName());
096    
097                                    return mapping.findForward(
098                                            "portlet.layout_set_prototypes.error");
099                            }
100                            else {
101                                    throw e;
102                            }
103                    }
104    
105                    return mapping.findForward(getForward(
106                            renderRequest,
107                            "portlet.layout_set_prototypes.edit_layout_set_prototype"));
108            }
109    
110            protected void deleteLayoutSetPrototypes(ActionRequest actionRequest)
111                    throws Exception {
112    
113                    long[] layoutSetPrototypeIds = StringUtil.split(
114                            ParamUtil.getString(actionRequest, "layoutSetPrototypeIds"), 0L);
115    
116                    for (long layoutSetPrototypeId : layoutSetPrototypeIds) {
117                            LayoutSetPrototypeServiceUtil.deleteLayoutSetPrototype(
118                                    layoutSetPrototypeId);
119                    }
120            }
121    
122            protected void updateLayoutSetPrototype(ActionRequest actionRequest)
123                    throws Exception {
124    
125                    long layoutSetPrototypeId = ParamUtil.getLong(
126                            actionRequest, "layoutSetPrototypeId");
127    
128                    Map<Locale, String> nameMap = LocalizationUtil.getLocalizationMap(
129                            actionRequest, "name");
130                    String description = ParamUtil.getString(actionRequest, "description");
131                    boolean active = ParamUtil.getBoolean(actionRequest, "active");
132                    boolean layoutsUpdateable = ParamUtil.getBoolean(
133                            actionRequest, "layoutsUpdateable");
134    
135                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
136                            actionRequest);
137    
138                    LayoutSetPrototype layoutSetPrototype = null;
139    
140                    if (layoutSetPrototypeId <= 0) {
141    
142                            // Add layout prototoype
143    
144                            layoutSetPrototype =
145                                    LayoutSetPrototypeServiceUtil.addLayoutSetPrototype(
146                                            nameMap, description, active, layoutsUpdateable,
147                                            serviceContext);
148                    }
149                    else {
150    
151                            // Update layout prototoype
152    
153                            layoutSetPrototype =
154                                    LayoutSetPrototypeServiceUtil.updateLayoutSetPrototype(
155                                            layoutSetPrototypeId, nameMap, description, active,
156                                            layoutsUpdateable, serviceContext);
157                    }
158    
159                    // Custom JSPs
160    
161                    String customJspServletContextName = ParamUtil.getString(
162                            actionRequest, "customJspServletContextName");
163    
164                    UnicodeProperties settingsProperties =
165                            layoutSetPrototype.getSettingsProperties();
166    
167                    settingsProperties.setProperty(
168                            "customJspServletContextName", customJspServletContextName);
169    
170                    LayoutSetPrototypeServiceUtil.updateLayoutSetPrototype(
171                            layoutSetPrototype.getLayoutSetPrototypeId(),
172                            settingsProperties.toString());
173            }
174    
175    }