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.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
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            public BookmarksEntry addEntry(
041                            long groupId, long folderId, String name, String url,
042                            String description, ServiceContext serviceContext)
043                    throws PortalException, SystemException {
044    
045                    BookmarksFolderPermission.check(
046                            getPermissionChecker(), groupId, folderId, ActionKeys.ADD_ENTRY);
047    
048                    return bookmarksEntryLocalService.addEntry(
049                            getUserId(), groupId, folderId, name, url, description,
050                            serviceContext);
051            }
052    
053            public void deleteEntry(long entryId)
054                    throws PortalException, SystemException {
055    
056                    BookmarksEntryPermission.check(
057                            getPermissionChecker(), entryId, ActionKeys.DELETE);
058    
059                    bookmarksEntryLocalService.deleteEntry(entryId);
060            }
061    
062            public List<BookmarksEntry> getEntries(
063                            long groupId, long folderId, int start, int end)
064                    throws SystemException {
065    
066                    return bookmarksEntryPersistence.filterFindByG_F_S(
067                            groupId, folderId, WorkflowConstants.STATUS_APPROVED, start, end);
068            }
069    
070            public List<BookmarksEntry> getEntries(
071                            long groupId, long folderId, int start, int end,
072                            OrderByComparator orderByComparator)
073                    throws SystemException {
074    
075                    return bookmarksEntryPersistence.filterFindByG_F_S(
076                            groupId, folderId, WorkflowConstants.STATUS_APPROVED, start, end,
077                            orderByComparator);
078            }
079    
080            public int getEntriesCount(long groupId, long folderId)
081                    throws SystemException {
082    
083                    return bookmarksEntryPersistence.filterCountByG_F_S(
084                            groupId, folderId, WorkflowConstants.STATUS_APPROVED);
085            }
086    
087            public BookmarksEntry getEntry(long entryId)
088                    throws PortalException, SystemException {
089    
090                    BookmarksEntryPermission.check(
091                            getPermissionChecker(), entryId, ActionKeys.VIEW);
092    
093                    return bookmarksEntryLocalService.getEntry(entryId);
094            }
095    
096            public int getFoldersEntriesCount(long groupId, List<Long> folderIds)
097                    throws SystemException {
098    
099                    return bookmarksEntryPersistence.filterCountByG_F_S(
100                            groupId,
101                            ArrayUtil.toArray(folderIds.toArray(new Long[folderIds.size()])),
102                            WorkflowConstants.STATUS_APPROVED);
103            }
104    
105            public List<BookmarksEntry> getGroupEntries(
106                            long groupId, int start, int end)
107                    throws PortalException, SystemException {
108    
109                    return getGroupEntries(
110                            groupId, 0, WorkflowConstants.STATUS_APPROVED, start, end);
111            }
112    
113            public List<BookmarksEntry> getGroupEntries(
114                            long groupId, long userId, int start, int end)
115                    throws PortalException, SystemException {
116    
117                    return getGroupEntries(
118                            groupId, userId, BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID,
119                            start, end);
120            }
121    
122            public List<BookmarksEntry> getGroupEntries(
123                            long groupId, long userId, long rootFolderId, int start, int end)
124                    throws PortalException, SystemException {
125    
126                    List<Long> folderIds = bookmarksFolderService.getFolderIds(
127                            groupId, rootFolderId);
128    
129                    if (folderIds.size() == 0) {
130                            return Collections.emptyList();
131                    }
132                    else if (userId <= 0) {
133                            return bookmarksEntryPersistence.filterFindByG_F_S(
134                                    groupId, ArrayUtil.toLongArray(folderIds),
135                                    WorkflowConstants.STATUS_APPROVED, start, end,
136                                    new EntryModifiedDateComparator());
137                    }
138                    else {
139                            return bookmarksEntryPersistence.filterFindByG_U_F_S(
140                                    groupId, userId, ArrayUtil.toLongArray(folderIds),
141                                    WorkflowConstants.STATUS_APPROVED, start, end,
142                                    new EntryModifiedDateComparator());
143                    }
144            }
145    
146            public int getGroupEntriesCount(long groupId)
147                    throws PortalException, SystemException {
148    
149                    return getGroupEntriesCount(groupId, 0);
150            }
151    
152            public int getGroupEntriesCount(long groupId, long userId)
153                    throws PortalException, SystemException {
154    
155                    return getGroupEntriesCount(
156                            groupId, userId, BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID);
157            }
158    
159            public int getGroupEntriesCount(
160                            long groupId, long userId, long rootFolderId)
161                    throws PortalException, SystemException {
162    
163                    List<Long> folderIds = bookmarksFolderService.getFolderIds(
164                            groupId, rootFolderId);
165    
166                    if (folderIds.size() == 0) {
167                            return 0;
168                    }
169                    else if (userId <= 0) {
170                            return bookmarksEntryPersistence.filterCountByG_F_S(
171                                    groupId, ArrayUtil.toLongArray(folderIds),
172                                    WorkflowConstants.STATUS_APPROVED);
173                    }
174                    else {
175                            return bookmarksEntryPersistence.filterCountByG_U_F_S(
176                                    groupId, userId, ArrayUtil.toLongArray(folderIds),
177                                    WorkflowConstants.STATUS_APPROVED);
178                    }
179            }
180    
181            public BookmarksEntry moveEntry(long entryId, long parentFolderId)
182                    throws PortalException, SystemException {
183    
184                    BookmarksEntryPermission.check(
185                            getPermissionChecker(), entryId, ActionKeys.UPDATE);
186    
187                    return bookmarksEntryLocalService.moveEntry(entryId, parentFolderId);
188            }
189    
190            public BookmarksEntry moveEntryFromTrash(long entryId, long parentFolderId)
191                    throws PortalException, SystemException {
192    
193                    BookmarksEntryPermission.check(
194                            getPermissionChecker(), entryId, ActionKeys.UPDATE);
195    
196                    return bookmarksEntryLocalService.moveEntryFromTrash(
197                            getUserId(), entryId, parentFolderId);
198            }
199    
200            public void moveEntryToTrash(long entryId)
201                    throws PortalException, SystemException {
202    
203                    BookmarksEntryPermission.check(
204                            getPermissionChecker(), entryId, ActionKeys.DELETE);
205    
206                    bookmarksEntryLocalService.moveEntryToTrash(getUserId(), entryId);
207            }
208    
209            public BookmarksEntry openEntry(BookmarksEntry entry)
210                    throws PortalException, SystemException {
211    
212                    BookmarksEntryPermission.check(
213                            getPermissionChecker(), entry, ActionKeys.VIEW);
214    
215                    return bookmarksEntryLocalService.openEntry(getGuestOrUserId(), entry);
216            }
217    
218            public BookmarksEntry openEntry(long entryId)
219                    throws PortalException, SystemException {
220    
221                    BookmarksEntryPermission.check(
222                            getPermissionChecker(), entryId, ActionKeys.VIEW);
223    
224                    return bookmarksEntryLocalService.openEntry(
225                            getGuestOrUserId(), entryId);
226            }
227    
228            public void restoreEntryFromTrash(long entryId)
229                    throws PortalException, SystemException {
230    
231                    BookmarksEntryPermission.check(
232                            getPermissionChecker(), entryId, ActionKeys.UPDATE);
233    
234                    bookmarksEntryLocalService.restoreEntryFromTrash(getUserId(), entryId);
235            }
236    
237            public void subscribeEntry(long entryId)
238                    throws PortalException, SystemException {
239    
240                    BookmarksEntryPermission.check(
241                            getPermissionChecker(), entryId, ActionKeys.SUBSCRIBE);
242    
243                    bookmarksEntryLocalService.subscribeEntry(getUserId(), entryId);
244            }
245    
246            public void unsubscribeEntry(long entryId)
247                    throws PortalException, SystemException {
248    
249                    BookmarksEntryPermission.check(
250                            getPermissionChecker(), entryId, ActionKeys.SUBSCRIBE);
251    
252                    bookmarksEntryLocalService.unsubscribeEntry(getUserId(), entryId);
253            }
254    
255            public BookmarksEntry updateEntry(
256                            long entryId, long groupId, long folderId, String name, String url,
257                            String description, ServiceContext serviceContext)
258                    throws PortalException, SystemException {
259    
260                    BookmarksEntryPermission.check(
261                            getPermissionChecker(), entryId, ActionKeys.UPDATE);
262    
263                    return bookmarksEntryLocalService.updateEntry(
264                            getUserId(), entryId, groupId, folderId, name, url, description,
265                            serviceContext);
266            }
267    
268    }