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