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