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                    List<Long> folderIds = bookmarksFolderService.getFolderIds(
152                            groupId, rootFolderId);
153    
154                    if (folderIds.size() == 0) {
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)
173                    throws PortalException, SystemException {
174    
175                    return getGroupEntriesCount(groupId, 0);
176            }
177    
178            @Override
179            public int getGroupEntriesCount(long groupId, long userId)
180                    throws PortalException, SystemException {
181    
182                    return getGroupEntriesCount(
183                            groupId, userId, BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID);
184            }
185    
186            @Override
187            public int getGroupEntriesCount(
188                            long groupId, long userId, long rootFolderId)
189                    throws PortalException, SystemException {
190    
191                    List<Long> folderIds = bookmarksFolderService.getFolderIds(
192                            groupId, rootFolderId);
193    
194                    if (folderIds.size() == 0) {
195                            return 0;
196                    }
197                    else if (userId <= 0) {
198                            return bookmarksEntryPersistence.filterCountByG_F_S(
199                                    groupId, ArrayUtil.toLongArray(folderIds),
200                                    WorkflowConstants.STATUS_APPROVED);
201                    }
202                    else {
203                            return bookmarksEntryPersistence.filterCountByG_U_F_S(
204                                    groupId, userId, ArrayUtil.toLongArray(folderIds),
205                                    WorkflowConstants.STATUS_APPROVED);
206                    }
207            }
208    
209            @Override
210            public BookmarksEntry moveEntry(long entryId, long parentFolderId)
211                    throws PortalException, SystemException {
212    
213                    BookmarksEntryPermission.check(
214                            getPermissionChecker(), entryId, ActionKeys.UPDATE);
215    
216                    return bookmarksEntryLocalService.moveEntry(entryId, parentFolderId);
217            }
218    
219            @Override
220            public BookmarksEntry moveEntryFromTrash(long entryId, long parentFolderId)
221                    throws PortalException, SystemException {
222    
223                    BookmarksEntryPermission.check(
224                            getPermissionChecker(), entryId, ActionKeys.UPDATE);
225    
226                    return bookmarksEntryLocalService.moveEntryFromTrash(
227                            getUserId(), entryId, parentFolderId);
228            }
229    
230            @Override
231            public BookmarksEntry moveEntryToTrash(long entryId)
232                    throws PortalException, SystemException {
233    
234                    BookmarksEntryPermission.check(
235                            getPermissionChecker(), entryId, ActionKeys.DELETE);
236    
237                    return bookmarksEntryLocalService.moveEntryToTrash(
238                            getUserId(), entryId);
239            }
240    
241            @Override
242            public BookmarksEntry openEntry(BookmarksEntry entry)
243                    throws PortalException, SystemException {
244    
245                    BookmarksEntryPermission.check(
246                            getPermissionChecker(), entry, ActionKeys.VIEW);
247    
248                    return bookmarksEntryLocalService.openEntry(getGuestOrUserId(), entry);
249            }
250    
251            @Override
252            public BookmarksEntry openEntry(long entryId)
253                    throws PortalException, SystemException {
254    
255                    BookmarksEntryPermission.check(
256                            getPermissionChecker(), entryId, ActionKeys.VIEW);
257    
258                    return bookmarksEntryLocalService.openEntry(
259                            getGuestOrUserId(), entryId);
260            }
261    
262            @Override
263            public void restoreEntryFromTrash(long entryId)
264                    throws PortalException, SystemException {
265    
266                    BookmarksEntryPermission.check(
267                            getPermissionChecker(), entryId, ActionKeys.UPDATE);
268    
269                    bookmarksEntryLocalService.restoreEntryFromTrash(getUserId(), entryId);
270            }
271    
272            @Override
273            public Hits search(
274                            long groupId, long creatorUserId, int status, int start, int end)
275                    throws PortalException, SystemException {
276    
277                    return bookmarksEntryLocalService.search(
278                            groupId, getUserId(), creatorUserId, status, start, end);
279            }
280    
281            @Override
282            public void subscribeEntry(long entryId)
283                    throws PortalException, SystemException {
284    
285                    BookmarksEntryPermission.check(
286                            getPermissionChecker(), entryId, ActionKeys.SUBSCRIBE);
287    
288                    bookmarksEntryLocalService.subscribeEntry(getUserId(), entryId);
289            }
290    
291            @Override
292            public void unsubscribeEntry(long entryId)
293                    throws PortalException, SystemException {
294    
295                    BookmarksEntryPermission.check(
296                            getPermissionChecker(), entryId, ActionKeys.SUBSCRIBE);
297    
298                    bookmarksEntryLocalService.unsubscribeEntry(getUserId(), entryId);
299            }
300    
301            @Override
302            public BookmarksEntry updateEntry(
303                            long entryId, long groupId, long folderId, String name, String url,
304                            String description, ServiceContext serviceContext)
305                    throws PortalException, SystemException {
306    
307                    BookmarksEntryPermission.check(
308                            getPermissionChecker(), entryId, ActionKeys.UPDATE);
309    
310                    return bookmarksEntryLocalService.updateEntry(
311                            getUserId(), entryId, groupId, folderId, name, url, description,
312                            serviceContext);
313            }
314    
315    }