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