001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.wiki.action;
016    
017    import com.liferay.portal.kernel.servlet.SessionErrors;
018    import com.liferay.portal.kernel.util.Constants;
019    import com.liferay.portal.kernel.util.ParamUtil;
020    import com.liferay.portal.kernel.util.StringPool;
021    import com.liferay.portal.security.auth.PrincipalException;
022    import com.liferay.portal.service.ServiceContext;
023    import com.liferay.portal.service.ServiceContextFactory;
024    import com.liferay.portal.struts.PortletAction;
025    import com.liferay.portlet.wiki.DuplicateNodeNameException;
026    import com.liferay.portlet.wiki.NoSuchNodeException;
027    import com.liferay.portlet.wiki.NodeNameException;
028    import com.liferay.portlet.wiki.model.WikiNode;
029    import com.liferay.portlet.wiki.service.WikiNodeServiceUtil;
030    import com.liferay.portlet.wiki.util.WikiCacheThreadLocal;
031    import com.liferay.portlet.wiki.util.WikiCacheUtil;
032    
033    import javax.portlet.ActionRequest;
034    import javax.portlet.ActionResponse;
035    import javax.portlet.PortletConfig;
036    import javax.portlet.PortletPreferences;
037    import javax.portlet.RenderRequest;
038    import javax.portlet.RenderResponse;
039    
040    import org.apache.struts.action.ActionForm;
041    import org.apache.struts.action.ActionForward;
042    import org.apache.struts.action.ActionMapping;
043    
044    /**
045     * @author Brian Wing Shun Chan
046     */
047    public class EditNodeAction extends PortletAction {
048    
049            public void processAction(
050                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
051                            ActionRequest actionRequest, ActionResponse actionResponse)
052                    throws Exception {
053    
054                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
055    
056                    try {
057                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
058                                    updateNode(actionRequest);
059                            }
060                            else if (cmd.equals(Constants.DELETE)) {
061                                    deleteNode(actionRequest);
062                            }
063                            else if (cmd.equals(Constants.SUBSCRIBE)) {
064                                    subscribeNode(actionRequest);
065                            }
066                            else if (cmd.equals(Constants.UNSUBSCRIBE)) {
067                                    unsubscribeNode(actionRequest);
068                            }
069    
070                            sendRedirect(actionRequest, actionResponse);
071                    }
072                    catch (Exception e) {
073                            if (e instanceof NoSuchNodeException ||
074                                    e instanceof PrincipalException) {
075    
076                                    SessionErrors.add(actionRequest, e.getClass().getName());
077    
078                                    setForward(actionRequest, "portlet.wiki.error");
079                            }
080                            else if (e instanceof DuplicateNodeNameException ||
081                                             e instanceof NodeNameException) {
082    
083                                    SessionErrors.add(actionRequest, e.getClass().getName());
084                            }
085                            else {
086                                    throw e;
087                            }
088                    }
089            }
090    
091            public ActionForward render(
092                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
093                            RenderRequest renderRequest, RenderResponse renderResponse)
094                    throws Exception {
095    
096                    try {
097                            long nodeId = ParamUtil.getLong(renderRequest, "nodeId");
098    
099                            if (nodeId > 0) {
100                                    ActionUtil.getNode(renderRequest);
101                            }
102                    }
103                    catch (Exception e) {
104                            if (e instanceof NoSuchNodeException ||
105                                    e instanceof PrincipalException) {
106    
107                                    SessionErrors.add(renderRequest, e.getClass().getName());
108    
109                                    return mapping.findForward("portlet.wiki.error");
110                            }
111                            else {
112                                    throw e;
113                            }
114                    }
115    
116                    return mapping.findForward(
117                            getForward(renderRequest, "portlet.wiki.edit_node"));
118            }
119    
120            protected void deleteNode(ActionRequest actionRequest) throws Exception {
121                    long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
122    
123                    String oldName = getNodeName(nodeId);
124    
125                    WikiCacheThreadLocal.setClearCache(false);
126    
127                    WikiNodeServiceUtil.deleteNode(nodeId);
128    
129                    WikiCacheUtil.clearCache(nodeId);
130    
131                    WikiCacheThreadLocal.setClearCache(true);
132    
133                    updatePreferences(actionRequest, oldName, StringPool.BLANK);
134            }
135    
136            protected String getNodeName(long nodeId) throws Exception {
137                    WikiNode node = WikiNodeServiceUtil.getNode(nodeId);
138    
139                    return node.getName();
140            }
141    
142            protected void subscribeNode(ActionRequest actionRequest)
143                    throws Exception {
144    
145                    long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
146    
147                    WikiNodeServiceUtil.subscribeNode(nodeId);
148            }
149    
150            protected void unsubscribeNode(ActionRequest actionRequest)
151                    throws Exception {
152    
153                    long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
154    
155                    WikiNodeServiceUtil.unsubscribeNode(nodeId);
156            }
157    
158            protected void updateNode(ActionRequest actionRequest) throws Exception {
159                    long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
160    
161                    String name = ParamUtil.getString(actionRequest, "name");
162                    String description = ParamUtil.getString(actionRequest, "description");
163    
164                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
165                            WikiNode.class.getName(), actionRequest);
166    
167                    if (nodeId <= 0) {
168    
169                            // Add node
170    
171                            WikiNodeServiceUtil.addNode(name, description, serviceContext);
172                    }
173                    else {
174    
175                            // Update node
176    
177                            String oldName = getNodeName(nodeId);
178    
179                            WikiNodeServiceUtil.updateNode(
180                                    nodeId, name, description, serviceContext);
181    
182                            updatePreferences(actionRequest, oldName, name);
183                    }
184            }
185    
186            protected void updatePreferences(
187                            ActionRequest actionRequest, String oldName, String newName)
188                    throws Exception {
189    
190                    PortletPreferences preferences = actionRequest.getPreferences();
191    
192                    String hiddenNodes = preferences.getValue(
193                            "hidden-nodes", StringPool.BLANK);
194                    String visibleNodes = preferences.getValue(
195                            "visible-nodes", StringPool.BLANK);
196    
197                    String regex = oldName + ",?";
198    
199                    preferences.setValue(
200                            "hidden-nodes", hiddenNodes.replaceFirst(regex, newName));
201                    preferences.setValue(
202                            "visible-nodes",
203                            visibleNodes.replaceFirst(regex, newName));
204    
205                    preferences.store();
206            }
207    
208    }