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.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.util.ParamUtil;
020    import com.liferay.portal.kernel.util.StringBundler;
021    import com.liferay.portal.kernel.util.Validator;
022    import com.liferay.portal.kernel.workflow.WorkflowThreadLocal;
023    import com.liferay.portal.model.Layout;
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.theme.ThemeDisplay;
028    import com.liferay.portal.util.PortalUtil;
029    import com.liferay.portal.util.WebKeys;
030    import com.liferay.portlet.wiki.NoSuchNodeException;
031    import com.liferay.portlet.wiki.NoSuchPageException;
032    import com.liferay.portlet.wiki.model.WikiNode;
033    import com.liferay.portlet.wiki.model.WikiPage;
034    import com.liferay.portlet.wiki.model.WikiPageConstants;
035    import com.liferay.portlet.wiki.service.WikiNodeLocalServiceUtil;
036    import com.liferay.portlet.wiki.service.WikiNodeServiceUtil;
037    import com.liferay.portlet.wiki.service.WikiPageLocalServiceUtil;
038    import com.liferay.portlet.wiki.service.WikiPageServiceUtil;
039    import com.liferay.portlet.wiki.util.WikiUtil;
040    
041    import javax.portlet.PortletRequest;
042    
043    import javax.servlet.http.HttpServletRequest;
044    
045    /**
046     * @author Brian Wing Shun Chan
047     * @author Jorge Ferrer
048     */
049    public class ActionUtil {
050    
051            public static WikiNode getFirstVisibleNode(PortletRequest portletRequest)
052                    throws PortalException, SystemException {
053    
054                    ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
055                            WebKeys.THEME_DISPLAY);
056    
057                    WikiNode node = null;
058    
059                    int nodesCount = WikiNodeLocalServiceUtil.getNodesCount(
060                            themeDisplay.getScopeGroupId());
061    
062                    if (nodesCount == 0) {
063                            Layout layout = themeDisplay.getLayout();
064    
065                            ServiceContext serviceContext = ServiceContextFactory.getInstance(
066                                    WikiNode.class.getName(), portletRequest);
067    
068                            serviceContext.setAddGroupPermissions(true);
069    
070                            if (layout.isPublicLayout()) {
071                                    serviceContext.setAddGuestPermissions(true);
072                            }
073                            else {
074                                    serviceContext.setAddGuestPermissions(false);
075                            }
076    
077                            node = WikiNodeLocalServiceUtil.addDefaultNode(
078                                    themeDisplay.getDefaultUserId(), serviceContext);
079                    }
080                    else {
081                            node = WikiUtil.getFirstNode(portletRequest);
082    
083                            if (node == null) {
084                                    throw new PrincipalException();
085                            }
086    
087                            return node;
088                    }
089    
090                    portletRequest.setAttribute(WebKeys.WIKI_NODE, node);
091    
092                    return node;
093            }
094    
095            public static WikiNode getNode(PortletRequest portletRequest)
096                    throws Exception {
097    
098                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
099                            portletRequest);
100    
101                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
102                            WebKeys.THEME_DISPLAY);
103    
104                    long nodeId = ParamUtil.getLong(portletRequest, "nodeId");
105                    String nodeName = ParamUtil.getString(portletRequest, "nodeName");
106    
107                    WikiNode node = null;
108    
109                    try {
110                            if (nodeId > 0) {
111                                    node = WikiNodeServiceUtil.getNode(nodeId);
112                            }
113                            else if (Validator.isNotNull(nodeName)) {
114                                    node = WikiNodeServiceUtil.getNode(
115                                            themeDisplay.getScopeGroupId(), nodeName);
116                            }
117                            else {
118                                    throw new NoSuchNodeException();
119                            }
120                    }
121                    catch (NoSuchNodeException nsne) {
122                            node = ActionUtil.getFirstVisibleNode(portletRequest);
123                    }
124    
125                    request.setAttribute(WebKeys.WIKI_NODE, node);
126    
127                    return node;
128            }
129    
130            public static void getPage(HttpServletRequest request) throws Exception {
131                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
132                            WebKeys.THEME_DISPLAY);
133    
134                    long nodeId = ParamUtil.getLong(request, "nodeId");
135                    String title = ParamUtil.getString(request, "title");
136                    double version = ParamUtil.getDouble(request, "version");
137    
138                    WikiNode node = null;
139    
140                    try {
141                            if (nodeId > 0) {
142                                    node = WikiNodeServiceUtil.getNode(nodeId);
143                            }
144                    }
145                    catch (NoSuchNodeException nsne) {
146                    }
147    
148                    if (node == null) {
149                            node = (WikiNode)request.getAttribute(WebKeys.WIKI_NODE);
150    
151                            if (node != null) {
152                                    nodeId = node.getNodeId();
153                            }
154                    }
155    
156                    if (Validator.isNull(title)) {
157                            title = WikiPageConstants.FRONT_PAGE;
158                    }
159    
160                    WikiPage page = null;
161    
162                    try {
163                            page = WikiPageServiceUtil.getPage(nodeId, title, version);
164    
165                            if (page.isDraft()) {
166                                    StringBundler sb = new StringBundler(7);
167    
168                                    sb.append("{nodeId=");
169                                    sb.append(nodeId);
170                                    sb.append(", title=");
171                                    sb.append(title);
172                                    sb.append(", version=");
173                                    sb.append(version);
174                                    sb.append("}");
175    
176                                    throw new NoSuchPageException(sb.toString());
177                            }
178                    }
179                    catch (NoSuchPageException nspe) {
180                            if (title.equals(WikiPageConstants.FRONT_PAGE) && (version == 0)) {
181                                    ServiceContext serviceContext = new ServiceContext();
182    
183                                    Layout layout = themeDisplay.getLayout();
184    
185                                    serviceContext.setAddGroupPermissions(true);
186    
187                                    if (layout.isPublicLayout()) {
188                                            serviceContext.setAddGuestPermissions(true);
189                                    }
190                                    else {
191                                            serviceContext.setAddGuestPermissions(false);
192                                    }
193    
194                                    boolean workflowEnabled = WorkflowThreadLocal.isEnabled();
195    
196                                    try {
197                                            WorkflowThreadLocal.setEnabled(false);
198    
199                                            page = WikiPageLocalServiceUtil.addPage(
200                                                    themeDisplay.getDefaultUserId(), nodeId, title, null,
201                                                    WikiPageConstants.NEW, true, serviceContext);
202                                    }
203                                    finally {
204                                            WorkflowThreadLocal.setEnabled(workflowEnabled);
205                                    }
206                            }
207                            else {
208                                    throw nspe;
209                            }
210                    }
211    
212                    request.setAttribute(WebKeys.WIKI_PAGE, page);
213            }
214    
215            public static void getPage(PortletRequest portletRequest) throws Exception {
216                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
217                            portletRequest);
218    
219                    getPage(request);
220            }
221    
222    }