1
14
15 package com.liferay.portlet.messageboards.service.impl;
16
17 import com.liferay.portal.kernel.exception.PortalException;
18 import com.liferay.portal.kernel.exception.SystemException;
19 import com.liferay.portal.kernel.search.Indexer;
20 import com.liferay.portal.kernel.search.IndexerRegistryUtil;
21 import com.liferay.portal.kernel.util.Validator;
22 import com.liferay.portal.model.CompanyConstants;
23 import com.liferay.portal.model.ResourceConstants;
24 import com.liferay.portal.model.User;
25 import com.liferay.portal.service.ServiceContext;
26 import com.liferay.portlet.expando.model.ExpandoBridge;
27 import com.liferay.portlet.messageboards.CategoryNameException;
28 import com.liferay.portlet.messageboards.NoSuchMailingListException;
29 import com.liferay.portlet.messageboards.model.MBCategory;
30 import com.liferay.portlet.messageboards.model.MBCategoryConstants;
31 import com.liferay.portlet.messageboards.model.MBMailingList;
32 import com.liferay.portlet.messageboards.model.MBMessage;
33 import com.liferay.portlet.messageboards.model.MBThread;
34 import com.liferay.portlet.messageboards.service.base.MBCategoryLocalServiceBaseImpl;
35
36 import java.util.ArrayList;
37 import java.util.Date;
38 import java.util.List;
39
40
46 public class MBCategoryLocalServiceImpl extends MBCategoryLocalServiceBaseImpl {
47
48 public MBCategory addCategory(
49 long userId, long parentCategoryId, String name, String description,
50 String emailAddress, String inProtocol, String inServerName,
51 int inServerPort, boolean inUseSSL, String inUserName,
52 String inPassword, int inReadInterval, String outEmailAddress,
53 boolean outCustom, String outServerName, int outServerPort,
54 boolean outUseSSL, String outUserName, String outPassword,
55 boolean mailingListActive, ServiceContext serviceContext)
56 throws PortalException, SystemException {
57
58 return addCategory(
59 null, userId, parentCategoryId, name, description, emailAddress,
60 inProtocol, inServerName, inServerPort, inUseSSL, inUserName,
61 inPassword, inReadInterval, outEmailAddress, outCustom,
62 outServerName, outServerPort, outUseSSL, outUserName, outPassword,
63 mailingListActive, serviceContext);
64 }
65
66 public MBCategory addCategory(
67 String uuid, long userId, long parentCategoryId,
68 String name, String description, String emailAddress,
69 String inProtocol, String inServerName, int inServerPort,
70 boolean inUseSSL, String inUserName, String inPassword,
71 int inReadInterval, String outEmailAddress, boolean outCustom,
72 String outServerName, int outServerPort, boolean outUseSSL,
73 String outUserName, String outPassword, boolean mailingListActive,
74 ServiceContext serviceContext)
75 throws PortalException, SystemException {
76
77
79 User user = userPersistence.findByPrimaryKey(userId);
80 long groupId = serviceContext.getScopeGroupId();
81 parentCategoryId = getParentCategoryId(groupId, parentCategoryId);
82 Date now = new Date();
83
84 validate(name);
85
86 long categoryId = counterLocalService.increment();
87
88 MBCategory category = mbCategoryPersistence.create(categoryId);
89
90 category.setUuid(uuid);
91 category.setGroupId(groupId);
92 category.setCompanyId(user.getCompanyId());
93 category.setUserId(user.getUserId());
94 category.setUserName(user.getFullName());
95 category.setCreateDate(serviceContext.getCreateDate(now));
96 category.setModifiedDate(serviceContext.getModifiedDate(now));
97 category.setParentCategoryId(parentCategoryId);
98 category.setName(name);
99 category.setDescription(description);
100
101 mbCategoryPersistence.update(category, false);
102
103
105 if (serviceContext.getAddCommunityPermissions() ||
106 serviceContext.getAddGuestPermissions()) {
107
108 addCategoryResources(
109 category, serviceContext.getAddCommunityPermissions(),
110 serviceContext.getAddGuestPermissions());
111 }
112 else {
113 addCategoryResources(
114 category, serviceContext.getCommunityPermissions(),
115 serviceContext.getGuestPermissions());
116 }
117
118
120 mbMailingListLocalService.addMailingList(
121 null, userId, groupId, category.getCategoryId(), emailAddress,
122 inProtocol, inServerName, inServerPort, inUseSSL, inUserName,
123 inPassword, inReadInterval, outEmailAddress, outCustom,
124 outServerName, outServerPort, outUseSSL, outUserName, outPassword,
125 mailingListActive, serviceContext);
126
127
129 ExpandoBridge expandoBridge = category.getExpandoBridge();
130
131 expandoBridge.setAttributes(serviceContext);
132
133 return category;
134 }
135
136 public void addCategoryResources(
137 long categoryId, boolean addCommunityPermissions,
138 boolean addGuestPermissions)
139 throws PortalException, SystemException {
140
141 MBCategory category = mbCategoryPersistence.findByPrimaryKey(
142 categoryId);
143
144 addCategoryResources(
145 category, addCommunityPermissions, addGuestPermissions);
146 }
147
148 public void addCategoryResources(
149 long categoryId, String[] communityPermissions,
150 String[] guestPermissions)
151 throws PortalException, SystemException {
152
153 MBCategory category = mbCategoryPersistence.findByPrimaryKey(
154 categoryId);
155
156 addCategoryResources(category, communityPermissions, guestPermissions);
157 }
158
159 public void addCategoryResources(
160 MBCategory category, boolean addCommunityPermissions,
161 boolean addGuestPermissions)
162 throws PortalException, SystemException {
163
164 resourceLocalService.addResources(
165 category.getCompanyId(), category.getGroupId(),
166 category.getUserId(), MBCategory.class.getName(),
167 category.getCategoryId(), false, addCommunityPermissions,
168 addGuestPermissions);
169 }
170
171 public void addCategoryResources(
172 MBCategory category, String[] communityPermissions,
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(), communityPermissions, guestPermissions);
180 }
181
182 public void deleteCategories(long groupId)
183 throws PortalException, SystemException {
184
185 List<MBCategory> categories = mbCategoryPersistence.findByG_P(
186 groupId, MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID);
187
188 for (MBCategory category : categories) {
189 deleteCategory(category);
190 }
191 }
192
193 public void deleteCategory(long categoryId)
194 throws PortalException, SystemException {
195
196 MBCategory category = mbCategoryPersistence.findByPrimaryKey(
197 categoryId);
198
199 deleteCategory(category);
200 }
201
202 public void deleteCategory(MBCategory category)
203 throws PortalException, SystemException {
204
205
207 List<MBCategory> categories = mbCategoryPersistence.findByG_P(
208 category.getGroupId(), category.getCategoryId());
209
210 for (MBCategory curCategory : categories) {
211 deleteCategory(curCategory);
212 }
213
214
216 Indexer indexer = IndexerRegistryUtil.getIndexer(MBMessage.class);
217
218 indexer.delete(category);
219
220
222 mbThreadLocalService.deleteThreads(
223 category.getGroupId(), category.getCategoryId());
224
225
227 try {
228 mbMailingListLocalService.deleteCategoryMailingList(
229 category.getGroupId(), category.getCategoryId());
230 }
231 catch (NoSuchMailingListException nsmle) {
232 }
233
234
236 subscriptionLocalService.deleteSubscriptions(
237 category.getCompanyId(), MBCategory.class.getName(),
238 category.getCategoryId());
239
240
242 expandoValueLocalService.deleteValues(
243 MBCategory.class.getName(), category.getCategoryId());
244
245
247 resourceLocalService.deleteResource(
248 category.getCompanyId(), MBCategory.class.getName(),
249 ResourceConstants.SCOPE_INDIVIDUAL, category.getCategoryId());
250
251
253 mbCategoryPersistence.remove(category);
254 }
255
256 public List<MBCategory> getCategories(long groupId) throws SystemException {
257 return mbCategoryPersistence.findByGroupId(groupId);
258 }
259
260 public List<MBCategory> getCategories(long groupId, long parentCategoryId)
261 throws SystemException {
262
263 return mbCategoryPersistence.findByG_P(groupId, parentCategoryId);
264 }
265
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 public int getCategoriesCount(long groupId) throws SystemException {
275 return mbCategoryPersistence.countByGroupId(groupId);
276 }
277
278 public int getCategoriesCount(long groupId, long parentCategoryId)
279 throws SystemException {
280
281 return mbCategoryPersistence.countByG_P(groupId, parentCategoryId);
282 }
283
284 public MBCategory getCategory(long categoryId)
285 throws PortalException, SystemException {
286
287 return mbCategoryPersistence.findByPrimaryKey(categoryId);
288 }
289
290 public List<MBCategory> getCompanyCategories(
291 long companyId, int start, int end)
292 throws SystemException {
293
294 return mbCategoryPersistence.findByCompanyId(companyId, start, end);
295 }
296
297 public int getCompanyCategoriesCount(long companyId)
298 throws SystemException {
299
300 return mbCategoryPersistence.countByCompanyId(companyId);
301 }
302
303 public List<Long> getSubcategoryIds(
304 List<Long> categoryIds, long groupId, long categoryId)
305 throws SystemException {
306
307 List<MBCategory> categories = mbCategoryPersistence.findByG_P(
308 groupId, categoryId);
309
310 for (MBCategory category : categories) {
311 categoryIds.add(category.getCategoryId());
312
313 getSubcategoryIds(
314 categoryIds, category.getGroupId(), category.getCategoryId());
315 }
316
317 return categoryIds;
318 }
319
320 public List<MBCategory> getSubscribedCategories(
321 long groupId, long userId, int start, int end)
322 throws SystemException {
323
324 return mbCategoryFinder.findByS_G_U(groupId, userId, start, end);
325 }
326
327 public int getSubscribedCategoriesCount(long groupId, long userId)
328 throws SystemException {
329
330 return mbCategoryFinder.countByS_G_U(groupId, userId);
331 }
332
333 public MBCategory getSystemCategory() throws SystemException {
334 long categoryId = CompanyConstants.SYSTEM;
335
336 MBCategory category = mbCategoryPersistence.fetchByPrimaryKey(
337 categoryId);
338
339 if (category == null) {
340 category = mbCategoryPersistence.create(categoryId);
341
342 category.setCompanyId(CompanyConstants.SYSTEM);
343 category.setUserId(CompanyConstants.SYSTEM);
344
345 mbCategoryPersistence.update(category, false);
346 }
347
348 return category;
349 }
350
351 public void subscribeCategory(long userId, long groupId, long categoryId)
352 throws PortalException, SystemException {
353
354 if (categoryId == MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) {
355 categoryId = groupId;
356 }
357
358 subscriptionLocalService.addSubscription(
359 userId, MBCategory.class.getName(), categoryId);
360 }
361
362 public void unsubscribeCategory(long userId, long groupId, long categoryId)
363 throws PortalException, SystemException {
364
365 if (categoryId == MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) {
366 categoryId = groupId;
367 }
368
369 subscriptionLocalService.deleteSubscription(
370 userId, MBCategory.class.getName(), categoryId);
371 }
372
373 public MBCategory updateCategory(
374 long categoryId, long parentCategoryId, String name,
375 String description, String emailAddress, String inProtocol,
376 String inServerName, int inServerPort, boolean inUseSSL,
377 String inUserName, String inPassword, int inReadInterval,
378 String outEmailAddress, boolean outCustom, String outServerName,
379 int outServerPort, boolean outUseSSL, String outUserName,
380 String outPassword, boolean mailingListActive,
381 boolean mergeWithParentCategory, ServiceContext serviceContext)
382 throws PortalException, SystemException {
383
384
386 MBCategory category = mbCategoryPersistence.findByPrimaryKey(
387 categoryId);
388
389 parentCategoryId = getParentCategoryId(category, parentCategoryId);
390
391 if (mergeWithParentCategory &&
392 (categoryId != parentCategoryId) &&
393 (parentCategoryId !=
394 MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) &&
395 (parentCategoryId != MBCategoryConstants.DISCUSSION_CATEGORY_ID)) {
396
397 mergeCategories(category, parentCategoryId);
398
399 return category;
400 }
401
402
404 validate(name);
405
406 category.setModifiedDate(serviceContext.getModifiedDate(null));
407 category.setParentCategoryId(parentCategoryId);
408 category.setName(name);
409 category.setDescription(description);
410
411 mbCategoryPersistence.update(category, false);
412
413
415 MBMailingList mailingList = mbMailingListPersistence.fetchByG_C(
416 category.getGroupId(), category.getCategoryId());
417
418 if (mailingList != null) {
419 mbMailingListLocalService.updateMailingList(
420 mailingList.getMailingListId(), emailAddress, inProtocol,
421 inServerName, inServerPort, inUseSSL, inUserName, inPassword,
422 inReadInterval, outEmailAddress, outCustom, outServerName,
423 outServerPort, outUseSSL, outUserName, outPassword,
424 mailingListActive, serviceContext);
425 }
426 else {
427 mbMailingListLocalService.addMailingList(
428 null, category.getUserId(), category.getGroupId(),
429 category.getCategoryId(), emailAddress, inProtocol,
430 inServerName, inServerPort, inUseSSL, inUserName, inPassword,
431 inReadInterval, outEmailAddress, outCustom, outServerName,
432 outServerPort, outUseSSL, outUserName, outPassword,
433 mailingListActive, serviceContext);
434 }
435
436
438 ExpandoBridge expandoBridge = category.getExpandoBridge();
439
440 expandoBridge.setAttributes(serviceContext);
441
442 return category;
443 }
444
445 protected long getParentCategoryId(long groupId, long parentCategoryId)
446 throws SystemException {
447
448 if ((parentCategoryId !=
449 MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) &&
450 (parentCategoryId != MBCategoryConstants.DISCUSSION_CATEGORY_ID)) {
451
452 MBCategory parentCategory = mbCategoryPersistence.fetchByPrimaryKey(
453 parentCategoryId);
454
455 if ((parentCategory == null) ||
456 (groupId != parentCategory.getGroupId())) {
457
458 parentCategoryId =
459 MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID;
460 }
461 }
462
463 return parentCategoryId;
464 }
465
466 protected long getParentCategoryId(
467 MBCategory category, long parentCategoryId)
468 throws SystemException {
469
470 if ((parentCategoryId ==
471 MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) ||
472 (parentCategoryId == MBCategoryConstants.DISCUSSION_CATEGORY_ID)) {
473
474 return parentCategoryId;
475 }
476
477 if (category.getCategoryId() == parentCategoryId) {
478 return category.getParentCategoryId();
479 }
480 else {
481 MBCategory parentCategory = mbCategoryPersistence.fetchByPrimaryKey(
482 parentCategoryId);
483
484 if ((parentCategory == null) ||
485 (category.getGroupId() != parentCategory.getGroupId())) {
486
487 return category.getParentCategoryId();
488 }
489
490 List<Long> subcategoryIds = new ArrayList<Long>();
491
492 getSubcategoryIds(
493 subcategoryIds, category.getGroupId(),
494 category.getCategoryId());
495
496 if (subcategoryIds.contains(parentCategoryId)) {
497 return category.getParentCategoryId();
498 }
499
500 return parentCategoryId;
501 }
502 }
503
504 protected void mergeCategories(MBCategory fromCategory, long toCategoryId)
505 throws PortalException, SystemException {
506
507 List<MBCategory> categories = mbCategoryPersistence.findByG_P(
508 fromCategory.getGroupId(), fromCategory.getCategoryId());
509
510 for (MBCategory category : categories) {
511 mergeCategories(category, toCategoryId);
512 }
513
514 List<MBThread> threads = mbThreadPersistence.findByG_C(
515 fromCategory.getGroupId(), fromCategory.getCategoryId());
516
517 for (MBThread thread : threads) {
518
519
521 thread.setCategoryId(toCategoryId);
522
523 mbThreadPersistence.update(thread, false);
524
525 List<MBMessage> messages = mbMessagePersistence.findByThreadId(
526 thread.getThreadId());
527
528 for (MBMessage message : messages) {
529
530
532 message.setCategoryId(toCategoryId);
533
534 mbMessagePersistence.update(message, false);
535
536
538 Indexer indexer = IndexerRegistryUtil.getIndexer(
539 MBMessage.class);
540
541 indexer.reindex(message);
542 }
543 }
544
545 MBCategory toCategory = mbCategoryPersistence.findByPrimaryKey(
546 toCategoryId);
547
548 toCategory.setThreadCount(
549 fromCategory.getThreadCount() + toCategory.getThreadCount());
550 toCategory.setMessageCount(
551 fromCategory.getMessageCount() + toCategory.getMessageCount());
552
553 mbCategoryPersistence.update(toCategory, false);
554
555 deleteCategory(fromCategory);
556 }
557
558 protected void validate(String name) throws PortalException {
559 if (Validator.isNull(name)) {
560 throw new CategoryNameException();
561 }
562 }
563
564 }