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