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 import com.liferay.portlet.messageboards.util.MBUtil;
036
037 import java.util.ArrayList;
038 import java.util.Date;
039 import java.util.List;
040
041
045 public class MBCategoryLocalServiceImpl extends MBCategoryLocalServiceBaseImpl {
046
047 public MBCategory addCategory(
048 long userId, long parentCategoryId, String name, String description,
049 String emailAddress, String inProtocol, String inServerName,
050 int inServerPort, boolean inUseSSL, String inUserName,
051 String inPassword, int inReadInterval, String outEmailAddress,
052 boolean outCustom, String outServerName, int outServerPort,
053 boolean outUseSSL, String outUserName, String outPassword,
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
081 mbCategoryPersistence.update(category, false);
082
083
084
085 if (serviceContext.getAddCommunityPermissions() ||
086 serviceContext.getAddGuestPermissions()) {
087
088 addCategoryResources(
089 category, serviceContext.getAddCommunityPermissions(),
090 serviceContext.getAddGuestPermissions());
091 }
092 else {
093 addCategoryResources(
094 category, serviceContext.getCommunityPermissions(),
095 serviceContext.getGuestPermissions());
096 }
097
098
099
100 mbMailingListLocalService.addMailingList(
101 userId, groupId, category.getCategoryId(), emailAddress, inProtocol,
102 inServerName, inServerPort, inUseSSL, inUserName, inPassword,
103 inReadInterval, outEmailAddress, outCustom, outServerName,
104 outServerPort, outUseSSL, outUserName, outPassword,
105 mailingListActive, serviceContext);
106
107
108
109 ExpandoBridge expandoBridge = category.getExpandoBridge();
110
111 expandoBridge.setAttributes(serviceContext);
112
113 return category;
114 }
115
116 public void addCategoryResources(
117 long categoryId, boolean addCommunityPermissions,
118 boolean addGuestPermissions)
119 throws PortalException, SystemException {
120
121 if (MBUtil.isDefaultParentCategoryId(categoryId) ||
122 MBUtil.isDiscussionCategoryId(categoryId)) {
123
124 return;
125 }
126
127 MBCategory category = mbCategoryPersistence.findByPrimaryKey(
128 categoryId);
129
130 addCategoryResources(
131 category, addCommunityPermissions, addGuestPermissions);
132 }
133
134 public void addCategoryResources(
135 long categoryId, String[] communityPermissions,
136 String[] guestPermissions)
137 throws PortalException, SystemException {
138
139 if (MBUtil.isDefaultParentCategoryId(categoryId) ||
140 MBUtil.isDiscussionCategoryId(categoryId)) {
141
142 return;
143 }
144
145 MBCategory category = mbCategoryPersistence.findByPrimaryKey(
146 categoryId);
147
148 addCategoryResources(category, communityPermissions, guestPermissions);
149 }
150
151 public void addCategoryResources(
152 MBCategory category, boolean addCommunityPermissions,
153 boolean addGuestPermissions)
154 throws PortalException, SystemException {
155
156 resourceLocalService.addResources(
157 category.getCompanyId(), category.getGroupId(),
158 category.getUserId(), MBCategory.class.getName(),
159 category.getCategoryId(), false, addCommunityPermissions,
160 addGuestPermissions);
161 }
162
163 public void addCategoryResources(
164 MBCategory category, String[] communityPermissions,
165 String[] guestPermissions)
166 throws PortalException, SystemException {
167
168 resourceLocalService.addModelResources(
169 category.getCompanyId(), category.getGroupId(),
170 category.getUserId(), MBCategory.class.getName(),
171 category.getCategoryId(), communityPermissions, guestPermissions);
172 }
173
174 public void deleteCategories(long groupId)
175 throws PortalException, SystemException {
176
177 List<MBCategory> categories = mbCategoryPersistence.findByG_P(
178 groupId, MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID);
179
180 for (MBCategory category : categories) {
181 deleteCategory(category);
182 }
183 }
184
185 public void deleteCategory(long categoryId)
186 throws PortalException, SystemException {
187
188 MBCategory category = mbCategoryPersistence.findByPrimaryKey(
189 categoryId);
190
191 deleteCategory(category);
192 }
193
194 public void deleteCategory(MBCategory category)
195 throws PortalException, SystemException {
196
197
198
199 List<MBCategory> categories = mbCategoryPersistence.findByG_P(
200 category.getGroupId(), category.getCategoryId());
201
202 for (MBCategory curCategory : categories) {
203 deleteCategory(curCategory);
204 }
205
206
207
208 Indexer indexer = IndexerRegistryUtil.getIndexer(MBMessage.class);
209
210 indexer.delete(category);
211
212
213
214 mbThreadLocalService.deleteThreads(
215 category.getGroupId(), category.getCategoryId());
216
217
218
219 try {
220 mbMailingListLocalService.deleteCategoryMailingList(
221 category.getGroupId(), category.getCategoryId());
222 }
223 catch (NoSuchMailingListException nsmle) {
224 }
225
226
227
228 subscriptionLocalService.deleteSubscriptions(
229 category.getCompanyId(), MBCategory.class.getName(),
230 category.getCategoryId());
231
232
233
234 expandoValueLocalService.deleteValues(
235 MBCategory.class.getName(), category.getCategoryId());
236
237
238
239 resourceLocalService.deleteResource(
240 category.getCompanyId(), MBCategory.class.getName(),
241 ResourceConstants.SCOPE_INDIVIDUAL, category.getCategoryId());
242
243
244
245 mbCategoryPersistence.remove(category);
246 }
247
248 public List<MBCategory> getCategories(long groupId) throws SystemException {
249 return mbCategoryPersistence.findByGroupId(groupId);
250 }
251
252 public List<MBCategory> getCategories(
253 long groupId, long parentCategoryId, int start, int end)
254 throws SystemException {
255
256 return mbCategoryPersistence.findByG_P(
257 groupId, parentCategoryId, start, end);
258 }
259
260 public List<MBCategory> getCategories(
261 long groupId, long[] parentCategoryIds, int start, int end)
262 throws SystemException {
263
264 return mbCategoryPersistence.findByG_P(
265 groupId, parentCategoryIds, 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 int getCategoriesCount(long groupId, long[] parentCategoryIds)
279 throws SystemException {
280
281 return mbCategoryPersistence.countByG_P(groupId, parentCategoryIds);
282 }
283
284 public MBCategory getCategory(long categoryId)
285 throws PortalException, SystemException {
286
287 MBCategory category = null;
288
289 if (!MBUtil.isDefaultParentCategoryId(categoryId) &&
290 !MBUtil.isDiscussionCategoryId(categoryId)) {
291
292 category = mbCategoryPersistence.findByPrimaryKey(categoryId);
293 }
294 else {
295 category = new MBCategoryImpl();
296
297 category.setCategoryId(categoryId);
298 category.setParentCategoryId(categoryId);
299 }
300
301 return category;
302 }
303
304 public List<MBCategory> getCompanyCategories(
305 long companyId, int start, int end)
306 throws SystemException {
307
308 return mbCategoryPersistence.findByCompanyId(companyId, start, end);
309 }
310
311 public int getCompanyCategoriesCount(long companyId)
312 throws SystemException {
313
314 return mbCategoryPersistence.countByCompanyId(companyId);
315 }
316
317 public List<Long> getSubcategoryIds(
318 List<Long> categoryIds, long groupId, long categoryId)
319 throws SystemException {
320
321 List<MBCategory> categories = mbCategoryPersistence.findByG_P(
322 groupId, categoryId);
323
324 for (MBCategory category : categories) {
325 categoryIds.add(category.getCategoryId());
326
327 getSubcategoryIds(
328 categoryIds, category.getGroupId(), category.getCategoryId());
329 }
330
331 return categoryIds;
332 }
333
334 public List<MBCategory> getSubscribedCategories(
335 long groupId, long userId, int start, int end)
336 throws SystemException {
337
338 return mbCategoryFinder.findByS_G_U_P(
339 groupId, userId, null, start, end);
340 }
341
342 public int getSubscribedCategoriesCount(long groupId, long userId)
343 throws SystemException {
344
345 return mbCategoryFinder.countByS_G_U_P(groupId, userId, null);
346 }
347
348 public void subscribeCategory(long userId, long groupId, long categoryId)
349 throws PortalException, SystemException {
350
351 if (MBUtil.isDefaultParentCategoryId(categoryId)) {
352 categoryId = groupId;
353 }
354
355 subscriptionLocalService.addSubscription(
356 userId, MBCategory.class.getName(), categoryId);
357 }
358
359 public void unsubscribeCategory(long userId, long groupId, long categoryId)
360 throws PortalException, SystemException {
361
362 if (MBUtil.isDefaultParentCategoryId(categoryId)) {
363 categoryId = groupId;
364 }
365
366 subscriptionLocalService.deleteSubscription(
367 userId, MBCategory.class.getName(), categoryId);
368 }
369
370 public MBCategory updateCategory(
371 long categoryId, long parentCategoryId, String name,
372 String description, String emailAddress, String inProtocol,
373 String inServerName, int inServerPort, boolean inUseSSL,
374 String inUserName, String inPassword, int inReadInterval,
375 String outEmailAddress, boolean outCustom, String outServerName,
376 int outServerPort, boolean outUseSSL, String outUserName,
377 String outPassword, boolean mailingListActive,
378 boolean mergeWithParentCategory, ServiceContext serviceContext)
379 throws PortalException, SystemException {
380
381
382
383 if (MBUtil.isDefaultParentCategoryId(categoryId) ||
384 MBUtil.isDiscussionCategoryId(categoryId)) {
385
386 return null;
387 }
388
389 MBCategory category = mbCategoryPersistence.findByPrimaryKey(
390 categoryId);
391
392 parentCategoryId = getParentCategoryId(category, parentCategoryId);
393
394 if (mergeWithParentCategory &&
395 (categoryId != parentCategoryId) &&
396 !MBUtil.isDefaultParentCategoryId(parentCategoryId) &&
397 !MBUtil.isDiscussionCategoryId(parentCategoryId)) {
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 (!MBUtil.isDefaultParentCategoryId(parentCategoryId) &&
451 !MBUtil.isDiscussionCategoryId(parentCategoryId)) {
452
453 MBCategory parentCategory = mbCategoryPersistence.fetchByPrimaryKey(
454 parentCategoryId);
455
456 if ((parentCategory == null) ||
457 (groupId != parentCategory.getGroupId())) {
458
459 parentCategoryId =
460 MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID;
461 }
462 }
463
464 return parentCategoryId;
465 }
466
467 protected long getParentCategoryId(
468 MBCategory category, long parentCategoryId)
469 throws SystemException {
470
471 if (MBUtil.isDefaultParentCategoryId(parentCategoryId) ||
472 MBUtil.isDiscussionCategoryId(parentCategoryId)) {
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 if (MBUtil.isDefaultParentCategoryId(toCategoryId) ||
508 MBUtil.isDiscussionCategoryId(toCategoryId)) {
509
510 return;
511 }
512
513 List<MBCategory> categories = mbCategoryPersistence.findByG_P(
514 fromCategory.getGroupId(), fromCategory.getCategoryId());
515
516 for (MBCategory category : categories) {
517 mergeCategories(category, toCategoryId);
518 }
519
520 List<MBThread> threads = mbThreadPersistence.findByG_C(
521 fromCategory.getGroupId(), fromCategory.getCategoryId());
522
523 for (MBThread thread : threads) {
524
525
526
527 thread.setCategoryId(toCategoryId);
528
529 mbThreadPersistence.update(thread, false);
530
531 List<MBMessage> messages = mbMessagePersistence.findByThreadId(
532 thread.getThreadId());
533
534 for (MBMessage message : messages) {
535
536
537
538 message.setCategoryId(toCategoryId);
539
540 mbMessagePersistence.update(message, false);
541
542
543
544 Indexer indexer = IndexerRegistryUtil.getIndexer(
545 MBMessage.class);
546
547 indexer.reindex(message);
548 }
549 }
550
551 MBCategory toCategory = mbCategoryPersistence.findByPrimaryKey(
552 toCategoryId);
553
554 toCategory.setThreadCount(
555 fromCategory.getThreadCount() + toCategory.getThreadCount());
556 toCategory.setMessageCount(
557 fromCategory.getMessageCount() + toCategory.getMessageCount());
558
559 mbCategoryPersistence.update(toCategory, false);
560
561 deleteCategory(fromCategory);
562 }
563
564 protected void validate(String name) throws PortalException {
565 if (Validator.isNull(name)) {
566 throw new CategoryNameException();
567 }
568 }
569
570 }