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