Skip to content

Commit b978e16

Browse files
committed
Added endpoints and service calls to get relationships by source, target, or both
1 parent 60b6233 commit b978e16

8 files changed

Lines changed: 124 additions & 14 deletions

File tree

app/controllers/ElementController.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.fasterxml.jackson.databind.JsonNode;
44
import models.Element;
5+
import models.Relationship;
56
import play.libs.Json;
67
import play.mvc.Controller;
78
import play.mvc.Result;
@@ -36,6 +37,21 @@ public Result byId(String id) {
3637
}
3738
}
3839

40+
public Result byIdAndModel(String eid, String mid) {
41+
try {
42+
UUID elementId = UUID.fromString(eid);
43+
UUID modelId = UUID.fromString(mid);
44+
Element element = elementService.getById(modelId, elementId);
45+
if(element!=null)
46+
return ok(Json.toJson(element).toString());
47+
else
48+
return notFound("Element with id " + eid + " cannot be found in model with id " + mid);
49+
}
50+
catch (IllegalArgumentException e) {
51+
return badRequest("Supplied identifiers are not UUIDs.");
52+
}
53+
}
54+
3955
public Result all() {
4056
Set<Element> elements = elementService.getAll();
4157
return ok(Json.toJson(elements));

app/controllers/ModelController.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
import com.fasterxml.jackson.databind.JsonNode;
44

5+
import models.Element;
56
import models.Model;
67
import play.libs.Json;
78
import play.mvc.Controller;
89
import play.mvc.Result;
910

11+
import services.ElementService;
1012
import services.ModelService;
1113

1214
import javax.inject.Inject;
@@ -29,9 +31,9 @@ public ModelController(ModelService modelService) {
2931

3032
public Result byId(String id) {
3133
try {
32-
UUID elementId = UUID.fromString(id);
33-
Model model = modelService.getById(elementId);
34-
return ok(Json.toJson(model).toString());
34+
UUID modelId = UUID.fromString(id);
35+
Model model = modelService.getById(modelId);
36+
return ok(Json.toJson(model));
3537
}
3638
catch (IllegalArgumentException e) {
3739
return badRequest("Supplied identifier is not a UUID.");

app/controllers/RelationshipController.java

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,50 @@ public RelationshipController(RelationshipService relationService) {
2828
}
2929

3030
public Result byId(String id) {
31+
try {
32+
UUID relationshipId = UUID.fromString(id);
33+
Relationship relation = relationService.getById(relationshipId);
34+
return ok(Json.toJson(relation));
35+
}
36+
catch (IllegalArgumentException e) {
37+
return badRequest("Supplied identifier is not a UUID.");
38+
}
39+
}
40+
41+
public Result byElementId(String id) {
42+
try {
43+
UUID elementId = UUID.fromString(id);
44+
Set<Relationship> relations = relationService.getByElementId(elementId);
45+
return ok(Json.toJson(relations));
46+
}
47+
catch (IllegalArgumentException e) {
48+
return badRequest("Supplied identifier is not a UUID.");
49+
}
50+
}
51+
52+
public Result bySourceElementId(String id) {
53+
try {
54+
UUID elementId = UUID.fromString(id);
55+
Set<Relationship> relations = relationService.getBySourceElementId(elementId);
56+
return ok(Json.toJson(relations));
57+
}
58+
catch (IllegalArgumentException e) {
59+
return badRequest("Supplied identifier is not a UUID.");
60+
}
61+
}
62+
63+
public Result byTargetElementId(String id) {
3164
try {
3265
UUID elementId = UUID.fromString(id);
33-
Relationship relation = relationService.getById(elementId);
34-
return ok(Json.toJson(relation).toString());
66+
Set<Relationship> relations = relationService.getByTargetElementId(elementId);
67+
return ok(Json.toJson(relations));
3568
}
3669
catch (IllegalArgumentException e) {
3770
return badRequest("Supplied identifier is not a UUID.");
3871
}
3972
}
4073

74+
4175
public Result all() {
4276
Set<Relationship> relations = relationService.getAll();
4377
return ok(Json.toJson(relations));

app/models/EndType.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package models;
2+
3+
public enum EndType {
4+
source,
5+
target
6+
}

app/services/ElementService.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import models.Element;
88

99
import javax.inject.Inject;
10+
import javax.inject.Singleton;
1011
import java.util.HashSet;
1112
import java.util.Set;
1213
import java.util.UUID;
@@ -17,7 +18,9 @@
1718
* Main service that provides CRUD operations for all SysML v2 elements
1819
*/
1920

21+
@Singleton
2022
public class ElementService {
23+
2124
@Inject private CassandraSessionBuilder sessionBuilder;
2225

2326
public Set<Element> getAll() {
@@ -38,6 +41,14 @@ public Element getById(UUID identifier) {
3841
return null;
3942
}
4043

44+
public Element getById(UUID modelId, UUID elementId) {
45+
Element element = getById(elementId);
46+
if(element.parent_model.equals(modelId))
47+
return element;
48+
else
49+
return null;
50+
}
51+
4152
public Element create(Element elem) {
4253
if(elem!=null) {
4354
UUID elementIdentifier = elem.identifier;

app/services/ModelService.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import models.Model;
99

1010
import javax.inject.Inject;
11+
import javax.inject.Singleton;
1112
import java.util.HashSet;
1213
import java.util.Set;
1314
import java.util.UUID;
@@ -18,7 +19,9 @@
1819
* Main service that provides CRUD operations for all SysML v2 models
1920
*/
2021

22+
@Singleton
2123
public class ModelService {
24+
2225
@Inject private CassandraSessionBuilder sessionBuilder;
2326

2427
public Set<Model> getAll() {

app/services/RelationshipService.java

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
import com.datastax.driver.core.utils.UUIDs;
66
import dao.CassandraSessionBuilder;
77

8+
import models.EndType;
89
import models.Relationship;
910
import play.Logger;
10-
import play.Logger.*;
1111

1212
import javax.inject.Inject;
13+
import javax.inject.Singleton;
14+
1315
import java.util.HashSet;
1416
import java.util.Set;
1517
import java.util.UUID;
@@ -20,7 +22,9 @@
2022
* Main service that provides CRUD operations for all SysML v2 elements
2123
*/
2224

25+
@Singleton
2326
public class RelationshipService {
27+
2428
@Inject private CassandraSessionBuilder sessionBuilder;
2529

2630
final Logger.ALogger logger = Logger.of(this.getClass());
@@ -48,6 +52,36 @@ public Relationship getById(UUID identifier) {
4852
return null;
4953
}
5054

55+
public Set<Relationship> getByElementId(UUID elementIdentifier) {
56+
Set<Relationship> relations = getByEndTypeId(elementIdentifier, EndType.source);
57+
relations.addAll(getByEndTypeId(elementIdentifier, EndType.target));
58+
return relations;
59+
}
60+
61+
public Set<Relationship> getBySourceElementId(UUID elementIdentifier) {
62+
return getByEndTypeId(elementIdentifier, EndType.source);
63+
}
64+
65+
public Set<Relationship> getByTargetElementId(UUID elementIdentifier) {
66+
return getByEndTypeId(elementIdentifier, EndType.target);
67+
}
68+
69+
private Set<Relationship> getByEndTypeId(UUID elementIdentifier, EndType endType) {
70+
Set<Relationship> relations = new HashSet<>();
71+
72+
String end = endType.toString();
73+
74+
ResultSet resultSet = sessionBuilder.getSession().execute("select identifier, name, description, parent_model, " +
75+
"type, source_element_role, source_element, target_element_role, target_element from sysml2.relationships where " + end + "_element = " + elementIdentifier + " allow filtering;");
76+
77+
for(Row r: resultSet)
78+
relations.add(new Relationship(r.getUUID(0), r.getString(1), r.getString(2), r.getUUID(3), r.getString(4),
79+
r.getString(5), r.getUUID(6), r.getString(7), r.getUUID(8)));
80+
81+
return relations;
82+
83+
}
84+
5185
public Relationship create(Relationship relation) {
5286
if(relation!=null) {
5387
UUID identifier = relation.identifier;

conf/routes

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,23 @@
66
GET / controllers.HomeController.index
77

88
# Model endpoints
9-
GET /model/:id controllers.ModelController.byId(id)
109
GET /model controllers.ModelController.all()
10+
GET /model/:id controllers.ModelController.byId(id)
1111
POST /model controllers.ModelController.create()
1212

1313
# Element endpoints
14-
GET /element/:id controllers.ElementController.byId(id)
15-
GET /element controllers.ElementController.all()
16-
POST /element controllers.ElementController.create()
14+
GET /element controllers.ElementController.all()
15+
GET /element/:id controllers.ElementController.byId(id)
16+
GET /element/:id/model/:mid controllers.ElementController.byIdAndModel(id,mid)
17+
POST /element controllers.ElementController.create()
1718

1819
# Relationship endpoints
19-
GET /relationship/:id controllers.RelationshipController.byId(id)
20-
GET /relationship controllers.RelationshipController.all()
21-
POST /relationship controllers.RelationshipController.create()
20+
GET /relationship/:id controllers.RelationshipController.byId(id)
21+
GET /relationship/element/:id controllers.RelationshipController.byElementId(id)
22+
GET /relationship/source/:id controllers.RelationshipController.bySourceElementId(id)
23+
GET /relationship/target/:id controllers.RelationshipController.byTargetElementId(id)
24+
GET /relationship controllers.RelationshipController.all()
25+
POST /relationship controllers.RelationshipController.create()
2226

2327
# Map static resources from the /public folder to the /assets URL path
24-
GET /assets/*file controllers.Assets.versioned(path="/public", file: Asset)
28+
GET /assets/*file controllers.Assets.versioned(path="/public", file: Asset)

0 commit comments

Comments
 (0)