/modules/apps/asset/asset-list-service/src/main/java/com/liferay/asset/list/service/impl/AssetListEntryAssetEntryRelLocalServiceImpl.java

https://github.com/kiyoshilee/liferay-portal · Java · 326 lines · 238 code · 72 blank · 16 comment · 15 complexity · 68dbb49a4c26a90490226580107159ef 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.asset.list.service.impl;
  15. import com.liferay.asset.kernel.AssetRendererFactoryRegistryUtil;
  16. import com.liferay.asset.kernel.model.AssetEntry;
  17. import com.liferay.asset.kernel.model.AssetRendererFactory;
  18. import com.liferay.asset.kernel.service.AssetEntryLocalService;
  19. import com.liferay.asset.list.exception.AssetListEntryAssetEntryRelPostionException;
  20. import com.liferay.asset.list.model.AssetListEntryAssetEntryRel;
  21. import com.liferay.asset.list.service.base.AssetListEntryAssetEntryRelLocalServiceBaseImpl;
  22. import com.liferay.portal.aop.AopService;
  23. import com.liferay.portal.kernel.exception.PortalException;
  24. import com.liferay.portal.kernel.log.Log;
  25. import com.liferay.portal.kernel.log.LogFactoryUtil;
  26. import com.liferay.portal.kernel.model.SystemEventConstants;
  27. import com.liferay.portal.kernel.model.User;
  28. import com.liferay.portal.kernel.service.ServiceContext;
  29. import com.liferay.portal.kernel.systemevent.SystemEvent;
  30. import com.liferay.portal.kernel.transaction.TransactionCommitCallbackUtil;
  31. import java.util.Date;
  32. import java.util.List;
  33. import java.util.stream.Collectors;
  34. import java.util.stream.Stream;
  35. import org.osgi.service.component.annotations.Component;
  36. import org.osgi.service.component.annotations.Reference;
  37. /**
  38. * @author Pavel savinov
  39. */
  40. @Component(
  41. property = "model.class.name=com.liferay.asset.list.model.AssetListEntryAssetEntryRel",
  42. service = AopService.class
  43. )
  44. public class AssetListEntryAssetEntryRelLocalServiceImpl
  45. extends AssetListEntryAssetEntryRelLocalServiceBaseImpl {
  46. @Override
  47. public AssetListEntryAssetEntryRel addAssetListEntryAssetEntryRel(
  48. long assetListEntryId, long assetEntryId, long segmentsEntryId,
  49. int position, ServiceContext serviceContext)
  50. throws PortalException {
  51. AssetListEntryAssetEntryRel assetListEntryAssetEntryRel =
  52. assetListEntryAssetEntryRelPersistence.fetchByA_S_P(
  53. assetListEntryId, segmentsEntryId, position);
  54. if (assetListEntryAssetEntryRel != null) {
  55. throw new AssetListEntryAssetEntryRelPostionException();
  56. }
  57. User user = userLocalService.getUser(serviceContext.getUserId());
  58. long assetListEntryAssetEntryRelId = counterLocalService.increment();
  59. assetListEntryAssetEntryRel =
  60. assetListEntryAssetEntryRelPersistence.create(
  61. assetListEntryAssetEntryRelId);
  62. assetListEntryAssetEntryRel.setUuid(serviceContext.getUuid());
  63. assetListEntryAssetEntryRel.setGroupId(
  64. serviceContext.getScopeGroupId());
  65. assetListEntryAssetEntryRel.setCompanyId(serviceContext.getCompanyId());
  66. assetListEntryAssetEntryRel.setUserId(serviceContext.getUserId());
  67. assetListEntryAssetEntryRel.setUserName(user.getFullName());
  68. assetListEntryAssetEntryRel.setCreateDate(
  69. serviceContext.getCreateDate(new Date()));
  70. assetListEntryAssetEntryRel.setModifiedDate(
  71. serviceContext.getModifiedDate(new Date()));
  72. assetListEntryAssetEntryRel.setAssetListEntryId(assetListEntryId);
  73. assetListEntryAssetEntryRel.setAssetEntryId(assetEntryId);
  74. assetListEntryAssetEntryRel.setSegmentsEntryId(segmentsEntryId);
  75. assetListEntryAssetEntryRel.setPosition(position);
  76. return assetListEntryAssetEntryRelPersistence.update(
  77. assetListEntryAssetEntryRel);
  78. }
  79. @Override
  80. public AssetListEntryAssetEntryRel addAssetListEntryAssetEntryRel(
  81. long assetListEntryId, long assetEntryId, long segmentsEntryId,
  82. ServiceContext serviceContext)
  83. throws PortalException {
  84. int position = getAssetListEntryAssetEntryRelsCount(
  85. assetListEntryId, segmentsEntryId);
  86. AssetListEntryAssetEntryRel assetListEntryAssetEntryRel =
  87. assetListEntryAssetEntryRelPersistence.fetchByA_S_P(
  88. assetListEntryId, segmentsEntryId, position);
  89. if (assetListEntryAssetEntryRel != null) {
  90. throw new AssetListEntryAssetEntryRelPostionException();
  91. }
  92. return addAssetListEntryAssetEntryRel(
  93. assetListEntryId, assetEntryId, segmentsEntryId, position,
  94. serviceContext);
  95. }
  96. @Override
  97. @SystemEvent(type = SystemEventConstants.TYPE_DELETE)
  98. public AssetListEntryAssetEntryRel deleteAssetListEntryAssetEntryRel(
  99. long assetListEntryId, long segmentsEntryId, int position)
  100. throws PortalException {
  101. AssetListEntryAssetEntryRel assetListEntryAssetEntryRel =
  102. assetListEntryAssetEntryRelPersistence.removeByA_S_P(
  103. assetListEntryId, segmentsEntryId, position);
  104. List<AssetListEntryAssetEntryRel> assetListEntryAssetEntryRels =
  105. assetListEntryAssetEntryRelPersistence.findByA_S_GtP(
  106. assetListEntryId, segmentsEntryId, position);
  107. for (AssetListEntryAssetEntryRel curAssetListEntryAssetEntryRel :
  108. assetListEntryAssetEntryRels) {
  109. curAssetListEntryAssetEntryRel.setPosition(
  110. curAssetListEntryAssetEntryRel.getPosition() - 1);
  111. assetListEntryAssetEntryRelPersistence.update(
  112. curAssetListEntryAssetEntryRel);
  113. }
  114. return assetListEntryAssetEntryRel;
  115. }
  116. @Override
  117. public void deleteAssetListEntryAssetEntryRelByAssetListEntryId(
  118. long assetListEntryId) {
  119. assetListEntryAssetEntryRelPersistence.removeByAssetListEntryId(
  120. assetListEntryId);
  121. }
  122. @Override
  123. public List<AssetListEntryAssetEntryRel> getAssetListEntryAssetEntryRels(
  124. long assetListEntryId, int start, int end) {
  125. List<AssetListEntryAssetEntryRel> assetListEntryAssetEntryRels =
  126. assetListEntryAssetEntryRelPersistence.findByAssetListEntryId(
  127. assetListEntryId, start, end);
  128. return _getAssetListEntryAssetEntryRels(assetListEntryAssetEntryRels);
  129. }
  130. @Override
  131. public List<AssetListEntryAssetEntryRel> getAssetListEntryAssetEntryRels(
  132. long assetListEntryId, long segmentsEntryId, int start, int end) {
  133. List<AssetListEntryAssetEntryRel> assetListEntryAssetEntryRels =
  134. assetListEntryAssetEntryRelPersistence.findByA_S(
  135. assetListEntryId, segmentsEntryId, start, end);
  136. return _getAssetListEntryAssetEntryRels(assetListEntryAssetEntryRels);
  137. }
  138. @Override
  139. public int getAssetListEntryAssetEntryRelsCount(long assetListEntryId) {
  140. return assetListEntryAssetEntryRelPersistence.countByAssetListEntryId(
  141. assetListEntryId);
  142. }
  143. @Override
  144. public int getAssetListEntryAssetEntryRelsCount(
  145. long assetListEntryId, long segmentsEntryId) {
  146. return assetListEntryAssetEntryRelPersistence.countByA_S(
  147. assetListEntryId, segmentsEntryId);
  148. }
  149. @Override
  150. public AssetListEntryAssetEntryRel moveAssetListEntryAssetEntryRel(
  151. long assetListEntryId, long segmentsEntryId, int position,
  152. int newPosition)
  153. throws PortalException {
  154. AssetListEntryAssetEntryRel assetListEntryAssetEntryRel =
  155. assetListEntryAssetEntryRelPersistence.findByA_S_P(
  156. assetListEntryId, segmentsEntryId, position);
  157. int count =
  158. assetListEntryAssetEntryRelPersistence.countByAssetListEntryId(
  159. assetListEntryId);
  160. if ((newPosition < 0) || (newPosition >= count)) {
  161. return assetListEntryAssetEntryRel;
  162. }
  163. AssetListEntryAssetEntryRel swapAssetListEntryAssetEntryRel =
  164. assetListEntryAssetEntryRelPersistence.fetchByA_S_P(
  165. assetListEntryId, segmentsEntryId, newPosition);
  166. if (swapAssetListEntryAssetEntryRel == null) {
  167. assetListEntryAssetEntryRel.setPosition(newPosition);
  168. return assetListEntryAssetEntryRelPersistence.update(
  169. assetListEntryAssetEntryRel);
  170. }
  171. assetListEntryAssetEntryRel.setPosition(-1);
  172. assetListEntryAssetEntryRelPersistence.update(
  173. assetListEntryAssetEntryRel);
  174. swapAssetListEntryAssetEntryRel.setPosition(-2);
  175. assetListEntryAssetEntryRelPersistence.update(
  176. swapAssetListEntryAssetEntryRel);
  177. TransactionCommitCallbackUtil.registerCallback(
  178. () -> {
  179. AssetListEntryAssetEntryRel
  180. callbackAssetListEntryAssetEntryRel =
  181. assetListEntryAssetEntryRelLocalService.
  182. fetchAssetListEntryAssetEntryRel(
  183. assetListEntryAssetEntryRel.
  184. getAssetListEntryAssetEntryRelId());
  185. callbackAssetListEntryAssetEntryRel.setPosition(newPosition);
  186. assetListEntryAssetEntryRelLocalService.
  187. updateAssetListEntryAssetEntryRel(
  188. callbackAssetListEntryAssetEntryRel);
  189. callbackAssetListEntryAssetEntryRel =
  190. assetListEntryAssetEntryRelLocalService.
  191. fetchAssetListEntryAssetEntryRel(
  192. swapAssetListEntryAssetEntryRel.
  193. getAssetListEntryAssetEntryRelId());
  194. callbackAssetListEntryAssetEntryRel.setPosition(position);
  195. assetListEntryAssetEntryRelLocalService.
  196. updateAssetListEntryAssetEntryRel(
  197. callbackAssetListEntryAssetEntryRel);
  198. return null;
  199. });
  200. return assetListEntryAssetEntryRel;
  201. }
  202. @Override
  203. public AssetListEntryAssetEntryRel updateAssetListEntryAssetEntryRel(
  204. long assetListEntryAssetEntryRelId, long assetListEntryId,
  205. long assetEntryId, long segmentsEntryId, int position)
  206. throws PortalException {
  207. AssetListEntryAssetEntryRel assetListEntryAssetEntryRel =
  208. assetListEntryAssetEntryRelPersistence.findByPrimaryKey(
  209. assetListEntryAssetEntryRelId);
  210. assetListEntryAssetEntryRel.setAssetListEntryId(assetListEntryId);
  211. assetListEntryAssetEntryRel.setAssetEntryId(assetEntryId);
  212. assetListEntryAssetEntryRel.setSegmentsEntryId(segmentsEntryId);
  213. assetListEntryAssetEntryRel.setPosition(position);
  214. assetListEntryAssetEntryRelPersistence.update(
  215. assetListEntryAssetEntryRel);
  216. return assetListEntryAssetEntryRel;
  217. }
  218. private List<AssetListEntryAssetEntryRel> _getAssetListEntryAssetEntryRels(
  219. List<AssetListEntryAssetEntryRel> assetListEntryAssetEntryRels) {
  220. Stream<AssetListEntryAssetEntryRel> stream =
  221. assetListEntryAssetEntryRels.stream();
  222. return stream.filter(
  223. assetListEntryAssetEntryRel -> {
  224. AssetEntry assetEntry = _assetEntryLocalService.fetchEntry(
  225. assetListEntryAssetEntryRel.getAssetEntryId());
  226. if (assetEntry == null) {
  227. return false;
  228. }
  229. if (!assetEntry.isVisible()) {
  230. return false;
  231. }
  232. AssetRendererFactory assetRendererFactory =
  233. AssetRendererFactoryRegistryUtil.
  234. getAssetRendererFactoryByClassName(
  235. assetEntry.getClassName());
  236. if (assetRendererFactory == null) {
  237. if (_log.isWarnEnabled()) {
  238. _log.warn(
  239. "No asset renderer factory found for class " +
  240. assetEntry.getClassName());
  241. }
  242. return false;
  243. }
  244. return true;
  245. }
  246. ).collect(
  247. Collectors.toList()
  248. );
  249. }
  250. private static final Log _log = LogFactoryUtil.getLog(
  251. AssetListEntryAssetEntryRelLocalServiceImpl.class);
  252. @Reference
  253. private AssetEntryLocalService _assetEntryLocalService;
  254. }