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