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(now);
96 category.setModifiedDate(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);
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 expandoValueLocalService.deleteValues(
237 MBCategory.class.getName(), category.getCategoryId());
238
239
241 resourceLocalService.deleteResource(
242 category.getCompanyId(), MBCategory.class.getName(),
243 ResourceConstants.SCOPE_INDIVIDUAL, category.getCategoryId());
244
245
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(long groupId, long parentCategoryId)
255 throws SystemException {
256
257 return mbCategoryPersistence.findByG_P(groupId, parentCategoryId);
258 }
259
260 public List<MBCategory> getCategories(
261 long groupId, long parentCategoryId, int start, int end)
262 throws SystemException {
263
264 return mbCategoryPersistence.findByG_P(
265 groupId, parentCategoryId, start, end);
266 }
267
268 public int getCategoriesCount(long groupId) throws SystemException {
269 return mbCategoryPersistence.countByGroupId(groupId);
270 }
271
272 public int getCategoriesCount(long groupId, long parentCategoryId)
273 throws SystemException {
274
275 return mbCategoryPersistence.countByG_P(groupId, parentCategoryId);
276 }
277
278 public MBCategory getCategory(long categoryId)
279 throws PortalException, SystemException {
280
281 return mbCategoryPersistence.findByPrimaryKey(categoryId);
282 }
283
284 public List<MBCategory> getCompanyCategories(
285 long companyId, int start, int end)
286 throws SystemException {
287
288 return mbCategoryPersistence.findByCompanyId(companyId, start, end);
289 }
290
291 public int getCompanyCategoriesCount(long companyId)
292 throws SystemException {
293
294 return mbCategoryPersistence.countByCompanyId(companyId);
295 }
296
297 public void getSubcategoryIds(
298 List<Long> categoryIds, long groupId, long categoryId)
299 throws SystemException {
300
301 List<MBCategory> categories = mbCategoryPersistence.findByG_P(
302 groupId, categoryId);
303
304 for (MBCategory category : categories) {
305 categoryIds.add(category.getCategoryId());
306
307 getSubcategoryIds(
308 categoryIds, category.getGroupId(), category.getCategoryId());
309 }
310 }
311
312 public List<MBCategory> getSubscribedCategories(
313 long groupId, long userId, int start, int end)
314 throws SystemException {
315
316 return mbCategoryFinder.findByS_G_U(groupId, userId, start, end);
317 }
318
319 public int getSubscribedCategoriesCount(long groupId, long userId)
320 throws SystemException {
321
322 return mbCategoryFinder.countByS_G_U(groupId, userId);
323 }
324
325 public MBCategory getSystemCategory() throws SystemException {
326 long categoryId = CompanyConstants.SYSTEM;
327
328 MBCategory category = mbCategoryPersistence.fetchByPrimaryKey(
329 categoryId);
330
331 if (category == null) {
332 category = mbCategoryPersistence.create(categoryId);
333
334 category.setCompanyId(CompanyConstants.SYSTEM);
335 category.setUserId(CompanyConstants.SYSTEM);
336
337 mbCategoryPersistence.update(category, false);
338 }
339
340 return category;
341 }
342
343 public void subscribeCategory(long userId, long groupId, long categoryId)
344 throws PortalException, SystemException {
345
346 if (categoryId == MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) {
347 categoryId = groupId;
348 }
349
350 subscriptionLocalService.addSubscription(
351 userId, MBCategory.class.getName(), categoryId);
352 }
353
354 public void unsubscribeCategory(long userId, long groupId, long categoryId)
355 throws PortalException, SystemException {
356
357 if (categoryId == MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) {
358 categoryId = groupId;
359 }
360
361 subscriptionLocalService.deleteSubscription(
362 userId, MBCategory.class.getName(), categoryId);
363 }
364
365 public MBCategory updateCategory(
366 long categoryId, long parentCategoryId, String name,
367 String description, String emailAddress, String inProtocol,
368 String inServerName, int inServerPort, boolean inUseSSL,
369 String inUserName, String inPassword, int inReadInterval,
370 String outEmailAddress, boolean outCustom, String outServerName,
371 int outServerPort, boolean outUseSSL, String outUserName,
372 String outPassword, boolean mailingListActive,
373 boolean mergeWithParentCategory, ServiceContext serviceContext)
374 throws PortalException, SystemException {
375
376
378 MBCategory category = mbCategoryPersistence.findByPrimaryKey(
379 categoryId);
380
381 parentCategoryId = getParentCategoryId(category, parentCategoryId);
382
383 if (mergeWithParentCategory &&
384 (categoryId != parentCategoryId) &&
385 (parentCategoryId !=
386 MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID)) {
387
388 mergeCategories(category, parentCategoryId);
389
390 return category;
391 }
392
393
395 validate(name);
396
397 category.setModifiedDate(new Date());
398 category.setParentCategoryId(parentCategoryId);
399 category.setName(name);
400 category.setDescription(description);
401
402 mbCategoryPersistence.update(category, false);
403
404
406 MBMailingList mailingList = mbMailingListPersistence.fetchByG_C(
407 category.getGroupId(), category.getCategoryId());
408
409 if (mailingList != null) {
410 mbMailingListLocalService.updateMailingList(
411 mailingList.getMailingListId(), emailAddress, inProtocol,
412 inServerName, inServerPort, inUseSSL, inUserName, inPassword,
413 inReadInterval, outEmailAddress, outCustom, outServerName,
414 outServerPort, outUseSSL, outUserName, outPassword,
415 mailingListActive);
416 }
417 else {
418 mbMailingListLocalService.addMailingList(
419 null, category.getUserId(), category.getGroupId(),
420 category.getCategoryId(), emailAddress, inProtocol,
421 inServerName, inServerPort, inUseSSL, inUserName, inPassword,
422 inReadInterval, outEmailAddress, outCustom, outServerName,
423 outServerPort, outUseSSL, outUserName, outPassword,
424 mailingListActive);
425 }
426
427
429 ExpandoBridge expandoBridge = category.getExpandoBridge();
430
431 expandoBridge.setAttributes(serviceContext);
432
433 return category;
434 }
435
436 protected long getParentCategoryId(long groupId, long parentCategoryId)
437 throws SystemException {
438
439 if (parentCategoryId !=
440 MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) {
441
442 MBCategory parentCategory = mbCategoryPersistence.fetchByPrimaryKey(
443 parentCategoryId);
444
445 if ((parentCategory == null) ||
446 (groupId != parentCategory.getGroupId())) {
447
448 parentCategoryId =
449 MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID;
450 }
451 }
452
453 return parentCategoryId;
454 }
455
456 protected long getParentCategoryId(
457 MBCategory category, long parentCategoryId)
458 throws SystemException {
459
460 if (parentCategoryId ==
461 MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) {
462
463 return parentCategoryId;
464 }
465
466 if (category.getCategoryId() == parentCategoryId) {
467 return category.getParentCategoryId();
468 }
469 else {
470 MBCategory parentCategory = mbCategoryPersistence.fetchByPrimaryKey(
471 parentCategoryId);
472
473 if ((parentCategory == null) ||
474 (category.getGroupId() != parentCategory.getGroupId())) {
475
476 return category.getParentCategoryId();
477 }
478
479 List<Long> subcategoryIds = new ArrayList<Long>();
480
481 getSubcategoryIds(
482 subcategoryIds, category.getGroupId(),
483 category.getCategoryId());
484
485 if (subcategoryIds.contains(parentCategoryId)) {
486 return category.getParentCategoryId();
487 }
488
489 return parentCategoryId;
490 }
491 }
492
493 protected void mergeCategories(MBCategory fromCategory, long toCategoryId)
494 throws PortalException, SystemException {
495
496 List<MBCategory> categories = mbCategoryPersistence.findByG_P(
497 fromCategory.getGroupId(), fromCategory.getCategoryId());
498
499 for (MBCategory category : categories) {
500 mergeCategories(category, toCategoryId);
501 }
502
503 List<MBThread> threads = mbThreadPersistence.findByG_C(
504 fromCategory.getGroupId(), fromCategory.getCategoryId());
505
506 for (MBThread thread : threads) {
507
508
510 thread.setCategoryId(toCategoryId);
511
512 mbThreadPersistence.update(thread, false);
513
514 List<MBMessage> messages = mbMessagePersistence.findByThreadId(
515 thread.getThreadId());
516
517 for (MBMessage message : messages) {
518
519
521 message.setCategoryId(toCategoryId);
522
523 mbMessagePersistence.update(message, false);
524
525
527 Indexer indexer = IndexerRegistryUtil.getIndexer(
528 MBMessage.class);
529
530 indexer.reindex(message);
531 }
532 }
533
534 deleteCategory(fromCategory);
535 }
536
537 protected void validate(String name) throws PortalException {
538 if (Validator.isNull(name)) {
539 throw new CategoryNameException();
540 }
541 }
542
543 }