1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.journal.action;
16  
17  import com.liferay.portal.kernel.servlet.SessionErrors;
18  import com.liferay.portal.kernel.util.Constants;
19  import com.liferay.portal.kernel.util.ParamUtil;
20  import com.liferay.portal.kernel.util.StringUtil;
21  import com.liferay.portal.kernel.util.Validator;
22  import com.liferay.portal.security.auth.PrincipalException;
23  import com.liferay.portal.service.ServiceContext;
24  import com.liferay.portal.service.ServiceContextFactory;
25  import com.liferay.portal.struts.PortletAction;
26  import com.liferay.portal.theme.ThemeDisplay;
27  import com.liferay.portal.util.WebKeys;
28  import com.liferay.portlet.ActionRequestImpl;
29  import com.liferay.portlet.PortletURLImpl;
30  import com.liferay.portlet.journal.DuplicateStructureIdException;
31  import com.liferay.portlet.journal.NoSuchStructureException;
32  import com.liferay.portlet.journal.RequiredStructureException;
33  import com.liferay.portlet.journal.StructureDescriptionException;
34  import com.liferay.portlet.journal.StructureIdException;
35  import com.liferay.portlet.journal.StructureInheritanceException;
36  import com.liferay.portlet.journal.StructureNameException;
37  import com.liferay.portlet.journal.StructureXsdException;
38  import com.liferay.portlet.journal.model.JournalStructure;
39  import com.liferay.portlet.journal.service.JournalStructureServiceUtil;
40  import com.liferay.portlet.journal.util.JournalUtil;
41  
42  import javax.portlet.ActionRequest;
43  import javax.portlet.ActionResponse;
44  import javax.portlet.PortletConfig;
45  import javax.portlet.PortletRequest;
46  import javax.portlet.RenderRequest;
47  import javax.portlet.RenderResponse;
48  import javax.portlet.WindowState;
49  
50  import org.apache.struts.action.ActionForm;
51  import org.apache.struts.action.ActionForward;
52  import org.apache.struts.action.ActionMapping;
53  
54  /**
55   * <a href="EditStructureAction.java.html"><b><i>View Source</i></b></a>
56   *
57   * @author Brian Wing Shun Chan
58   * @author Raymond Augé
59   */
60  public class EditStructureAction extends PortletAction {
61  
62      public void processAction(
63              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
64              ActionRequest actionRequest, ActionResponse actionResponse)
65          throws Exception {
66  
67          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
68  
69          JournalStructure structure = null;
70  
71          try {
72              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
73                  structure = updateStructure(actionRequest);
74              }
75              else if (cmd.equals(Constants.DELETE)) {
76                  deleteStructures(actionRequest);
77              }
78  
79              if (Validator.isNotNull(cmd)) {
80                  String redirect = ParamUtil.getString(
81                      actionRequest, "redirect");
82  
83                  if (structure != null) {
84                      boolean saveAndContinue = ParamUtil.getBoolean(
85                          actionRequest, "saveAndContinue");
86  
87                      if (saveAndContinue) {
88                          redirect = getSaveAndContinueRedirect(
89                              portletConfig, actionRequest, structure, redirect);
90                      }
91                  }
92  
93                  sendRedirect(actionRequest, actionResponse, redirect);
94              }
95          }
96          catch (Exception e) {
97              if (e instanceof NoSuchStructureException ||
98                  e instanceof PrincipalException) {
99  
100                 SessionErrors.add(actionRequest, e.getClass().getName());
101 
102                 setForward(actionRequest, "portlet.journal.error");
103             }
104             else if (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 (int i = 0; i < deleteStructureIds.length; i++) {
169             JournalStructureServiceUtil.deleteStructure(
170                 groupId, deleteStructureIds[i]);
171 
172             JournalUtil.removeRecentStructure(
173                 actionRequest, deleteStructureIds[i]);
174         }
175     }
176 
177     protected String getSaveAndContinueRedirect(
178             PortletConfig portletConfig, ActionRequest actionRequest,
179             JournalStructure structure, String redirect)
180         throws Exception {
181 
182         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
183             WebKeys.THEME_DISPLAY);
184 
185         String originalRedirect = ParamUtil.getString(
186             actionRequest, "originalRedirect");
187 
188         PortletURLImpl portletURL = new PortletURLImpl(
189             (ActionRequestImpl)actionRequest, portletConfig.getPortletName(),
190             themeDisplay.getPlid(), PortletRequest.RENDER_PHASE);
191 
192         portletURL.setWindowState(WindowState.MAXIMIZED);
193 
194         portletURL.setParameter("struts_action", "/journal/edit_structure");
195         portletURL.setParameter(Constants.CMD, Constants.UPDATE, false);
196         portletURL.setParameter("redirect", redirect, false);
197         portletURL.setParameter("originalRedirect", originalRedirect, false);
198         portletURL.setParameter(
199             "groupId", String.valueOf(structure.getGroupId()), false);
200         portletURL.setParameter(
201             "structureId", structure.getStructureId(), false);
202 
203         return portletURL.toString();
204     }
205 
206     protected JournalStructure updateStructure(ActionRequest actionRequest)
207         throws Exception {
208 
209         String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
210 
211         long groupId = ParamUtil.getLong(actionRequest, "groupId");
212 
213         String structureId = ParamUtil.getString(actionRequest, "structureId");
214         boolean autoStructureId = ParamUtil.getBoolean(
215             actionRequest, "autoStructureId");
216 
217         String parentStructureId = ParamUtil.getString(
218             actionRequest, "parentStructureId");
219         String name = ParamUtil.getString(actionRequest, "name");
220         String description = ParamUtil.getString(actionRequest, "description");
221         String xsd = ParamUtil.getString(actionRequest, "xsd");
222 
223         ServiceContext serviceContext = ServiceContextFactory.getInstance(
224             JournalStructure.class.getName(), actionRequest);
225 
226         JournalStructure structure = null;
227 
228         if (cmd.equals(Constants.ADD)) {
229 
230             // Add structure
231 
232             structure = JournalStructureServiceUtil.addStructure(
233                 groupId, structureId, autoStructureId, parentStructureId, name,
234                 description, xsd, serviceContext);
235         }
236         else {
237 
238             // Update structure
239 
240             structure = JournalStructureServiceUtil.updateStructure(
241                 groupId, structureId, parentStructureId, name, description,
242                 xsd, serviceContext);
243         }
244 
245         // Recent structures
246 
247         JournalUtil.addRecentStructure(actionRequest, structure);
248 
249         return structure;
250     }
251 
252 }