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