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