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.util.Constants;
26  import com.liferay.portal.kernel.util.ParamUtil;
27  import com.liferay.portal.kernel.util.StringUtil;
28  import com.liferay.portal.kernel.util.Validator;
29  import com.liferay.portal.model.Layout;
30  import com.liferay.portal.security.auth.PrincipalException;
31  import com.liferay.portal.struts.PortletAction;
32  import com.liferay.portal.util.WebKeys;
33  import com.liferay.portlet.journal.DuplicateStructureIdException;
34  import com.liferay.portlet.journal.NoSuchStructureException;
35  import com.liferay.portlet.journal.RequiredStructureException;
36  import com.liferay.portlet.journal.StructureDescriptionException;
37  import com.liferay.portlet.journal.StructureIdException;
38  import com.liferay.portlet.journal.StructureNameException;
39  import com.liferay.portlet.journal.StructureXsdException;
40  import com.liferay.portlet.journal.model.JournalStructure;
41  import com.liferay.portlet.journal.service.JournalStructureServiceUtil;
42  import com.liferay.portlet.journal.util.JournalUtil;
43  import com.liferay.util.servlet.SessionErrors;
44  
45  import javax.portlet.ActionRequest;
46  import javax.portlet.ActionResponse;
47  import javax.portlet.PortletConfig;
48  import javax.portlet.RenderRequest;
49  import javax.portlet.RenderResponse;
50  
51  import org.apache.struts.action.ActionForm;
52  import org.apache.struts.action.ActionForward;
53  import org.apache.struts.action.ActionMapping;
54  
55  /**
56   * <a href="EditStructureAction.java.html"><b><i>View Source</i></b></a>
57   *
58   * @author Brian Wing Shun Chan
59   *
60   */
61  public class EditStructureAction extends PortletAction {
62  
63      public void processAction(
64              ActionMapping mapping, ActionForm form, PortletConfig config,
65              ActionRequest req, ActionResponse res)
66          throws Exception {
67  
68          String cmd = ParamUtil.getString(req, Constants.CMD);
69  
70          try {
71              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
72                  updateStructure(req);
73              }
74              else if (cmd.equals(Constants.DELETE)) {
75                  deleteStructures(req);
76              }
77  
78              if (Validator.isNotNull(cmd)) {
79                  sendRedirect(req, res);
80              }
81          }
82          catch (Exception e) {
83              if (e instanceof NoSuchStructureException ||
84                  e instanceof PrincipalException) {
85  
86                  SessionErrors.add(req, e.getClass().getName());
87  
88                  setForward(req, "portlet.journal.error");
89              }
90              else if (e instanceof DuplicateStructureIdException ||
91                       e instanceof RequiredStructureException ||
92                       e instanceof StructureDescriptionException ||
93                       e instanceof StructureIdException ||
94                       e instanceof StructureNameException ||
95                       e instanceof StructureXsdException) {
96  
97                  SessionErrors.add(req, e.getClass().getName());
98  
99                  if (e instanceof RequiredStructureException) {
100                     res.sendRedirect(ParamUtil.getString(req, "redirect"));
101                 }
102             }
103             else {
104                 throw e;
105             }
106         }
107     }
108 
109     public ActionForward render(
110             ActionMapping mapping, ActionForm form, PortletConfig config,
111             RenderRequest req, RenderResponse res)
112         throws Exception {
113 
114         try {
115             String cmd = ParamUtil.getString(req, Constants.CMD);
116 
117             if (!cmd.equals(Constants.ADD)) {
118                 ActionUtil.getStructure(req);
119             }
120         }
121         catch (NoSuchStructureException nsse) {
122 
123             // Let this slide because the user can manually input a structure id
124             // for a new structure that does not yet exist.
125 
126         }
127         catch (Exception e) {
128             if (//e instanceof NoSuchStructureException ||
129                 e instanceof PrincipalException) {
130 
131                 SessionErrors.add(req, e.getClass().getName());
132 
133                 return mapping.findForward("portlet.journal.error");
134             }
135             else {
136                 throw e;
137             }
138         }
139 
140         return mapping.findForward(
141             getForward(req, "portlet.journal.edit_structure"));
142     }
143 
144     protected void deleteStructures(ActionRequest req) throws Exception {
145         long groupId = ParamUtil.getLong(req, "groupId");
146 
147         String[] deleteStructureIds = StringUtil.split(
148             ParamUtil.getString(req, "deleteStructureIds"));
149 
150         for (int i = 0; i < deleteStructureIds.length; i++) {
151             JournalStructureServiceUtil.deleteStructure(
152                 groupId, deleteStructureIds[i]);
153 
154             JournalUtil.removeRecentStructure(req, deleteStructureIds[i]);
155         }
156     }
157 
158     protected void updateStructure(ActionRequest req) throws Exception {
159         String cmd = ParamUtil.getString(req, Constants.CMD);
160 
161         Layout layout = (Layout)req.getAttribute(WebKeys.LAYOUT);
162 
163         long groupId = ParamUtil.getLong(req, "groupId");
164 
165         String structureId = ParamUtil.getString(req, "structureId");
166         boolean autoStructureId = ParamUtil.getBoolean(req, "autoStructureId");
167 
168         String name = ParamUtil.getString(req, "name");
169         String description = ParamUtil.getString(req, "description");
170         String xsd = ParamUtil.getString(req, "xsd");
171 
172         String[] communityPermissions = req.getParameterValues(
173             "communityPermissions");
174         String[] guestPermissions = req.getParameterValues(
175             "guestPermissions");
176 
177         JournalStructure structure = null;
178 
179         if (cmd.equals(Constants.ADD)) {
180 
181             // Add structure
182 
183             structure = JournalStructureServiceUtil.addStructure(
184                 structureId, autoStructureId, layout.getPlid(), name,
185                 description, xsd, communityPermissions, guestPermissions);
186         }
187         else {
188 
189             // Update structure
190 
191             structure = JournalStructureServiceUtil.updateStructure(
192                 groupId, structureId, name, description, xsd);
193         }
194 
195         // Recent structures
196 
197         JournalUtil.addRecentStructure(req, structure);
198     }
199 
200 }