1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
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(nodeId, name, description);
178 
179             updatePreferences(actionRequest, oldName, name);
180         }
181     }
182 
183     protected void updatePreferences(
184             ActionRequest actionRequest, String oldName, String newName)
185         throws Exception {
186 
187         PortletPreferences preferences = actionRequest.getPreferences();
188 
189         String hiddenNodes = preferences.getValue(
190             "hidden-nodes", StringPool.BLANK);
191         String visibleNodes = preferences.getValue(
192             "visible-nodes", StringPool.BLANK);
193 
194         String regex = oldName + ",?";
195 
196         preferences.setValue(
197             "hidden-nodes", hiddenNodes.replaceFirst(regex, newName));
198         preferences.setValue(
199             "visible-nodes",
200             visibleNodes.replaceFirst(regex, newName));
201 
202         preferences.store();
203     }
204 
205 }