1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.wiki.action;
21  
22  import com.liferay.portal.kernel.servlet.SessionErrors;
23  import com.liferay.portal.kernel.util.Constants;
24  import com.liferay.portal.kernel.util.ParamUtil;
25  import com.liferay.portal.model.Layout;
26  import com.liferay.portal.security.auth.PrincipalException;
27  import com.liferay.portal.struts.PortletAction;
28  import com.liferay.portal.util.WebKeys;
29  import com.liferay.portlet.wiki.DuplicateNodeNameException;
30  import com.liferay.portlet.wiki.NoSuchNodeException;
31  import com.liferay.portlet.wiki.NodeNameException;
32  import com.liferay.portlet.wiki.service.WikiNodeServiceUtil;
33  import com.liferay.portlet.wiki.util.WikiCacheThreadLocal;
34  import com.liferay.portlet.wiki.util.WikiCacheUtil;
35  
36  import javax.portlet.ActionRequest;
37  import javax.portlet.ActionResponse;
38  import javax.portlet.PortletConfig;
39  import javax.portlet.RenderRequest;
40  import javax.portlet.RenderResponse;
41  
42  import org.apache.struts.action.ActionForm;
43  import org.apache.struts.action.ActionForward;
44  import org.apache.struts.action.ActionMapping;
45  
46  /**
47   * <a href="EditNodeAction.java.html"><b><i>View Source</i></b></a>
48   *
49   * @author Brian Wing Shun Chan
50   *
51   */
52  public class EditNodeAction extends PortletAction {
53  
54      public void processAction(
55              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
56              ActionRequest actionRequest, ActionResponse actionResponse)
57          throws Exception {
58  
59          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
60  
61          try {
62              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
63                  updateNode(actionRequest);
64              }
65              else if (cmd.equals(Constants.DELETE)) {
66                  deleteNode(actionRequest);
67              }
68              else if (cmd.equals(Constants.SUBSCRIBE)) {
69                  subscribeNode(actionRequest);
70              }
71              else if (cmd.equals(Constants.UNSUBSCRIBE)) {
72                  unsubscribeNode(actionRequest);
73              }
74  
75              sendRedirect(actionRequest, actionResponse);
76          }
77          catch (Exception e) {
78              if (e instanceof NoSuchNodeException ||
79                  e instanceof PrincipalException) {
80  
81                  SessionErrors.add(actionRequest, e.getClass().getName());
82  
83                  setForward(actionRequest, "portlet.wiki.error");
84              }
85              else if (e instanceof DuplicateNodeNameException ||
86                       e instanceof NodeNameException) {
87  
88                  SessionErrors.add(actionRequest, e.getClass().getName());
89              }
90              else {
91                  throw e;
92              }
93          }
94      }
95  
96      public ActionForward render(
97              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
98              RenderRequest renderRequest, RenderResponse renderResponse)
99          throws Exception {
100 
101         try {
102             ActionUtil.getNode(renderRequest);
103         }
104         catch (Exception e) {
105             if (e instanceof NoSuchNodeException ||
106                 e instanceof PrincipalException) {
107 
108                 SessionErrors.add(renderRequest, e.getClass().getName());
109 
110                 return mapping.findForward("portlet.wiki.error");
111             }
112             else {
113                 throw e;
114             }
115         }
116 
117         return mapping.findForward(
118             getForward(renderRequest, "portlet.wiki.edit_node"));
119     }
120 
121     protected void deleteNode(ActionRequest actionRequest) throws Exception {
122         long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
123 
124         WikiCacheThreadLocal.setClearCache(false);
125 
126         WikiNodeServiceUtil.deleteNode(nodeId);
127 
128         WikiCacheUtil.clearCache(nodeId);
129 
130         WikiCacheThreadLocal.setClearCache(true);
131     }
132 
133     protected void subscribeNode(ActionRequest actionRequest)
134         throws Exception {
135 
136         long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
137 
138         WikiNodeServiceUtil.subscribeNode(nodeId);
139     }
140 
141     protected void unsubscribeNode(ActionRequest actionRequest)
142         throws Exception {
143 
144         long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
145 
146         WikiNodeServiceUtil.unsubscribeNode(nodeId);
147     }
148 
149     protected void updateNode(ActionRequest actionRequest) throws Exception {
150         Layout layout = (Layout)actionRequest.getAttribute(WebKeys.LAYOUT);
151 
152         long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
153 
154         String name = ParamUtil.getString(actionRequest, "name");
155         String description = ParamUtil.getString(actionRequest, "description");
156 
157         String[] communityPermissions = actionRequest.getParameterValues(
158             "communityPermissions");
159         String[] guestPermissions = actionRequest.getParameterValues(
160             "guestPermissions");
161 
162         if (nodeId <= 0) {
163 
164             // Add node
165 
166             WikiNodeServiceUtil.addNode(
167                 layout.getPlid(), name, description, communityPermissions,
168                 guestPermissions);
169         }
170         else {
171 
172             // Update node
173 
174             WikiNodeServiceUtil.updateNode(nodeId, name, description);
175         }
176     }
177 
178 }