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