|
2 | 2 | * SysML v2 REST/HTTP Pilot Implementation |
3 | 3 | * Copyright (C) 2020 InterCAX LLC |
4 | 4 | * Copyright (C) 2020 California Institute of Technology ("Caltech") |
5 | | - * Copyright (C) 2021 Twingineer LLC |
| 5 | + * Copyright (C) 2021-2022 Twingineer LLC |
6 | 6 | * |
7 | 7 | * This program is free software: you can redistribute it and/or modify |
8 | 8 | * it under the terms of the GNU Lesser General Public License as published by |
|
27 | 27 | import javabean.JavaBeanHelper; |
28 | 28 | import jpa.manager.JPAManager; |
29 | 29 | import org.omg.sysml.lifecycle.*; |
30 | | -import org.omg.sysml.lifecycle.impl.CommitImpl; |
31 | | -import org.omg.sysml.lifecycle.impl.CommitImpl_; |
32 | | -import org.omg.sysml.lifecycle.impl.DataIdentityImpl; |
33 | | -import org.omg.sysml.lifecycle.impl.DataImpl; |
| 30 | +import org.omg.sysml.lifecycle.impl.*; |
34 | 31 | import org.omg.sysml.metamodel.Element; |
35 | | -import org.omg.sysml.metamodel.impl.ElementImpl_; |
36 | 32 |
|
37 | 33 | import javax.inject.Inject; |
38 | 34 | import javax.inject.Singleton; |
39 | 35 | import javax.persistence.EntityManager; |
40 | 36 | import javax.persistence.NoResultException; |
41 | 37 | import javax.persistence.TypedQuery; |
42 | | -import javax.persistence.criteria.CriteriaBuilder; |
43 | | -import javax.persistence.criteria.CriteriaQuery; |
44 | | -import javax.persistence.criteria.Expression; |
45 | | -import javax.persistence.criteria.Root; |
| 38 | +import javax.persistence.criteria.*; |
46 | 39 | import java.lang.reflect.Method; |
47 | 40 | import java.util.*; |
48 | 41 | import java.util.function.BiFunction; |
@@ -334,9 +327,43 @@ protected Optional<Commit> findByProjectAndId(Project project, UUID id, EntityMa |
334 | 327 | } |
335 | 328 |
|
336 | 329 | @Override |
337 | | - public Optional<Commit> findByProjectAndIdResolved(Project project, UUID id) { |
| 330 | + public List<DataVersion> findChangesByCommit(Commit commit, UUID after, UUID before, int maxResults) { |
338 | 331 | return jpaManager.transact(em -> { |
339 | | - return findByProjectAndId(project, id, em).map(JpaCommitDao::resolve); |
| 332 | + CriteriaBuilder builder = em.getCriteriaBuilder(); |
| 333 | + CriteriaQuery<DataVersionImpl> query = builder.createQuery(DataVersionImpl.class); |
| 334 | + Root<CommitImpl> commitRoot = query.from(CommitImpl.class); |
| 335 | + SetJoin<CommitImpl, DataVersionImpl> dataVersionJoin = commitRoot.join(CommitImpl_.change); |
| 336 | + Path<UUID> idPath = dataVersionJoin.get(DataVersionImpl_.id); |
| 337 | + Expression<Boolean> where = builder.equal(commitRoot.get(CommitImpl_.id), commit.getId()); |
| 338 | + query.select(dataVersionJoin); |
| 339 | + Paginated<TypedQuery<DataVersionImpl>> paginated = paginateQuery(after, before, maxResults, query, builder, em, idPath, where); |
| 340 | + List<DataVersion> result = paginated.get() |
| 341 | + .getResultStream() |
| 342 | + .collect(Collectors.toList()); |
| 343 | + if (paginated.isReversed()) { |
| 344 | + Collections.reverse(result); |
| 345 | + } |
| 346 | + return result; |
| 347 | + }); |
| 348 | + } |
| 349 | + |
| 350 | + @Override |
| 351 | + public Optional<DataVersion> findChangeByCommitAndId(Commit commit, UUID id) { |
| 352 | + return jpaManager.transact(em -> { |
| 353 | + CriteriaBuilder builder = em.getCriteriaBuilder(); |
| 354 | + CriteriaQuery<DataVersionImpl> query = builder.createQuery(DataVersionImpl.class); |
| 355 | + Root<CommitImpl> commitRoot = query.from(CommitImpl.class); |
| 356 | + SetJoin<CommitImpl, DataVersionImpl> dataVersionJoin = commitRoot.join(CommitImpl_.change); |
| 357 | + Expression<Boolean> where = builder.and( |
| 358 | + builder.equal(commitRoot.get(CommitImpl_.id), commit.getId()), |
| 359 | + builder.equal(dataVersionJoin.get(DataVersionImpl_.id), id) |
| 360 | + ); |
| 361 | + query.select(dataVersionJoin).where(where); |
| 362 | + try { |
| 363 | + return Optional.of(em.createQuery(query).getSingleResult()); |
| 364 | + } catch (NoResultException e) { |
| 365 | + return Optional.empty(); |
| 366 | + } |
340 | 367 | }); |
341 | 368 | } |
342 | 369 | } |
0 commit comments