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