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