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.wiki.action;
24  
25  import com.liferay.portal.kernel.util.Constants;
26  import com.liferay.portal.kernel.util.ParamUtil;
27  import com.liferay.portal.model.Layout;
28  import com.liferay.portal.security.auth.PrincipalException;
29  import com.liferay.portal.struts.PortletAction;
30  import com.liferay.portal.util.WebKeys;
31  import com.liferay.portlet.wiki.DuplicateNodeNameException;
32  import com.liferay.portlet.wiki.NoSuchNodeException;
33  import com.liferay.portlet.wiki.NodeNameException;
34  import com.liferay.portlet.wiki.service.WikiNodeServiceUtil;
35  import com.liferay.util.servlet.SessionErrors;
36  
37  import javax.portlet.ActionRequest;
38  import javax.portlet.ActionResponse;
39  import javax.portlet.PortletConfig;
40  import javax.portlet.RenderRequest;
41  import javax.portlet.RenderResponse;
42  
43  import org.apache.struts.action.ActionForm;
44  import org.apache.struts.action.ActionForward;
45  import org.apache.struts.action.ActionMapping;
46  
47  /**
48   * <a href="EditNodeAction.java.html"><b><i>View Source</i></b></a>
49   *
50   * @author Brian Wing Shun Chan
51   *
52   */
53  public class EditNodeAction extends PortletAction {
54  
55      public void processAction(
56              ActionMapping mapping, ActionForm form, PortletConfig config,
57              ActionRequest req, ActionResponse res)
58          throws Exception {
59  
60          String cmd = ParamUtil.getString(req, Constants.CMD);
61  
62          try {
63              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
64                  updateNode(req);
65              }
66              else if (cmd.equals(Constants.DELETE)) {
67                  deleteNode(req);
68              }
69              else if (cmd.equals(Constants.SUBSCRIBE)) {
70                  subscribeNode(req);
71              }
72              else if (cmd.equals(Constants.UNSUBSCRIBE)) {
73                  unsubscribeNode(req);
74              }
75  
76              sendRedirect(req, res);
77          }
78          catch (Exception e) {
79              if (e instanceof NoSuchNodeException ||
80                  e instanceof PrincipalException) {
81  
82                  SessionErrors.add(req, e.getClass().getName());
83  
84                  setForward(req, "portlet.wiki.error");
85              }
86              else if (e instanceof DuplicateNodeNameException ||
87                       e instanceof NodeNameException) {
88  
89                  SessionErrors.add(req, e.getClass().getName());
90              }
91              else {
92                  throw e;
93              }
94          }
95      }
96  
97      public ActionForward render(
98              ActionMapping mapping, ActionForm form, PortletConfig config,
99              RenderRequest req, RenderResponse res)
100         throws Exception {
101 
102         try {
103             ActionUtil.getNode(req);
104         }
105         catch (Exception e) {
106             if (e instanceof NoSuchNodeException ||
107                 e instanceof PrincipalException) {
108 
109                 SessionErrors.add(req, e.getClass().getName());
110 
111                 return mapping.findForward("portlet.wiki.error");
112             }
113             else {
114                 throw e;
115             }
116         }
117 
118         return mapping.findForward(getForward(req, "portlet.wiki.edit_node"));
119     }
120 
121     protected void deleteNode(ActionRequest req) throws Exception {
122         long nodeId = ParamUtil.getLong(req, "nodeId");
123 
124         WikiNodeServiceUtil.deleteNode(nodeId);
125     }
126 
127     protected void subscribeNode(ActionRequest req) throws Exception {
128         long nodeId = ParamUtil.getLong(req, "nodeId");
129 
130         WikiNodeServiceUtil.subscribeNode(nodeId);
131     }
132 
133     protected void unsubscribeNode(ActionRequest req) throws Exception {
134         long nodeId = ParamUtil.getLong(req, "nodeId");
135 
136         WikiNodeServiceUtil.unsubscribeNode(nodeId);
137     }
138 
139     protected void updateNode(ActionRequest req) throws Exception {
140         Layout layout = (Layout)req.getAttribute(WebKeys.LAYOUT);
141 
142         long nodeId = ParamUtil.getLong(req, "nodeId");
143 
144         String name = ParamUtil.getString(req, "name");
145         String description = ParamUtil.getString(req, "description");
146 
147         String[] communityPermissions = req.getParameterValues(
148             "communityPermissions");
149         String[] guestPermissions = req.getParameterValues(
150             "guestPermissions");
151 
152         if (nodeId <= 0) {
153 
154             // Add node
155 
156             WikiNodeServiceUtil.addNode(
157                 layout.getPlid(), name, description, communityPermissions,
158                 guestPermissions);
159         }
160         else {
161 
162             // Update node
163 
164             WikiNodeServiceUtil.updateNode(nodeId, name, description);
165         }
166     }
167 
168 }