1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.journal.action;
24  
25  import com.liferay.portal.kernel.servlet.SessionErrors;
26  import com.liferay.portal.kernel.util.Constants;
27  import com.liferay.portal.kernel.util.ParamUtil;
28  import com.liferay.portal.kernel.util.StringUtil;
29  import com.liferay.portal.kernel.util.Validator;
30  import com.liferay.portal.security.auth.PrincipalException;
31  import com.liferay.portal.service.ServiceContext;
32  import com.liferay.portal.service.ServiceContextFactory;
33  import com.liferay.portal.struts.PortletAction;
34  import com.liferay.portal.theme.ThemeDisplay;
35  import com.liferay.portal.util.WebKeys;
36  import com.liferay.portlet.ActionRequestImpl;
37  import com.liferay.portlet.PortletURLImpl;
38  import com.liferay.portlet.journal.DuplicateStructureIdException;
39  import com.liferay.portlet.journal.NoSuchStructureException;
40  import com.liferay.portlet.journal.RequiredStructureException;
41  import com.liferay.portlet.journal.StructureDescriptionException;
42  import com.liferay.portlet.journal.StructureIdException;
43  import com.liferay.portlet.journal.StructureInheritanceException;
44  import com.liferay.portlet.journal.StructureNameException;
45  import com.liferay.portlet.journal.StructureXsdException;
46  import com.liferay.portlet.journal.model.JournalStructure;
47  import com.liferay.portlet.journal.service.JournalStructureServiceUtil;
48  import com.liferay.portlet.journal.util.JournalUtil;
49  
50  import javax.portlet.ActionRequest;
51  import javax.portlet.ActionResponse;
52  import javax.portlet.PortletConfig;
53  import javax.portlet.PortletRequest;
54  import javax.portlet.RenderRequest;
55  import javax.portlet.RenderResponse;
56  import javax.portlet.WindowState;
57  
58  import org.apache.struts.action.ActionForm;
59  import org.apache.struts.action.ActionForward;
60  import org.apache.struts.action.ActionMapping;
61  
62  /**
63   * <a href="EditStructureAction.java.html"><b><i>View Source</i></b></a>
64   *
65   * @author Brian Wing Shun Chan
66   * @author Raymond Augé
67   *
68   */
69  public class EditStructureAction extends PortletAction {
70  
71      public void processAction(
72              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
73              ActionRequest actionRequest, ActionResponse actionResponse)
74          throws Exception {
75  
76          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
77  
78          JournalStructure structure = null;
79  
80          try {
81              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
82                  structure = updateStructure(actionRequest);
83              }
84              else if (cmd.equals(Constants.DELETE)) {
85                  deleteStructures(actionRequest);
86              }
87  
88              if (Validator.isNotNull(cmd)) {
89                  String redirect = ParamUtil.getString(
90                      actionRequest, "redirect");
91  
92                  if (structure != null) {
93                      boolean saveAndContinue = ParamUtil.getBoolean(
94                          actionRequest, "saveAndContinue");
95  
96                      if (saveAndContinue) {
97                          redirect = getSaveAndContinueRedirect(
98                              portletConfig, actionRequest, structure, redirect);
99                      }
100                 }
101 
102                 sendRedirect(actionRequest, actionResponse, redirect);
103             }
104         }
105         catch (Exception e) {
106             if (e instanceof NoSuchStructureException ||
107                 e instanceof PrincipalException) {
108 
109                 SessionErrors.add(actionRequest, e.getClass().getName());
110 
111                 setForward(actionRequest, "portlet.journal.error");
112             }
113             else if (e instanceof DuplicateStructureIdException ||
114                      e instanceof RequiredStructureException ||
115                      e instanceof StructureDescriptionException ||
116                      e instanceof StructureIdException ||
117                      e instanceof StructureInheritanceException ||
118                      e instanceof StructureNameException ||
119                      e instanceof StructureXsdException) {
120 
121                 SessionErrors.add(actionRequest, e.getClass().getName());
122 
123                 if (e instanceof RequiredStructureException) {
124                     actionResponse.sendRedirect(
125                         ParamUtil.getString(actionRequest, "redirect"));
126                 }
127             }
128             else {
129                 throw e;
130             }
131         }
132     }
133 
134     public ActionForward render(
135             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
136             RenderRequest renderRequest, RenderResponse renderResponse)
137         throws Exception {
138 
139         try {
140             String cmd = ParamUtil.getString(renderRequest, Constants.CMD);
141 
142             if (!cmd.equals(Constants.ADD)) {
143                 ActionUtil.getStructure(renderRequest);
144             }
145         }
146         catch (NoSuchStructureException nsse) {
147 
148             // Let this slide because the user can manually input a structure id
149             // for a new structure that does not yet exist.
150 
151         }
152         catch (Exception e) {
153             if (//e instanceof NoSuchStructureException ||
154                 e instanceof PrincipalException) {
155 
156                 SessionErrors.add(renderRequest, e.getClass().getName());
157 
158                 return mapping.findForward("portlet.journal.error");
159             }
160             else {
161                 throw e;
162             }
163         }
164 
165         return mapping.findForward(
166             getForward(renderRequest, "portlet.journal.edit_structure"));
167     }
168 
169     protected void deleteStructures(ActionRequest actionRequest)
170         throws Exception {
171 
172         long groupId = ParamUtil.getLong(actionRequest, "groupId");
173 
174         String[] deleteStructureIds = StringUtil.split(
175             ParamUtil.getString(actionRequest, "deleteStructureIds"));
176 
177         for (int i = 0; i < deleteStructureIds.length; i++) {
178             JournalStructureServiceUtil.deleteStructure(
179                 groupId, deleteStructureIds[i]);
180 
181             JournalUtil.removeRecentStructure(
182                 actionRequest, deleteStructureIds[i]);
183         }
184     }
185 
186     protected String getSaveAndContinueRedirect(
187             PortletConfig portletConfig, ActionRequest actionRequest,
188             JournalStructure structure, String redirect)
189         throws Exception {
190 
191         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
192             WebKeys.THEME_DISPLAY);
193 
194         String originalRedirect = ParamUtil.getString(
195             actionRequest, "originalRedirect");
196 
197         PortletURLImpl portletURL = new PortletURLImpl(
198             (ActionRequestImpl)actionRequest, portletConfig.getPortletName(),
199             themeDisplay.getPlid(), PortletRequest.RENDER_PHASE);
200 
201         portletURL.setWindowState(WindowState.MAXIMIZED);
202 
203         portletURL.setParameter("struts_action", "/journal/edit_structure");
204         portletURL.setParameter(Constants.CMD, Constants.UPDATE, false);
205         portletURL.setParameter("redirect", redirect, false);
206         portletURL.setParameter("originalRedirect", originalRedirect, false);
207         portletURL.setParameter(
208             "groupId", String.valueOf(structure.getGroupId()), false);
209         portletURL.setParameter(
210             "structureId", structure.getStructureId(), false);
211 
212         return portletURL.toString();
213     }
214 
215     protected JournalStructure updateStructure(ActionRequest actionRequest)
216         throws Exception {
217 
218         String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
219 
220         long groupId = ParamUtil.getLong(actionRequest, "groupId");
221 
222         String structureId = ParamUtil.getString(actionRequest, "structureId");
223         boolean autoStructureId = ParamUtil.getBoolean(
224             actionRequest, "autoStructureId");
225 
226         String parentStructureId = ParamUtil.getString(
227             actionRequest, "parentStructureId");
228         String name = ParamUtil.getString(actionRequest, "name");
229         String description = ParamUtil.getString(actionRequest, "description");
230         String xsd = ParamUtil.getString(actionRequest, "xsd");
231 
232         ServiceContext serviceContext = ServiceContextFactory.getInstance(
233             JournalStructure.class.getName(), actionRequest);
234 
235         JournalStructure structure = null;
236 
237         if (cmd.equals(Constants.ADD)) {
238 
239             // Add structure
240 
241             structure = JournalStructureServiceUtil.addStructure(
242                 groupId, structureId, autoStructureId, parentStructureId, name,
243                 description, xsd, serviceContext);
244         }
245         else {
246 
247             // Update structure
248 
249             structure = JournalStructureServiceUtil.updateStructure(
250                 groupId, structureId, parentStructureId, name, description,
251                 xsd, serviceContext);
252         }
253 
254         // Recent structures
255 
256         JournalUtil.addRecentStructure(actionRequest, structure);
257 
258         return structure;
259     }
260 
261 }