001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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 allowModifications = ParamUtil.getBoolean(
133                            actionRequest, "allowModifications");
134                    boolean allowLayoutAddition = ParamUtil.getBoolean(
135                            actionRequest, "allowLayoutAdditions");
136    
137                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
138                            actionRequest);
139    
140                    LayoutSetPrototype layoutSetPrototype = null;
141    
142                    if (layoutSetPrototypeId <= 0) {
143    
144                            // Add layout prototoype
145    
146                            layoutSetPrototype =
147                                    LayoutSetPrototypeServiceUtil.addLayoutSetPrototype(
148                                            nameMap, description, active, allowModifications,
149                                            allowLayoutAddition, serviceContext);
150                    }
151                    else {
152    
153                            // Update layout prototoype
154    
155                            layoutSetPrototype =
156                                    LayoutSetPrototypeServiceUtil.updateLayoutSetPrototype(
157                                            layoutSetPrototypeId, nameMap, description, active,
158                                            allowModifications, allowLayoutAddition, serviceContext);
159                    }
160    
161                    // Custom JSPs
162    
163                    String customJspServletContextName = ParamUtil.getString(
164                            actionRequest, "customJspServletContextName");
165    
166                    UnicodeProperties settingsProperties =
167                            layoutSetPrototype.getSettingsProperties();
168    
169                    settingsProperties.setProperty(
170                            "customJspServletContextName", customJspServletContextName);
171    
172                    LayoutSetPrototypeServiceUtil.updateLayoutSetPrototype(
173                            layoutSetPrototype.getLayoutSetPrototypeId(),
174                            settingsProperties.toString());
175            }
176    
177    }