PageRenderTime 26ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/tm-server/src/main/java/ru/t1/ytarasov/tm/service/dto/ProjectServiceDTO.java

https://gitlab.com/ytarasov/task-manager-52
Java | 345 lines | 315 code | 30 blank | 0 comment | 109 complexity | a948aeebc52aca35f80ef9d493663775 MD5 | raw file
  1. package ru.t1.ytarasov.tm.service.dto;
  2. import org.jetbrains.annotations.NotNull;
  3. import org.jetbrains.annotations.Nullable;
  4. import ru.t1.ytarasov.tm.api.repository.dto.IProjectRepositoryDTO;
  5. import ru.t1.ytarasov.tm.api.service.IConnectionService;
  6. import ru.t1.ytarasov.tm.api.service.dto.IProjectServiceDTO;
  7. import ru.t1.ytarasov.tm.enumerated.Sort;
  8. import ru.t1.ytarasov.tm.enumerated.Status;
  9. import ru.t1.ytarasov.tm.exception.entity.ProjectNotFoundException;
  10. import ru.t1.ytarasov.tm.exception.field.*;
  11. import ru.t1.ytarasov.tm.dto.model.ProjectDTO;
  12. import ru.t1.ytarasov.tm.repository.dto.ProjectRepositoryDTO;
  13. import javax.persistence.EntityManager;
  14. import java.util.*;
  15. public final class ProjectServiceDTO
  16. extends AbstractUserOwnedServiceDTO<ProjectDTO, ProjectRepositoryDTO> implements IProjectServiceDTO {
  17. public ProjectServiceDTO(@NotNull IConnectionService connectionService) {
  18. super(connectionService);
  19. }
  20. @Override
  21. @NotNull
  22. public ProjectDTO add(@Nullable final ProjectDTO project) throws Exception {
  23. if (project == null) throw new ProjectNotFoundException();
  24. @NotNull final EntityManager entityManager = connectionService.getEntityManager();
  25. try {
  26. @NotNull final IProjectRepositoryDTO repository = new ProjectRepositoryDTO(entityManager);
  27. entityManager.getTransaction().begin();
  28. repository.add(project);
  29. entityManager.getTransaction().commit();
  30. } catch (@NotNull final Exception e) {
  31. entityManager.getTransaction().rollback();
  32. throw e;
  33. } finally {
  34. entityManager.close();
  35. }
  36. return project;
  37. }
  38. @NotNull
  39. @Override
  40. public Collection<ProjectDTO> add(@NotNull Collection<ProjectDTO> models) throws Exception {
  41. for (ProjectDTO model : models) add(model);
  42. return models;
  43. }
  44. @NotNull
  45. @Override
  46. public ProjectDTO updateById(@Nullable final String userId,
  47. @Nullable final String id,
  48. @Nullable final String name,
  49. @Nullable final String description
  50. ) throws Exception {
  51. if (userId == null || userId.isEmpty()) throw new UserIdEmptyException();
  52. if (id == null || id.isEmpty()) throw new IdEmptyException();
  53. if (name == null || name.isEmpty()) throw new NameEmptyException();
  54. if (description == null || description.isEmpty()) throw new DescriptionEmptyException();
  55. @Nullable final ProjectDTO project = findOneById(userId, id);
  56. if (project == null) throw new ProjectNotFoundException();
  57. project.setName(name);
  58. project.setDescription(description);
  59. project.setUpdated(new Date());
  60. return update(project);
  61. }
  62. @Override
  63. public void clear() throws Exception {
  64. @NotNull final EntityManager entityManager = connectionService.getEntityManager();
  65. try {
  66. @NotNull final IProjectRepositoryDTO repository = new ProjectRepositoryDTO(entityManager);
  67. entityManager.getTransaction().begin();
  68. repository.clear();
  69. entityManager.getTransaction().commit();
  70. } catch (@NotNull final Exception e) {
  71. entityManager.getTransaction().rollback();
  72. throw e;
  73. } finally {
  74. entityManager.close();
  75. }
  76. }
  77. @Override
  78. public void clear(@Nullable String userId) throws Exception {
  79. if (userId == null || userId.isEmpty()) throw new UserIdEmptyException();
  80. @NotNull final EntityManager entityManager = connectionService.getEntityManager();
  81. try {
  82. @NotNull final IProjectRepositoryDTO repository = new ProjectRepositoryDTO(entityManager);
  83. entityManager.getTransaction().begin();
  84. repository.clear(userId);
  85. entityManager.getTransaction().commit();
  86. } catch (@NotNull final Exception e) {
  87. entityManager.getTransaction().rollback();
  88. throw e;
  89. } finally {
  90. entityManager.close();
  91. }
  92. }
  93. @NotNull
  94. @Override
  95. public ProjectDTO create(@Nullable String userId, @Nullable String name, @Nullable String description) throws Exception {
  96. if (userId == null || userId.isEmpty()) throw new UserIdEmptyException();
  97. if (name == null || name.isEmpty()) throw new NameEmptyException();
  98. if (description == null || description.isEmpty()) throw new DescriptionEmptyException();
  99. @NotNull final ProjectDTO project = new ProjectDTO(name, description);
  100. project.setUserId(userId);
  101. return add(project);
  102. }
  103. @NotNull
  104. @Override
  105. public ProjectDTO changeProjectStatusById(@Nullable final String userId,
  106. @Nullable final String id,
  107. @Nullable final Status status) throws Exception {
  108. if (userId == null || userId.isEmpty()) throw new UserIdEmptyException();
  109. if (id == null || id.isEmpty()) throw new IdEmptyException();
  110. if (status == null || status.getDisplayName().isEmpty()) throw new StatusEmptyException();
  111. @Nullable final ProjectDTO project = findOneById(id);
  112. if (project == null) throw new ProjectNotFoundException();
  113. project.setStatus(status);
  114. project.setUpdated(new Date());
  115. return update(project);
  116. }
  117. @Override
  118. public @NotNull ProjectDTO update(@Nullable ProjectDTO project) throws Exception {
  119. if (project == null) throw new ProjectNotFoundException();
  120. @NotNull final EntityManager entityManager = connectionService.getEntityManager();
  121. try {
  122. @NotNull final IProjectRepositoryDTO repository = new ProjectRepositoryDTO(entityManager);
  123. entityManager.getTransaction().begin();
  124. repository.update(project);
  125. entityManager.getTransaction().commit();
  126. } catch (@NotNull final Exception e) {
  127. entityManager.getTransaction().rollback();
  128. throw e;
  129. } finally {
  130. entityManager.close();
  131. }
  132. return project;
  133. }
  134. @Override
  135. public @Nullable List<ProjectDTO> findAll() {
  136. @NotNull final EntityManager entityManager = connectionService.getEntityManager();
  137. try {
  138. @NotNull final IProjectRepositoryDTO repository = new ProjectRepositoryDTO(entityManager);
  139. return repository.findAll();
  140. } finally {
  141. entityManager.close();
  142. }
  143. }
  144. @Nullable
  145. @Override
  146. public List<ProjectDTO> findAll(@Nullable String userId) throws Exception {
  147. if (userId == null || userId.isEmpty()) throw new UserIdEmptyException();
  148. @NotNull final EntityManager entityManager = connectionService.getEntityManager();
  149. try {
  150. @NotNull final IProjectRepositoryDTO repository = new ProjectRepositoryDTO(entityManager);
  151. return repository.findAll(userId);
  152. } finally {
  153. entityManager.close();
  154. }
  155. }
  156. @Override
  157. public @Nullable List<ProjectDTO> findAll(@Nullable Comparator comparator) throws Exception {
  158. if (comparator == null) return findAll();
  159. @NotNull final EntityManager entityManager = connectionService.getEntityManager();
  160. try {
  161. @NotNull final IProjectRepositoryDTO repository = new ProjectRepositoryDTO(entityManager);
  162. return repository.findAll(comparator);
  163. } finally {
  164. entityManager.close();
  165. }
  166. }
  167. @Nullable
  168. @Override
  169. public List<ProjectDTO> findAll(@Nullable String userId, @Nullable Comparator comparator) throws Exception {
  170. if (userId == null || userId.isEmpty()) throw new UserIdEmptyException();
  171. if (comparator == null) return findAll();
  172. @NotNull final EntityManager entityManager = connectionService.getEntityManager();
  173. try {
  174. @NotNull final IProjectRepositoryDTO repository = new ProjectRepositoryDTO(entityManager);
  175. return repository.findAll(userId, comparator);
  176. } finally {
  177. entityManager.close();
  178. }
  179. }
  180. @Override
  181. public @Nullable List<ProjectDTO> findAll(@Nullable Sort sort) throws Exception {
  182. if (sort == null) return findAll();
  183. else return findAll(sort.getComparator());
  184. }
  185. @Override
  186. public Long getSize() throws Exception {
  187. @NotNull final EntityManager entityManager = connectionService.getEntityManager();
  188. try {
  189. @NotNull final IProjectRepositoryDTO repository = new ProjectRepositoryDTO(entityManager);
  190. return repository.getSize();
  191. } finally {
  192. entityManager.close();
  193. }
  194. }
  195. @Override
  196. public Long getSize(@Nullable String userId) throws Exception {
  197. if (userId == null || userId.isEmpty()) throw new UserIdEmptyException();
  198. @NotNull final EntityManager entityManager = connectionService.getEntityManager();
  199. try {
  200. @NotNull final IProjectRepositoryDTO repository = new ProjectRepositoryDTO(entityManager);
  201. return repository.getSize(userId);
  202. } finally {
  203. entityManager.close();
  204. }
  205. }
  206. @Override
  207. public @Nullable List<ProjectDTO> findAll(@Nullable String userId, @Nullable Sort sort) throws Exception {
  208. if (userId == null || userId.isEmpty()) throw new UserIdEmptyException();
  209. if (sort == null) return findAll(userId);
  210. else return findAll(userId, sort.getComparator());
  211. }
  212. @Nullable
  213. @Override
  214. public ProjectDTO findOneById(@Nullable String id) throws Exception {
  215. if (id == null || id.isEmpty()) throw new IdEmptyException();
  216. @NotNull final EntityManager entityManager = connectionService.getEntityManager();
  217. try {
  218. @NotNull final IProjectRepositoryDTO repository = new ProjectRepositoryDTO(entityManager);
  219. return repository.findOneById(id);
  220. } finally {
  221. entityManager.close();
  222. }
  223. }
  224. @Nullable
  225. @Override
  226. public ProjectDTO findOneById(@Nullable String userId, @Nullable String id) throws Exception {
  227. if (id == null || id.isEmpty()) throw new IdEmptyException();
  228. if (userId == null || userId.isEmpty()) throw new UserIdEmptyException();
  229. @NotNull final EntityManager entityManager = connectionService.getEntityManager();
  230. try {
  231. @NotNull final IProjectRepositoryDTO repository = new ProjectRepositoryDTO(entityManager);
  232. return repository.findOneById(userId, id);
  233. } finally {
  234. entityManager.close();
  235. }
  236. }
  237. @Nullable
  238. @Override
  239. public ProjectDTO remove(@Nullable final ProjectDTO project) throws Exception {
  240. if (project == null) throw new ProjectNotFoundException();
  241. @NotNull final EntityManager entityManager = connectionService.getEntityManager();
  242. try {
  243. @NotNull final IProjectRepositoryDTO repository = new ProjectRepositoryDTO(entityManager);
  244. entityManager.getTransaction().begin();
  245. repository.remove(project);
  246. entityManager.getTransaction().commit();
  247. } catch (@NotNull final Exception e) {
  248. entityManager.getTransaction().rollback();
  249. throw e;
  250. } finally {
  251. entityManager.close();
  252. }
  253. return project;
  254. }
  255. @Nullable
  256. @Override
  257. public ProjectDTO removeById(@Nullable String id) throws Exception {
  258. if (id == null || id.isEmpty()) throw new IdEmptyException();
  259. @Nullable final ProjectDTO project = findOneById(id);
  260. if (project == null) throw new ProjectNotFoundException();
  261. return remove(project);
  262. }
  263. @Nullable
  264. @Override
  265. public ProjectDTO removeById(@Nullable String userId, @Nullable String id) throws Exception {
  266. if (id == null || id.isEmpty()) throw new IdEmptyException();
  267. if (userId == null || userId.isEmpty()) throw new UserIdEmptyException();
  268. @Nullable final ProjectDTO project = findOneById(userId, id);
  269. if (project == null) throw new ProjectNotFoundException();
  270. return remove(project);
  271. }
  272. @Nullable
  273. @Override
  274. public ProjectDTO add(@Nullable String userId, @Nullable ProjectDTO model) throws Exception {
  275. if (userId == null || userId.isEmpty()) throw new UserIdEmptyException();
  276. if (model == null) throw new ProjectNotFoundException();
  277. model.setUserId(userId);
  278. return add(model);
  279. }
  280. @Override
  281. public @Nullable ProjectDTO remove(@Nullable String userId, @Nullable ProjectDTO model) throws Exception {
  282. if (userId == null || userId.isEmpty()) throw new UserIdEmptyException();
  283. if (model == null) throw new ProjectNotFoundException();
  284. @Nullable final ProjectDTO projectDTO = findOneById(userId, model.getId());
  285. if (projectDTO == null) throw new ProjectNotFoundException();
  286. return remove(projectDTO);
  287. }
  288. @Override
  289. public Boolean existsById(@Nullable String id) throws Exception {
  290. if (id == null || id.isEmpty()) throw new IdEmptyException();
  291. Boolean existsById;
  292. @NotNull final EntityManager entityManager = connectionService.getEntityManager();
  293. try {
  294. @NotNull final IProjectRepositoryDTO repository = new ProjectRepositoryDTO(entityManager);
  295. existsById = repository.existsById(id);
  296. } finally {
  297. entityManager.close();
  298. }
  299. return existsById;
  300. }
  301. @Override
  302. public Boolean existsById(@Nullable String userId, @Nullable String id) throws Exception {
  303. if (id == null || id.isEmpty()) throw new IdEmptyException();
  304. if (userId == null || userId.isEmpty()) throw new UserIdEmptyException();
  305. Boolean existsById;
  306. @NotNull final EntityManager entityManager = connectionService.getEntityManager();
  307. try {
  308. @NotNull final IProjectRepositoryDTO repository = new ProjectRepositoryDTO(entityManager);
  309. existsById = repository.existsById(userId, id);
  310. } finally {
  311. entityManager.close();
  312. }
  313. return existsById;
  314. }
  315. }