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