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