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