1
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.portlet.bookmarks.EntryURLException;
37 import com.liferay.portlet.bookmarks.model.BookmarksEntry;
38 import com.liferay.portlet.bookmarks.model.BookmarksFolder;
39 import com.liferay.portlet.bookmarks.service.base.BookmarksEntryLocalServiceBaseImpl;
40 import com.liferay.portlet.bookmarks.util.Indexer;
41 import com.liferay.portlet.bookmarks.util.comparator.EntryModifiedDateComparator;
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
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
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.setGroupId(folder.getGroupId());
122 entry.setCompanyId(user.getCompanyId());
123 entry.setUserId(user.getUserId());
124 entry.setCreateDate(now);
125 entry.setModifiedDate(now);
126 entry.setFolderId(folderId);
127 entry.setName(name);
128 entry.setUrl(url);
129 entry.setComments(comments);
130
131 bookmarksEntryPersistence.update(entry, false);
132
133
135 if ((addCommunityPermissions != null) &&
136 (addGuestPermissions != null)) {
137
138 addEntryResources(
139 entry, addCommunityPermissions.booleanValue(),
140 addGuestPermissions.booleanValue());
141 }
142 else {
143 addEntryResources(entry, communityPermissions, guestPermissions);
144 }
145
146
148 updateTagsAsset(userId, entry, tagsEntries);
149
150
152 reIndex(entry);
153
154 return entry;
155 }
156
157 public void addEntryResources(
158 long entryId, boolean addCommunityPermissions,
159 boolean addGuestPermissions)
160 throws PortalException, SystemException {
161
162 BookmarksEntry entry =
163 bookmarksEntryPersistence.findByPrimaryKey(entryId);
164
165 addEntryResources(entry, addCommunityPermissions, addGuestPermissions);
166 }
167
168 public void addEntryResources(
169 BookmarksEntry entry, boolean addCommunityPermissions,
170 boolean addGuestPermissions)
171 throws PortalException, SystemException {
172
173 resourceLocalService.addResources(
174 entry.getCompanyId(), entry.getGroupId(), entry.getUserId(),
175 BookmarksEntry.class.getName(), entry.getEntryId(), false,
176 addCommunityPermissions, addGuestPermissions);
177 }
178
179 public void addEntryResources(
180 long entryId, String[] communityPermissions,
181 String[] guestPermissions)
182 throws PortalException, SystemException {
183
184 BookmarksEntry entry =
185 bookmarksEntryPersistence.findByPrimaryKey(entryId);
186
187 addEntryResources(entry, communityPermissions, guestPermissions);
188 }
189
190 public void addEntryResources(
191 BookmarksEntry entry, String[] communityPermissions,
192 String[] guestPermissions)
193 throws PortalException, SystemException {
194
195 resourceLocalService.addModelResources(
196 entry.getCompanyId(), entry.getGroupId(), entry.getUserId(),
197 BookmarksEntry.class.getName(), entry.getEntryId(),
198 communityPermissions, guestPermissions);
199 }
200
201 public void deleteEntries(long folderId)
202 throws PortalException, SystemException {
203
204 Iterator<BookmarksEntry> itr = bookmarksEntryPersistence.findByFolderId(
205 folderId).iterator();
206
207 while (itr.hasNext()) {
208 BookmarksEntry entry = itr.next();
209
210 deleteEntry(entry);
211 }
212 }
213
214 public void deleteEntry(long entryId)
215 throws PortalException, SystemException {
216
217 BookmarksEntry entry =
218 bookmarksEntryPersistence.findByPrimaryKey(entryId);
219
220 deleteEntry(entry);
221 }
222
223 public void deleteEntry(BookmarksEntry entry)
224 throws PortalException, SystemException {
225
226
228 try {
229 Indexer.deleteEntry(entry.getCompanyId(), entry.getEntryId());
230 }
231 catch (SearchException se) {
232 _log.error("Deleting index " + entry.getEntryId(), se);
233 }
234
235
237 tagsAssetLocalService.deleteAsset(
238 BookmarksEntry.class.getName(), entry.getEntryId());
239
240
242 resourceLocalService.deleteResource(
243 entry.getCompanyId(), BookmarksEntry.class.getName(),
244 ResourceConstants.SCOPE_INDIVIDUAL, entry.getEntryId());
245
246
248 bookmarksEntryPersistence.remove(entry);
249 }
250
251 public List<BookmarksEntry> getEntries(long folderId, int start, int end)
252 throws SystemException {
253
254 return bookmarksEntryPersistence.findByFolderId(folderId, start, end);
255 }
256
257 public List<BookmarksEntry> getEntries(
258 long folderId, int start, int end,
259 OrderByComparator orderByComparator)
260 throws SystemException {
261
262 return bookmarksEntryPersistence.findByFolderId(
263 folderId, start, end, orderByComparator);
264 }
265
266 public int getEntriesCount(long folderId) throws SystemException {
267 return bookmarksEntryPersistence.countByFolderId(folderId);
268 }
269
270 public BookmarksEntry getEntry(long entryId)
271 throws PortalException, SystemException {
272
273 return bookmarksEntryPersistence.findByPrimaryKey(entryId);
274 }
275
276 public int getFoldersEntriesCount(List<Long> folderIds)
277 throws SystemException {
278
279 return bookmarksEntryFinder.countByFolderIds(folderIds);
280 }
281
282 public List<BookmarksEntry> getGroupEntries(
283 long groupId, int start, int end)
284 throws SystemException {
285
286 return bookmarksEntryPersistence.findByGroupId(
287 groupId, start, end, new EntryModifiedDateComparator());
288 }
289
290 public List<BookmarksEntry> getGroupEntries(
291 long groupId, long userId, int start, int end)
292 throws SystemException {
293
294 OrderByComparator orderByComparator = new EntryModifiedDateComparator();
295
296 if (userId <= 0) {
297 return bookmarksEntryPersistence.findByGroupId(
298 groupId, start, end, orderByComparator);
299 }
300 else {
301 return bookmarksEntryPersistence.findByG_U(
302 groupId, userId, start, end, orderByComparator);
303 }
304 }
305
306 public int getGroupEntriesCount(long groupId) throws SystemException {
307 return bookmarksEntryPersistence.countByGroupId(groupId);
308 }
309
310 public int getGroupEntriesCount(long groupId, long userId)
311 throws SystemException {
312
313 if (userId <= 0) {
314 return bookmarksEntryPersistence.countByGroupId(groupId);
315 }
316 else {
317 return bookmarksEntryPersistence.countByG_U(groupId, userId);
318 }
319 }
320
321 public List<BookmarksEntry> getNoAssetEntries() throws SystemException {
322 return bookmarksEntryFinder.findByNoAssets();
323 }
324
325 public BookmarksEntry openEntry(long entryId)
326 throws PortalException, SystemException {
327
328 BookmarksEntry entry =
329 bookmarksEntryPersistence.findByPrimaryKey(entryId);
330
331 entry.setVisits(entry.getVisits() + 1);
332
333 bookmarksEntryPersistence.update(entry, false);
334
335 tagsAssetLocalService.incrementViewCounter(
336 BookmarksEntry.class.getName(), entryId);
337
338 return entry;
339 }
340
341 public void reIndex(long entryId) throws SystemException {
342 if (SearchEngineUtil.isIndexReadOnly()) {
343 return;
344 }
345
346 BookmarksEntry entry = bookmarksEntryPersistence.fetchByPrimaryKey(
347 entryId);
348
349 if (entry == null) {
350 return;
351 }
352
353 reIndex(entry);
354 }
355
356 public void reIndex(BookmarksEntry entry) throws SystemException {
357 long companyId = entry.getCompanyId();
358 long groupId = entry.getGroupId();
359 long folderId = entry.getFolderId();
360 long entryId = entry.getEntryId();
361 String name = entry.getName();
362 String url = entry.getUrl();
363 String comments = entry.getComments();
364 Date modifiedDate = entry.getModifiedDate();
365
366 String[] tagsEntries = tagsEntryLocalService.getEntryNames(
367 BookmarksEntry.class.getName(), entryId);
368
369 try {
370 Indexer.updateEntry(
371 companyId, groupId, folderId, entryId, name, url, comments,
372 modifiedDate, tagsEntries);
373 }
374 catch (SearchException se) {
375 _log.error("Reindexing " + entryId, se);
376 }
377 }
378
379 public BookmarksEntry updateEntry(
380 long userId, long entryId, long folderId, String name, String url,
381 String comments, String[] tagsEntries)
382 throws PortalException, SystemException {
383
384
386 BookmarksEntry entry =
387 bookmarksEntryPersistence.findByPrimaryKey(entryId);
388
389 BookmarksFolder folder = getFolder(entry, folderId);
390
391 if (Validator.isNull(name)) {
392 name = url;
393 }
394
395 validate(url);
396
397 entry.setModifiedDate(new Date());
398 entry.setFolderId(folder.getFolderId());
399 entry.setName(name);
400 entry.setUrl(url);
401 entry.setComments(comments);
402
403 bookmarksEntryPersistence.update(entry, false);
404
405
407 updateTagsAsset(userId, entry, tagsEntries);
408
409
411 reIndex(entry);
412
413 return entry;
414 }
415
416 public void updateTagsAsset(
417 long userId, BookmarksEntry entry, String[] tagsEntries)
418 throws PortalException, SystemException {
419
420 tagsAssetLocalService.updateAsset(
421 userId, entry.getGroupId(), BookmarksEntry.class.getName(),
422 entry.getEntryId(), tagsEntries, null, null, null, null,
423 ContentTypes.TEXT_PLAIN, entry.getName(), entry.getComments(), null,
424 entry.getUrl(), 0, 0, null, false);
425 }
426
427 protected BookmarksFolder getFolder(BookmarksEntry entry, long folderId)
428 throws PortalException, SystemException {
429
430 if (entry.getFolderId() != folderId) {
431 BookmarksFolder oldFolder =
432 bookmarksFolderPersistence.findByPrimaryKey(
433 entry.getFolderId());
434
435 BookmarksFolder newFolder =
436 bookmarksFolderPersistence.fetchByPrimaryKey(folderId);
437
438 if ((newFolder == null) ||
439 (oldFolder.getGroupId() != newFolder.getGroupId())) {
440
441 folderId = entry.getFolderId();
442 }
443 }
444
445 return bookmarksFolderPersistence.findByPrimaryKey(folderId);
446 }
447
448 protected void validate(String url) throws PortalException {
449 if (Validator.isNull(url)) {
450 throw new EntryURLException();
451 }
452 else {
453 try {
454 new URL(url);
455 }
456 catch (MalformedURLException murle) {
457 throw new EntryURLException();
458 }
459 }
460 }
461
462 private static Log _log =
463 LogFactoryUtil.getLog(BookmarksEntryLocalServiceImpl.class);
464
465 }