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