001    /**
002     * Copyright (c) 2000-2013 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 actionMapping, ActionForm actionForm,
057                            PortletConfig portletConfig, ActionRequest actionRequest,
058                            ActionResponse actionResponse)
059                    throws Exception {
060    
061                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
062    
063                    try {
064                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
065                                    updateLayoutSetPrototype(actionRequest);
066                            }
067                            else if (cmd.equals(Constants.DELETE)) {
068                                    deleteLayoutSetPrototypes(actionRequest);
069                            }
070    
071                            sendRedirect(actionRequest, actionResponse);
072                    }
073                    catch (Exception e) {
074                            if (e instanceof PrincipalException) {
075                                    SessionErrors.add(actionRequest, e.getClass());
076    
077                                    setForward(
078                                            actionRequest, "portlet.layout_set_prototypes.error");
079                            }
080                            else if (e instanceof RequiredLayoutSetPrototypeException) {
081                                    SessionErrors.add(actionRequest, e.getClass());
082    
083                                    String redirect = PortalUtil.escapeRedirect(
084                                            ParamUtil.getString(actionRequest, "redirect"));
085    
086                                    if (Validator.isNotNull(redirect)) {
087                                            actionResponse.sendRedirect(redirect);
088                                    }
089                            }
090                            else {
091                                    throw e;
092                            }
093                    }
094            }
095    
096            @Override
097            public ActionForward render(
098                            ActionMapping actionMapping, ActionForm actionForm,
099                            PortletConfig portletConfig, RenderRequest renderRequest,
100                            RenderResponse renderResponse)
101                    throws Exception {
102    
103                    try {
104                            ActionUtil.getLayoutSetPrototype(renderRequest);
105                    }
106                    catch (Exception e) {
107                            if (e instanceof NoSuchLayoutSetPrototypeException ||
108                                    e instanceof PrincipalException) {
109    
110                                    SessionErrors.add(renderRequest, e.getClass());
111    
112                                    return actionMapping.findForward(
113                                            "portlet.layout_set_prototypes.error");
114                            }
115                            else {
116                                    throw e;
117                            }
118                    }
119    
120                    return actionMapping.findForward(
121                            getForward(
122                                    renderRequest,
123                                    "portlet.layout_set_prototypes.edit_layout_set_prototype"));
124            }
125    
126            protected void deleteLayoutSetPrototypes(ActionRequest actionRequest)
127                    throws Exception {
128    
129                    long[] layoutSetPrototypeIds = StringUtil.split(
130                            ParamUtil.getString(actionRequest, "layoutSetPrototypeIds"), 0L);
131    
132                    for (long layoutSetPrototypeId : layoutSetPrototypeIds) {
133                            LayoutSetPrototypeServiceUtil.deleteLayoutSetPrototype(
134                                    layoutSetPrototypeId);
135                    }
136            }
137    
138            protected void updateLayoutSetPrototype(ActionRequest actionRequest)
139                    throws Exception {
140    
141                    long layoutSetPrototypeId = ParamUtil.getLong(
142                            actionRequest, "layoutSetPrototypeId");
143    
144                    Map<Locale, String> nameMap = LocalizationUtil.getLocalizationMap(
145                            actionRequest, "name");
146                    String description = ParamUtil.getString(actionRequest, "description");
147                    boolean active = ParamUtil.getBoolean(actionRequest, "active");
148                    boolean layoutsUpdateable = ParamUtil.getBoolean(
149                            actionRequest, "layoutsUpdateable");
150    
151                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
152                            actionRequest);
153    
154                    LayoutSetPrototype layoutSetPrototype = null;
155    
156                    if (layoutSetPrototypeId <= 0) {
157    
158                            // Add layout prototoype
159    
160                            layoutSetPrototype =
161                                    LayoutSetPrototypeServiceUtil.addLayoutSetPrototype(
162                                            nameMap, description, active, layoutsUpdateable,
163                                            serviceContext);
164                    }
165                    else {
166    
167                            // Update layout prototoype
168    
169                            layoutSetPrototype =
170                                    LayoutSetPrototypeServiceUtil.updateLayoutSetPrototype(
171                                            layoutSetPrototypeId, nameMap, description, active,
172                                            layoutsUpdateable, serviceContext);
173                    }
174    
175                    // Custom JSPs
176    
177                    String customJspServletContextName = ParamUtil.getString(
178                            actionRequest, "customJspServletContextName");
179    
180                    UnicodeProperties settingsProperties =
181                            layoutSetPrototype.getSettingsProperties();
182    
183                    settingsProperties.setProperty(
184                            "customJspServletContextName", customJspServletContextName);
185    
186                    LayoutSetPrototypeServiceUtil.updateLayoutSetPrototype(
187                            layoutSetPrototype.getLayoutSetPrototypeId(),
188                            settingsProperties.toString());
189            }
190    
191    }