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