001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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());
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());
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());
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) throws Exception {
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                            "hiddenNodes", StringPool.BLANK);
194                    String visibleNodes = preferences.getValue(
195                            "visibleNodes", StringPool.BLANK);
196    
197                    String regex = oldName + ",?";
198    
199                    preferences.setValue(
200                            "hiddenNodes", hiddenNodes.replaceFirst(regex, newName));
201                    preferences.setValue(
202                            "visibleNodes",
203                            visibleNodes.replaceFirst(regex, newName));
204    
205                    preferences.store();
206            }
207    
208    }