001    /**
002     * Copyright (c) 2000-2012 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            @Override
050            public void processAction(
051                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
052                            ActionRequest actionRequest, ActionResponse actionResponse)
053                    throws Exception {
054    
055                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
056    
057                    try {
058                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
059                                    updateNode(actionRequest);
060                            }
061                            else if (cmd.equals(Constants.DELETE)) {
062                                    deleteNode(actionRequest);
063                            }
064                            else if (cmd.equals(Constants.SUBSCRIBE)) {
065                                    subscribeNode(actionRequest);
066                            }
067                            else if (cmd.equals(Constants.UNSUBSCRIBE)) {
068                                    unsubscribeNode(actionRequest);
069                            }
070    
071                            sendRedirect(actionRequest, actionResponse);
072                    }
073                    catch (Exception e) {
074                            if (e instanceof NoSuchNodeException ||
075                                    e instanceof PrincipalException) {
076    
077                                    SessionErrors.add(actionRequest, e.getClass().getName());
078    
079                                    setForward(actionRequest, "portlet.wiki.error");
080                            }
081                            else if (e instanceof DuplicateNodeNameException ||
082                                             e instanceof NodeNameException) {
083    
084                                    SessionErrors.add(actionRequest, e.getClass().getName());
085                            }
086                            else {
087                                    throw e;
088                            }
089                    }
090            }
091    
092            @Override
093            public ActionForward render(
094                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
095                            RenderRequest renderRequest, RenderResponse renderResponse)
096                    throws Exception {
097    
098                    try {
099                            long nodeId = ParamUtil.getLong(renderRequest, "nodeId");
100    
101                            if (nodeId > 0) {
102                                    ActionUtil.getNode(renderRequest);
103                            }
104                    }
105                    catch (Exception e) {
106                            if (e instanceof NoSuchNodeException ||
107                                    e instanceof PrincipalException) {
108    
109                                    SessionErrors.add(renderRequest, e.getClass().getName());
110    
111                                    return mapping.findForward("portlet.wiki.error");
112                            }
113                            else {
114                                    throw e;
115                            }
116                    }
117    
118                    return mapping.findForward(
119                            getForward(renderRequest, "portlet.wiki.edit_node"));
120            }
121    
122            protected void deleteNode(ActionRequest actionRequest) throws Exception {
123                    long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
124    
125                    String oldName = getNodeName(nodeId);
126    
127                    WikiCacheThreadLocal.setClearCache(false);
128    
129                    WikiNodeServiceUtil.deleteNode(nodeId);
130    
131                    WikiCacheUtil.clearCache(nodeId);
132    
133                    WikiCacheThreadLocal.setClearCache(true);
134    
135                    updatePreferences(actionRequest, oldName, StringPool.BLANK);
136            }
137    
138            protected String getNodeName(long nodeId) throws Exception {
139                    WikiNode node = WikiNodeServiceUtil.getNode(nodeId);
140    
141                    return node.getName();
142            }
143    
144            protected void subscribeNode(ActionRequest actionRequest)
145                    throws Exception {
146    
147                    long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
148    
149                    WikiNodeServiceUtil.subscribeNode(nodeId);
150            }
151    
152            protected void unsubscribeNode(ActionRequest actionRequest)
153                    throws Exception {
154    
155                    long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
156    
157                    WikiNodeServiceUtil.unsubscribeNode(nodeId);
158            }
159    
160            protected void updateNode(ActionRequest actionRequest) throws Exception {
161                    long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
162    
163                    String name = ParamUtil.getString(actionRequest, "name");
164                    String description = ParamUtil.getString(actionRequest, "description");
165    
166                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
167                            WikiNode.class.getName(), actionRequest);
168    
169                    if (nodeId <= 0) {
170    
171                            // Add node
172    
173                            WikiNodeServiceUtil.addNode(name, description, serviceContext);
174                    }
175                    else {
176    
177                            // Update node
178    
179                            String oldName = getNodeName(nodeId);
180    
181                            WikiNodeServiceUtil.updateNode(
182                                    nodeId, name, description, serviceContext);
183    
184                            updatePreferences(actionRequest, oldName, name);
185                    }
186            }
187    
188            protected void updatePreferences(
189                            ActionRequest actionRequest, String oldName, String newName)
190                    throws Exception {
191    
192                    PortletPreferences preferences = actionRequest.getPreferences();
193    
194                    String hiddenNodes = preferences.getValue(
195                            "hiddenNodes", StringPool.BLANK);
196                    String visibleNodes = preferences.getValue(
197                            "visibleNodes", StringPool.BLANK);
198    
199                    String regex = oldName + ",?";
200    
201                    preferences.setValue(
202                            "hiddenNodes", hiddenNodes.replaceFirst(regex, newName));
203                    preferences.setValue(
204                            "visibleNodes",
205                            visibleNodes.replaceFirst(regex, newName));
206    
207                    preferences.store();
208            }
209    
210    }