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