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