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