1   /**
2    * Copyright (c) 2000-2010 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   *
12   * 
13   */
14  
15  package com.liferay.portlet.wiki.action;
16  
17  import com.liferay.portal.kernel.servlet.SessionErrors;
18  import com.liferay.portal.kernel.util.Constants;
19  import com.liferay.portal.kernel.util.ParamUtil;
20  import com.liferay.portal.kernel.util.StringPool;
21  import com.liferay.portal.model.Layout;
22  import com.liferay.portal.security.auth.PrincipalException;
23  import com.liferay.portal.struts.PortletAction;
24  import com.liferay.portal.util.PortalUtil;
25  import com.liferay.portal.util.WebKeys;
26  import com.liferay.portlet.wiki.DuplicateNodeNameException;
27  import com.liferay.portlet.wiki.NoSuchNodeException;
28  import com.liferay.portlet.wiki.NodeNameException;
29  import com.liferay.portlet.wiki.model.WikiNode;
30  import com.liferay.portlet.wiki.service.WikiNodeServiceUtil;
31  import com.liferay.portlet.wiki.util.WikiCacheThreadLocal;
32  import com.liferay.portlet.wiki.util.WikiCacheUtil;
33  
34  import javax.portlet.ActionRequest;
35  import javax.portlet.ActionResponse;
36  import javax.portlet.PortletConfig;
37  import javax.portlet.PortletPreferences;
38  import javax.portlet.RenderRequest;
39  import javax.portlet.RenderResponse;
40  
41  import org.apache.struts.action.ActionForm;
42  import org.apache.struts.action.ActionForward;
43  import org.apache.struts.action.ActionMapping;
44  
45  /**
46   * <a href="EditNodeAction.java.html"><b><i>View Source</i></b></a>
47   *
48   * @author Brian Wing Shun Chan
49   */
50  public class EditNodeAction extends PortletAction {
51  
52      public void processAction(
53              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
54              ActionRequest actionRequest, ActionResponse actionResponse)
55          throws Exception {
56  
57          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
58  
59          try {
60              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
61                  updateNode(actionRequest);
62              }
63              else if (cmd.equals(Constants.DELETE)) {
64                  deleteNode(actionRequest);
65              }
66              else if (cmd.equals(Constants.SUBSCRIBE)) {
67                  subscribeNode(actionRequest);
68              }
69              else if (cmd.equals(Constants.UNSUBSCRIBE)) {
70                  unsubscribeNode(actionRequest);
71              }
72  
73              sendRedirect(actionRequest, actionResponse);
74          }
75          catch (Exception e) {
76              if (e instanceof NoSuchNodeException ||
77                  e instanceof PrincipalException) {
78  
79                  SessionErrors.add(actionRequest, e.getClass().getName());
80  
81                  setForward(actionRequest, "portlet.wiki.error");
82              }
83              else if (e instanceof DuplicateNodeNameException ||
84                       e instanceof NodeNameException) {
85  
86                  SessionErrors.add(actionRequest, e.getClass().getName());
87              }
88              else {
89                  throw e;
90              }
91          }
92      }
93  
94      public ActionForward render(
95              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
96              RenderRequest renderRequest, RenderResponse renderResponse)
97          throws Exception {
98  
99          try {
100             ActionUtil.getNode(renderRequest);
101         }
102         catch (Exception e) {
103             if (e instanceof NoSuchNodeException ||
104                 e instanceof PrincipalException) {
105 
106                 SessionErrors.add(renderRequest, e.getClass().getName());
107 
108                 return mapping.findForward("portlet.wiki.error");
109             }
110             else {
111                 throw e;
112             }
113         }
114 
115         return mapping.findForward(
116             getForward(renderRequest, "portlet.wiki.edit_node"));
117     }
118 
119     protected void deleteNode(ActionRequest actionRequest) throws Exception {
120         long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
121 
122         String oldName = getNodeName(nodeId);
123 
124         WikiCacheThreadLocal.setClearCache(false);
125 
126         WikiNodeServiceUtil.deleteNode(nodeId);
127 
128         WikiCacheUtil.clearCache(nodeId);
129 
130         WikiCacheThreadLocal.setClearCache(true);
131 
132         updatePreferences(actionRequest, oldName, StringPool.BLANK);
133     }
134 
135     protected String getNodeName(long nodeId) throws Exception {
136         WikiNode node = WikiNodeServiceUtil.getNode(nodeId);
137 
138         return node.getName();
139     }
140 
141     protected void subscribeNode(ActionRequest actionRequest)
142         throws Exception {
143 
144         long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
145 
146         WikiNodeServiceUtil.subscribeNode(nodeId);
147     }
148 
149     protected void unsubscribeNode(ActionRequest actionRequest)
150         throws Exception {
151 
152         long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
153 
154         WikiNodeServiceUtil.unsubscribeNode(nodeId);
155     }
156 
157     protected void updateNode(ActionRequest actionRequest) throws Exception {
158         Layout layout = (Layout)actionRequest.getAttribute(WebKeys.LAYOUT);
159 
160         long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
161 
162         String name = ParamUtil.getString(actionRequest, "name");
163         String description = ParamUtil.getString(actionRequest, "description");
164 
165         String[] communityPermissions = PortalUtil.getCommunityPermissions(
166             actionRequest);
167         String[] guestPermissions = PortalUtil.getGuestPermissions(
168             actionRequest);
169 
170         if (nodeId <= 0) {
171 
172             // Add node
173 
174             WikiNodeServiceUtil.addNode(
175                 layout.getPlid(), name, description, communityPermissions,
176                 guestPermissions);
177         }
178         else {
179 
180             // Update node
181 
182             String oldName = getNodeName(nodeId);
183 
184             WikiNodeServiceUtil.updateNode(nodeId, name, description);
185 
186             updatePreferences(actionRequest, oldName, name);
187         }
188     }
189 
190     protected void updatePreferences(
191             ActionRequest actionRequest, String oldName, String newName)
192         throws Exception {
193 
194         PortletPreferences preferences = actionRequest.getPreferences();
195 
196         String hiddenNodes = preferences.getValue(
197             "hidden-nodes", StringPool.BLANK);
198         String visibleNodes = preferences.getValue(
199             "visible-nodes", StringPool.BLANK);
200 
201         String regex = oldName + ",?";
202 
203         preferences.setValue(
204             "hidden-nodes", hiddenNodes.replaceFirst(regex, newName));
205         preferences.setValue(
206             "visible-nodes",
207             visibleNodes.replaceFirst(regex, newName));
208 
209         preferences.store();
210     }
211 
212 }