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.service;
11 import java.io.IOException;
12 import java.util.ArrayList;
13 import java.util.List;
16 import javax.xml.bind.JAXBException;
18 import com.fasterxml.jackson.core.JsonParseException;
19 import com.fasterxml.jackson.core.JsonProcessingException;
20 import com.fasterxml.jackson.databind.JsonMappingException;
22 import it.polito.neo4j.manager.Neo4jDBManager;
23 import it.polito.verigraph.exception.BadRequestException;
24 import it.polito.verigraph.exception.DataNotFoundException;
25 import it.polito.verigraph.exception.ForbiddenException;
26 import it.polito.verigraph.model.Graph;
27 import it.polito.verigraph.model.Neighbour;
28 import it.polito.verigraph.model.Node;
30 public class NeighbourService {
32 private Neo4jDBManager manager=new Neo4jDBManager();
35 public List<Neighbour> getAllNeighbours(long graphId, long nodeId) throws JsonProcessingException {
37 throw new ForbiddenException("Illegal graph id: " + graphId);
40 throw new ForbiddenException("Illegal node id: " + nodeId);
43 Map<Long, Neighbour> neighbours = manager.getNeighbours(graphId, nodeId);
44 return new ArrayList<Neighbour>(neighbours.values());
47 public Neighbour getNeighbour(long graphId, long nodeId, long neighbourId) throws JsonProcessingException {
49 throw new ForbiddenException("Illegal graph id: " + graphId);
52 throw new ForbiddenException("Illegal node id: " + nodeId);
54 if (neighbourId < 0) {
55 throw new ForbiddenException("Illegal neighbour id: " + neighbourId);
58 Neighbour neighbour=manager.getNeighbour(graphId, nodeId, neighbourId);
59 if (neighbour == null) {
60 throw new DataNotFoundException("Neighbour with id "+ neighbourId + " not found for node with id " + nodeId
61 + " in graph with id " + graphId);
66 public Neighbour addNeighbour(long graphId, long nodeId, Neighbour neighbour) throws JsonParseException, JsonMappingException, JAXBException, IOException {
68 throw new ForbiddenException("Illegal graph id: " + graphId);
71 throw new ForbiddenException("Illegal node id: " + nodeId);
73 Graph graph = manager.getGraph(graphId);
74 Node node=manager.getNodeById(nodeId, graphId);
75 validateNeighbour(graph, node, neighbour);
76 Neighbour out=manager.addNeighbours(graphId, nodeId, neighbour);
77 validateNeighbour(graph, node, neighbour);
81 public Neighbour updateNeighbour(long graphId, long nodeId, Neighbour neighbour) throws JAXBException, IOException {
83 throw new ForbiddenException("Illegal graph id: " + graphId);
86 throw new ForbiddenException("Illegal node id: " + nodeId);
88 if (neighbour.getId() < 0) {
89 throw new ForbiddenException("Illegal neighbour id: " + nodeId);
91 Graph graph=manager.getGraph(graphId);
92 Node node=manager.getNodeById(nodeId, graphId);
94 throw new DataNotFoundException("Node with id " + nodeId + " not found in graph with id " + graphId);
97 validateNeighbour(graph, node, neighbour);
98 Neighbour n=manager.updateNeighbour(graphId, nodeId, neighbour);
103 public Neighbour removeNeighbour(long graphId, long nodeId, long neighbourId) {
105 throw new ForbiddenException("Illegal graph id: " + graphId);
108 throw new ForbiddenException("Illegal node id: " + nodeId);
110 if (neighbourId < 0) {
111 throw new ForbiddenException("Illegal neighbour id: " + nodeId);
114 Neighbour n=manager.deleteNeighbour(graphId, nodeId, neighbourId);
118 public static void validateNeighbour(Graph graph, Node node, Neighbour neighbour) throws JsonProcessingException {
120 throw new BadRequestException("Neighbour validation failed: cannot validate null graph");
122 throw new BadRequestException("Neighbour validation failed: cannot validate null node");
123 if (neighbour == null)
124 throw new BadRequestException("Neighbour validation failed: cannot validate null neighbour");
126 if (neighbour.getName() == null)
127 throw new BadRequestException("Neighbour validation failed: neighbour 'name' field cannot be null");
128 if (neighbour.getName().equals(""))
129 throw new BadRequestException("Neighbour validation failed: neighbour 'name' field cannot be an empty string");
131 //Node nodeFound = graph.searchNodeByName(neighbour.getName());
133 Node nodeFound=graph.searchNodeByName(neighbour.getName());
134 if ((nodeFound == null) || (nodeFound.getName().equals(node.getName())))
135 throw new BadRequestException("Neighbour validation failed: '"+ neighbour.getName()
136 + "' is not a valid name for a neighbour of node '" + node.getName() + "'");
138 Neighbour neighbourFound = node.searchNeighbourByName(neighbour.getName());
139 if ((neighbourFound != null) && (neighbourFound.equals(neighbour) == false))
140 throw new BadRequestException("Neighbour validation failed: node '"+ node.getName()
141 + "' already has a neighbour named '" + neighbour.getName() + "'");