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