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                            folderIds.add(folder.getFolderId());
226    
227                            getSubfolderIds(
228                                    folderIds, folder.getGroupId(), folder.getFolderId());
229                    }
230            }
231    
232            @Override
233            public List<Long> getSubfolderIds(
234                            long groupId, long folderId, boolean recurse)
235                    throws SystemException {
236    
237                    List<Long> folderIds = new ArrayList<Long>();
238    
239                    getSubfolderIds(folderIds, groupId, folderId);
240    
241                    return folderIds;
242            }
243    
244            @Override
245            public BookmarksFolder moveFolder(long folderId, long parentFolderId)
246                    throws PortalException, SystemException {
247    
248                    BookmarksFolder folder = bookmarksFolderLocalService.getFolder(
249                            folderId);
250    
251                    BookmarksFolderPermission.check(
252                            getPermissionChecker(), folder, ActionKeys.UPDATE);
253    
254                    return bookmarksFolderLocalService.moveFolder(folderId, parentFolderId);
255            }
256    
257            @Override
258            public BookmarksFolder moveFolderFromTrash(
259                            long folderId, long parentFolderId)
260                    throws PortalException, SystemException {
261    
262                    BookmarksFolder folder = bookmarksFolderLocalService.getFolder(
263                            folderId);
264    
265                    BookmarksFolderPermission.check(
266                            getPermissionChecker(), folder, ActionKeys.UPDATE);
267    
268                    return bookmarksFolderLocalService.moveFolderFromTrash(
269                            getUserId(), folderId, parentFolderId);
270            }
271    
272            @Override
273            public BookmarksFolder moveFolderToTrash(long folderId)
274                    throws PortalException, SystemException {
275    
276                    BookmarksFolder folder = bookmarksFolderLocalService.getFolder(
277                            folderId);
278    
279                    BookmarksFolderPermission.check(
280                            getPermissionChecker(), folder, ActionKeys.DELETE);
281    
282                    return bookmarksFolderLocalService.moveFolderToTrash(
283                            getUserId(), folderId);
284            }
285    
286            @Override
287            public void restoreFolderFromTrash(long folderId)
288                    throws PortalException, SystemException {
289    
290                    BookmarksFolder folder = bookmarksFolderLocalService.getFolder(
291                            folderId);
292    
293                    BookmarksFolderPermission.check(
294                            getPermissionChecker(), folder, ActionKeys.UPDATE);
295    
296                    bookmarksFolderLocalService.restoreFolderFromTrash(
297                                    getUserId(), folderId);
298            }
299    
300            @Override
301            public void subscribeFolder(long groupId, long folderId)
302                    throws PortalException, SystemException {
303    
304                    BookmarksFolderPermission.check(
305                            getPermissionChecker(), groupId, folderId, ActionKeys.SUBSCRIBE);
306    
307                    bookmarksFolderLocalService.subscribeFolder(
308                            getUserId(), groupId, folderId);
309            }
310    
311            @Override
312            public void unsubscribeFolder(long groupId, long folderId)
313                    throws PortalException, SystemException {
314    
315                    BookmarksFolderPermission.check(
316                            getPermissionChecker(), groupId, folderId, ActionKeys.SUBSCRIBE);
317    
318                    bookmarksFolderLocalService.unsubscribeFolder(
319                            getUserId(), groupId, folderId);
320            }
321    
322            @Override
323            public BookmarksFolder updateFolder(
324                            long folderId, long parentFolderId, String name, String description,
325                            boolean mergeWithParentFolder, ServiceContext serviceContext)
326                    throws PortalException, SystemException {
327    
328                    BookmarksFolder folder = bookmarksFolderLocalService.getFolder(
329                            folderId);
330    
331                    BookmarksFolderPermission.check(
332                            getPermissionChecker(), folder, ActionKeys.UPDATE);
333    
334                    return bookmarksFolderLocalService.updateFolder(
335                            getUserId(), folderId, parentFolderId, name, description,
336                            mergeWithParentFolder, serviceContext);
337            }
338    
339    }