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