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.service.impl;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.workflow.WorkflowConstants;
019    import com.liferay.portal.security.permission.ActionKeys;
020    import com.liferay.portal.service.ServiceContext;
021    import com.liferay.portlet.wiki.model.WikiNode;
022    import com.liferay.portlet.wiki.service.base.WikiNodeServiceBaseImpl;
023    import com.liferay.portlet.wiki.service.permission.WikiNodePermission;
024    import com.liferay.portlet.wiki.service.permission.WikiPermission;
025    
026    import java.io.InputStream;
027    
028    import java.util.ArrayList;
029    import java.util.List;
030    import java.util.Map;
031    
032    /**
033     * Provides the remote service for accessing, adding, deleting, importing,
034     * subscription handling of, trash handling of, and updating wiki nodes. Its
035     * methods include permission checks.
036     *
037     * @author Brian Wing Shun Chan
038     * @author Charles May
039     */
040    public class WikiNodeServiceImpl extends WikiNodeServiceBaseImpl {
041    
042            @Override
043            public WikiNode addNode(
044                            String name, String description, ServiceContext serviceContext)
045                    throws PortalException {
046    
047                    WikiPermission.check(
048                            getPermissionChecker(), serviceContext.getScopeGroupId(),
049                            ActionKeys.ADD_NODE);
050    
051                    return wikiNodeLocalService.addNode(
052                            getUserId(), name, description, serviceContext);
053            }
054    
055            @Override
056            public void deleteNode(long nodeId) throws PortalException {
057                    WikiNodePermission.check(
058                            getPermissionChecker(), nodeId, ActionKeys.DELETE);
059    
060                    wikiNodeLocalService.deleteNode(nodeId);
061            }
062    
063            @Override
064            public WikiNode getNode(long nodeId) throws PortalException {
065                    WikiNodePermission.check(
066                            getPermissionChecker(), nodeId, ActionKeys.VIEW);
067    
068                    return wikiNodeLocalService.getNode(nodeId);
069            }
070    
071            @Override
072            public WikiNode getNode(long groupId, String name) throws PortalException {
073                    WikiNodePermission.check(
074                            getPermissionChecker(), groupId, name, ActionKeys.VIEW);
075    
076                    return wikiNodeLocalService.getNode(groupId, name);
077            }
078    
079            @Override
080            public List<WikiNode> getNodes(long groupId) throws PortalException {
081                    return getNodes(groupId, WorkflowConstants.STATUS_APPROVED);
082            }
083    
084            @Override
085            public List<WikiNode> getNodes(long groupId, int status)
086                    throws PortalException {
087    
088                    List<WikiNode> nodes = wikiNodePersistence.filterFindByG_S(
089                            groupId, status);
090    
091                    if (nodes.isEmpty()) {
092                            nodes = new ArrayList<WikiNode>();
093    
094                            List<WikiNode> allNodes = wikiNodeLocalService.getNodes(
095                                    groupId, status);
096    
097                            for (WikiNode node : allNodes) {
098                                    if (WikiNodePermission.contains(
099                                                    getPermissionChecker(), node, ActionKeys.VIEW)) {
100    
101                                            nodes.add(node);
102                                    }
103                            }
104                    }
105    
106                    return nodes;
107            }
108    
109            @Override
110            public List<WikiNode> getNodes(long groupId, int start, int end) {
111                    return getNodes(groupId, WorkflowConstants.STATUS_APPROVED, start, end);
112            }
113    
114            @Override
115            public List<WikiNode> getNodes(
116                    long groupId, int status, int start, int end) {
117    
118                    return wikiNodePersistence.filterFindByG_S(groupId, status, start, end);
119            }
120    
121            @Override
122            public int getNodesCount(long groupId) {
123                    return getNodesCount(groupId, WorkflowConstants.STATUS_APPROVED);
124            }
125    
126            @Override
127            public int getNodesCount(long groupId, int status) {
128                    return wikiNodePersistence.filterCountByG_S(groupId, status);
129            }
130    
131            @Override
132            public void importPages(
133                            long nodeId, String importer, InputStream[] inputStreams,
134                            Map<String, String[]> options)
135                    throws PortalException {
136    
137                    WikiNodePermission.check(
138                            getPermissionChecker(), nodeId, ActionKeys.IMPORT);
139    
140                    wikiNodeLocalService.importPages(
141                            getUserId(), nodeId, importer, inputStreams, options);
142            }
143    
144            @Override
145            public WikiNode moveNodeToTrash(long nodeId) throws PortalException {
146                    WikiNodePermission.check(
147                            getPermissionChecker(), nodeId, ActionKeys.DELETE);
148    
149                    return wikiNodeLocalService.moveNodeToTrash(getUserId(), nodeId);
150            }
151    
152            @Override
153            public void restoreNodeFromTrash(long nodeId) throws PortalException {
154                    WikiNode node = wikiNodeLocalService.getNode(nodeId);
155    
156                    WikiNodePermission.check(
157                            getPermissionChecker(), nodeId, ActionKeys.DELETE);
158    
159                    wikiNodeLocalService.restoreNodeFromTrash(getUserId(), node);
160            }
161    
162            @Override
163            public void subscribeNode(long nodeId) throws PortalException {
164                    WikiNodePermission.check(
165                            getPermissionChecker(), nodeId, ActionKeys.SUBSCRIBE);
166    
167                    wikiNodeLocalService.subscribeNode(getUserId(), nodeId);
168            }
169    
170            @Override
171            public void unsubscribeNode(long nodeId) throws PortalException {
172                    WikiNodePermission.check(
173                            getPermissionChecker(), nodeId, ActionKeys.SUBSCRIBE);
174    
175                    wikiNodeLocalService.unsubscribeNode(getUserId(), nodeId);
176            }
177    
178            @Override
179            public WikiNode updateNode(
180                            long nodeId, String name, String description,
181                            ServiceContext serviceContext)
182                    throws PortalException {
183    
184                    WikiNodePermission.check(
185                            getPermissionChecker(), nodeId, ActionKeys.UPDATE);
186    
187                    return wikiNodeLocalService.updateNode(
188                            nodeId, name, description, serviceContext);
189            }
190    
191    }