001    /**
002     * Copyright (c) 2000-2010 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.Indexer;
020    import com.liferay.portal.kernel.search.IndexerRegistryUtil;
021    import com.liferay.portal.kernel.util.ArrayUtil;
022    import com.liferay.portal.kernel.util.ContentTypes;
023    import com.liferay.portal.kernel.util.OrderByComparator;
024    import com.liferay.portal.kernel.util.Validator;
025    import com.liferay.portal.model.ResourceConstants;
026    import com.liferay.portal.model.User;
027    import com.liferay.portal.service.ServiceContext;
028    import com.liferay.portlet.bookmarks.EntryURLException;
029    import com.liferay.portlet.bookmarks.model.BookmarksEntry;
030    import com.liferay.portlet.bookmarks.model.BookmarksFolder;
031    import com.liferay.portlet.bookmarks.model.BookmarksFolderConstants;
032    import com.liferay.portlet.bookmarks.service.base.BookmarksEntryLocalServiceBaseImpl;
033    import com.liferay.portlet.bookmarks.util.comparator.EntryModifiedDateComparator;
034    
035    import java.util.Date;
036    import java.util.Iterator;
037    import java.util.List;
038    
039    /**
040     * @author Brian Wing Shun Chan
041     * @author Raymond Augé
042     */
043    public class BookmarksEntryLocalServiceImpl
044            extends BookmarksEntryLocalServiceBaseImpl {
045    
046            public BookmarksEntry addEntry(
047                            long userId, long groupId, long folderId, String name, String url,
048                            String comments, ServiceContext serviceContext)
049                    throws PortalException, SystemException {
050    
051                    // Entry
052    
053                    User user = userPersistence.findByPrimaryKey(userId);
054    
055                    if (Validator.isNull(name)) {
056                            name = url;
057                    }
058    
059                    Date now = new Date();
060    
061                    validate(url);
062    
063                    long entryId = counterLocalService.increment();
064    
065                    BookmarksEntry entry = bookmarksEntryPersistence.create(entryId);
066    
067                    entry.setUuid(serviceContext.getUuid());
068                    entry.setGroupId(groupId);
069                    entry.setCompanyId(user.getCompanyId());
070                    entry.setUserId(user.getUserId());
071                    entry.setCreateDate(serviceContext.getCreateDate(now));
072                    entry.setModifiedDate(serviceContext.getModifiedDate(now));
073                    entry.setFolderId(folderId);
074                    entry.setName(name);
075                    entry.setUrl(url);
076                    entry.setComments(comments);
077                    entry.setExpandoBridgeAttributes(serviceContext);
078    
079                    bookmarksEntryPersistence.update(entry, false);
080    
081                    // Resources
082    
083                    if (serviceContext.getAddCommunityPermissions() ||
084                            serviceContext.getAddGuestPermissions()) {
085    
086                            addEntryResources(
087                                    entry, serviceContext.getAddCommunityPermissions(),
088                                    serviceContext.getAddGuestPermissions());
089                    }
090                    else {
091                            addEntryResources(
092                                    entry, serviceContext.getCommunityPermissions(),
093                                    serviceContext.getGuestPermissions());
094                    }
095    
096                    // Asset
097    
098                    updateAsset(
099                            userId, entry, serviceContext.getAssetCategoryIds(),
100                            serviceContext.getAssetTagNames());
101    
102                    // Indexer
103    
104                    Indexer indexer = IndexerRegistryUtil.getIndexer(
105                            BookmarksEntry.class);
106    
107                    indexer.reindex(entry);
108    
109                    return entry;
110            }
111    
112            public void addEntryResources(
113                            BookmarksEntry entry, boolean addCommunityPermissions,
114                            boolean addGuestPermissions)
115                    throws PortalException, SystemException {
116    
117                    resourceLocalService.addResources(
118                            entry.getCompanyId(), entry.getGroupId(), entry.getUserId(),
119                            BookmarksEntry.class.getName(), entry.getEntryId(), false,
120                            addCommunityPermissions, addGuestPermissions);
121            }
122    
123            public void addEntryResources(
124                            BookmarksEntry entry, String[] communityPermissions,
125                            String[] guestPermissions)
126                    throws PortalException, SystemException {
127    
128                    resourceLocalService.addModelResources(
129                            entry.getCompanyId(), entry.getGroupId(), entry.getUserId(),
130                            BookmarksEntry.class.getName(), entry.getEntryId(),
131                            communityPermissions, guestPermissions);
132            }
133    
134            public void addEntryResources(
135                            long entryId, boolean addCommunityPermissions,
136                            boolean addGuestPermissions)
137                    throws PortalException, SystemException {
138    
139                    BookmarksEntry entry =
140                            bookmarksEntryPersistence.findByPrimaryKey(entryId);
141    
142                    addEntryResources(entry, addCommunityPermissions, addGuestPermissions);
143            }
144    
145            public void addEntryResources(
146                            long entryId, String[] communityPermissions,
147                            String[] guestPermissions)
148                    throws PortalException, SystemException {
149    
150                    BookmarksEntry entry =
151                            bookmarksEntryPersistence.findByPrimaryKey(entryId);
152    
153                    addEntryResources(entry, communityPermissions, guestPermissions);
154            }
155    
156            public void deleteEntries(long groupId, long folderId)
157                    throws PortalException, SystemException {
158    
159                    Iterator<BookmarksEntry> itr = bookmarksEntryPersistence.findByG_F(
160                            groupId, folderId).iterator();
161    
162                    while (itr.hasNext()) {
163                            BookmarksEntry entry = itr.next();
164    
165                            deleteEntry(entry);
166                    }
167            }
168    
169            public void deleteEntry(BookmarksEntry entry)
170                    throws PortalException, SystemException {
171    
172                    // Entry
173    
174                    bookmarksEntryPersistence.remove(entry);
175    
176                    // Resources
177    
178                    resourceLocalService.deleteResource(
179                            entry.getCompanyId(), BookmarksEntry.class.getName(),
180                            ResourceConstants.SCOPE_INDIVIDUAL, entry.getEntryId());
181    
182                    // Asset
183    
184                    assetEntryLocalService.deleteEntry(
185                            BookmarksEntry.class.getName(), entry.getEntryId());
186    
187                    // Expando
188    
189                    expandoValueLocalService.deleteValues(
190                            BookmarksEntry.class.getName(), entry.getEntryId());
191    
192                    // Indexer
193    
194                    Indexer indexer = IndexerRegistryUtil.getIndexer(BookmarksEntry.class);
195    
196                    indexer.delete(entry);
197            }
198    
199            public void deleteEntry(long entryId)
200                    throws PortalException, SystemException {
201    
202                    BookmarksEntry entry =
203                            bookmarksEntryPersistence.findByPrimaryKey(entryId);
204    
205                    deleteEntry(entry);
206            }
207    
208            public List<BookmarksEntry> getEntries(
209                            long groupId, long folderId, int start, int end)
210                    throws SystemException {
211    
212                    return bookmarksEntryPersistence.findByG_F(
213                            groupId, folderId, start, end);
214            }
215    
216            public List<BookmarksEntry> getEntries(
217                            long groupId, long folderId, int start, int end,
218                            OrderByComparator orderByComparator)
219                    throws SystemException {
220    
221                    return bookmarksEntryPersistence.findByG_F(
222                            groupId, folderId, start, end, orderByComparator);
223            }
224    
225            public int getEntriesCount(
226                            long groupId, long folderId)
227                    throws SystemException {
228    
229                    return bookmarksEntryPersistence.countByG_F(groupId, folderId);
230            }
231    
232            public BookmarksEntry getEntry(long entryId)
233                    throws PortalException, SystemException {
234    
235                    return bookmarksEntryPersistence.findByPrimaryKey(entryId);
236            }
237    
238            public int getFoldersEntriesCount(long groupId, List<Long> folderIds)
239                    throws SystemException {
240    
241                    return bookmarksEntryPersistence.countByG_F(
242                            groupId,
243                            ArrayUtil.toArray(folderIds.toArray(new Long[folderIds.size()])));
244            }
245    
246            public List<BookmarksEntry> getGroupEntries(
247                            long groupId, int start, int end)
248                    throws SystemException {
249    
250                    return bookmarksEntryPersistence.findByGroupId(
251                            groupId, start, end, new EntryModifiedDateComparator());
252            }
253    
254            public List<BookmarksEntry> getGroupEntries(
255                            long groupId, long userId, int start, int end)
256                    throws SystemException {
257    
258                    OrderByComparator orderByComparator = new EntryModifiedDateComparator();
259    
260                    if (userId <= 0) {
261                            return bookmarksEntryPersistence.findByGroupId(
262                                    groupId, start, end, orderByComparator);
263                    }
264                    else {
265                            return bookmarksEntryPersistence.findByG_U(
266                                    groupId, userId, start, end, orderByComparator);
267                    }
268            }
269    
270            public int getGroupEntriesCount(long groupId) throws SystemException {
271                    return bookmarksEntryPersistence.countByGroupId(groupId);
272            }
273    
274            public int getGroupEntriesCount(long groupId, long userId)
275                    throws SystemException {
276    
277                    if (userId <= 0) {
278                            return bookmarksEntryPersistence.countByGroupId(groupId);
279                    }
280                    else {
281                            return bookmarksEntryPersistence.countByG_U(groupId, userId);
282                    }
283            }
284    
285            public List<BookmarksEntry> getNoAssetEntries() throws SystemException {
286                    return bookmarksEntryFinder.findByNoAssets();
287            }
288    
289            public BookmarksEntry openEntry(long userId, long entryId)
290                    throws PortalException, SystemException {
291    
292                    BookmarksEntry entry =
293                            bookmarksEntryPersistence.findByPrimaryKey(entryId);
294    
295                    entry.setVisits(entry.getVisits() + 1);
296    
297                    bookmarksEntryPersistence.update(entry, false);
298    
299                    assetEntryLocalService.incrementViewCounter(
300                            userId, BookmarksEntry.class.getName(), entryId);
301    
302                    return entry;
303            }
304    
305            public void updateAsset(
306                            long userId, BookmarksEntry entry, long[] assetCategoryIds,
307                            String[] assetTagNames)
308                    throws PortalException, SystemException {
309    
310                    assetEntryLocalService.updateEntry(
311                            userId, entry.getGroupId(), BookmarksEntry.class.getName(),
312                            entry.getEntryId(), entry.getUuid(), assetCategoryIds,
313                            assetTagNames, true, null, null, null, null,
314                            ContentTypes.TEXT_PLAIN, entry.getName(), entry.getComments(), null,
315                            entry.getUrl(), 0, 0, null, false);
316            }
317    
318            public BookmarksEntry updateEntry(
319                            long userId, long entryId, long groupId, long folderId, String name,
320                            String url, String comments, ServiceContext serviceContext)
321                    throws PortalException, SystemException {
322    
323                    // Entry
324    
325                    BookmarksEntry entry =
326                            bookmarksEntryPersistence.findByPrimaryKey(entryId);
327    
328                    if (Validator.isNull(name)) {
329                            name = url;
330                    }
331    
332                    validate(url);
333    
334                    entry.setModifiedDate(serviceContext.getModifiedDate(null));
335                    entry.setFolderId(folderId);
336                    entry.setName(name);
337                    entry.setUrl(url);
338                    entry.setComments(comments);
339                    entry.setExpandoBridgeAttributes(serviceContext);
340    
341                    bookmarksEntryPersistence.update(entry, false);
342    
343                    // Asset
344    
345                    updateAsset(
346                            userId, entry, serviceContext.getAssetCategoryIds(),
347                            serviceContext.getAssetTagNames());
348    
349                    // Indexer
350    
351                    Indexer indexer = IndexerRegistryUtil.getIndexer(
352                            BookmarksEntry.class);
353    
354                    indexer.reindex(entry);
355    
356                    return entry;
357            }
358    
359            protected long getFolder(BookmarksEntry entry, long folderId)
360                    throws SystemException {
361    
362                    if ((entry.getFolderId() != folderId) &&
363                            (folderId != BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID)) {
364    
365                            BookmarksFolder newFolder =
366                                    bookmarksFolderPersistence.fetchByPrimaryKey(folderId);
367    
368                            if ((newFolder == null) ||
369                                    (entry.getGroupId() != newFolder.getGroupId())) {
370    
371                                    folderId = entry.getFolderId();
372                            }
373                    }
374    
375                    return folderId;
376            }
377    
378            protected void validate(String url) throws PortalException {
379                    if (!Validator.isUrl(url)) {
380                            throw new EntryURLException();
381                    }
382            }
383    
384    }