1 /*******************************************************************************
2 * Copyright (c) 2017 Politecnico di Torino and others.
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Apache License, Version 2.0
6 * which accompanies this distribution, and is available at
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *******************************************************************************/
9 package it.polito.verigraph.resources;
11 import java.io.IOException;
13 import java.util.List;
15 import javax.ws.rs.Consumes;
16 import javax.ws.rs.DELETE;
17 import javax.ws.rs.GET;
18 import javax.ws.rs.POST;
19 import javax.ws.rs.PUT;
20 import javax.ws.rs.Path;
21 import javax.ws.rs.PathParam;
22 import javax.ws.rs.Produces;
23 import javax.ws.rs.core.Context;
24 import javax.ws.rs.core.MediaType;
25 import javax.ws.rs.core.Response;
26 import javax.ws.rs.core.UriInfo;
27 import javax.xml.bind.JAXBException;
29 import com.fasterxml.jackson.core.JsonParseException;
30 import com.fasterxml.jackson.core.JsonProcessingException;
31 import com.fasterxml.jackson.databind.JsonMappingException;
33 import io.swagger.annotations.Api;
34 import io.swagger.annotations.ApiOperation;
35 import io.swagger.annotations.ApiParam;
36 import io.swagger.annotations.ApiResponse;
37 import io.swagger.annotations.ApiResponses;
38 import it.polito.verigraph.model.ErrorMessage;
39 import it.polito.verigraph.model.Neighbour;
40 import it.polito.verigraph.service.NeighbourService;
43 @Api( hidden= true, value = "", description = "Manage neighbours" )
44 @Produces(MediaType.APPLICATION_JSON)
45 @Consumes(MediaType.APPLICATION_JSON)
46 public class NeighbourResource {
47 private NeighbourService neighboursService = new NeighbourService();
52 value = "Returns all neighbours of a given node belonging to a given graph",
53 notes = "Returns an array of neighbours of a given node belonging to a given graph",
54 response = Neighbour.class,
55 responseContainer = "List")
56 @ApiResponses(value = { @ApiResponse(code = 403, message = "Invalid graph and/or node id", response=ErrorMessage.class),
57 @ApiResponse(code = 404, message = "Graph and/or node not found", response=ErrorMessage.class),
58 @ApiResponse(code = 500, message = "Internal server error", response = ErrorMessage.class),
59 @ApiResponse(code = 200, message = "All the neighbours have been returned in the message body", response=Neighbour.class, responseContainer="List")})
60 public List<Neighbour> getAllNeighbours(
61 @ApiParam(value = "Graph id", required = true) @PathParam("graphId") long graphId,
62 @ApiParam(value = "Node id", required = true) @PathParam("nodeId") long nodeId) throws JsonProcessingException{
63 return neighboursService.getAllNeighbours(graphId, nodeId);
69 value = "Adds a neighbour to a given node belonging to a given graph",
70 notes = "Adds single neighbour to a given node belonging to a given graph",
71 response = Neighbour.class)
72 @ApiResponses(value = { @ApiResponse(code = 403, message = "Invalid graph and/or node id", response=ErrorMessage.class),
73 @ApiResponse(code = 404, message = "Graph and/or node not found", response=ErrorMessage.class),
74 @ApiResponse(code = 400, message = "Invalid neighbour object", response=ErrorMessage.class),
75 @ApiResponse(code = 500, message = "Internal server error", response = ErrorMessage.class),
76 @ApiResponse(code = 201, message = "Neighbour successfully created", response=Neighbour.class)})
77 public Response addNeighbour(
78 @ApiParam(value = "Graph id", required = true) @PathParam("graphId") long graphId,
79 @ApiParam(value = "Node id", required = true) @PathParam("nodeId") long nodeId,
80 @ApiParam(value = "New neighbour object. Neighbour name must refer to the name of an existing node of the same graph", required = true) Neighbour neighbour,
81 @Context UriInfo uriInfo) throws JsonParseException, JsonMappingException, JAXBException, IOException{
82 Neighbour newNeighbour = neighboursService.addNeighbour(graphId, nodeId, neighbour);
83 String newId = String.valueOf(newNeighbour.getId());
84 URI uri = uriInfo.getAbsolutePathBuilder().path(newId).build();
85 return Response.created(uri)
91 @Path("{neighbourId}")
94 value = "Edits a neighbour of a given node belonging to a given graph",
95 notes = "Edits a single neighbour of a given node belonging to a given graph",
96 response = Neighbour.class)
97 @ApiResponses(value = { @ApiResponse(code = 403, message = "Invalid graph and/or node and/or neighbour id", response=ErrorMessage.class),
98 @ApiResponse(code = 404, message = "Graph and/or node and /or neighbour not found", response=ErrorMessage.class),
99 @ApiResponse(code = 400, message = "Invalid neighbour object", response=ErrorMessage.class),
100 @ApiResponse(code = 500, message = "Internal server error", response = ErrorMessage.class),
101 @ApiResponse(code = 200, message = "Neighbour edited successfully", response=Neighbour.class)})
102 public Neighbour updateNeighbour(
103 @ApiParam(value = "Graph id", required = true) @PathParam("graphId") long graphId,
104 @ApiParam(value = "Node id", required = true) @PathParam("nodeId") long nodeId,
105 @ApiParam(value = "Neighbour id", required = true) @PathParam("neighbourId") long neighbourId,
106 @ApiParam(value = "Updated neighbour object. Neighbour name must refer to the name of an existing node of the same graph", required = true) Neighbour neighbour) throws JAXBException, IOException{
107 neighbour.setId(neighbourId);
108 return neighboursService.updateNeighbour(graphId, nodeId, neighbour);
112 @Path("{neighbourId}")
114 httpMethod = "DELETE",
115 value = "Removes a neighbour from a given node belonging to a given graph",
116 notes = "Deletes a single neighbour of a given node belonging to a given graph",
117 response = Neighbour.class)
118 @ApiResponses(value = { @ApiResponse(code = 403, message = "Invalid graph and/or node and/or neighbour id", response=ErrorMessage.class),
119 @ApiResponse(code = 404, message = "Graph and/or node not found", response=ErrorMessage.class),
120 @ApiResponse(code = 500, message = "Internal server error", response = ErrorMessage.class),
121 @ApiResponse(code = 204, message = "Node successfully deleted")})
122 public void deleteNeighbour(
123 @ApiParam(value = "Graph id", required = true) @PathParam("graphId") long graphId,
124 @ApiParam(value = "Node id", required = true) @PathParam("nodeId") long nodeId,
125 @ApiParam(value = "Neighbour id", required = true) @PathParam("neighbourId") long neighbourId){
126 neighboursService.removeNeighbour(graphId, nodeId, neighbourId);
130 @Path("{neighbourId}")
133 value = "Returns a neighbour of a given node belonging to a given graph",
134 notes = "Returns a single neighbour of a given node belonging to a given graph",
135 response = Neighbour.class)
136 @ApiResponses(value = { @ApiResponse(code = 403, message = "Invalid graph and/or node and/or neighbour id", response=ErrorMessage.class),
137 @ApiResponse(code = 404, message = "Graph and/or node and /or neighbour not found", response=ErrorMessage.class),
138 @ApiResponse(code = 500, message = "Internal server error", response = ErrorMessage.class),
139 @ApiResponse(code = 200, message = "The requested neighbour has been returned in the message body", response=Neighbour.class)})
140 public Neighbour getNeighbour(
141 @ApiParam(value = "Graph id", required = true) @PathParam("graphId") long graphId,
142 @ApiParam(value = "Node id", required = true) @PathParam("nodeId") long nodeId,
143 @ApiParam(value = "Neighbour id", required = true) @PathParam("neighbourId") long neighbourId) throws JsonProcessingException{
144 return neighboursService.getNeighbour(graphId, nodeId, neighbourId);