1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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.PortalUtil;
32  import com.liferay.portal.util.WebKeys;
33  import com.liferay.portlet.wiki.DuplicateNodeNameException;
34  import com.liferay.portlet.wiki.NoSuchNodeException;
35  import com.liferay.portlet.wiki.NodeNameException;
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  public class EditNodeAction extends PortletAction {
56  
57      public void processAction(
58              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
59              ActionRequest actionRequest, ActionResponse actionResponse)
60          throws Exception {
61  
62          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
63  
64          try {
65              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
66                  updateNode(actionRequest);
67              }
68              else if (cmd.equals(Constants.DELETE)) {
69                  deleteNode(actionRequest);
70              }
71              else if (cmd.equals(Constants.SUBSCRIBE)) {
72                  subscribeNode(actionRequest);
73              }
74              else if (cmd.equals(Constants.UNSUBSCRIBE)) {
75                  unsubscribeNode(actionRequest);
76              }
77  
78              sendRedirect(actionRequest, actionResponse);
79          }
80          catch (Exception e) {
81              if (e instanceof NoSuchNodeException ||
82                  e instanceof PrincipalException) {
83  
84                  SessionErrors.add(actionRequest, e.getClass().getName());
85  
86                  setForward(actionRequest, "portlet.wiki.error");
87              }
88              else if (e instanceof DuplicateNodeNameException ||
89                       e instanceof NodeNameException) {
90  
91                  SessionErrors.add(actionRequest, e.getClass().getName());
92              }
93              else {
94                  throw e;
95              }
96          }
97      }
98  
99      public ActionForward render(
100             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
101             RenderRequest renderRequest, RenderResponse renderResponse)
102         throws Exception {
103 
104         try {
105             ActionUtil.getNode(renderRequest);
106         }
107         catch (Exception e) {
108             if (e instanceof NoSuchNodeException ||
109                 e instanceof PrincipalException) {
110 
111                 SessionErrors.add(renderRequest, e.getClass().getName());
112 
113                 return mapping.findForward("portlet.wiki.error");
114             }
115             else {
116                 throw e;
117             }
118         }
119 
120         return mapping.findForward(
121             getForward(renderRequest, "portlet.wiki.edit_node"));
122     }
123 
124     protected void deleteNode(ActionRequest actionRequest) throws Exception {
125         long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
126 
127         WikiCacheThreadLocal.setClearCache(false);
128 
129         WikiNodeServiceUtil.deleteNode(nodeId);
130 
131         WikiCacheUtil.clearCache(nodeId);
132 
133         WikiCacheThreadLocal.setClearCache(true);
134     }
135 
136     protected void subscribeNode(ActionRequest actionRequest)
137         throws Exception {
138 
139         long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
140 
141         WikiNodeServiceUtil.subscribeNode(nodeId);
142     }
143 
144     protected void unsubscribeNode(ActionRequest actionRequest)
145         throws Exception {
146 
147         long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
148 
149         WikiNodeServiceUtil.unsubscribeNode(nodeId);
150     }
151 
152     protected void updateNode(ActionRequest actionRequest) throws Exception {
153         Layout layout = (Layout)actionRequest.getAttribute(WebKeys.LAYOUT);
154 
155         long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
156 
157         String name = ParamUtil.getString(actionRequest, "name");
158         String description = ParamUtil.getString(actionRequest, "description");
159 
160         String[] communityPermissions = PortalUtil.getCommunityPermissions(
161             actionRequest);
162         String[] guestPermissions = PortalUtil.getGuestPermissions(
163             actionRequest);
164 
165         if (nodeId <= 0) {
166 
167             // Add node
168 
169             WikiNodeServiceUtil.addNode(
170                 layout.getPlid(), name, description, communityPermissions,
171                 guestPermissions);
172         }
173         else {
174 
175             // Update node
176 
177             WikiNodeServiceUtil.updateNode(nodeId, name, description);
178         }
179     }
180 
181 }