1   /**
2    * Copyright (c) 2000-2008 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.model.Layout;
31  import com.liferay.portal.security.auth.PrincipalException;
32  import com.liferay.portal.struts.PortletAction;
33  import com.liferay.portal.theme.ThemeDisplay;
34  import com.liferay.portal.util.WebKeys;
35  import com.liferay.portlet.ActionRequestImpl;
36  import com.liferay.portlet.PortletURLImpl;
37  import com.liferay.portlet.journal.DuplicateStructureIdException;
38  import com.liferay.portlet.journal.NoSuchStructureException;
39  import com.liferay.portlet.journal.RequiredStructureException;
40  import com.liferay.portlet.journal.StructureDescriptionException;
41  import com.liferay.portlet.journal.StructureIdException;
42  import com.liferay.portlet.journal.StructureNameException;
43  import com.liferay.portlet.journal.StructureXsdException;
44  import com.liferay.portlet.journal.model.JournalStructure;
45  import com.liferay.portlet.journal.service.JournalStructureServiceUtil;
46  import com.liferay.portlet.journal.util.JournalUtil;
47  
48  import javax.portlet.ActionRequest;
49  import javax.portlet.ActionResponse;
50  import javax.portlet.PortletConfig;
51  import javax.portlet.PortletRequest;
52  import javax.portlet.RenderRequest;
53  import javax.portlet.RenderResponse;
54  import javax.portlet.WindowState;
55  
56  import org.apache.struts.action.ActionForm;
57  import org.apache.struts.action.ActionForward;
58  import org.apache.struts.action.ActionMapping;
59  
60  /**
61   * <a href="EditStructureAction.java.html"><b><i>View Source</i></b></a>
62   *
63   * @author Brian Wing Shun Chan
64   *
65   */
66  public class EditStructureAction extends PortletAction {
67  
68      public void processAction(
69              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
70              ActionRequest actionRequest, ActionResponse actionResponse)
71          throws Exception {
72  
73          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
74  
75          JournalStructure structure = null;
76  
77          try {
78              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
79                  structure = updateStructure(actionRequest);
80              }
81              else if (cmd.equals(Constants.DELETE)) {
82                  deleteStructures(actionRequest);
83              }
84  
85              if (Validator.isNotNull(cmd)) {
86                  String redirect = ParamUtil.getString(
87                      actionRequest, "redirect");
88  
89                  if (structure != null) {
90                      boolean saveAndContinue = ParamUtil.getBoolean(
91                          actionRequest, "saveAndContinue");
92  
93                      if (saveAndContinue) {
94                          redirect = getSaveAndContinueRedirect(
95                              portletConfig, actionRequest, structure, redirect);
96                      }
97                  }
98  
99                  sendRedirect(actionRequest, actionResponse, redirect);
100             }
101         }
102         catch (Exception e) {
103             if (e instanceof NoSuchStructureException ||
104                 e instanceof PrincipalException) {
105 
106                 SessionErrors.add(actionRequest, e.getClass().getName());
107 
108                 setForward(actionRequest, "portlet.journal.error");
109             }
110             else if (e instanceof DuplicateStructureIdException ||
111                      e instanceof RequiredStructureException ||
112                      e instanceof StructureDescriptionException ||
113                      e instanceof StructureIdException ||
114                      e instanceof StructureNameException ||
115                      e instanceof StructureXsdException) {
116 
117                 SessionErrors.add(actionRequest, e.getClass().getName());
118 
119                 if (e instanceof RequiredStructureException) {
120                     actionResponse.sendRedirect(
121                         ParamUtil.getString(actionRequest, "redirect"));
122                 }
123             }
124             else {
125                 throw e;
126             }
127         }
128     }
129 
130     public ActionForward render(
131             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
132             RenderRequest renderRequest, RenderResponse renderResponse)
133         throws Exception {
134 
135         try {
136             String cmd = ParamUtil.getString(renderRequest, Constants.CMD);
137 
138             if (!cmd.equals(Constants.ADD)) {
139                 ActionUtil.getStructure(renderRequest);
140             }
141         }
142         catch (NoSuchStructureException nsse) {
143 
144             // Let this slide because the user can manually input a structure id
145             // for a new structure that does not yet exist.
146 
147         }
148         catch (Exception e) {
149             if (//e instanceof NoSuchStructureException ||
150                 e instanceof PrincipalException) {
151 
152                 SessionErrors.add(renderRequest, e.getClass().getName());
153 
154                 return mapping.findForward("portlet.journal.error");
155             }
156             else {
157                 throw e;
158             }
159         }
160 
161         return mapping.findForward(
162             getForward(renderRequest, "portlet.journal.edit_structure"));
163     }
164 
165     protected void deleteStructures(ActionRequest actionRequest)
166         throws Exception {
167 
168         long groupId = ParamUtil.getLong(actionRequest, "groupId");
169 
170         String[] deleteStructureIds = StringUtil.split(
171             ParamUtil.getString(actionRequest, "deleteStructureIds"));
172 
173         for (int i = 0; i < deleteStructureIds.length; i++) {
174             JournalStructureServiceUtil.deleteStructure(
175                 groupId, deleteStructureIds[i]);
176 
177             JournalUtil.removeRecentStructure(
178                 actionRequest, deleteStructureIds[i]);
179         }
180     }
181 
182     protected String getSaveAndContinueRedirect(
183             PortletConfig portletConfig, ActionRequest actionRequest,
184             JournalStructure structure, String redirect)
185         throws Exception {
186 
187         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
188             WebKeys.THEME_DISPLAY);
189 
190         String originalRedirect = ParamUtil.getString(
191             actionRequest, "originalRedirect");
192 
193         PortletURLImpl portletURL = new PortletURLImpl(
194             (ActionRequestImpl)actionRequest, portletConfig.getPortletName(),
195             themeDisplay.getPlid(), PortletRequest.RENDER_PHASE);
196 
197         portletURL.setWindowState(WindowState.MAXIMIZED);
198 
199         portletURL.setParameter("struts_action", "/journal/edit_structure");
200         portletURL.setParameter(Constants.CMD, Constants.UPDATE, false);
201         portletURL.setParameter("redirect", redirect, false);
202         portletURL.setParameter("originalRedirect", originalRedirect, false);
203         portletURL.setParameter(
204             "groupId", String.valueOf(structure.getGroupId()), false);
205         portletURL.setParameter(
206             "structureId", structure.getStructureId(), false);
207 
208         return portletURL.toString();
209     }
210 
211     protected JournalStructure updateStructure(ActionRequest actionRequest)
212         throws Exception {
213 
214         String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
215 
216         Layout layout = (Layout)actionRequest.getAttribute(WebKeys.LAYOUT);
217 
218         long groupId = ParamUtil.getLong(actionRequest, "groupId");
219 
220         String structureId = ParamUtil.getString(actionRequest, "structureId");
221         boolean autoStructureId = ParamUtil.getBoolean(
222             actionRequest, "autoStructureId");
223 
224         String name = ParamUtil.getString(actionRequest, "name");
225         String description = ParamUtil.getString(actionRequest, "description");
226         String xsd = ParamUtil.getString(actionRequest, "xsd");
227 
228         String[] communityPermissions = actionRequest.getParameterValues(
229             "communityPermissions");
230         String[] guestPermissions = actionRequest.getParameterValues(
231             "guestPermissions");
232 
233         JournalStructure structure = null;
234 
235         if (cmd.equals(Constants.ADD)) {
236 
237             // Add structure
238 
239             structure = JournalStructureServiceUtil.addStructure(
240                 structureId, autoStructureId, layout.getPlid(), name,
241                 description, xsd, communityPermissions, guestPermissions);
242         }
243         else {
244 
245             // Update structure
246 
247             structure = JournalStructureServiceUtil.updateStructure(
248                 groupId, structureId, name, description, xsd);
249         }
250 
251         // Recent structures
252 
253         JournalUtil.addRecentStructure(actionRequest, structure);
254 
255         return structure;
256     }
257 
258 }