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