1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.bookmarks.service.impl;
16  
17  import com.liferay.portal.kernel.exception.PortalException;
18  import com.liferay.portal.kernel.exception.SystemException;
19  import com.liferay.portal.kernel.search.Indexer;
20  import com.liferay.portal.kernel.search.IndexerRegistryUtil;
21  import com.liferay.portal.kernel.util.ContentTypes;
22  import com.liferay.portal.kernel.util.OrderByComparator;
23  import com.liferay.portal.kernel.util.Validator;
24  import com.liferay.portal.model.ResourceConstants;
25  import com.liferay.portal.model.User;
26  import com.liferay.portal.service.ServiceContext;
27  import com.liferay.portlet.bookmarks.EntryURLException;
28  import com.liferay.portlet.bookmarks.model.BookmarksEntry;
29  import com.liferay.portlet.bookmarks.model.BookmarksFolder;
30  import com.liferay.portlet.bookmarks.model.BookmarksFolderConstants;
31  import com.liferay.portlet.bookmarks.service.base.BookmarksEntryLocalServiceBaseImpl;
32  import com.liferay.portlet.bookmarks.util.comparator.EntryModifiedDateComparator;
33  
34  import java.net.MalformedURLException;
35  import java.net.URL;
36  
37  import java.util.Date;
38  import java.util.Iterator;
39  import java.util.List;
40  
41  /**
42   * <a href="BookmarksEntryLocalServiceImpl.java.html"><b><i>View Source</i></b>
43   * </a>
44   *
45   * @author Brian Wing Shun Chan
46   * @author Raymond Augé
47   */
48  public class BookmarksEntryLocalServiceImpl
49      extends BookmarksEntryLocalServiceBaseImpl {
50  
51      public BookmarksEntry addEntry(
52              String uuid, long userId, long groupId, long folderId, String name,
53              String url, String comments, ServiceContext serviceContext)
54          throws PortalException, SystemException {
55  
56          // Entry
57  
58          User user = userPersistence.findByPrimaryKey(userId);
59  
60          if (Validator.isNull(name)) {
61              name = url;
62          }
63  
64          Date now = new Date();
65  
66          validate(url);
67  
68          long entryId = counterLocalService.increment();
69  
70          BookmarksEntry entry = bookmarksEntryPersistence.create(entryId);
71  
72          entry.setUuid(uuid);
73          entry.setGroupId(groupId);
74          entry.setCompanyId(user.getCompanyId());
75          entry.setUserId(user.getUserId());
76          entry.setCreateDate(now);
77          entry.setModifiedDate(now);
78          entry.setFolderId(folderId);
79          entry.setName(name);
80          entry.setUrl(url);
81          entry.setComments(comments);
82          entry.setExpandoBridgeAttributes(serviceContext);
83  
84          bookmarksEntryPersistence.update(entry, false);
85  
86          // Resources
87  
88          if (serviceContext.getAddCommunityPermissions() ||
89              serviceContext.getAddGuestPermissions()) {
90  
91              addEntryResources(
92                  entry, serviceContext.getAddCommunityPermissions(),
93                  serviceContext.getAddGuestPermissions());
94          }
95          else {
96              addEntryResources(
97                  entry, serviceContext.getCommunityPermissions(),
98                  serviceContext.getGuestPermissions());
99          }
100 
101         // Asset
102 
103         updateAsset(
104             userId, entry, serviceContext.getAssetCategoryIds(),
105             serviceContext.getAssetTagNames());
106 
107         // Indexer
108 
109         Indexer indexer = IndexerRegistryUtil.getIndexer(
110             BookmarksEntry.class);
111 
112         indexer.reindex(entry);
113 
114         return entry;
115     }
116 
117     public void addEntryResources(
118             BookmarksEntry entry, boolean addCommunityPermissions,
119             boolean addGuestPermissions)
120         throws PortalException, SystemException {
121 
122         resourceLocalService.addResources(
123             entry.getCompanyId(), entry.getGroupId(), entry.getUserId(),
124             BookmarksEntry.class.getName(), entry.getEntryId(), false,
125             addCommunityPermissions, addGuestPermissions);
126     }
127 
128     public void addEntryResources(
129             BookmarksEntry entry, String[] communityPermissions,
130             String[] guestPermissions)
131         throws PortalException, SystemException {
132 
133         resourceLocalService.addModelResources(
134             entry.getCompanyId(), entry.getGroupId(), entry.getUserId(),
135             BookmarksEntry.class.getName(), entry.getEntryId(),
136             communityPermissions, guestPermissions);
137     }
138 
139     public void addEntryResources(
140             long entryId, boolean addCommunityPermissions,
141             boolean addGuestPermissions)
142         throws PortalException, SystemException {
143 
144         BookmarksEntry entry =
145             bookmarksEntryPersistence.findByPrimaryKey(entryId);
146 
147         addEntryResources(entry, addCommunityPermissions, addGuestPermissions);
148     }
149 
150     public void addEntryResources(
151             long entryId, String[] communityPermissions,
152             String[] guestPermissions)
153         throws PortalException, SystemException {
154 
155         BookmarksEntry entry =
156             bookmarksEntryPersistence.findByPrimaryKey(entryId);
157 
158         addEntryResources(entry, communityPermissions, guestPermissions);
159     }
160 
161     public void deleteEntries(long groupId, long folderId)
162         throws PortalException, SystemException {
163 
164         Iterator<BookmarksEntry> itr = bookmarksEntryPersistence.findByG_F(
165             groupId, folderId).iterator();
166 
167         while (itr.hasNext()) {
168             BookmarksEntry entry = itr.next();
169 
170             deleteEntry(entry);
171         }
172     }
173 
174     public void deleteEntry(BookmarksEntry entry)
175         throws PortalException, SystemException {
176 
177         // Entry
178 
179         bookmarksEntryPersistence.remove(entry);
180 
181         // Resources
182 
183         resourceLocalService.deleteResource(
184             entry.getCompanyId(), BookmarksEntry.class.getName(),
185             ResourceConstants.SCOPE_INDIVIDUAL, entry.getEntryId());
186 
187         // Asset
188 
189         assetEntryLocalService.deleteEntry(
190             BookmarksEntry.class.getName(), entry.getEntryId());
191 
192         // Expando
193 
194         expandoValueLocalService.deleteValues(
195             BookmarksEntry.class.getName(), entry.getEntryId());
196 
197         // Indexer
198 
199         Indexer indexer = IndexerRegistryUtil.getIndexer(BookmarksEntry.class);
200 
201         indexer.delete(entry);
202     }
203 
204     public void deleteEntry(long entryId)
205         throws PortalException, SystemException {
206 
207         BookmarksEntry entry =
208             bookmarksEntryPersistence.findByPrimaryKey(entryId);
209 
210         deleteEntry(entry);
211     }
212 
213     public List<BookmarksEntry> getEntries(
214             long groupId, long folderId, int start, int end)
215         throws SystemException {
216 
217         return bookmarksEntryPersistence.findByG_F(
218             groupId, folderId, start, end);
219     }
220 
221     public List<BookmarksEntry> getEntries(
222             long groupId, long folderId, int start, int end,
223             OrderByComparator orderByComparator)
224         throws SystemException {
225 
226         return bookmarksEntryPersistence.findByG_F(
227             groupId, folderId, start, end, orderByComparator);
228     }
229 
230     public int getEntriesCount(
231             long groupId, long folderId)
232         throws SystemException {
233 
234         return bookmarksEntryPersistence.countByG_F(groupId, folderId);
235     }
236 
237     public BookmarksEntry getEntry(long entryId)
238         throws PortalException, SystemException {
239 
240         return bookmarksEntryPersistence.findByPrimaryKey(entryId);
241     }
242 
243     public int getFoldersEntriesCount(long groupId, List<Long> folderIds)
244         throws SystemException {
245 
246         return bookmarksEntryFinder.countByG_F(groupId, folderIds);
247     }
248 
249     public List<BookmarksEntry> getGroupEntries(
250             long groupId, int start, int end)
251         throws SystemException {
252 
253         return bookmarksEntryPersistence.findByGroupId(
254             groupId, start, end, new EntryModifiedDateComparator());
255     }
256 
257     public List<BookmarksEntry> getGroupEntries(
258             long groupId, long userId, int start, int end)
259         throws SystemException {
260 
261         OrderByComparator orderByComparator = new EntryModifiedDateComparator();
262 
263         if (userId <= 0) {
264             return bookmarksEntryPersistence.findByGroupId(
265                 groupId, start, end, orderByComparator);
266         }
267         else {
268             return bookmarksEntryPersistence.findByG_U(
269                 groupId, userId, start, end, orderByComparator);
270         }
271     }
272 
273     public int getGroupEntriesCount(long groupId) throws SystemException {
274         return bookmarksEntryPersistence.countByGroupId(groupId);
275     }
276 
277     public int getGroupEntriesCount(long groupId, long userId)
278         throws SystemException {
279 
280         if (userId <= 0) {
281             return bookmarksEntryPersistence.countByGroupId(groupId);
282         }
283         else {
284             return bookmarksEntryPersistence.countByG_U(groupId, userId);
285         }
286     }
287 
288     public List<BookmarksEntry> getNoAssetEntries() throws SystemException {
289         return bookmarksEntryFinder.findByNoAssets();
290     }
291 
292     public BookmarksEntry openEntry(long entryId)
293         throws PortalException, SystemException {
294 
295         BookmarksEntry entry =
296             bookmarksEntryPersistence.findByPrimaryKey(entryId);
297 
298         entry.setVisits(entry.getVisits() + 1);
299 
300         bookmarksEntryPersistence.update(entry, false);
301 
302         assetEntryLocalService.incrementViewCounter(
303             BookmarksEntry.class.getName(), entryId);
304 
305         return entry;
306     }
307 
308     public void updateAsset(
309             long userId, BookmarksEntry entry, long[] assetCategoryIds,
310             String[] assetTagNames)
311         throws PortalException, SystemException {
312 
313         assetEntryLocalService.updateEntry(
314             userId, entry.getGroupId(), BookmarksEntry.class.getName(),
315             entry.getEntryId(), assetCategoryIds, assetTagNames, true, null,
316             null, null, null, ContentTypes.TEXT_PLAIN, entry.getName(),
317             entry.getComments(), null, entry.getUrl(), 0, 0, null, false);
318     }
319 
320     public BookmarksEntry updateEntry(
321             long userId, long entryId, long groupId, long folderId, String name,
322             String url, String comments, ServiceContext serviceContext)
323         throws PortalException, SystemException {
324 
325         // Entry
326 
327         BookmarksEntry entry =
328             bookmarksEntryPersistence.findByPrimaryKey(entryId);
329 
330         if (Validator.isNull(name)) {
331             name = url;
332         }
333 
334         validate(url);
335 
336         entry.setModifiedDate(new Date());
337         entry.setFolderId(folderId);
338         entry.setName(name);
339         entry.setUrl(url);
340         entry.setComments(comments);
341         entry.setExpandoBridgeAttributes(serviceContext);
342 
343         bookmarksEntryPersistence.update(entry, false);
344 
345         // Asset
346 
347         updateAsset(
348             userId, entry, serviceContext.getAssetCategoryIds(),
349             serviceContext.getAssetTagNames());
350 
351         // Indexer
352 
353         Indexer indexer = IndexerRegistryUtil.getIndexer(
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.isNull(url)) {
382             throw new EntryURLException();
383         }
384         else {
385             try {
386                 new URL(url);
387             }
388             catch (MalformedURLException murle) {
389                 throw new EntryURLException();
390             }
391         }
392     }
393 
394 }