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.util.Date;
35  import java.util.Iterator;
36  import java.util.List;
37  
38  /**
39   * <a href="BookmarksEntryLocalServiceImpl.java.html"><b><i>View Source</i></b>
40   * </a>
41   *
42   * @author Brian Wing Shun Chan
43   * @author Raymond Augé
44   */
45  public class BookmarksEntryLocalServiceImpl
46      extends BookmarksEntryLocalServiceBaseImpl {
47  
48      public BookmarksEntry addEntry(
49              String uuid, long userId, long groupId, long folderId, String name,
50              String url, String comments, ServiceContext serviceContext)
51          throws PortalException, SystemException {
52  
53          // Entry
54  
55          User user = userPersistence.findByPrimaryKey(userId);
56  
57          if (Validator.isNull(name)) {
58              name = url;
59          }
60  
61          Date now = new Date();
62  
63          validate(url);
64  
65          long entryId = counterLocalService.increment();
66  
67          BookmarksEntry entry = bookmarksEntryPersistence.create(entryId);
68  
69          entry.setUuid(uuid);
70          entry.setGroupId(groupId);
71          entry.setCompanyId(user.getCompanyId());
72          entry.setUserId(user.getUserId());
73          entry.setCreateDate(serviceContext.getCreateDate(now));
74          entry.setModifiedDate(serviceContext.getModifiedDate(now));
75          entry.setFolderId(folderId);
76          entry.setName(name);
77          entry.setUrl(url);
78          entry.setComments(comments);
79          entry.setExpandoBridgeAttributes(serviceContext);
80  
81          bookmarksEntryPersistence.update(entry, false);
82  
83          // Resources
84  
85          if (serviceContext.getAddCommunityPermissions() ||
86              serviceContext.getAddGuestPermissions()) {
87  
88              addEntryResources(
89                  entry, serviceContext.getAddCommunityPermissions(),
90                  serviceContext.getAddGuestPermissions());
91          }
92          else {
93              addEntryResources(
94                  entry, serviceContext.getCommunityPermissions(),
95                  serviceContext.getGuestPermissions());
96          }
97  
98          // Asset
99  
100         updateAsset(
101             userId, entry, serviceContext.getAssetCategoryIds(),
102             serviceContext.getAssetTagNames());
103 
104         // Indexer
105 
106         Indexer indexer = IndexerRegistryUtil.getIndexer(
107             BookmarksEntry.class);
108 
109         indexer.reindex(entry);
110 
111         return entry;
112     }
113 
114     public void addEntryResources(
115             BookmarksEntry entry, boolean addCommunityPermissions,
116             boolean addGuestPermissions)
117         throws PortalException, SystemException {
118 
119         resourceLocalService.addResources(
120             entry.getCompanyId(), entry.getGroupId(), entry.getUserId(),
121             BookmarksEntry.class.getName(), entry.getEntryId(), false,
122             addCommunityPermissions, addGuestPermissions);
123     }
124 
125     public void addEntryResources(
126             BookmarksEntry entry, String[] communityPermissions,
127             String[] guestPermissions)
128         throws PortalException, SystemException {
129 
130         resourceLocalService.addModelResources(
131             entry.getCompanyId(), entry.getGroupId(), entry.getUserId(),
132             BookmarksEntry.class.getName(), entry.getEntryId(),
133             communityPermissions, guestPermissions);
134     }
135 
136     public void addEntryResources(
137             long entryId, boolean addCommunityPermissions,
138             boolean addGuestPermissions)
139         throws PortalException, SystemException {
140 
141         BookmarksEntry entry =
142             bookmarksEntryPersistence.findByPrimaryKey(entryId);
143 
144         addEntryResources(entry, addCommunityPermissions, addGuestPermissions);
145     }
146 
147     public void addEntryResources(
148             long entryId, String[] communityPermissions,
149             String[] guestPermissions)
150         throws PortalException, SystemException {
151 
152         BookmarksEntry entry =
153             bookmarksEntryPersistence.findByPrimaryKey(entryId);
154 
155         addEntryResources(entry, communityPermissions, guestPermissions);
156     }
157 
158     public void deleteEntries(long groupId, long folderId)
159         throws PortalException, SystemException {
160 
161         Iterator<BookmarksEntry> itr = bookmarksEntryPersistence.findByG_F(
162             groupId, folderId).iterator();
163 
164         while (itr.hasNext()) {
165             BookmarksEntry entry = itr.next();
166 
167             deleteEntry(entry);
168         }
169     }
170 
171     public void deleteEntry(BookmarksEntry entry)
172         throws PortalException, SystemException {
173 
174         // Entry
175 
176         bookmarksEntryPersistence.remove(entry);
177 
178         // Resources
179 
180         resourceLocalService.deleteResource(
181             entry.getCompanyId(), BookmarksEntry.class.getName(),
182             ResourceConstants.SCOPE_INDIVIDUAL, entry.getEntryId());
183 
184         // Asset
185 
186         assetEntryLocalService.deleteEntry(
187             BookmarksEntry.class.getName(), entry.getEntryId());
188 
189         // Expando
190 
191         expandoValueLocalService.deleteValues(
192             BookmarksEntry.class.getName(), entry.getEntryId());
193 
194         // Indexer
195 
196         Indexer indexer = IndexerRegistryUtil.getIndexer(BookmarksEntry.class);
197 
198         indexer.delete(entry);
199     }
200 
201     public void deleteEntry(long entryId)
202         throws PortalException, SystemException {
203 
204         BookmarksEntry entry =
205             bookmarksEntryPersistence.findByPrimaryKey(entryId);
206 
207         deleteEntry(entry);
208     }
209 
210     public List<BookmarksEntry> getEntries(
211             long groupId, long folderId, int start, int end)
212         throws SystemException {
213 
214         return bookmarksEntryPersistence.findByG_F(
215             groupId, folderId, start, end);
216     }
217 
218     public List<BookmarksEntry> getEntries(
219             long groupId, long folderId, int start, int end,
220             OrderByComparator orderByComparator)
221         throws SystemException {
222 
223         return bookmarksEntryPersistence.findByG_F(
224             groupId, folderId, start, end, orderByComparator);
225     }
226 
227     public int getEntriesCount(
228             long groupId, long folderId)
229         throws SystemException {
230 
231         return bookmarksEntryPersistence.countByG_F(groupId, folderId);
232     }
233 
234     public BookmarksEntry getEntry(long entryId)
235         throws PortalException, SystemException {
236 
237         return bookmarksEntryPersistence.findByPrimaryKey(entryId);
238     }
239 
240     public int getFoldersEntriesCount(long groupId, List<Long> folderIds)
241         throws SystemException {
242 
243         return bookmarksEntryFinder.countByG_F(groupId, folderIds);
244     }
245 
246     public List<BookmarksEntry> getGroupEntries(
247             long groupId, int start, int end)
248         throws SystemException {
249 
250         return bookmarksEntryPersistence.findByGroupId(
251             groupId, start, end, new EntryModifiedDateComparator());
252     }
253 
254     public List<BookmarksEntry> getGroupEntries(
255             long groupId, long userId, int start, int end)
256         throws SystemException {
257 
258         OrderByComparator orderByComparator = new EntryModifiedDateComparator();
259 
260         if (userId <= 0) {
261             return bookmarksEntryPersistence.findByGroupId(
262                 groupId, start, end, orderByComparator);
263         }
264         else {
265             return bookmarksEntryPersistence.findByG_U(
266                 groupId, userId, start, end, orderByComparator);
267         }
268     }
269 
270     public int getGroupEntriesCount(long groupId) throws SystemException {
271         return bookmarksEntryPersistence.countByGroupId(groupId);
272     }
273 
274     public int getGroupEntriesCount(long groupId, long userId)
275         throws SystemException {
276 
277         if (userId <= 0) {
278             return bookmarksEntryPersistence.countByGroupId(groupId);
279         }
280         else {
281             return bookmarksEntryPersistence.countByG_U(groupId, userId);
282         }
283     }
284 
285     public List<BookmarksEntry> getNoAssetEntries() throws SystemException {
286         return bookmarksEntryFinder.findByNoAssets();
287     }
288 
289     public BookmarksEntry openEntry(long userId, long entryId)
290         throws PortalException, SystemException {
291 
292         BookmarksEntry entry =
293             bookmarksEntryPersistence.findByPrimaryKey(entryId);
294 
295         entry.setVisits(entry.getVisits() + 1);
296 
297         bookmarksEntryPersistence.update(entry, false);
298 
299         assetEntryLocalService.incrementViewCounter(
300             userId, BookmarksEntry.class.getName(), entryId);
301 
302         return entry;
303     }
304 
305     public void updateAsset(
306             long userId, BookmarksEntry entry, long[] assetCategoryIds,
307             String[] assetTagNames)
308         throws PortalException, SystemException {
309 
310         assetEntryLocalService.updateEntry(
311             userId, entry.getGroupId(), BookmarksEntry.class.getName(),
312             entry.getEntryId(), assetCategoryIds, assetTagNames, true, null,
313             null, null, null, ContentTypes.TEXT_PLAIN, entry.getName(),
314             entry.getComments(), null, entry.getUrl(), 0, 0, null, false);
315     }
316 
317     public BookmarksEntry updateEntry(
318             long userId, long entryId, long groupId, long folderId, String name,
319             String url, String comments, ServiceContext serviceContext)
320         throws PortalException, SystemException {
321 
322         // Entry
323 
324         BookmarksEntry entry =
325             bookmarksEntryPersistence.findByPrimaryKey(entryId);
326 
327         if (Validator.isNull(name)) {
328             name = url;
329         }
330 
331         validate(url);
332 
333         entry.setModifiedDate(serviceContext.getModifiedDate(null));
334         entry.setFolderId(folderId);
335         entry.setName(name);
336         entry.setUrl(url);
337         entry.setComments(comments);
338         entry.setExpandoBridgeAttributes(serviceContext);
339 
340         bookmarksEntryPersistence.update(entry, false);
341 
342         // Asset
343 
344         updateAsset(
345             userId, entry, serviceContext.getAssetCategoryIds(),
346             serviceContext.getAssetTagNames());
347 
348         // Indexer
349 
350         Indexer indexer = IndexerRegistryUtil.getIndexer(
351             BookmarksEntry.class);
352 
353         indexer.reindex(entry);
354 
355         return entry;
356     }
357 
358     protected long getFolder(BookmarksEntry entry, long folderId)
359         throws SystemException {
360 
361         if ((entry.getFolderId() != folderId) &&
362             (folderId != BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID)) {
363 
364             BookmarksFolder newFolder =
365                 bookmarksFolderPersistence.fetchByPrimaryKey(folderId);
366 
367             if ((newFolder == null) ||
368                 (entry.getGroupId() != newFolder.getGroupId())) {
369 
370                 folderId = entry.getFolderId();
371             }
372         }
373 
374         return folderId;
375     }
376 
377     protected void validate(String url) throws PortalException {
378         if (!Validator.isUrl(url)) {
379             throw new EntryURLException();
380         }
381     }
382 
383 }