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