|
8 | 8 | import org.omg.sysml.lifecycle.Commit; |
9 | 9 | import org.omg.sysml.metamodel.Element; |
10 | 10 | import org.omg.sysml.metamodel.Relationship; |
| 11 | +import org.omg.sysml.utils.RelationshipDirection; |
11 | 12 |
|
12 | 13 | import javax.inject.Inject; |
13 | 14 | import javax.inject.Singleton; |
14 | 15 | import java.util.*; |
| 16 | +import java.util.stream.Collectors; |
15 | 17 |
|
16 | 18 | @Singleton |
17 | 19 | public class RelationshipService { |
@@ -39,9 +41,25 @@ public Optional<Relationship> create(Relationship relationship) { |
39 | 41 | return relationship.getIdentifier() != null ? relationshipDao.update(relationship) : relationshipDao.persist(relationship); |
40 | 42 | } |
41 | 43 |
|
42 | | - public Set<Relationship> getRelationshipsByProjectCommitRelatedElement(UUID projectId, UUID commitId, UUID relatedElementId) { |
| 44 | + public Set<Relationship> getRelationshipsByProjectCommitRelatedElement(UUID projectId, UUID commitId, UUID relatedElementId, RelationshipDirection direction) { |
43 | 45 | Commit commit = projectDao.findById(projectId).flatMap(project -> commitDao.findByProjectAndId(project, commitId)).orElseThrow(() -> new IllegalArgumentException("Commit " + commitId + " not found.")); |
44 | 46 | Element relatedElement = elementDao.findByCommitAndId(commit, relatedElementId).orElseThrow(() -> new IllegalArgumentException("Element " + relatedElementId + " not found.")); |
45 | | - return relationshipDao.findAllByCommitRelatedElement(commit, relatedElement); |
| 47 | + Set<Relationship> allRelationships = relationshipDao.findAllByCommitRelatedElement(commit, relatedElement); |
| 48 | + Set<Relationship> results = allRelationships; |
| 49 | + if (RelationshipDirection.OUT.equals(direction)) { |
| 50 | + results = allRelationships.stream() |
| 51 | + .filter(r -> r.getSource().stream() |
| 52 | + .anyMatch(e -> Objects.equals(e.getIdentifier(), relatedElementId)) |
| 53 | + ) |
| 54 | + .collect(Collectors.toSet()); |
| 55 | + } else if (RelationshipDirection.IN.equals(direction)) { |
| 56 | + results = allRelationships.stream() |
| 57 | + .filter(r -> r.getTarget().stream() |
| 58 | + .anyMatch(e -> Objects.equals(e.getIdentifier(), relatedElementId)) |
| 59 | + ) |
| 60 | + .collect(Collectors.toSet()); |
| 61 | + } |
| 62 | + |
| 63 | + return results; |
46 | 64 | } |
47 | 65 | } |
0 commit comments