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());
075    
076                                    setForward(
077                                            actionRequest, "portlet.layout_set_prototypes.error");
078                            }
079                            else if (e instanceof RequiredLayoutSetPrototypeException) {
080                                    SessionErrors.add(actionRequest, e.getClass());
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());
109    
110                                    return mapping.findForward(
111                                            "portlet.layout_set_prototypes.error");
112                            }
113                            else {
114                                    throw e;
115                            }
116                    }
117    
118                    return mapping.findForward(
119                            getForward(
120                                    renderRequest,
121                                    "portlet.layout_set_prototypes.edit_layout_set_prototype"));
122            }
123    
124            protected void deleteLayoutSetPrototypes(ActionRequest actionRequest)
125                    throws Exception {
126    
127                    long[] layoutSetPrototypeIds = StringUtil.split(
128                            ParamUtil.getString(actionRequest, "layoutSetPrototypeIds"), 0L);
129    
130                    for (long layoutSetPrototypeId : layoutSetPrototypeIds) {
131                            LayoutSetPrototypeServiceUtil.deleteLayoutSetPrototype(
132                                    layoutSetPrototypeId);
133                    }
134            }
135    
136            protected void updateLayoutSetPrototype(ActionRequest actionRequest)
137                    throws Exception {
138    
139                    long layoutSetPrototypeId = ParamUtil.getLong(
140                            actionRequest, "layoutSetPrototypeId");
141    
142                    Map<Locale, String> nameMap = LocalizationUtil.getLocalizationMap(
143                            actionRequest, "name");
144                    String description = ParamUtil.getString(actionRequest, "description");
145                    boolean active = ParamUtil.getBoolean(actionRequest, "active");
146                    boolean layoutsUpdateable = ParamUtil.getBoolean(
147                            actionRequest, "layoutsUpdateable");
148    
149                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
150                            actionRequest);
151    
152                    LayoutSetPrototype layoutSetPrototype = null;
153    
154                    if (layoutSetPrototypeId <= 0) {
155    
156                            // Add layout prototoype
157    
158                            layoutSetPrototype =
159                                    LayoutSetPrototypeServiceUtil.addLayoutSetPrototype(
160                                            nameMap, description, active, layoutsUpdateable,
161                                            serviceContext);
162                    }
163                    else {
164    
165                            // Update layout prototoype
166    
167                            layoutSetPrototype =
168                                    LayoutSetPrototypeServiceUtil.updateLayoutSetPrototype(
169                                            layoutSetPrototypeId, nameMap, description, active,
170                                            layoutsUpdateable, serviceContext);
171                    }
172    
173                    // Custom JSPs
174    
175                    String customJspServletContextName = ParamUtil.getString(
176                            actionRequest, "customJspServletContextName");
177    
178                    UnicodeProperties settingsProperties =
179                            layoutSetPrototype.getSettingsProperties();
180    
181                    settingsProperties.setProperty(
182                            "customJspServletContextName", customJspServletContextName);
183    
184                    LayoutSetPrototypeServiceUtil.updateLayoutSetPrototype(
185                            layoutSetPrototype.getLayoutSetPrototypeId(),
186                            settingsProperties.toString());
187            }
188    
189    }