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