001    /**
002     * Copyright (c) 2000-2010 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.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.service.UserLocalServiceUtil;
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.setAddCommunityPermissions(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.getUserId(), 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(request, "nodeId");
105                    String nodeName = ParamUtil.getString(request, "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                    catch (NoSuchPageException nspe) {
166                            if (title.equals(WikiPageConstants.FRONT_PAGE) && (version == 0)) {
167                                    long userId = PortalUtil.getUserId(request);
168    
169                                    if (userId == 0) {
170                                            long companyId = PortalUtil.getCompanyId(request);
171    
172                                            userId = UserLocalServiceUtil.getDefaultUserId(companyId);
173                                    }
174    
175                                    ServiceContext serviceContext = new ServiceContext();
176    
177                                    Layout layout = themeDisplay.getLayout();
178    
179                                    serviceContext.setAddCommunityPermissions(true);
180    
181                                    if (layout.isPublicLayout()) {
182                                            serviceContext.setAddGuestPermissions(true);
183                                    }
184                                    else {
185                                            serviceContext.setAddGuestPermissions(false);
186                                    }
187    
188                                    boolean workflowEnabled = WorkflowThreadLocal.isEnabled();
189    
190                                    try {
191                                            WorkflowThreadLocal.setEnabled(false);
192    
193                                            page = WikiPageLocalServiceUtil.addPage(
194                                                    userId, nodeId, title, null, WikiPageConstants.NEW,
195                                                    true, serviceContext);
196                                    }
197                                    finally {
198                                            WorkflowThreadLocal.setEnabled(workflowEnabled);
199                                    }
200                            }
201                            else {
202                                    throw nspe;
203                            }
204                    }
205    
206                    request.setAttribute(WebKeys.WIKI_PAGE, page);
207            }
208    
209            public static void getPage(PortletRequest portletRequest) throws Exception {
210                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
211                            portletRequest);
212    
213                    getPage(request);
214            }
215    
216    }