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.bookmarks.service.impl;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.search.Hits;
019    import com.liferay.portal.kernel.util.ArrayUtil;
020    import com.liferay.portal.kernel.util.OrderByComparator;
021    import com.liferay.portal.kernel.workflow.WorkflowConstants;
022    import com.liferay.portal.security.permission.ActionKeys;
023    import com.liferay.portal.service.ServiceContext;
024    import com.liferay.portlet.bookmarks.model.BookmarksEntry;
025    import com.liferay.portlet.bookmarks.model.BookmarksFolderConstants;
026    import com.liferay.portlet.bookmarks.service.base.BookmarksEntryServiceBaseImpl;
027    import com.liferay.portlet.bookmarks.service.permission.BookmarksEntryPermission;
028    import com.liferay.portlet.bookmarks.service.permission.BookmarksFolderPermission;
029    import com.liferay.portlet.bookmarks.util.comparator.EntryModifiedDateComparator;
030    
031    import java.util.Collections;
032    import java.util.List;
033    
034    /**
035     * @author Brian Wing Shun Chan
036     * @author Levente Hud??k
037     */
038    public class BookmarksEntryServiceImpl extends BookmarksEntryServiceBaseImpl {
039    
040            @Override
041            public BookmarksEntry addEntry(
042                            long groupId, long folderId, String name, String url,
043                            String description, ServiceContext serviceContext)
044                    throws PortalException {
045    
046                    BookmarksFolderPermission.check(
047                            getPermissionChecker(), groupId, folderId, ActionKeys.ADD_ENTRY);
048    
049                    return bookmarksEntryLocalService.addEntry(
050                            getUserId(), groupId, folderId, name, url, description,
051                            serviceContext);
052            }
053    
054            @Override
055            public void deleteEntry(long entryId) throws PortalException {
056                    BookmarksEntryPermission.check(
057                            getPermissionChecker(), entryId, ActionKeys.DELETE);
058    
059                    bookmarksEntryLocalService.deleteEntry(entryId);
060            }
061    
062            @Override
063            public List<BookmarksEntry> getEntries(
064                    long groupId, long folderId, int start, int end) {
065    
066                    return bookmarksEntryPersistence.filterFindByG_F_S(
067                            groupId, folderId, WorkflowConstants.STATUS_APPROVED, start, end);
068            }
069    
070            @Override
071            public List<BookmarksEntry> getEntries(
072                    long groupId, long folderId, int start, int end,
073                    OrderByComparator<BookmarksEntry> orderByComparator) {
074    
075                    return bookmarksEntryPersistence.filterFindByG_F_S(
076                            groupId, folderId, WorkflowConstants.STATUS_APPROVED, start, end,
077                            orderByComparator);
078            }
079    
080            @Override
081            public int getEntriesCount(long groupId, long folderId) {
082                    return getEntriesCount(
083                            groupId, folderId, WorkflowConstants.STATUS_APPROVED);
084            }
085    
086            @Override
087            public int getEntriesCount(long groupId, long folderId, int status) {
088                    if (status == WorkflowConstants.STATUS_ANY) {
089                            return bookmarksEntryPersistence.filterCountByG_F_NotS(
090                                    groupId, folderId, WorkflowConstants.STATUS_IN_TRASH);
091                    }
092                    else {
093                            return bookmarksEntryPersistence.filterCountByG_F_S(
094                                    groupId, folderId, status);
095                    }
096            }
097    
098            @Override
099            public BookmarksEntry getEntry(long entryId) throws PortalException {
100                    BookmarksEntryPermission.check(
101                            getPermissionChecker(), entryId, ActionKeys.VIEW);
102    
103                    return bookmarksEntryLocalService.getEntry(entryId);
104            }
105    
106            @Override
107            public int getFoldersEntriesCount(long groupId, List<Long> folderIds) {
108                    return bookmarksEntryPersistence.filterCountByG_F_S(
109                            groupId,
110                            ArrayUtil.toArray(folderIds.toArray(new Long[folderIds.size()])),
111                            WorkflowConstants.STATUS_APPROVED);
112            }
113    
114            @Override
115            public List<BookmarksEntry> getGroupEntries(
116                            long groupId, int start, int end)
117                    throws PortalException {
118    
119                    return getGroupEntries(
120                            groupId, 0, WorkflowConstants.STATUS_APPROVED, start, end);
121            }
122    
123            @Override
124            public List<BookmarksEntry> getGroupEntries(
125                            long groupId, long userId, int start, int end)
126                    throws PortalException {
127    
128                    return getGroupEntries(
129                            groupId, userId, BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID,
130                            start, end);
131            }
132    
133            @Override
134            public List<BookmarksEntry> getGroupEntries(
135                            long groupId, long userId, long rootFolderId, int start, int end)
136                    throws PortalException {
137    
138                    if (rootFolderId == BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
139                            if (userId <= 0) {
140                                    return bookmarksEntryPersistence.filterFindByG_NotS(
141                                            groupId, WorkflowConstants.STATUS_IN_TRASH, start, end,
142                                            new EntryModifiedDateComparator());
143                            }
144                            else {
145                                    return bookmarksEntryPersistence.filterFindByG_U_NotS(
146                                            groupId, userId, WorkflowConstants.STATUS_IN_TRASH, start,
147                                            end, new EntryModifiedDateComparator());
148                            }
149                    }
150    
151                    List<Long> folderIds = bookmarksFolderService.getFolderIds(
152                            groupId, rootFolderId);
153    
154                    if (folderIds.isEmpty()) {
155                            return Collections.emptyList();
156                    }
157                    else if (userId <= 0) {
158                            return bookmarksEntryPersistence.filterFindByG_F_S(
159                                    groupId, ArrayUtil.toLongArray(folderIds),
160                                    WorkflowConstants.STATUS_APPROVED, start, end,
161                                    new EntryModifiedDateComparator());
162                    }
163                    else {
164                            return bookmarksEntryPersistence.filterFindByG_U_F_S(
165                                    groupId, userId, ArrayUtil.toLongArray(folderIds),
166                                    WorkflowConstants.STATUS_APPROVED, start, end,
167                                    new EntryModifiedDateComparator());
168                    }
169            }
170    
171            @Override
172            public int getGroupEntriesCount(long groupId) throws PortalException {
173                    return getGroupEntriesCount(groupId, 0);
174            }
175    
176            @Override
177            public int getGroupEntriesCount(long groupId, long userId)
178                    throws PortalException {
179    
180                    return getGroupEntriesCount(
181                            groupId, userId, BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID);
182            }
183    
184            @Override
185            public int getGroupEntriesCount(
186                            long groupId, long userId, long rootFolderId)
187                    throws PortalException {
188    
189                    if (rootFolderId == BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
190                            if (userId <= 0) {
191                                    return bookmarksEntryPersistence.filterCountByG_NotS(
192                                            groupId, WorkflowConstants.STATUS_IN_TRASH);
193                            }
194                            else {
195                                    return bookmarksEntryPersistence.filterCountByG_U_NotS(
196                                            groupId, userId, WorkflowConstants.STATUS_IN_TRASH);
197                            }
198                    }
199    
200                    List<Long> folderIds = bookmarksFolderService.getFolderIds(
201                            groupId, rootFolderId);
202    
203                    if (folderIds.isEmpty()) {
204                            return 0;
205                    }
206                    else if (userId <= 0) {
207                            return bookmarksEntryPersistence.filterCountByG_F_S(
208                                    groupId, ArrayUtil.toLongArray(folderIds),
209                                    WorkflowConstants.STATUS_APPROVED);
210                    }
211                    else {
212                            return bookmarksEntryPersistence.filterCountByG_U_F_S(
213                                    groupId, userId, ArrayUtil.toLongArray(folderIds),
214                                    WorkflowConstants.STATUS_APPROVED);
215                    }
216            }
217    
218            @Override
219            public BookmarksEntry moveEntry(long entryId, long parentFolderId)
220                    throws PortalException {
221    
222                    BookmarksEntryPermission.check(
223                            getPermissionChecker(), entryId, ActionKeys.UPDATE);
224    
225                    return bookmarksEntryLocalService.moveEntry(entryId, parentFolderId);
226            }
227    
228            @Override
229            public BookmarksEntry moveEntryFromTrash(long entryId, long parentFolderId)
230                    throws PortalException {
231    
232                    BookmarksEntryPermission.check(
233                            getPermissionChecker(), entryId, ActionKeys.UPDATE);
234    
235                    return bookmarksEntryLocalService.moveEntryFromTrash(
236                            getUserId(), entryId, parentFolderId);
237            }
238    
239            @Override
240            public BookmarksEntry moveEntryToTrash(long entryId)
241                    throws PortalException {
242    
243                    BookmarksEntryPermission.check(
244                            getPermissionChecker(), entryId, ActionKeys.DELETE);
245    
246                    return bookmarksEntryLocalService.moveEntryToTrash(
247                            getUserId(), entryId);
248            }
249    
250            @Override
251            public BookmarksEntry openEntry(BookmarksEntry entry)
252                    throws PortalException {
253    
254                    BookmarksEntryPermission.check(
255                            getPermissionChecker(), entry, ActionKeys.VIEW);
256    
257                    return bookmarksEntryLocalService.openEntry(getGuestOrUserId(), entry);
258            }
259    
260            @Override
261            public BookmarksEntry openEntry(long entryId) throws PortalException {
262                    BookmarksEntryPermission.check(
263                            getPermissionChecker(), entryId, ActionKeys.VIEW);
264    
265                    return bookmarksEntryLocalService.openEntry(
266                            getGuestOrUserId(), entryId);
267            }
268    
269            @Override
270            public void restoreEntryFromTrash(long entryId) throws PortalException {
271                    BookmarksEntryPermission.check(
272                            getPermissionChecker(), entryId, ActionKeys.UPDATE);
273    
274                    bookmarksEntryLocalService.restoreEntryFromTrash(getUserId(), entryId);
275            }
276    
277            @Override
278            public Hits search(
279                            long groupId, long creatorUserId, int status, int start, int end)
280                    throws PortalException {
281    
282                    return bookmarksEntryLocalService.search(
283                            groupId, getUserId(), creatorUserId, status, start, end);
284            }
285    
286            @Override
287            public void subscribeEntry(long entryId) throws PortalException {
288                    BookmarksEntryPermission.check(
289                            getPermissionChecker(), entryId, ActionKeys.SUBSCRIBE);
290    
291                    bookmarksEntryLocalService.subscribeEntry(getUserId(), entryId);
292            }
293    
294            @Override
295            public void unsubscribeEntry(long entryId) throws PortalException {
296                    BookmarksEntryPermission.check(
297                            getPermissionChecker(), entryId, ActionKeys.SUBSCRIBE);
298    
299                    bookmarksEntryLocalService.unsubscribeEntry(getUserId(), entryId);
300            }
301    
302            @Override
303            public BookmarksEntry updateEntry(
304                            long entryId, long groupId, long folderId, String name, String url,
305                            String description, ServiceContext serviceContext)
306                    throws PortalException {
307    
308                    BookmarksEntryPermission.check(
309                            getPermissionChecker(), entryId, ActionKeys.UPDATE);
310    
311                    return bookmarksEntryLocalService.updateEntry(
312                            getUserId(), entryId, groupId, folderId, name, url, description,
313                            serviceContext);
314            }
315    
316    }