PageRenderTime 51ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/portal-impl/src/com/liferay/portlet/messageboards/service/impl/MBCategoryLocalServiceImpl.java

https://github.com/viktorkovacs/liferay-portal-trunk
Java | 575 lines | 384 code | 155 blank | 36 comment | 49 complexity | 41c67fae0f3ebf8dd22ef5c0e897ac1a MD5 | raw file
  1. /**
  2. * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
  3. *
  4. * This library is free software; you can redistribute it and/or modify it under
  5. * the terms of the GNU Lesser General Public License as published by the Free
  6. * Software Foundation; either version 2.1 of the License, or (at your option)
  7. * any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  12. * details.
  13. */
  14. package com.liferay.portlet.messageboards.service.impl;
  15. import com.liferay.portal.kernel.exception.PortalException;
  16. import com.liferay.portal.kernel.exception.SystemException;
  17. import com.liferay.portal.kernel.search.Indexer;
  18. import com.liferay.portal.kernel.search.IndexerRegistryUtil;
  19. import com.liferay.portal.kernel.util.Validator;
  20. import com.liferay.portal.model.ResourceConstants;
  21. import com.liferay.portal.model.User;
  22. import com.liferay.portal.service.ServiceContext;
  23. import com.liferay.portlet.expando.model.ExpandoBridge;
  24. import com.liferay.portlet.messageboards.CategoryNameException;
  25. import com.liferay.portlet.messageboards.NoSuchMailingListException;
  26. import com.liferay.portlet.messageboards.model.MBCategory;
  27. import com.liferay.portlet.messageboards.model.MBCategoryConstants;
  28. import com.liferay.portlet.messageboards.model.MBMailingList;
  29. import com.liferay.portlet.messageboards.model.MBMessage;
  30. import com.liferay.portlet.messageboards.model.MBThread;
  31. import com.liferay.portlet.messageboards.model.impl.MBCategoryImpl;
  32. import com.liferay.portlet.messageboards.service.base.MBCategoryLocalServiceBaseImpl;
  33. import java.util.ArrayList;
  34. import java.util.Date;
  35. import java.util.List;
  36. /**
  37. * @author Brian Wing Shun Chan
  38. * @author Wesley Gong
  39. */
  40. public class MBCategoryLocalServiceImpl extends MBCategoryLocalServiceBaseImpl {
  41. public MBCategory addCategory(
  42. long userId, long parentCategoryId, String name, String description,
  43. String displayStyle, String emailAddress, String inProtocol,
  44. String inServerName, int inServerPort, boolean inUseSSL,
  45. String inUserName, String inPassword, int inReadInterval,
  46. String outEmailAddress, boolean outCustom, String outServerName,
  47. int outServerPort, boolean outUseSSL, String outUserName,
  48. String outPassword, boolean mailingListActive,
  49. ServiceContext serviceContext)
  50. throws PortalException, SystemException {
  51. // Category
  52. User user = userPersistence.findByPrimaryKey(userId);
  53. long groupId = serviceContext.getScopeGroupId();
  54. parentCategoryId = getParentCategoryId(groupId, parentCategoryId);
  55. Date now = new Date();
  56. validate(name);
  57. long categoryId = counterLocalService.increment();
  58. MBCategory category = mbCategoryPersistence.create(categoryId);
  59. category.setUuid(serviceContext.getUuid());
  60. category.setGroupId(groupId);
  61. category.setCompanyId(user.getCompanyId());
  62. category.setUserId(user.getUserId());
  63. category.setUserName(user.getFullName());
  64. category.setCreateDate(serviceContext.getCreateDate(now));
  65. category.setModifiedDate(serviceContext.getModifiedDate(now));
  66. category.setParentCategoryId(parentCategoryId);
  67. category.setName(name);
  68. category.setDescription(description);
  69. category.setDisplayStyle(displayStyle);
  70. mbCategoryPersistence.update(category, false);
  71. // Resources
  72. if (serviceContext.getAddCommunityPermissions() ||
  73. serviceContext.getAddGuestPermissions()) {
  74. addCategoryResources(
  75. category, serviceContext.getAddCommunityPermissions(),
  76. serviceContext.getAddGuestPermissions());
  77. }
  78. else {
  79. addCategoryResources(
  80. category, serviceContext.getCommunityPermissions(),
  81. serviceContext.getGuestPermissions());
  82. }
  83. // Mailing list
  84. mbMailingListLocalService.addMailingList(
  85. userId, groupId, category.getCategoryId(), emailAddress, inProtocol,
  86. inServerName, inServerPort, inUseSSL, inUserName, inPassword,
  87. inReadInterval, outEmailAddress, outCustom, outServerName,
  88. outServerPort, outUseSSL, outUserName, outPassword,
  89. mailingListActive, serviceContext);
  90. // Expando
  91. ExpandoBridge expandoBridge = category.getExpandoBridge();
  92. expandoBridge.setAttributes(serviceContext);
  93. return category;
  94. }
  95. public void addCategoryResources(
  96. long categoryId, boolean addCommunityPermissions,
  97. boolean addGuestPermissions)
  98. throws PortalException, SystemException {
  99. if ((categoryId == MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) ||
  100. (categoryId == MBCategoryConstants.DISCUSSION_CATEGORY_ID)) {
  101. return;
  102. }
  103. MBCategory category = mbCategoryPersistence.findByPrimaryKey(
  104. categoryId);
  105. addCategoryResources(
  106. category, addCommunityPermissions, addGuestPermissions);
  107. }
  108. public void addCategoryResources(
  109. long categoryId, String[] communityPermissions,
  110. String[] guestPermissions)
  111. throws PortalException, SystemException {
  112. if ((categoryId == MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) ||
  113. (categoryId == MBCategoryConstants.DISCUSSION_CATEGORY_ID)) {
  114. return;
  115. }
  116. MBCategory category = mbCategoryPersistence.findByPrimaryKey(
  117. categoryId);
  118. addCategoryResources(category, communityPermissions, guestPermissions);
  119. }
  120. public void addCategoryResources(
  121. MBCategory category, boolean addCommunityPermissions,
  122. boolean addGuestPermissions)
  123. throws PortalException, SystemException {
  124. resourceLocalService.addResources(
  125. category.getCompanyId(), category.getGroupId(),
  126. category.getUserId(), MBCategory.class.getName(),
  127. category.getCategoryId(), false, addCommunityPermissions,
  128. addGuestPermissions);
  129. }
  130. public void addCategoryResources(
  131. MBCategory category, String[] communityPermissions,
  132. String[] guestPermissions)
  133. throws PortalException, SystemException {
  134. resourceLocalService.addModelResources(
  135. category.getCompanyId(), category.getGroupId(),
  136. category.getUserId(), MBCategory.class.getName(),
  137. category.getCategoryId(), communityPermissions, guestPermissions);
  138. }
  139. public void deleteCategories(long groupId)
  140. throws PortalException, SystemException {
  141. List<MBCategory> categories = mbCategoryPersistence.findByG_P(
  142. groupId, MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID);
  143. for (MBCategory category : categories) {
  144. deleteCategory(category);
  145. }
  146. }
  147. public void deleteCategory(long categoryId)
  148. throws PortalException, SystemException {
  149. MBCategory category = mbCategoryPersistence.findByPrimaryKey(
  150. categoryId);
  151. deleteCategory(category);
  152. }
  153. public void deleteCategory(MBCategory category)
  154. throws PortalException, SystemException {
  155. // Categories
  156. List<MBCategory> categories = mbCategoryPersistence.findByG_P(
  157. category.getGroupId(), category.getCategoryId());
  158. for (MBCategory curCategory : categories) {
  159. deleteCategory(curCategory);
  160. }
  161. // Indexer
  162. Indexer indexer = IndexerRegistryUtil.getIndexer(MBMessage.class);
  163. indexer.delete(category);
  164. // Threads
  165. mbThreadLocalService.deleteThreads(
  166. category.getGroupId(), category.getCategoryId());
  167. // Mailing list
  168. try {
  169. mbMailingListLocalService.deleteCategoryMailingList(
  170. category.getGroupId(), category.getCategoryId());
  171. }
  172. catch (NoSuchMailingListException nsmle) {
  173. }
  174. // Subscriptions
  175. subscriptionLocalService.deleteSubscriptions(
  176. category.getCompanyId(), MBCategory.class.getName(),
  177. category.getCategoryId());
  178. // Expando
  179. expandoValueLocalService.deleteValues(
  180. MBCategory.class.getName(), category.getCategoryId());
  181. // Resources
  182. resourceLocalService.deleteResource(
  183. category.getCompanyId(), MBCategory.class.getName(),
  184. ResourceConstants.SCOPE_INDIVIDUAL, category.getCategoryId());
  185. // Category
  186. mbCategoryPersistence.remove(category);
  187. }
  188. public List<MBCategory> getCategories(long groupId) throws SystemException {
  189. return mbCategoryPersistence.findByGroupId(groupId);
  190. }
  191. public List<MBCategory> getCategories(
  192. long groupId, long parentCategoryId, int start, int end)
  193. throws SystemException {
  194. return mbCategoryPersistence.findByG_P(
  195. groupId, parentCategoryId, start, end);
  196. }
  197. public List<MBCategory> getCategories(
  198. long groupId, long[] parentCategoryIds, int start, int end)
  199. throws SystemException {
  200. return mbCategoryPersistence.findByG_P(
  201. groupId, parentCategoryIds, start, end);
  202. }
  203. public int getCategoriesCount(long groupId) throws SystemException {
  204. return mbCategoryPersistence.countByGroupId(groupId);
  205. }
  206. public int getCategoriesCount(long groupId, long parentCategoryId)
  207. throws SystemException {
  208. return mbCategoryPersistence.countByG_P(groupId, parentCategoryId);
  209. }
  210. public int getCategoriesCount(long groupId, long[] parentCategoryIds)
  211. throws SystemException {
  212. return mbCategoryPersistence.countByG_P(groupId, parentCategoryIds);
  213. }
  214. public MBCategory getCategory(long categoryId)
  215. throws PortalException, SystemException {
  216. MBCategory category = null;
  217. if ((categoryId != MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) &&
  218. (categoryId != MBCategoryConstants.DISCUSSION_CATEGORY_ID)) {
  219. category = mbCategoryPersistence.findByPrimaryKey(categoryId);
  220. }
  221. else {
  222. category = new MBCategoryImpl();
  223. category.setCategoryId(categoryId);
  224. category.setParentCategoryId(categoryId);
  225. }
  226. return category;
  227. }
  228. public List<MBCategory> getCompanyCategories(
  229. long companyId, int start, int end)
  230. throws SystemException {
  231. return mbCategoryPersistence.findByCompanyId(companyId, start, end);
  232. }
  233. public int getCompanyCategoriesCount(long companyId)
  234. throws SystemException {
  235. return mbCategoryPersistence.countByCompanyId(companyId);
  236. }
  237. public List<Long> getSubcategoryIds(
  238. List<Long> categoryIds, long groupId, long categoryId)
  239. throws SystemException {
  240. List<MBCategory> categories = mbCategoryPersistence.findByG_P(
  241. groupId, categoryId);
  242. for (MBCategory category : categories) {
  243. categoryIds.add(category.getCategoryId());
  244. getSubcategoryIds(
  245. categoryIds, category.getGroupId(), category.getCategoryId());
  246. }
  247. return categoryIds;
  248. }
  249. public List<MBCategory> getSubscribedCategories(
  250. long groupId, long userId, int start, int end)
  251. throws SystemException {
  252. return mbCategoryFinder.findByS_G_U_P(
  253. groupId, userId, null, start, end);
  254. }
  255. public int getSubscribedCategoriesCount(long groupId, long userId)
  256. throws SystemException {
  257. return mbCategoryFinder.countByS_G_U_P(groupId, userId, null);
  258. }
  259. public void subscribeCategory(long userId, long groupId, long categoryId)
  260. throws PortalException, SystemException {
  261. if (categoryId == MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) {
  262. categoryId = groupId;
  263. }
  264. subscriptionLocalService.addSubscription(
  265. userId, groupId, MBCategory.class.getName(), categoryId);
  266. }
  267. public void unsubscribeCategory(long userId, long groupId, long categoryId)
  268. throws PortalException, SystemException {
  269. if (categoryId == MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) {
  270. categoryId = groupId;
  271. }
  272. subscriptionLocalService.deleteSubscription(
  273. userId, MBCategory.class.getName(), categoryId);
  274. }
  275. public MBCategory updateCategory(
  276. long categoryId, long parentCategoryId, String name,
  277. String description, String displayStyle, String emailAddress,
  278. String inProtocol, String inServerName, int inServerPort,
  279. boolean inUseSSL, String inUserName, String inPassword,
  280. int inReadInterval, String outEmailAddress, boolean outCustom,
  281. String outServerName, int outServerPort, boolean outUseSSL,
  282. String outUserName, String outPassword, boolean mailingListActive,
  283. boolean mergeWithParentCategory, ServiceContext serviceContext)
  284. throws PortalException, SystemException {
  285. // Merge categories
  286. if ((categoryId == MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) ||
  287. (categoryId == MBCategoryConstants.DISCUSSION_CATEGORY_ID)) {
  288. return null;
  289. }
  290. MBCategory category = mbCategoryPersistence.findByPrimaryKey(
  291. categoryId);
  292. parentCategoryId = getParentCategoryId(category, parentCategoryId);
  293. if (mergeWithParentCategory &&
  294. (categoryId != parentCategoryId) &&
  295. (parentCategoryId !=
  296. MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) &&
  297. (parentCategoryId != MBCategoryConstants.DISCUSSION_CATEGORY_ID)) {
  298. mergeCategories(category, parentCategoryId);
  299. return category;
  300. }
  301. // Category
  302. validate(name);
  303. category.setModifiedDate(serviceContext.getModifiedDate(null));
  304. category.setParentCategoryId(parentCategoryId);
  305. category.setName(name);
  306. category.setDescription(description);
  307. category.setDisplayStyle(displayStyle);
  308. mbCategoryPersistence.update(category, false);
  309. // Mailing list
  310. MBMailingList mailingList = mbMailingListPersistence.fetchByG_C(
  311. category.getGroupId(), category.getCategoryId());
  312. if (mailingList != null) {
  313. mbMailingListLocalService.updateMailingList(
  314. mailingList.getMailingListId(), emailAddress, inProtocol,
  315. inServerName, inServerPort, inUseSSL, inUserName, inPassword,
  316. inReadInterval, outEmailAddress, outCustom, outServerName,
  317. outServerPort, outUseSSL, outUserName, outPassword,
  318. mailingListActive, serviceContext);
  319. }
  320. else {
  321. mbMailingListLocalService.addMailingList(
  322. category.getUserId(), category.getGroupId(),
  323. category.getCategoryId(), emailAddress, inProtocol,
  324. inServerName, inServerPort, inUseSSL, inUserName, inPassword,
  325. inReadInterval, outEmailAddress, outCustom, outServerName,
  326. outServerPort, outUseSSL, outUserName, outPassword,
  327. mailingListActive, serviceContext);
  328. }
  329. // Expando
  330. ExpandoBridge expandoBridge = category.getExpandoBridge();
  331. expandoBridge.setAttributes(serviceContext);
  332. return category;
  333. }
  334. protected long getParentCategoryId(long groupId, long parentCategoryId)
  335. throws SystemException {
  336. if ((parentCategoryId !=
  337. MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) &&
  338. (parentCategoryId != MBCategoryConstants.DISCUSSION_CATEGORY_ID)) {
  339. MBCategory parentCategory = mbCategoryPersistence.fetchByPrimaryKey(
  340. parentCategoryId);
  341. if ((parentCategory == null) ||
  342. (groupId != parentCategory.getGroupId())) {
  343. parentCategoryId =
  344. MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID;
  345. }
  346. }
  347. return parentCategoryId;
  348. }
  349. protected long getParentCategoryId(
  350. MBCategory category, long parentCategoryId)
  351. throws SystemException {
  352. if ((parentCategoryId ==
  353. MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) ||
  354. (parentCategoryId == MBCategoryConstants.DISCUSSION_CATEGORY_ID)) {
  355. return parentCategoryId;
  356. }
  357. if (category.getCategoryId() == parentCategoryId) {
  358. return category.getParentCategoryId();
  359. }
  360. else {
  361. MBCategory parentCategory = mbCategoryPersistence.fetchByPrimaryKey(
  362. parentCategoryId);
  363. if ((parentCategory == null) ||
  364. (category.getGroupId() != parentCategory.getGroupId())) {
  365. return category.getParentCategoryId();
  366. }
  367. List<Long> subcategoryIds = new ArrayList<Long>();
  368. getSubcategoryIds(
  369. subcategoryIds, category.getGroupId(),
  370. category.getCategoryId());
  371. if (subcategoryIds.contains(parentCategoryId)) {
  372. return category.getParentCategoryId();
  373. }
  374. return parentCategoryId;
  375. }
  376. }
  377. protected void mergeCategories(MBCategory fromCategory, long toCategoryId)
  378. throws PortalException, SystemException {
  379. if ((toCategoryId == MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) ||
  380. (toCategoryId == MBCategoryConstants.DISCUSSION_CATEGORY_ID)) {
  381. return;
  382. }
  383. List<MBCategory> categories = mbCategoryPersistence.findByG_P(
  384. fromCategory.getGroupId(), fromCategory.getCategoryId());
  385. for (MBCategory category : categories) {
  386. mergeCategories(category, toCategoryId);
  387. }
  388. List<MBThread> threads = mbThreadPersistence.findByG_C(
  389. fromCategory.getGroupId(), fromCategory.getCategoryId());
  390. for (MBThread thread : threads) {
  391. // Thread
  392. thread.setCategoryId(toCategoryId);
  393. mbThreadPersistence.update(thread, false);
  394. List<MBMessage> messages = mbMessagePersistence.findByThreadId(
  395. thread.getThreadId());
  396. for (MBMessage message : messages) {
  397. // Message
  398. message.setCategoryId(toCategoryId);
  399. mbMessagePersistence.update(message, false);
  400. // Indexer
  401. Indexer indexer = IndexerRegistryUtil.getIndexer(
  402. MBMessage.class);
  403. indexer.reindex(message);
  404. }
  405. }
  406. MBCategory toCategory = mbCategoryPersistence.findByPrimaryKey(
  407. toCategoryId);
  408. toCategory.setThreadCount(
  409. fromCategory.getThreadCount() + toCategory.getThreadCount());
  410. toCategory.setMessageCount(
  411. fromCategory.getMessageCount() + toCategory.getMessageCount());
  412. mbCategoryPersistence.update(toCategory, false);
  413. deleteCategory(fromCategory);
  414. }
  415. protected void validate(String name) throws PortalException {
  416. if (Validator.isNull(name)) {
  417. throw new CategoryNameException();
  418. }
  419. }
  420. }