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,
089                                                            redirect);
090                                            }
091                                    }
092    
093                                    sendRedirect(actionRequest, actionResponse, redirect);
094                            }
095                    }
096                    catch (Exception e) {
097                            if (e instanceof NoSuchStructureException ||
098                                    e instanceof PrincipalException) {
099    
100                                    SessionErrors.add(actionRequest, e.getClass().getName());
101    
102                                    setForward(actionRequest, "portlet.dynamic_data_mapping.error");
103                            }
104                            else if (e instanceof RequiredStructureException ||
105                                             e instanceof StructureDuplicateElementException ||
106                                             e instanceof StructureNameException ||
107                                             e instanceof StructureXsdException) {
108    
109                                    SessionErrors.add(actionRequest, e.getClass().getName(), e);
110    
111                                    if (e instanceof RequiredStructureException) {
112                                            String redirect = PortalUtil.escapeRedirect(
113                                                    ParamUtil.getString(actionRequest, "redirect"));
114    
115                                            if (Validator.isNotNull(redirect)) {
116                                                    actionResponse.sendRedirect(redirect);
117                                            }
118                                    }
119                            }
120                            else {
121                                    throw e;
122                            }
123                    }
124            }
125    
126            @Override
127            public ActionForward render(
128                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
129                            RenderRequest renderRequest, RenderResponse renderResponse)
130                    throws Exception {
131    
132                    try {
133                            String cmd = ParamUtil.getString(renderRequest, Constants.CMD);
134    
135                            if (!cmd.equals(Constants.ADD)) {
136                                    ActionUtil.getStructure(renderRequest);
137                            }
138                    }
139                    catch (NoSuchStructureException nsse) {
140    
141                            // Let this slide because the user can manually input a structure
142                            // key for a new structure that does not yet exist
143    
144                    }
145                    catch (Exception e) {
146                            if (//e instanceof NoSuchStructureException ||
147                                    e instanceof PrincipalException) {
148    
149                                    SessionErrors.add(renderRequest, e.getClass().getName());
150    
151                                    return mapping.findForward(
152                                            "portlet.dynamic_data_mapping.error");
153                            }
154                            else {
155                                    throw e;
156                            }
157                    }
158    
159                    return mapping.findForward(
160                            getForward(
161                                    renderRequest,
162                                    "portlet.dynamic_data_mapping.edit_structure"));
163            }
164    
165            protected void deleteStructure(ActionRequest actionRequest)
166                    throws Exception {
167    
168                    long structureId = ParamUtil.getLong(actionRequest, "structureId");
169    
170                    DDMStructureServiceUtil.deleteStructure(structureId);
171            }
172    
173            protected String getSaveAndContinueRedirect(
174                            PortletConfig portletConfig, ActionRequest actionRequest,
175                            DDMStructure structure, String redirect)
176                    throws Exception {
177    
178                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
179                            WebKeys.THEME_DISPLAY);
180    
181                    String availableFields = ParamUtil.getString(
182                            actionRequest, "availableFields");
183                    String saveCallback = ParamUtil.getString(
184                            actionRequest, "saveCallback");
185    
186                    PortletURLImpl portletURL = new PortletURLImpl(
187                            actionRequest, portletConfig.getPortletName(),
188                            themeDisplay.getPlid(), PortletRequest.RENDER_PHASE);
189    
190                    portletURL.setWindowState(actionRequest.getWindowState());
191    
192                    portletURL.setParameter(Constants.CMD, Constants.UPDATE, false);
193                    portletURL.setParameter(
194                            "struts_action", "/dynamic_data_mapping/edit_structure");
195                    portletURL.setParameter("redirect", redirect, false);
196                    portletURL.setParameter(
197                            "groupId", String.valueOf(structure.getGroupId()), false);
198                    portletURL.setParameter(
199                            "structureId", String.valueOf(structure.getStructureId()), false);
200                    portletURL.setParameter("availableFields", availableFields, false);
201                    portletURL.setParameter("saveCallback", saveCallback, false);
202    
203                    return portletURL.toString();
204            }
205    
206            protected DDMStructure updateStructure(ActionRequest actionRequest)
207                    throws Exception {
208    
209                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
210    
211                    long structureId = ParamUtil.getLong(actionRequest, "structureId");
212    
213                    long groupId = ParamUtil.getLong(actionRequest, "groupId");
214                    long classNameId = ParamUtil.getLong(actionRequest, "classNameId");
215                    Map<Locale, String> nameMap = LocalizationUtil.getLocalizationMap(
216                            actionRequest, "name");
217                    Map<Locale, String> descriptionMap =
218                            LocalizationUtil.getLocalizationMap(actionRequest, "description");
219                    String xsd = ParamUtil.getString(actionRequest, "xsd");
220                    String storageType = ParamUtil.getString(actionRequest, "storageType");
221    
222                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
223                            DDMStructure.class.getName(), actionRequest);
224    
225                    DDMStructure structure = null;
226    
227                    if (cmd.equals(Constants.ADD)) {
228                            structure = DDMStructureServiceUtil.addStructure(
229                                    groupId, classNameId, null, nameMap, descriptionMap, xsd,
230                                    storageType, DDMStructureConstants.TYPE_DEFAULT,
231                                    serviceContext);
232                    }
233                    else if (cmd.equals(Constants.UPDATE)) {
234                            structure = DDMStructureServiceUtil.updateStructure(
235                                    structureId, nameMap, descriptionMap, xsd, serviceContext);
236                    }
237    
238                    return structure;
239            }
240    
241    }