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