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