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