-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathNodeController.java
More file actions
109 lines (87 loc) · 3.13 KB
/
Copy pathNodeController.java
File metadata and controls
109 lines (87 loc) · 3.13 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
package controllers;
import com.fasterxml.jackson.databind.JsonNode;
import dao.models.NodeFactory;
import dao.models.NodeVersionFactory;
import db.DbClient;
import exceptions.GroundException;
import exceptions.GroundItemNotFoundException;
import models.models.Node;
import models.models.NodeVersion;
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 NodeController extends Controller {
private final NodeFactory nodeFactory;
private final NodeVersionFactory nodeVersionFactory;
private final DbClient dbClient;
@Inject
public NodeController(FactoryGenerator generator) throws GroundException {
this.dbClient = generator.getDbClient();
this.nodeFactory = generator.getNodeFactory();
this.nodeVersionFactory = generator.getNodeVersionFactory();
}
public Result getNode(String sourceKey) throws GroundException {
try {
JsonNode json = Json.toJson(this.nodeFactory.retrieveFromDatabase(sourceKey));
return ok(json);
} finally {
this.dbClient.commit();
}
}
public Result getNodeVersion(Long id) throws GroundException {
try {
JsonNode json = Json.toJson(this.nodeVersionFactory.retrieveFromDatabase(id));
return ok(json);
} finally {
this.dbClient.commit();
}
}
public Result createNode(String sourceKey, String name) throws GroundException {
Node node;
try {
JsonNode requestBody = request().body().asJson();
Map<String, Tag> tags = ControllerUtils.getTagsFromJson(requestBody);
node = this.nodeFactory.create(name, sourceKey, tags);
this.dbClient.commit();
} catch (GroundException e) {
this.dbClient.abort();
throw e;
}
JsonNode json = Json.toJson(node);
return ok(json);
}
public Result createNodeVersion(String sourceKey) throws GroundException {
try {
long nodeId;
try {
nodeId = this.nodeFactory.retrieveFromDatabase(sourceKey).getId();
} catch (GroundException e) {
if (e instanceof GroundItemNotFoundException) {
nodeId = this.nodeFactory.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");
NodeVersion created = this.nodeVersionFactory.create(tags, structureVersionId, reference,
referenceParameters, nodeId, parents);
this.dbClient.commit();
return ok(Json.toJson(created));
} catch (GroundException e) {
this.dbClient.abort();
throw e;
}
}
}