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