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            /**
216             * @deprecated As of 7.0.0, replaced by {@link #getSubfolderIds(List, long,
217             *             long, boolean)}
218             */
219            @Deprecated
220            @Override
221            public void getSubfolderIds(
222                            List<Long> folderIds, long groupId, long folderId)
223                    throws SystemException {
224    
225                    getSubfolderIds(folderIds, groupId, folderId, true);
226            }
227    
228            @Override
229            public void getSubfolderIds(
230                            List<Long> folderIds, long groupId, long folderId, boolean recurse)
231                    throws SystemException {
232    
233                    List<BookmarksFolder> folders =
234                            bookmarksFolderPersistence.filterFindByG_P_S(
235                                    groupId, folderId, WorkflowConstants.STATUS_APPROVED);
236    
237                    for (BookmarksFolder folder : folders) {
238                            folderIds.add(folder.getFolderId());
239    
240                            if (recurse) {
241                                    getSubfolderIds(
242                                            folderIds, folder.getGroupId(), folder.getFolderId(),
243                                            recurse);
244                            }
245                    }
246            }
247    
248            @Override
249            public List<Long> getSubfolderIds(
250                            long groupId, long folderId, boolean recurse)
251                    throws SystemException {
252    
253                    List<Long> folderIds = new ArrayList<Long>();
254    
255                    getSubfolderIds(folderIds, groupId, folderId, recurse);
256    
257                    return folderIds;
258            }
259    
260            @Override
261            public BookmarksFolder moveFolder(long folderId, long parentFolderId)
262                    throws PortalException, SystemException {
263    
264                    BookmarksFolder folder = bookmarksFolderLocalService.getFolder(
265                            folderId);
266    
267                    BookmarksFolderPermission.check(
268                            getPermissionChecker(), folder, ActionKeys.UPDATE);
269    
270                    return bookmarksFolderLocalService.moveFolder(folderId, parentFolderId);
271            }
272    
273            @Override
274            public BookmarksFolder moveFolderFromTrash(
275                            long folderId, long parentFolderId)
276                    throws PortalException, SystemException {
277    
278                    BookmarksFolder folder = bookmarksFolderLocalService.getFolder(
279                            folderId);
280    
281                    BookmarksFolderPermission.check(
282                            getPermissionChecker(), folder, ActionKeys.UPDATE);
283    
284                    return bookmarksFolderLocalService.moveFolderFromTrash(
285                            getUserId(), folderId, parentFolderId);
286            }
287    
288            @Override
289            public BookmarksFolder moveFolderToTrash(long folderId)
290                    throws PortalException, SystemException {
291    
292                    BookmarksFolder folder = bookmarksFolderLocalService.getFolder(
293                            folderId);
294    
295                    BookmarksFolderPermission.check(
296                            getPermissionChecker(), folder, ActionKeys.DELETE);
297    
298                    return bookmarksFolderLocalService.moveFolderToTrash(
299                            getUserId(), folderId);
300            }
301    
302            @Override
303            public void restoreFolderFromTrash(long folderId)
304                    throws PortalException, SystemException {
305    
306                    BookmarksFolder folder = bookmarksFolderLocalService.getFolder(
307                            folderId);
308    
309                    BookmarksFolderPermission.check(
310                            getPermissionChecker(), folder, ActionKeys.UPDATE);
311    
312                    bookmarksFolderLocalService.restoreFolderFromTrash(
313                                    getUserId(), folderId);
314            }
315    
316            @Override
317            public void subscribeFolder(long groupId, long folderId)
318                    throws PortalException, SystemException {
319    
320                    BookmarksFolderPermission.check(
321                            getPermissionChecker(), groupId, folderId, ActionKeys.SUBSCRIBE);
322    
323                    bookmarksFolderLocalService.subscribeFolder(
324                            getUserId(), groupId, folderId);
325            }
326    
327            @Override
328            public void unsubscribeFolder(long groupId, long folderId)
329                    throws PortalException, SystemException {
330    
331                    BookmarksFolderPermission.check(
332                            getPermissionChecker(), groupId, folderId, ActionKeys.SUBSCRIBE);
333    
334                    bookmarksFolderLocalService.unsubscribeFolder(
335                            getUserId(), groupId, folderId);
336            }
337    
338            @Override
339            public BookmarksFolder updateFolder(
340                            long folderId, long parentFolderId, String name, String description,
341                            boolean mergeWithParentFolder, ServiceContext serviceContext)
342                    throws PortalException, SystemException {
343    
344                    BookmarksFolder folder = bookmarksFolderLocalService.getFolder(
345                            folderId);
346    
347                    BookmarksFolderPermission.check(
348                            getPermissionChecker(), folder, ActionKeys.UPDATE);
349    
350                    return bookmarksFolderLocalService.updateFolder(
351                            getUserId(), folderId, parentFolderId, name, description,
352                            mergeWithParentFolder, serviceContext);
353            }
354    
355    }