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