/modules/apps/fragment/fragment-service/src/main/java/com/liferay/fragment/service/impl/FragmentCollectionLocalServiceImpl.java

https://github.com/danielreuther/liferay-portal · Java · 333 lines · 233 code · 78 blank · 22 comment · 16 complexity · c66932628d3c0ef24d2c9046cb3f1286 MD5 · raw file

  1. /**
  2. * Copyright (c) 2000-present 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.fragment.service.impl;
  15. import com.liferay.fragment.exception.DuplicateFragmentCollectionKeyException;
  16. import com.liferay.fragment.exception.FragmentCollectionNameException;
  17. import com.liferay.fragment.model.FragmentCollection;
  18. import com.liferay.fragment.model.FragmentComposition;
  19. import com.liferay.fragment.model.FragmentEntry;
  20. import com.liferay.fragment.service.FragmentCompositionLocalService;
  21. import com.liferay.fragment.service.FragmentEntryLocalService;
  22. import com.liferay.fragment.service.base.FragmentCollectionLocalServiceBaseImpl;
  23. import com.liferay.fragment.service.persistence.FragmentCompositionPersistence;
  24. import com.liferay.fragment.service.persistence.FragmentEntryPersistence;
  25. import com.liferay.petra.string.CharPool;
  26. import com.liferay.petra.string.StringPool;
  27. import com.liferay.portal.aop.AopService;
  28. import com.liferay.portal.kernel.exception.PortalException;
  29. import com.liferay.portal.kernel.model.ResourceConstants;
  30. import com.liferay.portal.kernel.model.SystemEventConstants;
  31. import com.liferay.portal.kernel.model.User;
  32. import com.liferay.portal.kernel.portletfilerepository.PortletFileRepositoryUtil;
  33. import com.liferay.portal.kernel.service.ResourceLocalService;
  34. import com.liferay.portal.kernel.service.ServiceContext;
  35. import com.liferay.portal.kernel.service.UserLocalService;
  36. import com.liferay.portal.kernel.systemevent.SystemEvent;
  37. import com.liferay.portal.kernel.util.OrderByComparator;
  38. import com.liferay.portal.kernel.util.StringUtil;
  39. import com.liferay.portal.kernel.util.TempFileEntryUtil;
  40. import com.liferay.portal.kernel.util.Validator;
  41. import java.util.Date;
  42. import java.util.List;
  43. import org.osgi.service.component.annotations.Component;
  44. import org.osgi.service.component.annotations.Reference;
  45. /**
  46. * @author Jürgen Kappler
  47. */
  48. @Component(
  49. property = "model.class.name=com.liferay.fragment.model.FragmentCollection",
  50. service = AopService.class
  51. )
  52. public class FragmentCollectionLocalServiceImpl
  53. extends FragmentCollectionLocalServiceBaseImpl {
  54. @Override
  55. public FragmentCollection addFragmentCollection(
  56. long userId, long groupId, String name, String description,
  57. ServiceContext serviceContext)
  58. throws PortalException {
  59. return addFragmentCollection(
  60. userId, groupId, StringPool.BLANK, name, description,
  61. serviceContext);
  62. }
  63. @Override
  64. public FragmentCollection addFragmentCollection(
  65. long userId, long groupId, String fragmentCollectionKey,
  66. String name, String description, ServiceContext serviceContext)
  67. throws PortalException {
  68. // Fragment collection
  69. User user = _userLocalService.getUser(userId);
  70. long companyId = user.getCompanyId();
  71. if (serviceContext != null) {
  72. companyId = serviceContext.getCompanyId();
  73. }
  74. else {
  75. serviceContext = new ServiceContext();
  76. }
  77. validate(name);
  78. if (Validator.isNull(fragmentCollectionKey)) {
  79. fragmentCollectionKey = generateFragmentCollectionKey(
  80. groupId, name);
  81. }
  82. fragmentCollectionKey = _getFragmentCollectionKey(
  83. fragmentCollectionKey);
  84. validateFragmentCollectionKey(groupId, fragmentCollectionKey);
  85. long fragmentCollectionId = counterLocalService.increment();
  86. FragmentCollection fragmentCollection =
  87. fragmentCollectionPersistence.create(fragmentCollectionId);
  88. fragmentCollection.setUuid(serviceContext.getUuid());
  89. fragmentCollection.setGroupId(groupId);
  90. fragmentCollection.setCompanyId(companyId);
  91. fragmentCollection.setUserId(user.getUserId());
  92. fragmentCollection.setUserName(user.getFullName());
  93. fragmentCollection.setCreateDate(
  94. serviceContext.getCreateDate(new Date()));
  95. fragmentCollection.setModifiedDate(
  96. serviceContext.getModifiedDate(new Date()));
  97. fragmentCollection.setFragmentCollectionKey(fragmentCollectionKey);
  98. fragmentCollection.setName(name);
  99. fragmentCollection.setDescription(description);
  100. return fragmentCollectionPersistence.update(fragmentCollection);
  101. }
  102. @Override
  103. @SystemEvent(type = SystemEventConstants.TYPE_DELETE)
  104. public FragmentCollection deleteFragmentCollection(
  105. FragmentCollection fragmentCollection)
  106. throws PortalException {
  107. // Fragment collection
  108. fragmentCollectionPersistence.remove(fragmentCollection);
  109. // Resources
  110. _resourceLocalService.deleteResource(
  111. fragmentCollection.getCompanyId(),
  112. FragmentCollection.class.getName(),
  113. ResourceConstants.SCOPE_INDIVIDUAL,
  114. fragmentCollection.getFragmentCollectionId());
  115. // Images
  116. PortletFileRepositoryUtil.deletePortletFolder(
  117. fragmentCollection.getResourcesFolderId(false));
  118. // Fragment compositions
  119. List<FragmentComposition> fragmentCompositions =
  120. _fragmentCompositionPersistence.findByFragmentCollectionId(
  121. fragmentCollection.getFragmentCollectionId());
  122. for (FragmentComposition fragmentComposition : fragmentCompositions) {
  123. _fragmentCompositionLocalService.deleteFragmentComposition(
  124. fragmentComposition);
  125. }
  126. // Fragment entries
  127. List<FragmentEntry> fragmentEntries =
  128. _fragmentEntryPersistence.findByFragmentCollectionId(
  129. fragmentCollection.getFragmentCollectionId());
  130. for (FragmentEntry fragmentEntry : fragmentEntries) {
  131. _fragmentEntryLocalService.deleteFragmentEntry(fragmentEntry);
  132. }
  133. return fragmentCollection;
  134. }
  135. @Override
  136. public FragmentCollection deleteFragmentCollection(
  137. long fragmentCollectionId)
  138. throws PortalException {
  139. return fragmentCollectionLocalService.deleteFragmentCollection(
  140. getFragmentCollection(fragmentCollectionId));
  141. }
  142. @Override
  143. public FragmentCollection fetchFragmentCollection(
  144. long fragmentCollectionId) {
  145. return fragmentCollectionPersistence.fetchByPrimaryKey(
  146. fragmentCollectionId);
  147. }
  148. @Override
  149. public FragmentCollection fetchFragmentCollection(
  150. long groupId, String fragmentCollectionKey) {
  151. return fragmentCollectionPersistence.fetchByG_FCK(
  152. groupId, _getFragmentCollectionKey(fragmentCollectionKey));
  153. }
  154. @Override
  155. public String generateFragmentCollectionKey(long groupId, String name) {
  156. String fragmentCollectionKey = _getFragmentCollectionKey(name);
  157. fragmentCollectionKey = StringUtil.replace(
  158. fragmentCollectionKey, CharPool.SPACE, CharPool.DASH);
  159. String curFragmentCollectionKey = fragmentCollectionKey;
  160. int count = 0;
  161. while (true) {
  162. FragmentCollection fragmentCollection =
  163. fragmentCollectionPersistence.fetchByG_FCK(
  164. groupId, curFragmentCollectionKey);
  165. if (fragmentCollection == null) {
  166. return curFragmentCollectionKey;
  167. }
  168. curFragmentCollectionKey =
  169. fragmentCollectionKey + CharPool.DASH + count++;
  170. }
  171. }
  172. @Override
  173. public List<FragmentCollection> getFragmentCollections(
  174. long groupId, int start, int end) {
  175. return getFragmentCollections(groupId, start, end, null);
  176. }
  177. @Override
  178. public List<FragmentCollection> getFragmentCollections(
  179. long groupId, int start, int end,
  180. OrderByComparator<FragmentCollection> orderByComparator) {
  181. return fragmentCollectionPersistence.findByGroupId(
  182. groupId, start, end, orderByComparator);
  183. }
  184. @Override
  185. public List<FragmentCollection> getFragmentCollections(
  186. long groupId, String name, int start, int end,
  187. OrderByComparator<FragmentCollection> orderByComparator) {
  188. if (Validator.isNull(name)) {
  189. return fragmentCollectionPersistence.findByGroupId(
  190. groupId, start, end, orderByComparator);
  191. }
  192. return fragmentCollectionPersistence.findByG_LikeN(
  193. groupId, name, start, end, orderByComparator);
  194. }
  195. @Override
  196. public String[] getTempFileNames(
  197. long userId, long groupId, String folderName)
  198. throws PortalException {
  199. return TempFileEntryUtil.getTempFileNames(groupId, userId, folderName);
  200. }
  201. @Override
  202. public FragmentCollection updateFragmentCollection(
  203. long fragmentCollectionId, String name, String description)
  204. throws PortalException {
  205. FragmentCollection fragmentCollection =
  206. fragmentCollectionPersistence.findByPrimaryKey(
  207. fragmentCollectionId);
  208. validate(name);
  209. fragmentCollection.setModifiedDate(new Date());
  210. fragmentCollection.setName(name);
  211. fragmentCollection.setDescription(description);
  212. return fragmentCollectionPersistence.update(fragmentCollection);
  213. }
  214. protected void validate(String name) throws PortalException {
  215. if (Validator.isNull(name)) {
  216. throw new FragmentCollectionNameException("Name must not be null");
  217. }
  218. if (name.contains(StringPool.PERIOD) ||
  219. name.contains(StringPool.SLASH)) {
  220. throw new FragmentCollectionNameException(
  221. "Name contains invalid characters");
  222. }
  223. }
  224. protected void validateFragmentCollectionKey(
  225. long groupId, String fragmentCollectionKey)
  226. throws PortalException {
  227. fragmentCollectionKey = _getFragmentCollectionKey(
  228. fragmentCollectionKey);
  229. FragmentCollection fragmentCollection =
  230. fragmentCollectionPersistence.fetchByG_FCK(
  231. groupId, fragmentCollectionKey);
  232. if (fragmentCollection != null) {
  233. throw new DuplicateFragmentCollectionKeyException();
  234. }
  235. }
  236. private String _getFragmentCollectionKey(String fragmentCollectionKey) {
  237. if (fragmentCollectionKey != null) {
  238. fragmentCollectionKey = fragmentCollectionKey.trim();
  239. return StringUtil.toLowerCase(fragmentCollectionKey);
  240. }
  241. return StringPool.BLANK;
  242. }
  243. @Reference
  244. private FragmentCompositionLocalService _fragmentCompositionLocalService;
  245. @Reference
  246. private FragmentCompositionPersistence _fragmentCompositionPersistence;
  247. @Reference
  248. private FragmentEntryLocalService _fragmentEntryLocalService;
  249. @Reference
  250. private FragmentEntryPersistence _fragmentEntryPersistence;
  251. @Reference
  252. private ResourceLocalService _resourceLocalService;
  253. @Reference
  254. private UserLocalService _userLocalService;
  255. }