Skip to content

Commit 2ae3545

Browse files
committed
Merge branch 'release/2022-06'
2 parents 9fa3476 + e6bea8b commit 2ae3545

21 files changed

+243
-99
lines changed

app/controllers/ElementController.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import java.util.Optional;
3737
import java.util.UUID;
3838

39+
@SuppressWarnings("OptionalUsedAsFieldOrParameterType")
3940
public final class ElementController extends JsonLdController<Element, DataJsonLdAdorner.Parameters> {
4041

4142
private final ElementService elementService;
@@ -49,20 +50,20 @@ public ElementController(ElementService elementService, MetamodelProvider metamo
4950
this.adorner = new DataJsonLdAdorner<>(metamodelProvider, environment, INLINE_JSON_LD_CONTEXT);
5051
}
5152

52-
public Result getElementsByProjectIdCommitId(UUID projectId, UUID commitId, Request request) {
53+
public Result getElementsByProjectIdCommitId(UUID projectId, UUID commitId, Optional<Boolean> excludeUsed, Request request) {
5354
PageRequest pageRequest = PageRequest.from(request);
54-
List<Element> elements = elementService.getElementsByProjectIdCommitId(projectId, commitId, pageRequest.getAfter(), pageRequest.getBefore(), pageRequest.getSize());
55+
List<Element> elements = elementService.getElementsByProjectIdCommitId(projectId, commitId, excludeUsed.orElse(false), pageRequest.getAfter(), pageRequest.getBefore(), pageRequest.getSize());
5556
return buildPaginatedResult(elements, projectId, commitId, request, pageRequest);
5657
}
5758

58-
public Result getElementByProjectIdCommitIdElementId(UUID projectId, UUID commitId, UUID elementId, Request request) {
59-
Optional<Element> element = elementService.getElementByProjectIdCommitIdElementId(projectId, commitId, elementId);
59+
public Result getElementByProjectIdCommitIdElementId(UUID projectId, UUID commitId, UUID elementId, Optional<Boolean> excludeUsed, Request request) {
60+
Optional<Element> element = elementService.getElementByProjectIdCommitIdElementId(projectId, commitId, elementId, excludeUsed.orElse(false));
6061
return buildResult(element.orElse(null), request, new DataJsonLdAdorner.Parameters(projectId, commitId));
6162
}
6263

63-
public Result getRootsByProjectIdCommitId(UUID projectId, UUID commitId, Request request) {
64+
public Result getRootsByProjectIdCommitId(UUID projectId, UUID commitId, Optional<Boolean> excludeUsed, Request request) {
6465
PageRequest pageRequest = PageRequest.from(request);
65-
List<Element> roots = elementService.getRootsByProjectIdCommitId(projectId, commitId, pageRequest.getAfter(), pageRequest.getBefore(), pageRequest.getSize());
66+
List<Element> roots = elementService.getRootsByProjectIdCommitId(projectId, commitId, excludeUsed.orElse(false), pageRequest.getAfter(), pageRequest.getBefore(), pageRequest.getSize());
6667
return buildPaginatedResult(roots, projectId, commitId, request, pageRequest);
6768
}
6869

app/controllers/RelationshipController.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ public RelationshipController(RelationshipService relationshipService, Metamodel
5151
this.adorner = new DataJsonLdAdorner<>(metamodelProvider, environment, INLINE_JSON_LD_CONTEXT);
5252
}
5353

54-
public Result getRelationshipsByProjectIdCommitIdRelatedElementId(UUID projectId, UUID commitId, UUID relatedElementId, @SuppressWarnings("OptionalUsedAsFieldOrParameterType") Optional<String> direction, Request request) {
54+
@SuppressWarnings("OptionalUsedAsFieldOrParameterType")
55+
public Result getRelationshipsByProjectIdCommitIdRelatedElementId(UUID projectId, UUID commitId, UUID relatedElementId, Optional<String> direction, Optional<Boolean> excludeUsed, Request request) {
5556
PageRequest pageRequest = PageRequest.from(request);
5657
RelationshipDirection relationshipDirection = direction
5758
.flatMap(d -> Arrays.stream(RelationshipDirection.values())
@@ -64,6 +65,7 @@ public Result getRelationshipsByProjectIdCommitIdRelatedElementId(UUID projectId
6465
commitId,
6566
relatedElementId,
6667
relationshipDirection,
68+
excludeUsed.orElse(false),
6769
pageRequest.getAfter(),
6870
pageRequest.getBefore(),
6971
pageRequest.getSize()

app/dao/ElementDao.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@
3232

3333
public interface ElementDao extends Dao<Element> {
3434

35-
List<Element> findAllByCommit(Commit commit, @Nullable UUID after, @Nullable UUID before, int maxResults);
35+
List<Element> findAllByCommit(Commit commit, boolean excludeUsed, @Nullable UUID after, @Nullable UUID before, int maxResults);
3636

37-
Optional<Element> findByCommitAndId(Commit commit, UUID id);
37+
Optional<Element> findByCommitAndId(Commit commit, UUID id, boolean excludeUsed);
3838

39-
List<Element> findRootsByCommit(Commit commit, @Nullable UUID after, @Nullable UUID before, int maxResults);
39+
List<Element> findRootsByCommit(Commit commit, boolean excludeUsed, @Nullable UUID after, @Nullable UUID before, int maxResults);
4040

4141
Optional<Element> findByCommitAndQualifiedName(Commit commit, String qualifiedName);
4242
}

app/dao/RelationshipDao.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,5 @@
3333

3434
public interface RelationshipDao extends Dao<Relationship> {
3535

36-
List<Relationship> findAllByCommitRelatedElement(Commit commit, Element relatedElement, RelationshipDirection direction, @Nullable UUID after, @Nullable UUID before, int maxResults);
36+
List<Relationship> findAllByCommitRelatedElement(Commit commit, Element relatedElement, RelationshipDirection direction, boolean excludeUsed, @Nullable UUID after, @Nullable UUID before, int maxResults);
3737
}

app/dao/impl/jpa/JpaCommitDao.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public Optional<Commit> persist(Commit commit, Branch branch) {
8989
(fnData, fnCommit) -> {
9090
UUID id = fnData.getId();
9191
// TODO change to dataDao
92-
return elementDao.findByCommitAndId(fnCommit, id)
92+
return elementDao.findByCommitAndId(fnCommit, id, false)
9393
.orElseThrow(() -> new NoSuchElementException(
9494
String.format("Element with %s %s not found", IDENTITY_FIELD, id)
9595
));
@@ -122,7 +122,7 @@ public Optional<Commit> persistNameResolved(Commit commit, Branch branch) {
122122
if (fnData.getId() != null) {
123123
UUID id = fnData.getId();
124124
// TODO change to dataDao
125-
return elementDao.findByCommitAndId(fnCommit, id)
125+
return elementDao.findByCommitAndId(fnCommit, id, false)
126126
.orElseThrow(() -> new NoSuchElementException(
127127
String.format("Element with %s %s not found", IDENTITY_FIELD, id)
128128
));

app/dao/impl/jpa/JpaDataDao.java

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@
2929
import org.hibernate.proxy.HibernateProxy;
3030
import org.omg.sysml.data.ProjectUsage;
3131
import org.omg.sysml.internal.CommitDataVersionIndex;
32+
import org.omg.sysml.internal.WorkingDataVersion;
3233
import org.omg.sysml.internal.impl.CommitDataVersionIndexImpl;
3334
import org.omg.sysml.internal.impl.CommitDataVersionIndexImpl_;
35+
import org.omg.sysml.internal.impl.WorkingDataVersionImpl;
3436
import org.omg.sysml.lifecycle.Commit;
3537
import org.omg.sysml.lifecycle.Data;
3638
import org.omg.sysml.lifecycle.DataIdentity;
@@ -95,6 +97,7 @@ public List<Data> findByCommitAndQuery(Commit commit, Query query) {
9597
Query q = query.getId() == null || em.contains(query) ? query : em.find(QueryImpl.class, query.getId());
9698
return getCommitIndex(c, em).getWorkingDataVersion().stream()
9799
.filter(scope(q))
100+
.map(WorkingDataVersion::getDataVersion)
98101
.map(DataVersion::getPayload)
99102
.filter(constrain(q.getWhere()))
100103
.map(data -> JpaDataDao.resolve(data, Data.class))
@@ -115,17 +118,21 @@ protected CommitDataVersionIndex getCommitIndex(Commit commit, EntityManager em)
115118
CommitDataVersionIndex index = new CommitDataVersionIndexImpl();
116119
index.setCommit(commit);
117120
index.setWorkingDataVersion(streamWorkingDataVersions(commit, em).collect(Collectors.toSet()));
121+
118122
EntityTransaction transaction = em.getTransaction();
119123
transaction.begin();
124+
index.getWorkingDataVersion().forEach(em::persist);
120125
em.persist(index);
121126
transaction.commit();
127+
122128
return index;
123129
}
124130

125-
protected Stream<DataVersion> streamWorkingDataVersions(Commit commit, EntityManager em) {
131+
protected Stream<WorkingDataVersion> streamWorkingDataVersions(Commit commit, EntityManager em) {
126132
Set<UUID> visitedIds = ConcurrentHashMap.newKeySet();
127133
Set<ProjectUsage> projectUsages = ConcurrentHashMap.newKeySet();
128-
Map<Commit, Set<DataVersion>> results = queryCommitTree(commit,
134+
135+
Map<Commit, Set<WorkingDataVersion>> _ownedDataVersions = queryCommitTree(commit,
129136
c -> c.getChange().stream()
130137
.filter(record -> record.getIdentity() != null && record.getIdentity().getId() != null)
131138
.filter(record -> !visitedIds.contains(record.getIdentity().getId()))
@@ -136,20 +143,36 @@ protected Stream<DataVersion> streamWorkingDataVersions(Commit commit, EntityMan
136143
projectUsages.add(((ProjectUsage) record.getPayload()));
137144
}
138145
})
146+
.map(version -> {
147+
WorkingDataVersionImpl workingDataVersion = new WorkingDataVersionImpl();
148+
workingDataVersion.setDataVersion(version);
149+
return workingDataVersion;
150+
})
139151
.collect(Collectors.toSet())
140152
);
141-
Stream<DataVersion> ownedDataVersions = results.values().stream()
153+
Stream<WorkingDataVersion> ownedDataVersions = _ownedDataVersions.values().stream()
142154
.flatMap(Set::stream);
143-
Stream<DataVersion> usedDataVersions = projectUsages.stream()
144-
.map(ProjectUsage::getUsedProjectCommit)
145-
.filter(Objects::nonNull)
146-
.map(fnCommit -> getCommitIndex(fnCommit, em))
147-
.map(CommitDataVersionIndex::getWorkingDataVersion)
148-
.flatMap(Set::stream)
149-
.filter(record -> !visitedIds.contains(record.getIdentity().getId()))
150-
.peek(record -> visitedIds.add(record.getIdentity().getId()))
151-
.collect(Collectors.toSet())
152-
.stream();
155+
156+
Set<WorkingDataVersion> _usedDataVersions = ConcurrentHashMap.newKeySet();
157+
for (ProjectUsage projectUsage : projectUsages) {
158+
Commit usedCommit = projectUsage.getUsedProjectCommit();
159+
if (usedCommit == null) {
160+
continue;
161+
}
162+
getCommitIndex(usedCommit, em).getWorkingDataVersion().stream()
163+
.map(WorkingDataVersion::getDataVersion)
164+
.filter(version -> !visitedIds.contains(version.getIdentity().getId()))
165+
.peek(version -> visitedIds.add(version.getIdentity().getId()))
166+
.map(version -> {
167+
WorkingDataVersionImpl workingDataVersion = new WorkingDataVersionImpl();
168+
workingDataVersion.setSource(projectUsage);
169+
workingDataVersion.setDataVersion(version);
170+
return workingDataVersion;
171+
})
172+
.forEach(_usedDataVersions::add);
173+
}
174+
Stream<WorkingDataVersion> usedDataVersions = _usedDataVersions.stream();
175+
153176
return Streams.concat(ownedDataVersions, usedDataVersions);
154177
}
155178

@@ -241,12 +264,12 @@ else if (constraint instanceof CompositeConstraint) {
241264
}
242265
}
243266

244-
protected Predicate<DataVersion> scope(Query query) {
267+
protected Predicate<WorkingDataVersion> scope(Query query) {
245268
if (query.getScope() == null || query.getScope().isEmpty()) {
246269
return ev -> true;
247270
}
248-
return ev -> ev.getIdentity() != null && query.getScope().stream()
271+
return working -> working.getDataVersion().getIdentity() != null && query.getScope().stream()
249272
.map(DataIdentity::getId)
250-
.anyMatch(id -> Objects.equals(id, ev.getIdentity().getId()));
273+
.anyMatch(id -> Objects.equals(id, working.getDataVersion().getIdentity().getId()));
251274
}
252275
}

0 commit comments

Comments
 (0)