1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   * 
13   */
14  
15  package com.liferay.portlet.bookmarks.service.impl;
16  
17  import com.liferay.portal.PortalException;
18  import com.liferay.portal.SystemException;
19  import com.liferay.portal.kernel.log.Log;
20  import com.liferay.portal.kernel.log.LogFactoryUtil;
21  import com.liferay.portal.kernel.search.SearchEngineUtil;
22  import com.liferay.portal.kernel.search.SearchException;
23  import com.liferay.portal.kernel.util.ContentTypes;
24  import com.liferay.portal.kernel.util.OrderByComparator;
25  import com.liferay.portal.kernel.util.Validator;
26  import com.liferay.portal.model.ResourceConstants;
27  import com.liferay.portal.model.User;
28  import com.liferay.portlet.bookmarks.EntryURLException;
29  import com.liferay.portlet.bookmarks.model.BookmarksEntry;
30  import com.liferay.portlet.bookmarks.model.BookmarksFolder;
31  import com.liferay.portlet.bookmarks.service.base.BookmarksEntryLocalServiceBaseImpl;
32  import com.liferay.portlet.bookmarks.util.Indexer;
33  import com.liferay.portlet.bookmarks.util.comparator.EntryModifiedDateComparator;
34  
35  import java.net.MalformedURLException;
36  import java.net.URL;
37  
38  import java.util.Date;
39  import java.util.Iterator;
40  import java.util.List;
41  
42  /**
43   * <a href="BookmarksEntryLocalServiceImpl.java.html"><b><i>View Source</i></b>
44   * </a>
45   *
46   * @author Brian Wing Shun Chan
47   */
48  public class BookmarksEntryLocalServiceImpl
49      extends BookmarksEntryLocalServiceBaseImpl {
50  
51      public BookmarksEntry addEntry(
52              long userId, long folderId, String name, String url,
53              String comments, String[] tagsEntries,
54              boolean addCommunityPermissions, boolean addGuestPermissions)
55          throws PortalException, SystemException {
56  
57          return addEntry(
58              null, userId, folderId, name, url, comments, tagsEntries,
59              Boolean.valueOf(addCommunityPermissions),
60              Boolean.valueOf(addGuestPermissions), null, null);
61      }
62  
63      public BookmarksEntry addEntry(
64              long userId, long folderId, String name, String url,
65              String comments, String[] tagsEntries,
66              String[] communityPermissions, String[] guestPermissions)
67          throws PortalException, SystemException {
68  
69          return addEntry(
70              null, userId, folderId, name, url, comments, tagsEntries, null,
71              null, communityPermissions, guestPermissions);
72      }
73  
74      public BookmarksEntry addEntry(
75              String uuid, long userId, long folderId, String name, String url,
76              String comments, String[] tagsEntries,
77              boolean addCommunityPermissions, boolean addGuestPermissions)
78          throws PortalException, SystemException {
79  
80          return addEntry(
81              uuid, userId, folderId, name, url, comments, tagsEntries,
82              Boolean.valueOf(addCommunityPermissions),
83              Boolean.valueOf(addGuestPermissions), null, null);
84      }
85  
86      public BookmarksEntry addEntry(
87              String uuid, long userId, long folderId, String name, String url,
88              String comments, String[] tagsEntries,
89              Boolean addCommunityPermissions, Boolean addGuestPermissions,
90              String[] communityPermissions, String[] guestPermissions)
91          throws PortalException, SystemException {
92  
93          // Entry
94  
95          User user = userPersistence.findByPrimaryKey(userId);
96          BookmarksFolder folder =
97              bookmarksFolderPersistence.findByPrimaryKey(folderId);
98  
99          if (Validator.isNull(name)) {
100             name = url;
101         }
102 
103         Date now = new Date();
104 
105         validate(url);
106 
107         long entryId = counterLocalService.increment();
108 
109         BookmarksEntry entry = bookmarksEntryPersistence.create(entryId);
110 
111         entry.setUuid(uuid);
112         entry.setGroupId(folder.getGroupId());
113         entry.setCompanyId(user.getCompanyId());
114         entry.setUserId(user.getUserId());
115         entry.setCreateDate(now);
116         entry.setModifiedDate(now);
117         entry.setFolderId(folderId);
118         entry.setName(name);
119         entry.setUrl(url);
120         entry.setComments(comments);
121 
122         bookmarksEntryPersistence.update(entry, false);
123 
124         // Resources
125 
126         if ((addCommunityPermissions != null) &&
127             (addGuestPermissions != null)) {
128 
129             addEntryResources(
130                 entry, addCommunityPermissions.booleanValue(),
131                 addGuestPermissions.booleanValue());
132         }
133         else {
134             addEntryResources(entry, communityPermissions, guestPermissions);
135         }
136 
137         // Tags
138 
139         updateTagsAsset(userId, entry, tagsEntries);
140 
141         // Indexer
142 
143         reIndex(entry);
144 
145         return entry;
146     }
147 
148     public void addEntryResources(
149             BookmarksEntry entry, boolean addCommunityPermissions,
150             boolean addGuestPermissions)
151         throws PortalException, SystemException {
152 
153         resourceLocalService.addResources(
154             entry.getCompanyId(), entry.getGroupId(), entry.getUserId(),
155             BookmarksEntry.class.getName(), entry.getEntryId(), false,
156             addCommunityPermissions, addGuestPermissions);
157     }
158 
159     public void addEntryResources(
160             BookmarksEntry entry, String[] communityPermissions,
161             String[] guestPermissions)
162         throws PortalException, SystemException {
163 
164         resourceLocalService.addModelResources(
165             entry.getCompanyId(), entry.getGroupId(), entry.getUserId(),
166             BookmarksEntry.class.getName(), entry.getEntryId(),
167             communityPermissions, guestPermissions);
168     }
169 
170     public void addEntryResources(
171             long entryId, boolean addCommunityPermissions,
172             boolean addGuestPermissions)
173         throws PortalException, SystemException {
174 
175         BookmarksEntry entry =
176             bookmarksEntryPersistence.findByPrimaryKey(entryId);
177 
178         addEntryResources(entry, addCommunityPermissions, addGuestPermissions);
179     }
180 
181     public void addEntryResources(
182             long entryId, String[] communityPermissions,
183             String[] guestPermissions)
184         throws PortalException, SystemException {
185 
186         BookmarksEntry entry =
187             bookmarksEntryPersistence.findByPrimaryKey(entryId);
188 
189         addEntryResources(entry, communityPermissions, guestPermissions);
190     }
191 
192     public void deleteEntries(long folderId)
193         throws PortalException, SystemException {
194 
195         Iterator<BookmarksEntry> itr = bookmarksEntryPersistence.findByFolderId(
196             folderId).iterator();
197 
198         while (itr.hasNext()) {
199             BookmarksEntry entry = itr.next();
200 
201             deleteEntry(entry);
202         }
203     }
204 
205     public void deleteEntry(BookmarksEntry entry)
206         throws PortalException, SystemException {
207 
208         // Entry
209 
210         bookmarksEntryPersistence.remove(entry);
211 
212         // Resources
213 
214         resourceLocalService.deleteResource(
215             entry.getCompanyId(), BookmarksEntry.class.getName(),
216             ResourceConstants.SCOPE_INDIVIDUAL, entry.getEntryId());
217 
218         // Tags
219 
220         tagsAssetLocalService.deleteAsset(
221             BookmarksEntry.class.getName(), entry.getEntryId());
222 
223         // Indexer
224 
225         try {
226             Indexer.deleteEntry(entry.getCompanyId(), entry.getEntryId());
227         }
228         catch (SearchException se) {
229             _log.error("Deleting index " + entry.getEntryId(), se);
230         }
231     }
232 
233     public void deleteEntry(long entryId)
234         throws PortalException, SystemException {
235 
236         BookmarksEntry entry =
237             bookmarksEntryPersistence.findByPrimaryKey(entryId);
238 
239         deleteEntry(entry);
240     }
241 
242     public List<BookmarksEntry> getEntries(long folderId, int start, int end)
243         throws SystemException {
244 
245         return bookmarksEntryPersistence.findByFolderId(folderId, start, end);
246     }
247 
248     public List<BookmarksEntry> getEntries(
249             long folderId, int start, int end,
250             OrderByComparator orderByComparator)
251         throws SystemException {
252 
253         return bookmarksEntryPersistence.findByFolderId(
254             folderId, start, end, orderByComparator);
255     }
256 
257     public int getEntriesCount(long folderId) throws SystemException {
258         return bookmarksEntryPersistence.countByFolderId(folderId);
259     }
260 
261     public BookmarksEntry getEntry(long entryId)
262         throws PortalException, SystemException {
263 
264         return bookmarksEntryPersistence.findByPrimaryKey(entryId);
265     }
266 
267     public int getFoldersEntriesCount(List<Long> folderIds)
268         throws SystemException {
269 
270         return bookmarksEntryFinder.countByFolderIds(folderIds);
271     }
272 
273     public List<BookmarksEntry> getGroupEntries(
274             long groupId, int start, int end)
275         throws SystemException {
276 
277         return bookmarksEntryPersistence.findByGroupId(
278             groupId, start, end, new EntryModifiedDateComparator());
279     }
280 
281     public List<BookmarksEntry> getGroupEntries(
282             long groupId, long userId, int start, int end)
283         throws SystemException {
284 
285         OrderByComparator orderByComparator = new EntryModifiedDateComparator();
286 
287         if (userId <= 0) {
288             return bookmarksEntryPersistence.findByGroupId(
289                 groupId, start, end, orderByComparator);
290         }
291         else {
292             return bookmarksEntryPersistence.findByG_U(
293                 groupId, userId, start, end, orderByComparator);
294         }
295     }
296 
297     public int getGroupEntriesCount(long groupId) throws SystemException {
298         return bookmarksEntryPersistence.countByGroupId(groupId);
299     }
300 
301     public int getGroupEntriesCount(long groupId, long userId)
302         throws SystemException {
303 
304         if (userId <= 0) {
305             return bookmarksEntryPersistence.countByGroupId(groupId);
306         }
307         else {
308             return bookmarksEntryPersistence.countByG_U(groupId, userId);
309         }
310     }
311 
312     public List<BookmarksEntry> getNoAssetEntries() throws SystemException {
313         return bookmarksEntryFinder.findByNoAssets();
314     }
315 
316     public BookmarksEntry openEntry(long entryId)
317         throws PortalException, SystemException {
318 
319         BookmarksEntry entry =
320             bookmarksEntryPersistence.findByPrimaryKey(entryId);
321 
322         entry.setVisits(entry.getVisits() + 1);
323 
324         bookmarksEntryPersistence.update(entry, false);
325 
326         tagsAssetLocalService.incrementViewCounter(
327             BookmarksEntry.class.getName(), entryId);
328 
329         return entry;
330     }
331 
332     public void reIndex(BookmarksEntry entry) throws SystemException {
333         long companyId = entry.getCompanyId();
334         long groupId = entry.getGroupId();
335         long folderId = entry.getFolderId();
336         long entryId = entry.getEntryId();
337         String name = entry.getName();
338         String url = entry.getUrl();
339         String comments = entry.getComments();
340         Date modifiedDate = entry.getModifiedDate();
341 
342         String[] tagsEntries = tagsEntryLocalService.getEntryNames(
343             BookmarksEntry.class.getName(), entryId);
344 
345         try {
346             Indexer.updateEntry(
347                 companyId, groupId, folderId, entryId, name, url, comments,
348                 modifiedDate, tagsEntries);
349         }
350         catch (SearchException se) {
351             _log.error("Reindexing " + entryId, se);
352         }
353     }
354 
355     public void reIndex(long entryId) throws SystemException {
356         if (SearchEngineUtil.isIndexReadOnly()) {
357             return;
358         }
359 
360         BookmarksEntry entry = bookmarksEntryPersistence.fetchByPrimaryKey(
361             entryId);
362 
363         if (entry == null) {
364             return;
365         }
366 
367         reIndex(entry);
368     }
369 
370     public BookmarksEntry updateEntry(
371             long userId, long entryId, long folderId, String name, String url,
372             String comments, String[] tagsEntries)
373         throws PortalException, SystemException {
374 
375         // Entry
376 
377         BookmarksEntry entry =
378             bookmarksEntryPersistence.findByPrimaryKey(entryId);
379 
380         BookmarksFolder folder = getFolder(entry, folderId);
381 
382         if (Validator.isNull(name)) {
383             name = url;
384         }
385 
386         validate(url);
387 
388         entry.setModifiedDate(new Date());
389         entry.setFolderId(folder.getFolderId());
390         entry.setName(name);
391         entry.setUrl(url);
392         entry.setComments(comments);
393 
394         bookmarksEntryPersistence.update(entry, false);
395 
396         // Tags
397 
398         updateTagsAsset(userId, entry, tagsEntries);
399 
400         // Indexer
401 
402         reIndex(entry);
403 
404         return entry;
405     }
406 
407     public void updateTagsAsset(
408             long userId, BookmarksEntry entry, String[] tagsEntries)
409         throws PortalException, SystemException {
410 
411         tagsAssetLocalService.updateAsset(
412             userId, entry.getGroupId(), BookmarksEntry.class.getName(),
413             entry.getEntryId(), tagsEntries, null, null, null, null,
414             ContentTypes.TEXT_PLAIN, entry.getName(), entry.getComments(), null,
415             entry.getUrl(), 0, 0, null, false);
416     }
417 
418     protected BookmarksFolder getFolder(BookmarksEntry entry, long folderId)
419         throws PortalException, SystemException {
420 
421         if (entry.getFolderId() != folderId) {
422             BookmarksFolder oldFolder =
423                 bookmarksFolderPersistence.findByPrimaryKey(
424                     entry.getFolderId());
425 
426             BookmarksFolder newFolder =
427                 bookmarksFolderPersistence.fetchByPrimaryKey(folderId);
428 
429             if ((newFolder == null) ||
430                 (oldFolder.getGroupId() != newFolder.getGroupId())) {
431 
432                 folderId = entry.getFolderId();
433             }
434         }
435 
436         return bookmarksFolderPersistence.findByPrimaryKey(folderId);
437     }
438 
439     protected void validate(String url) throws PortalException {
440         if (Validator.isNull(url)) {
441             throw new EntryURLException();
442         }
443         else {
444             try {
445                 new URL(url);
446             }
447             catch (MalformedURLException murle) {
448                 throw new EntryURLException();
449             }
450         }
451     }
452 
453     private static Log _log = LogFactoryUtil.getLog(
454         BookmarksEntryLocalServiceImpl.class);
455 
456 }