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.layoutprototypes.action;
016    
017    import com.liferay.portal.NoSuchLayoutPrototypeException;
018    import com.liferay.portal.RequiredLayoutPrototypeException;
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.Validator;
025    import com.liferay.portal.model.LayoutPrototype;
026    import com.liferay.portal.security.auth.PrincipalException;
027    import com.liferay.portal.service.LayoutPrototypeServiceUtil;
028    import com.liferay.portal.service.ServiceContext;
029    import com.liferay.portal.service.ServiceContextFactory;
030    import com.liferay.portal.struts.PortletAction;
031    import com.liferay.portal.util.PortalUtil;
032    import com.liferay.portlet.sites.util.SitesUtil;
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 Jorge Ferrer
049     * @author Vilmos Papp
050     * @author Josef Sustacek
051     */
052    public class EditLayoutPrototypeAction 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                                    updateLayoutPrototype(actionRequest);
065                            }
066                            else if (cmd.equals(Constants.DELETE)) {
067                                    deleteLayoutPrototypes(actionRequest);
068                            }
069                            else if (cmd.equals("reset_merge_fail_count")) {
070                                    resetMergeFailCount(actionRequest);
071                            }
072    
073                            sendRedirect(actionRequest, actionResponse);
074                    }
075                    catch (Exception e) {
076                            if (e instanceof PrincipalException) {
077                                    SessionErrors.add(actionRequest, e.getClass());
078    
079                                    setForward(actionRequest, "portlet.layout_prototypes.error");
080                            }
081                            else if (e instanceof RequiredLayoutPrototypeException) {
082                                    SessionErrors.add(actionRequest, e.getClass());
083    
084                                    String redirect = PortalUtil.escapeRedirect(
085                                            ParamUtil.getString(actionRequest, "redirect"));
086    
087                                    if (Validator.isNotNull(redirect)) {
088                                            actionResponse.sendRedirect(redirect);
089                                    }
090                            }
091                            else {
092                                    throw e;
093                            }
094                    }
095            }
096    
097            @Override
098            public ActionForward render(
099                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
100                            RenderRequest renderRequest, RenderResponse renderResponse)
101                    throws Exception {
102    
103                    try {
104                            ActionUtil.getLayoutPrototype(renderRequest);
105                    }
106                    catch (Exception e) {
107                            if (e instanceof NoSuchLayoutPrototypeException ||
108                                    e instanceof PrincipalException) {
109    
110                                    SessionErrors.add(renderRequest, e.getClass());
111    
112                                    return mapping.findForward("portlet.layout_prototypes.error");
113                            }
114                            else {
115                                    throw e;
116                            }
117                    }
118    
119                    return mapping.findForward(
120                            getForward(
121                                    renderRequest,
122                                    "portlet.layout_prototypes.edit_layout_prototype"));
123            }
124    
125            protected void deleteLayoutPrototypes(ActionRequest actionRequest)
126                    throws Exception {
127    
128                    long[] layoutPrototypeIds = StringUtil.split(
129                            ParamUtil.getString(actionRequest, "layoutPrototypeIds"), 0L);
130    
131                    for (long layoutPrototypeId : layoutPrototypeIds) {
132                            LayoutPrototypeServiceUtil.deleteLayoutPrototype(layoutPrototypeId);
133                    }
134            }
135    
136            protected void resetMergeFailCount(ActionRequest actionRequest)
137                    throws Exception {
138    
139                    long layoutPrototypeId = ParamUtil.getLong(
140                            actionRequest, "layoutPrototypeId");
141    
142                    LayoutPrototype layoutPrototype =
143                            LayoutPrototypeServiceUtil.getLayoutPrototype(layoutPrototypeId);
144    
145                    SitesUtil.setMergeFailCount(layoutPrototype, 0);
146            }
147    
148            protected void updateLayoutPrototype(ActionRequest actionRequest)
149                    throws Exception {
150    
151                    long layoutPrototypeId = ParamUtil.getLong(
152                            actionRequest, "layoutPrototypeId");
153    
154                    Map<Locale, String> nameMap = LocalizationUtil.getLocalizationMap(
155                            actionRequest, "name");
156                    String description = ParamUtil.getString(actionRequest, "description");
157                    boolean active = ParamUtil.getBoolean(actionRequest, "active");
158    
159                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
160                            LayoutPrototype.class.getName(), actionRequest);
161    
162                    if (layoutPrototypeId <= 0) {
163    
164                            // Add layout prototoype
165    
166                            LayoutPrototypeServiceUtil.addLayoutPrototype(
167                                    nameMap, description, active, serviceContext);
168                    }
169                    else {
170    
171                            // Update layout prototoype
172    
173                            LayoutPrototypeServiceUtil.updateLayoutPrototype(
174                                    layoutPrototypeId, nameMap, description, active,
175                                    serviceContext);
176                    }
177            }
178    
179    }