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