001
014
015 package com.liferay.portlet.messageboards.service.impl;
016
017 import com.liferay.portal.kernel.dao.orm.QueryUtil;
018 import com.liferay.portal.kernel.exception.PortalException;
019 import com.liferay.portal.kernel.exception.SystemException;
020 import com.liferay.portal.kernel.search.Indexer;
021 import com.liferay.portal.kernel.search.IndexerRegistryUtil;
022 import com.liferay.portal.kernel.util.Validator;
023 import com.liferay.portal.kernel.workflow.WorkflowConstants;
024 import com.liferay.portal.model.ResourceConstants;
025 import com.liferay.portal.model.User;
026 import com.liferay.portal.service.ServiceContext;
027 import com.liferay.portlet.expando.model.ExpandoBridge;
028 import com.liferay.portlet.messageboards.CategoryNameException;
029 import com.liferay.portlet.messageboards.NoSuchMailingListException;
030 import com.liferay.portlet.messageboards.model.MBCategory;
031 import com.liferay.portlet.messageboards.model.MBCategoryConstants;
032 import com.liferay.portlet.messageboards.model.MBMailingList;
033 import com.liferay.portlet.messageboards.model.MBMessage;
034 import com.liferay.portlet.messageboards.model.MBThread;
035 import com.liferay.portlet.messageboards.model.impl.MBCategoryImpl;
036 import com.liferay.portlet.messageboards.service.base.MBCategoryLocalServiceBaseImpl;
037
038 import java.util.ArrayList;
039 import java.util.Date;
040 import java.util.List;
041
042
046 public class MBCategoryLocalServiceImpl extends MBCategoryLocalServiceBaseImpl {
047
048 @Override
049 public MBCategory addCategory(
050 long userId, long parentCategoryId, String name, String description,
051 String displayStyle, String emailAddress, String inProtocol,
052 String inServerName, int inServerPort, boolean inUseSSL,
053 String inUserName, String inPassword, int inReadInterval,
054 String outEmailAddress, boolean outCustom, String outServerName,
055 int outServerPort, boolean outUseSSL, String outUserName,
056 String outPassword, boolean allowAnonymous,
057 boolean mailingListActive, ServiceContext serviceContext)
058 throws PortalException, SystemException {
059
060
061
062 User user = userPersistence.findByPrimaryKey(userId);
063 long groupId = serviceContext.getScopeGroupId();
064 parentCategoryId = getParentCategoryId(groupId, parentCategoryId);
065 Date now = new Date();
066
067 validate(name);
068
069 long categoryId = counterLocalService.increment();
070
071 MBCategory category = mbCategoryPersistence.create(categoryId);
072
073 category.setUuid(serviceContext.getUuid());
074 category.setGroupId(groupId);
075 category.setCompanyId(user.getCompanyId());
076 category.setUserId(user.getUserId());
077 category.setUserName(user.getFullName());
078 category.setCreateDate(serviceContext.getCreateDate(now));
079 category.setModifiedDate(serviceContext.getModifiedDate(now));
080 category.setParentCategoryId(parentCategoryId);
081 category.setName(name);
082 category.setDescription(description);
083 category.setDisplayStyle(displayStyle);
084
085 mbCategoryPersistence.update(category, false);
086
087
088
089 if (serviceContext.isAddGroupPermissions() ||
090 serviceContext.isAddGuestPermissions()) {
091
092 addCategoryResources(
093 category, serviceContext.isAddGroupPermissions(),
094 serviceContext.isAddGuestPermissions());
095 }
096 else {
097 addCategoryResources(
098 category, serviceContext.getGroupPermissions(),
099 serviceContext.getGuestPermissions());
100 }
101
102
103
104 mbMailingListLocalService.addMailingList(
105 userId, groupId, category.getCategoryId(), emailAddress, inProtocol,
106 inServerName, inServerPort, inUseSSL, inUserName, inPassword,
107 inReadInterval, outEmailAddress, outCustom, outServerName,
108 outServerPort, outUseSSL, outUserName, outPassword, allowAnonymous,
109 mailingListActive, serviceContext);
110
111
112
113 ExpandoBridge expandoBridge = category.getExpandoBridge();
114
115 expandoBridge.setAttributes(serviceContext);
116
117 return category;
118 }
119
120 @Override
121 public void addCategoryResources(
122 long categoryId, boolean addGroupPermissions,
123 boolean addGuestPermissions)
124 throws PortalException, SystemException {
125
126 if ((categoryId == MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) ||
127 (categoryId == MBCategoryConstants.DISCUSSION_CATEGORY_ID)) {
128
129 return;
130 }
131
132 MBCategory category = mbCategoryPersistence.findByPrimaryKey(
133 categoryId);
134
135 addCategoryResources(
136 category, addGroupPermissions, addGuestPermissions);
137 }
138
139 @Override
140 public void addCategoryResources(
141 long categoryId, String[] groupPermissions,
142 String[] guestPermissions)
143 throws PortalException, SystemException {
144
145 if ((categoryId == MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) ||
146 (categoryId == MBCategoryConstants.DISCUSSION_CATEGORY_ID)) {
147
148 return;
149 }
150
151 MBCategory category = mbCategoryPersistence.findByPrimaryKey(
152 categoryId);
153
154 addCategoryResources(category, groupPermissions, guestPermissions);
155 }
156
157 @Override
158 public void addCategoryResources(
159 MBCategory category, boolean addGroupPermissions,
160 boolean addGuestPermissions)
161 throws PortalException, SystemException {
162
163 resourceLocalService.addResources(
164 category.getCompanyId(), category.getGroupId(),
165 category.getUserId(), MBCategory.class.getName(),
166 category.getCategoryId(), false, addGroupPermissions,
167 addGuestPermissions);
168 }
169
170 @Override
171 public void addCategoryResources(
172 MBCategory category, String[] groupPermissions,
173 String[] guestPermissions)
174 throws PortalException, SystemException {
175
176 resourceLocalService.addModelResources(
177 category.getCompanyId(), category.getGroupId(),
178 category.getUserId(), MBCategory.class.getName(),
179 category.getCategoryId(), groupPermissions, guestPermissions);
180 }
181
182 @Override
183 public void deleteCategories(long groupId)
184 throws PortalException, SystemException {
185
186 List<MBCategory> categories = mbCategoryPersistence.findByG_P(
187 groupId, MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID);
188
189 for (MBCategory category : categories) {
190 deleteCategory(category);
191 }
192 }
193
194 @Override
195 public void deleteCategory(long categoryId)
196 throws PortalException, SystemException {
197
198 MBCategory category = mbCategoryPersistence.findByPrimaryKey(
199 categoryId);
200
201 deleteCategory(category);
202 }
203
204 @Override
205 public void deleteCategory(MBCategory category)
206 throws PortalException, SystemException {
207
208
209
210 List<MBCategory> categories = mbCategoryPersistence.findByG_P(
211 category.getGroupId(), category.getCategoryId());
212
213 for (MBCategory curCategory : categories) {
214 deleteCategory(curCategory);
215 }
216
217
218
219 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
220 MBMessage.class);
221
222 indexer.delete(category);
223
224
225
226 mbThreadLocalService.deleteThreads(
227 category.getGroupId(), category.getCategoryId());
228
229
230
231 try {
232 mbMailingListLocalService.deleteCategoryMailingList(
233 category.getGroupId(), category.getCategoryId());
234 }
235 catch (NoSuchMailingListException nsmle) {
236 }
237
238
239
240 subscriptionLocalService.deleteSubscriptions(
241 category.getCompanyId(), MBCategory.class.getName(),
242 category.getCategoryId());
243
244
245
246 expandoValueLocalService.deleteValues(
247 MBCategory.class.getName(), category.getCategoryId());
248
249
250
251 resourceLocalService.deleteResource(
252 category.getCompanyId(), MBCategory.class.getName(),
253 ResourceConstants.SCOPE_INDIVIDUAL, category.getCategoryId());
254
255
256
257 mbCategoryPersistence.remove(category);
258 }
259
260 @Override
261 public List<MBCategory> getCategories(long groupId) throws SystemException {
262 return mbCategoryPersistence.findByGroupId(groupId);
263 }
264
265 @Override
266 public List<MBCategory> getCategories(
267 long groupId, long parentCategoryId, int start, int end)
268 throws SystemException {
269
270 return mbCategoryPersistence.findByG_P(
271 groupId, parentCategoryId, start, end);
272 }
273
274 @Override
275 public List<MBCategory> getCategories(
276 long groupId, long[] parentCategoryIds, int start, int end)
277 throws SystemException {
278
279 return mbCategoryPersistence.findByG_P(
280 groupId, parentCategoryIds, start, end);
281 }
282
283 @Override
284 public List<Object> getCategoriesAndThreads(long groupId, long categoryId)
285 throws SystemException {
286
287 List<MBCategory> categories = getCategories(
288 groupId, categoryId, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
289
290 List<MBThread> threads = mbThreadLocalService.getThreads(
291 groupId, categoryId, WorkflowConstants.STATUS_ANY,
292 QueryUtil.ALL_POS, QueryUtil.ALL_POS);
293
294 List<Object> categoriesAndThreads = new ArrayList<Object>();
295
296 categoriesAndThreads.addAll(categories);
297 categoriesAndThreads.addAll(threads);
298
299 return categoriesAndThreads;
300 }
301
302 @Override
303 public int getCategoriesCount(long groupId) throws SystemException {
304 return mbCategoryPersistence.countByGroupId(groupId);
305 }
306
307 @Override
308 public int getCategoriesCount(long groupId, long parentCategoryId)
309 throws SystemException {
310
311 return mbCategoryPersistence.countByG_P(groupId, parentCategoryId);
312 }
313
314 @Override
315 public int getCategoriesCount(long groupId, long[] parentCategoryIds)
316 throws SystemException {
317
318 return mbCategoryPersistence.countByG_P(groupId, parentCategoryIds);
319 }
320
321 @Override
322 public MBCategory getCategory(long categoryId)
323 throws PortalException, SystemException {
324
325 MBCategory category = null;
326
327 if ((categoryId != MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) &&
328 (categoryId != MBCategoryConstants.DISCUSSION_CATEGORY_ID)) {
329
330 category = mbCategoryPersistence.findByPrimaryKey(categoryId);
331 }
332 else {
333 category = new MBCategoryImpl();
334
335 category.setCategoryId(categoryId);
336 category.setParentCategoryId(categoryId);
337 }
338
339 return category;
340 }
341
342 @Override
343 public List<MBCategory> getCompanyCategories(
344 long companyId, int start, int end)
345 throws SystemException {
346
347 return mbCategoryPersistence.findByCompanyId(companyId, start, end);
348 }
349
350 @Override
351 public int getCompanyCategoriesCount(long companyId)
352 throws SystemException {
353
354 return mbCategoryPersistence.countByCompanyId(companyId);
355 }
356
357 @Override
358 public List<Long> getSubcategoryIds(
359 List<Long> categoryIds, long groupId, long categoryId)
360 throws SystemException {
361
362 List<MBCategory> categories = mbCategoryPersistence.findByG_P(
363 groupId, categoryId);
364
365 for (MBCategory category : categories) {
366 categoryIds.add(category.getCategoryId());
367
368 getSubcategoryIds(
369 categoryIds, category.getGroupId(), category.getCategoryId());
370 }
371
372 return categoryIds;
373 }
374
375 @Override
376 public List<MBCategory> getSubscribedCategories(
377 long groupId, long userId, int start, int end)
378 throws SystemException {
379
380 return mbCategoryFinder.findByS_G_U_P(
381 groupId, userId, null, start, end);
382 }
383
384 @Override
385 public int getSubscribedCategoriesCount(long groupId, long userId)
386 throws SystemException {
387
388 return mbCategoryFinder.countByS_G_U_P(groupId, userId, null);
389 }
390
391 @Override
392 public void subscribeCategory(long userId, long groupId, long categoryId)
393 throws PortalException, SystemException {
394
395 if (categoryId == MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) {
396 categoryId = groupId;
397 }
398
399 subscriptionLocalService.addSubscription(
400 userId, groupId, MBCategory.class.getName(), categoryId);
401 }
402
403 @Override
404 public void unsubscribeCategory(long userId, long groupId, long categoryId)
405 throws PortalException, SystemException {
406
407 if (categoryId == MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) {
408 categoryId = groupId;
409 }
410
411 subscriptionLocalService.deleteSubscription(
412 userId, MBCategory.class.getName(), categoryId);
413 }
414
415 @Override
416 public MBCategory updateCategory(
417 long categoryId, long parentCategoryId, String name,
418 String description, String displayStyle, String emailAddress,
419 String inProtocol, String inServerName, int inServerPort,
420 boolean inUseSSL, String inUserName, String inPassword,
421 int inReadInterval, String outEmailAddress, boolean outCustom,
422 String outServerName, int outServerPort, boolean outUseSSL,
423 String outUserName, String outPassword, boolean allowAnonymous,
424 boolean mailingListActive, boolean mergeWithParentCategory,
425 ServiceContext serviceContext)
426 throws PortalException, SystemException {
427
428
429
430 if ((categoryId == MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) ||
431 (categoryId == MBCategoryConstants.DISCUSSION_CATEGORY_ID)) {
432
433 return null;
434 }
435
436 MBCategory category = mbCategoryPersistence.findByPrimaryKey(
437 categoryId);
438
439 parentCategoryId = getParentCategoryId(category, parentCategoryId);
440
441 if (mergeWithParentCategory &&
442 (categoryId != parentCategoryId) &&
443 (parentCategoryId !=
444 MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) &&
445 (parentCategoryId != MBCategoryConstants.DISCUSSION_CATEGORY_ID)) {
446
447 mergeCategories(category, parentCategoryId);
448
449 return category;
450 }
451
452
453
454 validate(name);
455
456 category.setModifiedDate(serviceContext.getModifiedDate(null));
457 category.setParentCategoryId(parentCategoryId);
458 category.setName(name);
459 category.setDescription(description);
460
461 if (!displayStyle.equals(category.getDisplayStyle())) {
462 category.setDisplayStyle(displayStyle);
463
464 updateChildCategoriesDisplayStyle(category, displayStyle);
465 }
466
467 mbCategoryPersistence.update(category, false);
468
469
470
471 MBMailingList mailingList = mbMailingListPersistence.fetchByG_C(
472 category.getGroupId(), category.getCategoryId());
473
474 if (mailingList != null) {
475 mbMailingListLocalService.updateMailingList(
476 mailingList.getMailingListId(), emailAddress, inProtocol,
477 inServerName, inServerPort, inUseSSL, inUserName, inPassword,
478 inReadInterval, outEmailAddress, outCustom, outServerName,
479 outServerPort, outUseSSL, outUserName, outPassword,
480 allowAnonymous, mailingListActive, serviceContext);
481 }
482 else {
483 mbMailingListLocalService.addMailingList(
484 category.getUserId(), category.getGroupId(),
485 category.getCategoryId(), emailAddress, inProtocol,
486 inServerName, inServerPort, inUseSSL, inUserName, inPassword,
487 inReadInterval, outEmailAddress, outCustom, outServerName,
488 outServerPort, outUseSSL, outUserName, outPassword,
489 allowAnonymous, mailingListActive, serviceContext);
490 }
491
492
493
494 ExpandoBridge expandoBridge = category.getExpandoBridge();
495
496 expandoBridge.setAttributes(serviceContext);
497
498 return category;
499 }
500
501 protected long getParentCategoryId(long groupId, long parentCategoryId)
502 throws SystemException {
503
504 if ((parentCategoryId !=
505 MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) &&
506 (parentCategoryId != MBCategoryConstants.DISCUSSION_CATEGORY_ID)) {
507
508 MBCategory parentCategory = mbCategoryPersistence.fetchByPrimaryKey(
509 parentCategoryId);
510
511 if ((parentCategory == null) ||
512 (groupId != parentCategory.getGroupId())) {
513
514 parentCategoryId =
515 MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID;
516 }
517 }
518
519 return parentCategoryId;
520 }
521
522 protected long getParentCategoryId(
523 MBCategory category, long parentCategoryId)
524 throws SystemException {
525
526 if ((parentCategoryId ==
527 MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) ||
528 (parentCategoryId == MBCategoryConstants.DISCUSSION_CATEGORY_ID)) {
529
530 return parentCategoryId;
531 }
532
533 if (category.getCategoryId() == parentCategoryId) {
534 return category.getParentCategoryId();
535 }
536 else {
537 MBCategory parentCategory = mbCategoryPersistence.fetchByPrimaryKey(
538 parentCategoryId);
539
540 if ((parentCategory == null) ||
541 (category.getGroupId() != parentCategory.getGroupId())) {
542
543 return category.getParentCategoryId();
544 }
545
546 List<Long> subcategoryIds = new ArrayList<Long>();
547
548 getSubcategoryIds(
549 subcategoryIds, category.getGroupId(),
550 category.getCategoryId());
551
552 if (subcategoryIds.contains(parentCategoryId)) {
553 return category.getParentCategoryId();
554 }
555
556 return parentCategoryId;
557 }
558 }
559
560 protected void mergeCategories(MBCategory fromCategory, long toCategoryId)
561 throws PortalException, SystemException {
562
563 if ((toCategoryId == MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) ||
564 (toCategoryId == MBCategoryConstants.DISCUSSION_CATEGORY_ID)) {
565
566 return;
567 }
568
569 List<MBCategory> categories = mbCategoryPersistence.findByG_P(
570 fromCategory.getGroupId(), fromCategory.getCategoryId());
571
572 for (MBCategory category : categories) {
573 mergeCategories(category, toCategoryId);
574 }
575
576 List<MBThread> threads = mbThreadPersistence.findByG_C(
577 fromCategory.getGroupId(), fromCategory.getCategoryId());
578
579 for (MBThread thread : threads) {
580
581
582
583 thread.setCategoryId(toCategoryId);
584
585 mbThreadPersistence.update(thread, false);
586
587 List<MBMessage> messages = mbMessagePersistence.findByThreadId(
588 thread.getThreadId());
589
590 for (MBMessage message : messages) {
591
592
593
594 message.setCategoryId(toCategoryId);
595
596 mbMessagePersistence.update(message, false);
597
598
599
600 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
601 MBMessage.class);
602
603 indexer.reindex(message);
604 }
605 }
606
607 MBCategory toCategory = mbCategoryPersistence.findByPrimaryKey(
608 toCategoryId);
609
610 toCategory.setThreadCount(
611 fromCategory.getThreadCount() + toCategory.getThreadCount());
612 toCategory.setMessageCount(
613 fromCategory.getMessageCount() + toCategory.getMessageCount());
614
615 mbCategoryPersistence.update(toCategory, false);
616
617 deleteCategory(fromCategory);
618 }
619
620 protected void updateChildCategoriesDisplayStyle(
621 MBCategory category, String displayStyle)
622 throws PortalException, SystemException {
623
624 List<MBCategory> categories = getCategories(
625 category.getGroupId(), category.getCategoryId(), QueryUtil.ALL_POS,
626 QueryUtil.ALL_POS);
627
628 for (MBCategory curCategory : categories) {
629 updateChildCategoriesDisplayStyle(curCategory, displayStyle);
630
631 curCategory.setDisplayStyle(displayStyle);
632
633 mbCategoryPersistence.update(curCategory, false);
634 }
635 }
636
637 protected void validate(String name) throws PortalException {
638 if (Validator.isNull(name)) {
639 throw new CategoryNameException();
640 }
641 }
642
643 }