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