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