-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathGraphController.java
More file actions
111 lines (88 loc) · 3.27 KB
/
Copy pathGraphController.java
File metadata and controls
111 lines (88 loc) · 3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package controllers;
import com.fasterxml.jackson.databind.JsonNode;
import dao.models.GraphFactory;
import dao.models.GraphVersionFactory;
import db.DbClient;
import exceptions.GroundException;
import exceptions.GroundItemNotFoundException;
import models.models.Graph;
import models.models.GraphVersion;
import models.models.Tag;
import play.libs.Json;
import play.mvc.Controller;
import play.mvc.Result;
import util.ControllerUtils;
import util.FactoryGenerator;
import javax.inject.Inject;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class GraphController extends Controller {
private final GraphFactory graphFactory;
private final GraphVersionFactory graphVersionFactory;
private final DbClient dbClient;
@Inject
public GraphController(FactoryGenerator generator) throws GroundException {
this.dbClient = generator.getDbClient();
this.graphFactory = generator.getGraphFactory();
this.graphVersionFactory = generator.getGraphVersionFactory();
}
public Result getGraph(String sourceKey) throws GroundException {
try {
JsonNode json = Json.toJson(this.graphFactory.retrieveFromDatabase(sourceKey));
return ok(json);
} finally {
this.dbClient.commit();
}
}
public Result getGraphVersion(Long id) throws GroundException {
try {
JsonNode json = Json.toJson(this.graphVersionFactory.retrieveFromDatabase(id));
return ok(json);
} finally {
this.dbClient.commit();
}
}
public Result createGraph(String sourceKey, String name) throws GroundException {
Graph graph;
try {
JsonNode requestBody = request().body().asJson();
Map<String, Tag> tags = ControllerUtils.getTagsFromJson(requestBody);
graph = this.graphFactory.create(name, sourceKey, tags);
this.dbClient.commit();
} catch (GroundException e) {
this.dbClient.abort();
throw e;
}
JsonNode json = Json.toJson(graph);
return ok(json);
}
public Result createGraphVersion(String sourceKey) throws GroundException {
try {
long graphId;
try {
graphId = this.graphFactory.retrieveFromDatabase(sourceKey).getId();
} catch (GroundException e) {
if (e instanceof GroundItemNotFoundException) {
graphId = this.graphFactory.create(null, sourceKey, new HashMap<>()).getId();
} else {
throw e;
}
}
JsonNode requestBody = request().body().asJson();
Map<String, Tag> tags = ControllerUtils.getTagsFromJson(requestBody);
Map<String, String> referenceParameters = ControllerUtils.getParametersFromJson(requestBody);
List<Long> parents = ControllerUtils.getListFromJson(requestBody, "parents");
long structureVersionId = ControllerUtils.getLongFromJson(requestBody, "structureVersionId");
String reference = ControllerUtils.getStringFromJson(requestBody, "reference");
List<Long> edgeVersionIds = ControllerUtils.getListFromJson(requestBody, "edgeVersionIds");
GraphVersion created = this.graphVersionFactory.create(tags, structureVersionId, reference,
referenceParameters, graphId, edgeVersionIds, parents);
this.dbClient.commit();
return ok(Json.toJson(created));
} catch (GroundException e) {
this.dbClient.abort();
throw e;
}
}
}