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.bookmarks.service.impl;
016    
017    import com.liferay.portal.kernel.dao.orm.QueryDefinition;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.exception.SystemException;
020    import com.liferay.portal.kernel.workflow.WorkflowConstants;
021    import com.liferay.portal.security.permission.ActionKeys;
022    import com.liferay.portal.service.ServiceContext;
023    import com.liferay.portlet.bookmarks.model.BookmarksFolder;
024    import com.liferay.portlet.bookmarks.service.base.BookmarksFolderServiceBaseImpl;
025    import com.liferay.portlet.bookmarks.service.permission.BookmarksFolderPermission;
026    
027    import java.util.ArrayList;
028    import java.util.List;
029    
030    /**
031     * @author Brian Wing Shun Chan
032     */
033    public class BookmarksFolderServiceImpl extends BookmarksFolderServiceBaseImpl {
034    
035            @Override
036            public BookmarksFolder addFolder(
037                            long parentFolderId, String name, String description,
038                            ServiceContext serviceContext)
039                    throws PortalException, SystemException {
040    
041                    BookmarksFolderPermission.check(
042                            getPermissionChecker(), serviceContext.getScopeGroupId(),
043                            parentFolderId, ActionKeys.ADD_FOLDER);
044    
045                    return bookmarksFolderLocalService.addFolder(
046                            getUserId(), parentFolderId, name, description, serviceContext);
047            }
048    
049            @Override
050            public void deleteFolder(long folderId)
051                    throws PortalException, SystemException {
052    
053                    BookmarksFolder folder = bookmarksFolderLocalService.getFolder(
054                            folderId);
055    
056                    BookmarksFolderPermission.check(
057                            getPermissionChecker(), folder, ActionKeys.DELETE);
058    
059                    bookmarksFolderLocalService.deleteFolder(folderId);
060            }
061    
062            @Override
063            public void deleteFolder(long folderId, boolean includeTrashedEntries)
064                    throws PortalException, SystemException {
065    
066                    BookmarksFolder folder = bookmarksFolderLocalService.getFolder(
067                            folderId);
068    
069                    BookmarksFolderPermission.check(
070                            getPermissionChecker(), folder, ActionKeys.DELETE);
071    
072                    bookmarksFolderLocalService.deleteFolder(
073                            folderId, includeTrashedEntries);
074            }
075    
076            @Override
077            public BookmarksFolder getFolder(long folderId)
078                    throws PortalException, SystemException {
079    
080                    BookmarksFolder folder = bookmarksFolderLocalService.getFolder(
081                            folderId);
082    
083                    BookmarksFolderPermission.check(
084                            getPermissionChecker(), folder, ActionKeys.VIEW);
085    
086                    return folder;
087            }
088    
089            @Override
090            public List<Long> getFolderIds(long groupId, long folderId)
091                    throws PortalException, SystemException {
092    
093                    BookmarksFolderPermission.check(
094                            getPermissionChecker(), groupId, folderId, ActionKeys.VIEW);
095    
096                    List<Long> folderIds = getSubfolderIds(groupId, folderId, true);
097    
098                    folderIds.add(0, folderId);
099    
100                    return folderIds;
101            }
102    
103            @Override
104            public List<BookmarksFolder> getFolders(long groupId)
105                    throws SystemException {
106    
107                    return bookmarksFolderPersistence.filterFindByGroupId(groupId);
108            }
109    
110            @Override
111            public List<BookmarksFolder> getFolders(long groupId, long parentFolderId)
112                    throws SystemException {
113    
114                    return bookmarksFolderPersistence.filterFindByG_P_S(
115                            groupId, parentFolderId, WorkflowConstants.STATUS_APPROVED);
116            }
117    
118            @Override
119            public List<BookmarksFolder> getFolders(
120                            long groupId, long parentFolderId, int start, int end)
121                    throws SystemException {
122    
123                    return getFolders(
124                            groupId, parentFolderId, WorkflowConstants.STATUS_APPROVED, start,
125                            end);
126            }
127    
128            @Override
129            public List<BookmarksFolder> getFolders(
130                            long groupId, long parentFolderId, int status, int start, int end)
131                    throws SystemException {
132    
133                    if (status == WorkflowConstants.STATUS_ANY) {
134                            return bookmarksFolderPersistence.filterFindByG_P(
135                                    groupId, parentFolderId, start, end);
136                    }
137                    else {
138                            return bookmarksFolderPersistence.filterFindByG_P_S(
139                                    groupId, parentFolderId, status, start, end);
140                    }
141            }
142    
143            @Override
144            public List<Object> getFoldersAndEntries(long groupId, long folderId)
145                    throws SystemException {
146    
147                    return getFoldersAndEntries(
148                            groupId, folderId, WorkflowConstants.STATUS_ANY);
149            }
150    
151            @Override
152            public List<Object> getFoldersAndEntries(
153                            long groupId, long folderId, int status)
154                    throws SystemException {
155    
156                    QueryDefinition queryDefinition = new QueryDefinition(status);
157    
158                    return bookmarksFolderFinder.filterFindBF_E_ByG_F(
159                            groupId, folderId, queryDefinition);
160            }
161    
162            @Override
163            public List<Object> getFoldersAndEntries(
164                            long groupId, long folderId, int status, int start, int end)
165                    throws SystemException {
166    
167                    QueryDefinition queryDefinition = new QueryDefinition(
168                            status, start, end, null);
169    
170                    return bookmarksFolderFinder.filterFindBF_E_ByG_F(
171                            groupId, folderId, queryDefinition);
172            }
173    
174            @Override
175            public int getFoldersAndEntriesCount(long groupId, long folderId)
176                    throws SystemException {
177    
178                    return getFoldersAndEntriesCount(
179                            groupId, folderId, WorkflowConstants.STATUS_ANY);
180            }
181    
182            @Override
183            public int getFoldersAndEntriesCount(
184                            long groupId, long folderId, int status)
185                    throws SystemException {
186    
187                    QueryDefinition queryDefinition = new QueryDefinition(status);
188    
189                    return bookmarksFolderFinder.filterCountF_E_ByG_F(
190                            groupId, folderId, queryDefinition);
191            }
192    
193            @Override
194            public int getFoldersCount(long groupId, long parentFolderId)
195                    throws SystemException {
196    
197                    return getFoldersCount(
198                            groupId, parentFolderId, WorkflowConstants.STATUS_APPROVED);
199            }
200    
201            @Override
202            public int getFoldersCount(long groupId, long parentFolderId, int status)
203                    throws SystemException {
204    
205                    if (status == WorkflowConstants.STATUS_ANY) {
206                            return bookmarksFolderPersistence.filterCountByG_P_NotS(
207                                    groupId, parentFolderId, WorkflowConstants.STATUS_IN_TRASH);
208                    }
209                    else {
210                            return bookmarksFolderPersistence.filterCountByG_P_S(
211                                    groupId, parentFolderId, status);
212                    }
213            }
214    
215            @Override
216            public void getSubfolderIds(
217                            List<Long> folderIds, long groupId, long folderId)
218                    throws SystemException {
219    
220                    List<BookmarksFolder> folders =
221                            bookmarksFolderPersistence.filterFindByG_P_S(
222                                    groupId, folderId, WorkflowConstants.STATUS_APPROVED);
223    
224                    for (BookmarksFolder folder : folders) {
225                            if (folder.isInTrashContainer()) {
226                                    continue;
227                            }
228    
229                            folderIds.add(folder.getFolderId());
230    
231                            getSubfolderIds(
232                                    folderIds, folder.getGroupId(), folder.getFolderId());
233                    }
234            }
235    
236            @Override
237            public List<Long> getSubfolderIds(
238                            long groupId, long folderId, boolean recurse)
239                    throws SystemException {
240    
241                    List<Long> folderIds = new ArrayList<Long>();
242    
243                    getSubfolderIds(folderIds, groupId, folderId);
244    
245                    return folderIds;
246            }
247    
248            @Override
249            public BookmarksFolder moveFolder(long folderId, long parentFolderId)
250                    throws PortalException, SystemException {
251    
252                    BookmarksFolder folder = bookmarksFolderLocalService.getFolder(
253                            folderId);
254    
255                    BookmarksFolderPermission.check(
256                            getPermissionChecker(), folder, ActionKeys.UPDATE);
257    
258                    return bookmarksFolderLocalService.moveFolder(folderId, parentFolderId);
259            }
260    
261            @Override
262            public BookmarksFolder moveFolderFromTrash(
263                            long folderId, long parentFolderId)
264                    throws PortalException, SystemException {
265    
266                    BookmarksFolder folder = bookmarksFolderLocalService.getFolder(
267                            folderId);
268    
269                    BookmarksFolderPermission.check(
270                            getPermissionChecker(), folder, ActionKeys.UPDATE);
271    
272                    return bookmarksFolderLocalService.moveFolderFromTrash(
273                            getUserId(), folderId, parentFolderId);
274            }
275    
276            @Override
277            public BookmarksFolder moveFolderToTrash(long folderId)
278                    throws PortalException, SystemException {
279    
280                    BookmarksFolder folder = bookmarksFolderLocalService.getFolder(
281                            folderId);
282    
283                    BookmarksFolderPermission.check(
284                            getPermissionChecker(), folder, ActionKeys.DELETE);
285    
286                    return bookmarksFolderLocalService.moveFolderToTrash(
287                            getUserId(), folderId);
288            }
289    
290            @Override
291            public void restoreFolderFromTrash(long folderId)
292                    throws PortalException, SystemException {
293    
294                    BookmarksFolder folder = bookmarksFolderLocalService.getFolder(
295                            folderId);
296    
297                    BookmarksFolderPermission.check(
298                            getPermissionChecker(), folder, ActionKeys.UPDATE);
299    
300                    bookmarksFolderLocalService.restoreFolderFromTrash(
301                                    getUserId(), folderId);
302            }
303    
304            @Override
305            public void subscribeFolder(long groupId, long folderId)
306                    throws PortalException, SystemException {
307    
308                    BookmarksFolderPermission.check(
309                            getPermissionChecker(), groupId, folderId, ActionKeys.SUBSCRIBE);
310    
311                    bookmarksFolderLocalService.subscribeFolder(
312                            getUserId(), groupId, folderId);
313            }
314    
315            @Override
316            public void unsubscribeFolder(long groupId, long folderId)
317                    throws PortalException, SystemException {
318    
319                    BookmarksFolderPermission.check(
320                            getPermissionChecker(), groupId, folderId, ActionKeys.SUBSCRIBE);
321    
322                    bookmarksFolderLocalService.unsubscribeFolder(
323                            getUserId(), groupId, folderId);
324            }
325    
326            @Override
327            public BookmarksFolder updateFolder(
328                            long folderId, long parentFolderId, String name, String description,
329                            boolean mergeWithParentFolder, ServiceContext serviceContext)
330                    throws PortalException, SystemException {
331    
332                    BookmarksFolder folder = bookmarksFolderLocalService.getFolder(
333                            folderId);
334    
335                    BookmarksFolderPermission.check(
336                            getPermissionChecker(), folder, ActionKeys.UPDATE);
337    
338                    return bookmarksFolderLocalService.updateFolder(
339                            getUserId(), folderId, parentFolderId, name, description,
340                            mergeWithParentFolder, serviceContext);
341            }
342    
343    }