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.portlet.LiferayPortletConfig;
018    import com.liferay.portal.kernel.servlet.SessionErrors;
019    import com.liferay.portal.kernel.servlet.SessionMessages;
020    import com.liferay.portal.kernel.util.Constants;
021    import com.liferay.portal.kernel.util.ParamUtil;
022    import com.liferay.portal.kernel.util.StringPool;
023    import com.liferay.portal.kernel.util.StringUtil;
024    import com.liferay.portal.security.auth.PrincipalException;
025    import com.liferay.portal.service.ServiceContext;
026    import com.liferay.portal.service.ServiceContextFactory;
027    import com.liferay.portal.struts.PortletAction;
028    import com.liferay.portlet.wiki.DuplicateNodeNameException;
029    import com.liferay.portlet.wiki.NoSuchNodeException;
030    import com.liferay.portlet.wiki.NodeNameException;
031    import com.liferay.portlet.wiki.model.WikiNode;
032    import com.liferay.portlet.wiki.service.WikiNodeServiceUtil;
033    import com.liferay.portlet.wiki.util.WikiCacheThreadLocal;
034    import com.liferay.portlet.wiki.util.WikiCacheUtil;
035    
036    import java.util.HashMap;
037    import java.util.Map;
038    
039    import javax.portlet.ActionRequest;
040    import javax.portlet.ActionResponse;
041    import javax.portlet.PortletConfig;
042    import javax.portlet.PortletPreferences;
043    import javax.portlet.RenderRequest;
044    import javax.portlet.RenderResponse;
045    
046    import org.apache.struts.action.ActionForm;
047    import org.apache.struts.action.ActionForward;
048    import org.apache.struts.action.ActionMapping;
049    
050    /**
051     * @author Brian Wing Shun Chan
052     */
053    public class EditNodeAction extends PortletAction {
054    
055            @Override
056            public void processAction(
057                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
058                            ActionRequest actionRequest, ActionResponse actionResponse)
059                    throws Exception {
060    
061                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
062    
063                    try {
064                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
065                                    updateNode(actionRequest);
066                            }
067                            else if (cmd.equals(Constants.DELETE)) {
068                                    deleteNode(
069                                            (LiferayPortletConfig)portletConfig, actionRequest, false);
070                            }
071                            else if (cmd.equals(Constants.MOVE_TO_TRASH)) {
072                                    deleteNode(
073                                            (LiferayPortletConfig)portletConfig, actionRequest, true);
074                            }
075                            else if (cmd.equals(Constants.RESTORE)) {
076                                    restoreNode(actionRequest);
077                            }
078                            else if (cmd.equals(Constants.SUBSCRIBE)) {
079                                    subscribeNode(actionRequest);
080                            }
081                            else if (cmd.equals(Constants.UNSUBSCRIBE)) {
082                                    unsubscribeNode(actionRequest);
083                            }
084    
085                            sendRedirect(actionRequest, actionResponse);
086                    }
087                    catch (Exception e) {
088                            if (e instanceof NoSuchNodeException ||
089                                    e instanceof PrincipalException) {
090    
091                                    SessionErrors.add(actionRequest, e.getClass());
092    
093                                    setForward(actionRequest, "portlet.wiki.error");
094                            }
095                            else if (e instanceof DuplicateNodeNameException ||
096                                             e instanceof NodeNameException) {
097    
098                                    SessionErrors.add(actionRequest, e.getClass());
099                            }
100                            else {
101                                    throw e;
102                            }
103                    }
104            }
105    
106            @Override
107            public ActionForward render(
108                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
109                            RenderRequest renderRequest, RenderResponse renderResponse)
110                    throws Exception {
111    
112                    try {
113                            long nodeId = ParamUtil.getLong(renderRequest, "nodeId");
114    
115                            if (nodeId > 0) {
116                                    ActionUtil.getNode(renderRequest);
117                            }
118                    }
119                    catch (Exception e) {
120                            if (e instanceof NoSuchNodeException ||
121                                    e instanceof PrincipalException) {
122    
123                                    SessionErrors.add(renderRequest, e.getClass());
124    
125                                    return mapping.findForward("portlet.wiki.error");
126                            }
127                            else {
128                                    throw e;
129                            }
130                    }
131    
132                    return mapping.findForward(
133                            getForward(renderRequest, "portlet.wiki.edit_node"));
134            }
135    
136            protected void deleteNode(
137                            LiferayPortletConfig liferayPortletConfig,
138                            ActionRequest actionRequest, boolean moveToTrash)
139                    throws Exception {
140    
141                    long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
142    
143                    String oldName = getNodeName(nodeId);
144    
145                    WikiCacheThreadLocal.setClearCache(false);
146    
147                    if (moveToTrash) {
148                            WikiNodeServiceUtil.moveNodeToTrash(nodeId);
149                    }
150                    else {
151                            WikiNodeServiceUtil.deleteNode(nodeId);
152                    }
153    
154                    WikiCacheUtil.clearCache(nodeId);
155    
156                    WikiCacheThreadLocal.setClearCache(true);
157    
158                    updatePreferences(actionRequest, oldName, StringPool.BLANK);
159    
160                    if (moveToTrash) {
161                            Map<String, String[]> data = new HashMap<String, String[]>();
162    
163                            data.put("restoreEntryIds", new String[]{String.valueOf(nodeId)});
164    
165                            SessionMessages.add(
166                                    actionRequest,
167                                    liferayPortletConfig.getPortletId() +
168                                            SessionMessages.KEY_SUFFIX_DELETE_SUCCESS_DATA, data);
169    
170                            SessionMessages.add(
171                                    actionRequest,
172                                    liferayPortletConfig.getPortletId() +
173                                            SessionMessages.KEY_SUFFIX_HIDE_DEFAULT_SUCCESS_MESSAGE);
174                    }
175            }
176    
177            protected String getNodeName(long nodeId) throws Exception {
178                    WikiNode node = WikiNodeServiceUtil.getNode(nodeId);
179    
180                    return node.getName();
181            }
182    
183            protected void restoreNode(ActionRequest actionRequest) throws Exception {
184                    long[] restoreEntryIds = StringUtil.split(
185                            ParamUtil.getString(actionRequest, "restoreEntryIds"), 0L);
186    
187                    for (long restoreEntryId : restoreEntryIds) {
188                            WikiNodeServiceUtil.restoreNodeFromTrash(restoreEntryId);
189                    }
190            }
191    
192            protected void subscribeNode(ActionRequest actionRequest) throws Exception {
193                    long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
194    
195                    WikiNodeServiceUtil.subscribeNode(nodeId);
196            }
197    
198            protected void unsubscribeNode(ActionRequest actionRequest)
199                    throws Exception {
200    
201                    long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
202    
203                    WikiNodeServiceUtil.unsubscribeNode(nodeId);
204            }
205    
206            protected void updateNode(ActionRequest actionRequest) throws Exception {
207                    long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
208    
209                    String name = ParamUtil.getString(actionRequest, "name");
210                    String description = ParamUtil.getString(actionRequest, "description");
211    
212                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
213                            WikiNode.class.getName(), actionRequest);
214    
215                    if (nodeId <= 0) {
216    
217                            // Add node
218    
219                            WikiNodeServiceUtil.addNode(name, description, serviceContext);
220                    }
221                    else {
222    
223                            // Update node
224    
225                            String oldName = getNodeName(nodeId);
226    
227                            WikiNodeServiceUtil.updateNode(
228                                    nodeId, name, description, serviceContext);
229    
230                            updatePreferences(actionRequest, oldName, name);
231                    }
232            }
233    
234            protected void updatePreferences(
235                            ActionRequest actionRequest, String oldName, String newName)
236                    throws Exception {
237    
238                    PortletPreferences preferences = actionRequest.getPreferences();
239    
240                    String hiddenNodes = preferences.getValue(
241                            "hiddenNodes", StringPool.BLANK);
242                    String visibleNodes = preferences.getValue(
243                            "visibleNodes", StringPool.BLANK);
244    
245                    String regex = oldName + ",?";
246    
247                    preferences.setValue(
248                            "hiddenNodes", hiddenNodes.replaceFirst(regex, newName));
249                    preferences.setValue(
250                            "visibleNodes",
251                            visibleNodes.replaceFirst(regex, newName));
252    
253                    preferences.store();
254            }
255    
256    }