/
NodesResource.java
111 lines (95 loc) · 3.7 KB
/
NodesResource.java
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
/**
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package edu.berkeley.ground.resources;
import com.codahale.metrics.annotation.Timed;
import edu.berkeley.ground.model.models.Node;
import edu.berkeley.ground.dao.models.NodeFactory;
import edu.berkeley.ground.model.models.NodeVersion;
import edu.berkeley.ground.dao.models.NodeVersionFactory;
import edu.berkeley.ground.model.models.Tag;
import edu.berkeley.ground.exceptions.GroundException;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.validation.Valid;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.util.List;
import java.util.Map;
@Path("/nodes")
@Api(value = "/nodes", description = "Interact with the nodes in the graph")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class NodesResource {
private static final Logger LOGGER = LoggerFactory.getLogger(NodesResource.class);
private NodeFactory nodeFactory;
private NodeVersionFactory nodeVersionFactory;
public NodesResource(NodeFactory nodeFactory, NodeVersionFactory nodeVersionFactory) {
this.nodeFactory = nodeFactory;
this.nodeVersionFactory = nodeVersionFactory;
}
@GET
@Timed
@Path("/{name}")
public Node getNode(@PathParam("name") String name) throws GroundException {
LOGGER.info("Retrieving node " + name + ".");
return this.nodeFactory.retrieveFromDatabase(name);
}
@GET
@Timed
@Path("/versions/{id}")
public NodeVersion getNodeVersion(@PathParam("id") long id) throws GroundException {
LOGGER.info("Retrieving node version " + id + ".");
return this.nodeVersionFactory.retrieveFromDatabase(id);
}
@POST
@Timed
@Path("/{name}/{key}")
public Node createNode(@PathParam("name") String name,
@PathParam("key") String sourceKey,
@Valid Map<String, Tag> tags) throws GroundException {
LOGGER.info("Creating node " + name + ".");
return this.nodeFactory.create(name, sourceKey, tags);
}
@POST
@Timed
@Path("/versions")
public NodeVersion createNodeVersion(@Valid NodeVersion nodeVersion,
@QueryParam("parents") List<Long> parentIds)
throws GroundException {
LOGGER.info("Creating node version in node " + nodeVersion.getNodeId() + ".");
return this.nodeVersionFactory.create(nodeVersion.getTags(),
nodeVersion.getStructureVersionId(),
nodeVersion.getReference(),
nodeVersion.getParameters(),
nodeVersion.getNodeId(),
parentIds);
}
@GET
@Timed
@Path("/{name}/latest")
public List<Long> getLatestVersions(@PathParam("name") String name) throws GroundException {
LOGGER.info("Retrieving the latest version of node " + name + ".");
return this.nodeFactory.getLeaves(name);
}
@GET
@Timed
@Path("/adjacent/{id}/{edgeName}")
public List<Long> adjacentNodes(@PathParam("id") long nodeVersionId,
@PathParam("edgeName") String edgeNameRegex)
throws GroundException {
LOGGER.info("Retrieving adjancent nodes to node version " + nodeVersionId + ".");
return this.nodeVersionFactory.getAdjacentNodes(nodeVersionId, edgeNameRegex);
}
}