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