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.dynamicdatamapping.action;
016    
017    import com.liferay.portal.kernel.servlet.SessionErrors;
018    import com.liferay.portal.kernel.util.Constants;
019    import com.liferay.portal.kernel.util.LocalizationUtil;
020    import com.liferay.portal.kernel.util.ParamUtil;
021    import com.liferay.portal.kernel.util.Validator;
022    import com.liferay.portal.security.auth.PrincipalException;
023    import com.liferay.portal.service.ServiceContext;
024    import com.liferay.portal.service.ServiceContextFactory;
025    import com.liferay.portal.struts.PortletAction;
026    import com.liferay.portal.theme.ThemeDisplay;
027    import com.liferay.portal.util.PortalUtil;
028    import com.liferay.portal.util.WebKeys;
029    import com.liferay.portlet.PortletURLImpl;
030    import com.liferay.portlet.dynamicdatamapping.NoSuchStructureException;
031    import com.liferay.portlet.dynamicdatamapping.RequiredStructureException;
032    import com.liferay.portlet.dynamicdatamapping.StructureDuplicateElementException;
033    import com.liferay.portlet.dynamicdatamapping.StructureNameException;
034    import com.liferay.portlet.dynamicdatamapping.StructureXsdException;
035    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
036    import com.liferay.portlet.dynamicdatamapping.model.DDMStructureConstants;
037    import com.liferay.portlet.dynamicdatamapping.service.DDMStructureServiceUtil;
038    
039    import java.util.Locale;
040    import java.util.Map;
041    
042    import javax.portlet.ActionRequest;
043    import javax.portlet.ActionResponse;
044    import javax.portlet.PortletConfig;
045    import javax.portlet.PortletRequest;
046    import javax.portlet.RenderRequest;
047    import javax.portlet.RenderResponse;
048    
049    import org.apache.struts.action.ActionForm;
050    import org.apache.struts.action.ActionForward;
051    import org.apache.struts.action.ActionMapping;
052    
053    /**
054     * @author Brian Wing Shun Chan
055     * @author Bruno Basto
056     * @author Eduardo Lundgren
057     */
058    public class EditStructureAction extends PortletAction {
059    
060            @Override
061            public void processAction(
062                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
063                            ActionRequest actionRequest, ActionResponse actionResponse)
064                    throws Exception {
065    
066                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
067    
068                    DDMStructure structure = null;
069    
070                    try {
071                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
072                                    structure = updateStructure(actionRequest);
073                            }
074                            else if (cmd.equals(Constants.DELETE)) {
075                                    deleteStructure(actionRequest);
076                            }
077    
078                            if (Validator.isNotNull(cmd)) {
079                                    String redirect = ParamUtil.getString(
080                                            actionRequest, "redirect");
081    
082                                    if (structure != null) {
083                                            boolean saveAndContinue = ParamUtil.getBoolean(
084                                                    actionRequest, "saveAndContinue");
085    
086                                            if (saveAndContinue) {
087                                                    redirect = getSaveAndContinueRedirect(
088                                                            portletConfig, actionRequest, structure, redirect);
089                                            }
090                                    }
091    
092                                    sendRedirect(actionRequest, actionResponse, redirect);
093                            }
094                    }
095                    catch (Exception e) {
096                            if (e instanceof NoSuchStructureException ||
097                                    e instanceof PrincipalException) {
098    
099                                    SessionErrors.add(actionRequest, e.getClass().getName());
100    
101                                    setForward(actionRequest, "portlet.dynamic_data_mapping.error");
102                            }
103                            else if (e instanceof RequiredStructureException ||
104                                             e instanceof StructureDuplicateElementException ||
105                                             e instanceof StructureNameException ||
106                                             e instanceof StructureXsdException) {
107    
108                                    SessionErrors.add(actionRequest, e.getClass().getName(), e);
109    
110                                    if (e instanceof RequiredStructureException) {
111                                            String redirect = PortalUtil.escapeRedirect(
112                                                    ParamUtil.getString(actionRequest, "redirect"));
113    
114                                            if (Validator.isNotNull(redirect)) {
115                                                    actionResponse.sendRedirect(redirect);
116                                            }
117                                    }
118                            }
119                            else {
120                                    throw e;
121                            }
122                    }
123            }
124    
125            @Override
126            public ActionForward render(
127                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
128                            RenderRequest renderRequest, RenderResponse renderResponse)
129                    throws Exception {
130    
131                    try {
132                            String cmd = ParamUtil.getString(renderRequest, Constants.CMD);
133    
134                            if (!cmd.equals(Constants.ADD)) {
135                                    ActionUtil.getStructure(renderRequest);
136                            }
137                    }
138                    catch (NoSuchStructureException nsse) {
139    
140                            // Let this slide because the user can manually input a structure
141                            // key for a new structure that does not yet exist
142    
143                    }
144                    catch (Exception e) {
145                            if (//e instanceof NoSuchStructureException ||
146                                    e instanceof PrincipalException) {
147    
148                                    SessionErrors.add(renderRequest, e.getClass().getName());
149    
150                                    return mapping.findForward(
151                                            "portlet.dynamic_data_mapping.error");
152                            }
153                            else {
154                                    throw e;
155                            }
156                    }
157    
158                    return mapping.findForward(
159                            getForward(
160                                    renderRequest, "portlet.dynamic_data_mapping.edit_structure"));
161            }
162    
163            protected void deleteStructure(ActionRequest actionRequest)
164                    throws Exception {
165    
166                    long structureId = ParamUtil.getLong(actionRequest, "structureId");
167    
168                    DDMStructureServiceUtil.deleteStructure(structureId);
169            }
170    
171            protected String getSaveAndContinueRedirect(
172                            PortletConfig portletConfig, ActionRequest actionRequest,
173                            DDMStructure structure, String redirect)
174                    throws Exception {
175    
176                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
177                            WebKeys.THEME_DISPLAY);
178    
179                    String availableFields = ParamUtil.getString(
180                            actionRequest, "availableFields");
181                    String saveCallback = ParamUtil.getString(
182                            actionRequest, "saveCallback");
183    
184                    PortletURLImpl portletURL = new PortletURLImpl(
185                            actionRequest, portletConfig.getPortletName(),
186                            themeDisplay.getPlid(), PortletRequest.RENDER_PHASE);
187    
188                    portletURL.setWindowState(actionRequest.getWindowState());
189    
190                    portletURL.setParameter(Constants.CMD, Constants.UPDATE, false);
191                    portletURL.setParameter(
192                            "struts_action", "/dynamic_data_mapping/edit_structure");
193                    portletURL.setParameter("redirect", redirect, false);
194                    portletURL.setParameter(
195                            "groupId", String.valueOf(structure.getGroupId()), false);
196                    portletURL.setParameter(
197                            "structureId", String.valueOf(structure.getStructureId()), false);
198                    portletURL.setParameter("availableFields", availableFields, false);
199                    portletURL.setParameter("saveCallback", saveCallback, false);
200    
201                    return portletURL.toString();
202            }
203    
204            protected DDMStructure updateStructure(ActionRequest actionRequest)
205                    throws Exception {
206    
207                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
208    
209                    long structureId = ParamUtil.getLong(actionRequest, "structureId");
210    
211                    long groupId = ParamUtil.getLong(actionRequest, "groupId");
212                    long classNameId = ParamUtil.getLong(actionRequest, "classNameId");
213                    Map<Locale, String> nameMap = LocalizationUtil.getLocalizationMap(
214                            actionRequest, "name");
215                    Map<Locale, String> descriptionMap =
216                            LocalizationUtil.getLocalizationMap(actionRequest, "description");
217                    String xsd = ParamUtil.getString(actionRequest, "xsd");
218                    String storageType = ParamUtil.getString(actionRequest, "storageType");
219    
220                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
221                            DDMStructure.class.getName(), actionRequest);
222    
223                    DDMStructure structure = null;
224    
225                    if (cmd.equals(Constants.ADD)) {
226                            structure = DDMStructureServiceUtil.addStructure(
227                                    groupId, classNameId, null, nameMap, descriptionMap, xsd,
228                                    storageType, DDMStructureConstants.TYPE_DEFAULT,
229                                    serviceContext);
230                    }
231                    else if (cmd.equals(Constants.UPDATE)) {
232                            structure = DDMStructureServiceUtil.updateStructure(
233                                    structureId, nameMap, descriptionMap, xsd, serviceContext);
234                    }
235    
236                    return structure;
237            }
238    
239    }