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.search.BooleanClauseOccur;
20 import com.liferay.portal.kernel.search.BooleanQuery;
21 import com.liferay.portal.kernel.search.BooleanQueryFactoryUtil;
22 import com.liferay.portal.kernel.search.Field;
23 import com.liferay.portal.kernel.search.Hits;
24 import com.liferay.portal.kernel.search.SearchEngineUtil;
25 import com.liferay.portal.kernel.search.TermQuery;
26 import com.liferay.portal.kernel.search.TermQueryFactoryUtil;
27 import com.liferay.portal.kernel.util.GetterUtil;
28 import com.liferay.portal.kernel.util.Validator;
29 import com.liferay.portal.model.Group;
30 import com.liferay.portal.model.ResourceConstants;
31 import com.liferay.portal.model.User;
32 import com.liferay.portal.service.ServiceContext;
33 import com.liferay.portlet.bookmarks.FolderNameException;
34 import com.liferay.portlet.bookmarks.model.BookmarksEntry;
35 import com.liferay.portlet.bookmarks.model.BookmarksFolder;
36 import com.liferay.portlet.bookmarks.model.impl.BookmarksFolderImpl;
37 import com.liferay.portlet.bookmarks.service.base.BookmarksFolderLocalServiceBaseImpl;
38 import com.liferay.portlet.bookmarks.util.Indexer;
39
40 import java.util.ArrayList;
41 import java.util.Date;
42 import java.util.List;
43
44
50 public class BookmarksFolderLocalServiceImpl
51 extends BookmarksFolderLocalServiceBaseImpl {
52
53 public BookmarksFolder addFolder(
54 long userId, long parentFolderId, String name, String description,
55 ServiceContext serviceContext)
56 throws PortalException, SystemException {
57
58 return addFolder(
59 null, userId, parentFolderId, name, description, serviceContext);
60 }
61
62 public BookmarksFolder addFolder(
63 String uuid, long userId, long parentFolderId, String name,
64 String description, ServiceContext serviceContext)
65 throws PortalException, SystemException {
66
67
69 User user = userPersistence.findByPrimaryKey(userId);
70 long groupId = serviceContext.getScopeGroupId();
71 parentFolderId = getParentFolderId(groupId, parentFolderId);
72 Date now = new Date();
73
74 validate(name);
75
76 long folderId = counterLocalService.increment();
77
78 BookmarksFolder folder = bookmarksFolderPersistence.create(folderId);
79
80 folder.setUuid(uuid);
81 folder.setGroupId(groupId);
82 folder.setCompanyId(user.getCompanyId());
83 folder.setUserId(user.getUserId());
84 folder.setCreateDate(serviceContext.getCreateDate(now));
85 folder.setModifiedDate(serviceContext.getModifiedDate(now));
86 folder.setParentFolderId(parentFolderId);
87 folder.setName(name);
88 folder.setDescription(description);
89 folder.setExpandoBridgeAttributes(serviceContext);
90
91 bookmarksFolderPersistence.update(folder, false);
92
93
95 if (serviceContext.getAddCommunityPermissions() ||
96 serviceContext.getAddGuestPermissions()) {
97
98 addFolderResources(
99 folder, serviceContext.getAddCommunityPermissions(),
100 serviceContext.getAddGuestPermissions());
101 }
102 else {
103 addFolderResources(
104 folder, serviceContext.getCommunityPermissions(),
105 serviceContext.getGuestPermissions());
106 }
107
108 return folder;
109 }
110
111 public void addFolderResources(
112 BookmarksFolder folder, boolean addCommunityPermissions,
113 boolean addGuestPermissions)
114 throws PortalException, SystemException {
115
116 resourceLocalService.addResources(
117 folder.getCompanyId(), folder.getGroupId(), folder.getUserId(),
118 BookmarksFolder.class.getName(), folder.getFolderId(), false,
119 addCommunityPermissions, addGuestPermissions);
120 }
121
122 public void addFolderResources(
123 BookmarksFolder folder, String[] communityPermissions,
124 String[] guestPermissions)
125 throws PortalException, SystemException {
126
127 resourceLocalService.addModelResources(
128 folder.getCompanyId(), folder.getGroupId(), folder.getUserId(),
129 BookmarksFolder.class.getName(), folder.getFolderId(),
130 communityPermissions, guestPermissions);
131 }
132
133 public void addFolderResources(
134 long folderId, boolean addCommunityPermissions,
135 boolean addGuestPermissions)
136 throws PortalException, SystemException {
137
138 BookmarksFolder folder = bookmarksFolderPersistence.findByPrimaryKey(
139 folderId);
140
141 addFolderResources(
142 folder, addCommunityPermissions, addGuestPermissions);
143 }
144
145 public void addFolderResources(
146 long folderId, String[] communityPermissions,
147 String[] guestPermissions)
148 throws PortalException, SystemException {
149
150 BookmarksFolder folder = bookmarksFolderPersistence.findByPrimaryKey(
151 folderId);
152
153 addFolderResources(folder, communityPermissions, guestPermissions);
154 }
155
156 public void deleteFolder(BookmarksFolder folder)
157 throws PortalException, SystemException {
158
159
161 bookmarksFolderPersistence.remove(folder);
162
163
165 resourceLocalService.deleteResource(
166 folder.getCompanyId(), BookmarksFolder.class.getName(),
167 ResourceConstants.SCOPE_INDIVIDUAL, folder.getFolderId());
168
169
171 bookmarksEntryLocalService.deleteEntries(folder.getFolderId());
172
173
175 List<BookmarksFolder> folders = bookmarksFolderPersistence.findByG_P(
176 folder.getGroupId(), folder.getFolderId());
177
178 for (BookmarksFolder curFolder : folders) {
179 deleteFolder(curFolder);
180 }
181
182
184 expandoValueLocalService.deleteValues(
185 BookmarksFolder.class.getName(), folder.getFolderId());
186 }
187
188 public void deleteFolder(long folderId)
189 throws PortalException, SystemException {
190
191 BookmarksFolder folder = bookmarksFolderPersistence.findByPrimaryKey(
192 folderId);
193
194 deleteFolder(folder);
195 }
196
197 public void deleteFolders(long groupId)
198 throws PortalException, SystemException {
199
200 List<BookmarksFolder> folders = bookmarksFolderPersistence.findByG_P(
201 groupId, BookmarksFolderImpl.DEFAULT_PARENT_FOLDER_ID);
202
203 for (BookmarksFolder folder : folders) {
204 deleteFolder(folder);
205 }
206 }
207
208 public BookmarksFolder getFolder(long folderId)
209 throws PortalException, SystemException {
210
211 return bookmarksFolderPersistence.findByPrimaryKey(folderId);
212 }
213
214 public List<BookmarksFolder> getFolders(
215 long groupId, long parentFolderId, int start, int end)
216 throws SystemException {
217
218 return bookmarksFolderPersistence.findByG_P(
219 groupId, parentFolderId, start, end);
220 }
221
222 public int getFoldersCount(long groupId, long parentFolderId)
223 throws SystemException {
224
225 return bookmarksFolderPersistence.countByG_P(groupId, parentFolderId);
226 }
227
228 public void getSubfolderIds(
229 List<Long> folderIds, long groupId, long folderId)
230 throws SystemException {
231
232 List<BookmarksFolder> folders = bookmarksFolderPersistence.findByG_P(
233 groupId, folderId);
234
235 for (BookmarksFolder folder : folders) {
236 folderIds.add(folder.getFolderId());
237
238 getSubfolderIds(
239 folderIds, folder.getGroupId(), folder.getFolderId());
240 }
241 }
242
243 public void reIndex(String[] ids) throws SystemException {
244 if (SearchEngineUtil.isIndexReadOnly()) {
245 return;
246 }
247
248 long companyId = GetterUtil.getLong(ids[0]);
249
250 try {
251 reIndexFolders(companyId);
252 }
253 catch (SystemException se) {
254 throw se;
255 }
256 catch (Exception e) {
257 throw new SystemException(e);
258 }
259 }
260
261 public Hits search(
262 long companyId, long groupId, long userId, long[] folderIds,
263 String keywords, int start, int end)
264 throws SystemException {
265
266 try {
267 BooleanQuery contextQuery = BooleanQueryFactoryUtil.create();
268
269 contextQuery.addRequiredTerm(Field.PORTLET_ID, Indexer.PORTLET_ID);
270
271 if (groupId > 0) {
272 Group group = groupLocalService.getGroup(groupId);
273
274 if (group.isLayout()) {
275 contextQuery.addRequiredTerm(Field.SCOPE_GROUP_ID, groupId);
276
277 groupId = group.getParentGroupId();
278 }
279
280 contextQuery.addRequiredTerm(Field.GROUP_ID, groupId);
281 }
282
283 if ((folderIds != null) && (folderIds.length > 0)) {
284 BooleanQuery folderIdsQuery = BooleanQueryFactoryUtil.create();
285
286 for (long folderId : folderIds) {
287 if (userId > 0) {
288 try {
289 bookmarksFolderService.getFolder(folderId);
290 }
291 catch (Exception e) {
292 continue;
293 }
294 }
295
296 TermQuery termQuery = TermQueryFactoryUtil.create(
297 "folderId", folderId);
298
299 folderIdsQuery.add(termQuery, BooleanClauseOccur.SHOULD);
300 }
301
302 contextQuery.add(folderIdsQuery, BooleanClauseOccur.MUST);
303 }
304
305 BooleanQuery searchQuery = BooleanQueryFactoryUtil.create();
306
307 if (Validator.isNotNull(keywords)) {
308 searchQuery.addTerm(Field.TITLE, keywords);
309 searchQuery.addTerm(Field.TAGS_ENTRIES, keywords, true);
310 searchQuery.addTerm(Field.URL, keywords);
311 searchQuery.addTerm(Field.COMMENTS, keywords);
312 }
313
314 BooleanQuery fullQuery = BooleanQueryFactoryUtil.create();
315
316 fullQuery.add(contextQuery, BooleanClauseOccur.MUST);
317
318 if (searchQuery.clauses().size() > 0) {
319 fullQuery.add(searchQuery, BooleanClauseOccur.MUST);
320 }
321
322 return SearchEngineUtil.search(
323 companyId, groupId, userId, BookmarksEntry.class.getName(),
324 fullQuery, start, end);
325 }
326 catch (Exception e) {
327 throw new SystemException(e);
328 }
329 }
330
331 public BookmarksFolder updateFolder(
332 long folderId, long parentFolderId, String name,
333 String description, boolean mergeWithParentFolder,
334 ServiceContext serviceContext)
335 throws PortalException, SystemException {
336
337
339 BookmarksFolder folder = bookmarksFolderPersistence.findByPrimaryKey(
340 folderId);
341
342 parentFolderId = getParentFolderId(folder, parentFolderId);
343
344 if (mergeWithParentFolder && (folderId != parentFolderId) &&
345 (parentFolderId != BookmarksFolderImpl.DEFAULT_PARENT_FOLDER_ID)) {
346
347 mergeFolders(folder, parentFolderId);
348
349 return folder;
350 }
351
352
354 validate(name);
355
356 folder.setModifiedDate(serviceContext.getModifiedDate(null));
357 folder.setParentFolderId(parentFolderId);
358 folder.setName(name);
359 folder.setDescription(description);
360 folder.setExpandoBridgeAttributes(serviceContext);
361
362 bookmarksFolderPersistence.update(folder, false);
363
364 return folder;
365 }
366
367 protected long getParentFolderId(
368 BookmarksFolder folder, long parentFolderId)
369 throws SystemException {
370
371 if (parentFolderId == BookmarksFolderImpl.DEFAULT_PARENT_FOLDER_ID) {
372 return parentFolderId;
373 }
374
375 if (folder.getFolderId() == parentFolderId) {
376 return folder.getParentFolderId();
377 }
378 else {
379 BookmarksFolder parentFolder =
380 bookmarksFolderPersistence.fetchByPrimaryKey(parentFolderId);
381
382 if ((parentFolder == null) ||
383 (folder.getGroupId() != parentFolder.getGroupId())) {
384
385 return folder.getParentFolderId();
386 }
387
388 List<Long> subfolderIds = new ArrayList<Long>();
389
390 getSubfolderIds(
391 subfolderIds, folder.getGroupId(), folder.getFolderId());
392
393 if (subfolderIds.contains(parentFolderId)) {
394 return folder.getParentFolderId();
395 }
396
397 return parentFolderId;
398 }
399 }
400
401 protected long getParentFolderId(long groupId, long parentFolderId)
402 throws SystemException {
403
404 if (parentFolderId != BookmarksFolderImpl.DEFAULT_PARENT_FOLDER_ID) {
405 BookmarksFolder parentFolder =
406 bookmarksFolderPersistence.fetchByPrimaryKey(parentFolderId);
407
408 if ((parentFolder == null) ||
409 (groupId != parentFolder.getGroupId())) {
410
411 parentFolderId = BookmarksFolderImpl.DEFAULT_PARENT_FOLDER_ID;
412 }
413 }
414
415 return parentFolderId;
416 }
417
418 protected void mergeFolders(BookmarksFolder fromFolder, long toFolderId)
419 throws PortalException, SystemException {
420
421 List<BookmarksFolder> folders = bookmarksFolderPersistence.findByG_P(
422 fromFolder.getGroupId(), fromFolder.getFolderId());
423
424 for (BookmarksFolder folder : folders) {
425 mergeFolders(folder, toFolderId);
426 }
427
428 List<BookmarksEntry> entries = bookmarksEntryPersistence.findByFolderId(
429 fromFolder.getFolderId());
430
431 for (BookmarksEntry entry : entries) {
432 entry.setFolderId(toFolderId);
433
434 bookmarksEntryPersistence.update(entry, false);
435
436 bookmarksEntryLocalService.reIndex(entry);
437 }
438
439 deleteFolder(fromFolder);
440 }
441
442 protected void reIndexEntries(long folderId, int entryStart, int entryEnd)
443 throws SystemException {
444
445 List<BookmarksEntry> entries = bookmarksEntryPersistence.findByFolderId(
446 folderId, entryStart, entryEnd);
447
448 for (BookmarksEntry entry : entries) {
449 bookmarksEntryLocalService.reIndex(entry);
450 }
451 }
452
453 protected void reIndexFolders(long companyId) throws SystemException {
454 int folderCount = bookmarksFolderPersistence.countByCompanyId(
455 companyId);
456
457 int folderPages = folderCount / Indexer.DEFAULT_INTERVAL;
458
459 for (int i = 0; i <= folderPages; i++) {
460 int folderStart = (i * Indexer.DEFAULT_INTERVAL);
461 int folderEnd = folderStart + Indexer.DEFAULT_INTERVAL;
462
463 reIndexFolders(companyId, folderStart, folderEnd);
464 }
465 }
466
467 protected void reIndexFolders(
468 long companyId, int folderStart, int folderEnd)
469 throws SystemException {
470
471 List<BookmarksFolder> folders =
472 bookmarksFolderPersistence.findByCompanyId(
473 companyId, folderStart, folderEnd);
474
475 for (BookmarksFolder folder : folders) {
476 long folderId = folder.getFolderId();
477
478 int entryCount = bookmarksEntryPersistence.countByFolderId(
479 folderId);
480
481 int entryPages = entryCount / Indexer.DEFAULT_INTERVAL;
482
483 for (int i = 0; i <= entryPages; i++) {
484 int entryStart = (i * Indexer.DEFAULT_INTERVAL);
485 int entryEnd = entryStart + Indexer.DEFAULT_INTERVAL;
486
487 reIndexEntries(folderId, entryStart, entryEnd);
488 }
489 }
490 }
491
492 protected void validate(String name) throws PortalException {
493 if ((Validator.isNull(name)) || (name.indexOf("\\\\") != -1) ||
494 (name.indexOf("//") != -1)) {
495
496 throw new FolderNameException();
497 }
498 }
499
500 }