001    /**
002     * Copyright (c) 2000-2012 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.service.base.BookmarksEntryServiceBaseImpl;
026    import com.liferay.portlet.bookmarks.service.permission.BookmarksEntryPermission;
027    import com.liferay.portlet.bookmarks.service.permission.BookmarksFolderPermission;
028    import com.liferay.portlet.bookmarks.util.comparator.EntryModifiedDateComparator;
029    
030    import java.util.List;
031    
032    /**
033     * @author Brian Wing Shun Chan
034     * @author Levente Hudák
035     */
036    public class BookmarksEntryServiceImpl extends BookmarksEntryServiceBaseImpl {
037    
038            public BookmarksEntry addEntry(
039                            long groupId, long folderId, String name, String url,
040                            String description, ServiceContext serviceContext)
041                    throws PortalException, SystemException {
042    
043                    BookmarksFolderPermission.check(
044                            getPermissionChecker(), groupId, folderId, ActionKeys.ADD_ENTRY);
045    
046                    return bookmarksEntryLocalService.addEntry(
047                            getUserId(), groupId, folderId, name, url, description,
048                            serviceContext);
049            }
050    
051            public void deleteEntry(long entryId)
052                    throws PortalException, SystemException {
053    
054                    BookmarksEntryPermission.check(
055                            getPermissionChecker(), entryId, ActionKeys.DELETE);
056    
057                    bookmarksEntryLocalService.deleteEntry(entryId);
058            }
059    
060            public List<BookmarksEntry> getEntries(
061                            long groupId, long folderId, int start, int end)
062                    throws SystemException {
063    
064                    return bookmarksEntryPersistence.filterFindByG_F_S(
065                            groupId, folderId, WorkflowConstants.STATUS_APPROVED, start, end);
066            }
067    
068            public List<BookmarksEntry> getEntries(
069                            long groupId, long folderId, int start, int end,
070                            OrderByComparator orderByComparator)
071                    throws SystemException {
072    
073                    return bookmarksEntryPersistence.filterFindByG_F_S(
074                            groupId, folderId, WorkflowConstants.STATUS_APPROVED, start, end,
075                            orderByComparator);
076            }
077    
078            public int getEntriesCount(long groupId, long folderId)
079                    throws SystemException {
080    
081                    return bookmarksEntryPersistence.filterCountByG_F_S(
082                            groupId, folderId, WorkflowConstants.STATUS_APPROVED);
083            }
084    
085            public BookmarksEntry getEntry(long entryId)
086                    throws PortalException, SystemException {
087    
088                    BookmarksEntryPermission.check(
089                            getPermissionChecker(), entryId, ActionKeys.VIEW);
090    
091                    return bookmarksEntryLocalService.getEntry(entryId);
092            }
093    
094            public int getFoldersEntriesCount(long groupId, List<Long> folderIds)
095                    throws SystemException {
096    
097                    return bookmarksEntryPersistence.filterCountByG_F_S(
098                            groupId,
099                            ArrayUtil.toArray(folderIds.toArray(new Long[folderIds.size()])),
100                            WorkflowConstants.STATUS_APPROVED);
101            }
102    
103            public List<BookmarksEntry> getGroupEntries(
104                            long groupId, int start, int end)
105                    throws SystemException {
106    
107                    return bookmarksEntryPersistence.filterFindByG_S(
108                            groupId, WorkflowConstants.STATUS_APPROVED, start, end);
109            }
110    
111            public List<BookmarksEntry> getGroupEntries(
112                            long groupId, long userId, int start, int end)
113                    throws SystemException {
114    
115                    OrderByComparator orderByComparator = new EntryModifiedDateComparator();
116    
117                    if (userId <= 0) {
118                            return bookmarksEntryPersistence.filterFindByG_S(
119                                    groupId, WorkflowConstants.STATUS_APPROVED, start, end,
120                                    orderByComparator);
121                    }
122                    else {
123                            return bookmarksEntryPersistence.filterFindByG_U_S(
124                                    groupId, userId, WorkflowConstants.STATUS_APPROVED, start, end,
125                                    orderByComparator);
126                    }
127            }
128    
129            public int getGroupEntriesCount(long groupId) throws SystemException {
130                    return bookmarksEntryPersistence.filterCountByG_S(
131                            groupId, WorkflowConstants.STATUS_APPROVED);
132            }
133    
134            public int getGroupEntriesCount(long groupId, long userId)
135                    throws SystemException {
136    
137                    if (userId <= 0) {
138                            return getGroupEntriesCount(groupId);
139                    }
140                    else {
141                            return bookmarksEntryPersistence.filterCountByG_U_S(
142                                    groupId, userId, WorkflowConstants.STATUS_APPROVED);
143                    }
144            }
145    
146            public BookmarksEntry moveEntry(long entryId, long parentFolderId)
147                    throws PortalException, SystemException {
148    
149                    BookmarksEntryPermission.check(
150                            getPermissionChecker(), entryId, ActionKeys.UPDATE);
151    
152                    return bookmarksEntryLocalService.moveEntry(entryId, parentFolderId);
153            }
154    
155            public BookmarksEntry moveEntryFromTrash(long entryId, long parentFolderId)
156                    throws PortalException, SystemException {
157    
158                    BookmarksEntryPermission.check(
159                            getPermissionChecker(), entryId, ActionKeys.UPDATE);
160    
161                    return bookmarksEntryLocalService.moveEntryFromTrash(
162                            getUserId(), entryId, parentFolderId);
163            }
164    
165            public void moveEntryToTrash(long entryId)
166                    throws PortalException, SystemException {
167    
168                    BookmarksEntryPermission.check(
169                            getPermissionChecker(), entryId, ActionKeys.DELETE);
170    
171                    bookmarksEntryLocalService.moveEntryToTrash(getUserId(), entryId);
172            }
173    
174            public BookmarksEntry openEntry(BookmarksEntry entry)
175                    throws PortalException, SystemException {
176    
177                    BookmarksEntryPermission.check(
178                            getPermissionChecker(), entry, ActionKeys.VIEW);
179    
180                    return bookmarksEntryLocalService.openEntry(getGuestOrUserId(), entry);
181            }
182    
183            public BookmarksEntry openEntry(long entryId)
184                    throws PortalException, SystemException {
185    
186                    BookmarksEntryPermission.check(
187                            getPermissionChecker(), entryId, ActionKeys.VIEW);
188    
189                    return bookmarksEntryLocalService.openEntry(
190                            getGuestOrUserId(), entryId);
191            }
192    
193            public void restoreEntryFromTrash(long entryId)
194                    throws PortalException, SystemException {
195    
196                    BookmarksEntryPermission.check(
197                            getPermissionChecker(), entryId, ActionKeys.UPDATE);
198    
199                    bookmarksEntryLocalService.restoreEntryFromTrash(getUserId(), entryId);
200            }
201    
202            public void subscribeEntry(long entryId)
203                    throws PortalException, SystemException {
204    
205                    BookmarksEntryPermission.check(
206                            getPermissionChecker(), entryId, ActionKeys.SUBSCRIBE);
207    
208                    bookmarksEntryLocalService.subscribeEntry(getUserId(), entryId);
209            }
210    
211            public void unsubscribeEntry(long entryId)
212                    throws PortalException, SystemException {
213    
214                    BookmarksEntryPermission.check(
215                            getPermissionChecker(), entryId, ActionKeys.SUBSCRIBE);
216    
217                    bookmarksEntryLocalService.unsubscribeEntry(getUserId(), entryId);
218            }
219    
220            public BookmarksEntry updateEntry(
221                            long entryId, long groupId, long folderId, String name, String url,
222                            String description, ServiceContext serviceContext)
223                    throws PortalException, SystemException {
224    
225                    BookmarksEntryPermission.check(
226                            getPermissionChecker(), entryId, ActionKeys.UPDATE);
227    
228                    return bookmarksEntryLocalService.updateEntry(
229                            getUserId(), entryId, groupId, folderId, name, url, description,
230                            serviceContext);
231            }
232    
233    }