1
22
23 package com.liferay.portlet.bookmarks.service.impl;
24
25 import com.liferay.counter.service.CounterLocalServiceUtil;
26 import com.liferay.portal.PortalException;
27 import com.liferay.portal.SystemException;
28 import com.liferay.portal.kernel.util.ContentTypes;
29 import com.liferay.portal.kernel.util.OrderByComparator;
30 import com.liferay.portal.kernel.util.Validator;
31 import com.liferay.portal.model.User;
32 import com.liferay.portal.model.impl.ResourceImpl;
33 import com.liferay.portal.service.ResourceLocalServiceUtil;
34 import com.liferay.portal.service.persistence.UserUtil;
35 import com.liferay.portlet.bookmarks.EntryURLException;
36 import com.liferay.portlet.bookmarks.model.BookmarksEntry;
37 import com.liferay.portlet.bookmarks.model.BookmarksFolder;
38 import com.liferay.portlet.bookmarks.service.base.BookmarksEntryLocalServiceBaseImpl;
39 import com.liferay.portlet.bookmarks.service.persistence.BookmarksEntryFinder;
40 import com.liferay.portlet.bookmarks.service.persistence.BookmarksEntryUtil;
41 import com.liferay.portlet.bookmarks.service.persistence.BookmarksFolderUtil;
42 import com.liferay.portlet.tags.service.TagsAssetLocalServiceUtil;
43
44 import java.net.MalformedURLException;
45 import java.net.URL;
46
47 import java.util.Date;
48 import java.util.Iterator;
49 import java.util.List;
50
51
58 public class BookmarksEntryLocalServiceImpl
59 extends BookmarksEntryLocalServiceBaseImpl {
60
61 public BookmarksEntry addEntry(
62 long userId, long folderId, String name, String url,
63 String comments, String[] tagsEntries,
64 boolean addCommunityPermissions, boolean addGuestPermissions)
65 throws PortalException, SystemException {
66
67 return addEntry(
68 userId, folderId, name, url, comments, tagsEntries,
69 Boolean.valueOf(addCommunityPermissions),
70 Boolean.valueOf(addGuestPermissions), null, null);
71 }
72
73 public BookmarksEntry addEntry(
74 long userId, long folderId, String name, String url,
75 String comments, String[] tagsEntries,
76 String[] communityPermissions, String[] guestPermissions)
77 throws PortalException, SystemException {
78
79 return addEntry(
80 userId, folderId, name, url, comments, tagsEntries, null, null,
81 communityPermissions, guestPermissions);
82 }
83
84 public BookmarksEntry addEntry(
85 long userId, long folderId, String name, String url,
86 String comments, String[] tagsEntries,
87 Boolean addCommunityPermissions, Boolean addGuestPermissions,
88 String[] communityPermissions, String[] guestPermissions)
89 throws PortalException, SystemException {
90
91
93 User user = UserUtil.findByPrimaryKey(userId);
94 BookmarksFolder folder = BookmarksFolderUtil.findByPrimaryKey(folderId);
95
96 if (Validator.isNull(name)) {
97 name = url;
98 }
99
100 Date now = new Date();
101
102 validate(url);
103
104 long entryId = CounterLocalServiceUtil.increment();
105
106 BookmarksEntry entry = BookmarksEntryUtil.create(entryId);
107
108 entry.setCompanyId(user.getCompanyId());
109 entry.setUserId(user.getUserId());
110 entry.setCreateDate(now);
111 entry.setModifiedDate(now);
112 entry.setFolderId(folderId);
113 entry.setName(name);
114 entry.setUrl(url);
115 entry.setComments(comments);
116
117 BookmarksEntryUtil.update(entry);
118
119
121 if ((addCommunityPermissions != null) &&
122 (addGuestPermissions != null)) {
123
124 addEntryResources(
125 folder, entry, addCommunityPermissions.booleanValue(),
126 addGuestPermissions.booleanValue());
127 }
128 else {
129 addEntryResources(
130 folder, entry, communityPermissions, guestPermissions);
131 }
132
133
135 updateTagsAsset(userId, entry, tagsEntries);
136
137 return entry;
138 }
139
140 public void addEntryResources(
141 long folderId, long entryId, boolean addCommunityPermissions,
142 boolean addGuestPermissions)
143 throws PortalException, SystemException {
144
145 BookmarksFolder folder = BookmarksFolderUtil.findByPrimaryKey(folderId);
146 BookmarksEntry entry = BookmarksEntryUtil.findByPrimaryKey(entryId);
147
148 addEntryResources(
149 folder, entry, addCommunityPermissions, addGuestPermissions);
150 }
151
152 public void addEntryResources(
153 BookmarksFolder folder, BookmarksEntry entry,
154 boolean addCommunityPermissions, boolean addGuestPermissions)
155 throws PortalException, SystemException {
156
157 ResourceLocalServiceUtil.addResources(
158 entry.getCompanyId(), folder.getGroupId(), entry.getUserId(),
159 BookmarksEntry.class.getName(), entry.getEntryId(), false,
160 addCommunityPermissions, addGuestPermissions);
161 }
162
163 public void addEntryResources(
164 long folderId, long entryId, String[] communityPermissions,
165 String[] guestPermissions)
166 throws PortalException, SystemException {
167
168 BookmarksFolder folder = BookmarksFolderUtil.findByPrimaryKey(folderId);
169 BookmarksEntry entry = BookmarksEntryUtil.findByPrimaryKey(entryId);
170
171 addEntryResources(
172 folder, entry, communityPermissions, guestPermissions);
173 }
174
175 public void addEntryResources(
176 BookmarksFolder folder, BookmarksEntry entry,
177 String[] communityPermissions, String[] guestPermissions)
178 throws PortalException, SystemException {
179
180 ResourceLocalServiceUtil.addModelResources(
181 entry.getCompanyId(), folder.getGroupId(), entry.getUserId(),
182 BookmarksEntry.class.getName(), entry.getEntryId(),
183 communityPermissions, guestPermissions);
184 }
185
186 public void deleteEntries(long folderId)
187 throws PortalException, SystemException {
188
189 Iterator itr = BookmarksEntryUtil.findByFolderId(folderId).iterator();
190
191 while (itr.hasNext()) {
192 BookmarksEntry entry = (BookmarksEntry)itr.next();
193
194 deleteEntry(entry);
195 }
196 }
197
198 public void deleteEntry(long entryId)
199 throws PortalException, SystemException {
200
201 BookmarksEntry entry = BookmarksEntryUtil.findByPrimaryKey(entryId);
202
203 deleteEntry(entry);
204 }
205
206 public void deleteEntry(BookmarksEntry entry)
207 throws PortalException, SystemException {
208
209
211 TagsAssetLocalServiceUtil.deleteAsset(
212 BookmarksEntry.class.getName(), entry.getEntryId());
213
214
216 ResourceLocalServiceUtil.deleteResource(
217 entry.getCompanyId(), BookmarksEntry.class.getName(),
218 ResourceImpl.SCOPE_INDIVIDUAL, entry.getEntryId());
219
220
222 BookmarksEntryUtil.remove(entry.getEntryId());
223 }
224
225 public List getEntries(long folderId, int begin, int end)
226 throws SystemException {
227
228 return BookmarksEntryUtil.findByFolderId(folderId, begin, end);
229 }
230
231 public List getEntries(
232 long folderId, int begin, int end,
233 OrderByComparator orderByComparator)
234 throws SystemException {
235
236 return BookmarksEntryUtil.findByFolderId(
237 folderId, begin, end, orderByComparator);
238 }
239
240 public int getEntriesCount(long folderId) throws SystemException {
241 return BookmarksEntryUtil.countByFolderId(folderId);
242 }
243
244 public BookmarksEntry getEntry(long entryId)
245 throws PortalException, SystemException {
246
247 return BookmarksEntryUtil.findByPrimaryKey(entryId);
248 }
249
250 public int getFoldersEntriesCount(List folderIds)
251 throws SystemException {
252
253 return BookmarksEntryFinder.countByFolderIds(folderIds);
254 }
255
256 public List getGroupEntries(long groupId, int begin, int end)
257 throws SystemException {
258
259 return BookmarksEntryFinder.findByGroupId(groupId, begin, end);
260 }
261
262 public List getGroupEntries(long groupId, long userId, int begin, int end)
263 throws SystemException {
264
265 if (userId <= 0) {
266 return BookmarksEntryFinder.findByGroupId(groupId, begin, end);
267 }
268 else {
269 return BookmarksEntryFinder.findByG_U(groupId, userId, begin, end);
270 }
271 }
272
273 public int getGroupEntriesCount(long groupId) throws SystemException {
274 return BookmarksEntryFinder.countByGroupId(groupId);
275 }
276
277 public int getGroupEntriesCount(long groupId, long userId)
278 throws SystemException {
279
280 if (userId <= 0) {
281 return BookmarksEntryFinder.countByGroupId(groupId);
282 }
283 else {
284 return BookmarksEntryFinder.countByG_U(groupId, userId);
285 }
286 }
287
288 public List getNoAssetEntries() throws SystemException {
289 return BookmarksEntryFinder.findByNoAssets();
290 }
291
292 public BookmarksEntry openEntry(long entryId)
293 throws PortalException, SystemException {
294
295 BookmarksEntry entry = BookmarksEntryUtil.findByPrimaryKey(entryId);
296
297 entry.setVisits(entry.getVisits() + 1);
298
299 BookmarksEntryUtil.update(entry);
300
301 return entry;
302 }
303
304 public BookmarksEntry updateEntry(
305 long userId, long entryId, long folderId, String name, String url,
306 String comments, String[] tagsEntries)
307 throws PortalException, SystemException {
308
309
311 BookmarksEntry entry = BookmarksEntryUtil.findByPrimaryKey(entryId);
312
313 BookmarksFolder folder = getFolder(entry, folderId);
314
315 if (Validator.isNull(name)) {
316 name = url;
317 }
318
319 validate(url);
320
321 entry.setModifiedDate(new Date());
322 entry.setFolderId(folder.getFolderId());
323 entry.setName(name);
324 entry.setUrl(url);
325 entry.setComments(comments);
326
327 BookmarksEntryUtil.update(entry);
328
329
331 updateTagsAsset(userId, entry, tagsEntries);
332
333 return entry;
334 }
335
336 public void updateTagsAsset(
337 long userId, BookmarksEntry entry, String[] tagsEntries)
338 throws PortalException, SystemException {
339
340 TagsAssetLocalServiceUtil.updateAsset(
341 userId, BookmarksEntry.class.getName(), entry.getEntryId(),
342 tagsEntries, null, null, null, null, ContentTypes.TEXT_PLAIN,
343 entry.getName(), entry.getComments(), entry.getComments(),
344 entry.getUrl(), 0, 0);
345 }
346
347 protected BookmarksFolder getFolder(BookmarksEntry entry, long folderId)
348 throws PortalException, SystemException {
349
350 if (entry.getFolderId() != folderId) {
351 BookmarksFolder oldFolder = BookmarksFolderUtil.findByPrimaryKey(
352 entry.getFolderId());
353
354 BookmarksFolder newFolder = BookmarksFolderUtil.fetchByPrimaryKey(
355 folderId);
356
357 if ((newFolder == null) ||
358 (oldFolder.getGroupId() != newFolder.getGroupId())) {
359
360 folderId = entry.getFolderId();
361 }
362 }
363
364 return BookmarksFolderUtil.findByPrimaryKey(folderId);
365 }
366
367 protected void validate(String url) throws PortalException {
368 if (Validator.isNull(url)) {
369 throw new EntryURLException();
370 }
371 else {
372 try {
373 new URL(url);
374 }
375 catch (MalformedURLException murle) {
376 throw new EntryURLException();
377 }
378 }
379 }
380
381 }