update verigraph
[parser.git] / verigraph / src / it / polito / verigraph / service / NeighbourService.java
1 /*******************************************************************************
2  * Copyright (c) 2017 Politecnico di Torino and others.
3  *
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;
10
11 import java.io.IOException;
12 import java.util.ArrayList;
13 import java.util.List;
14 import java.util.Map;
15
16 import javax.xml.bind.JAXBException;
17
18 import com.fasterxml.jackson.core.JsonParseException;
19 import com.fasterxml.jackson.core.JsonProcessingException;
20 import com.fasterxml.jackson.databind.JsonMappingException;
21
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;
29
30 public class NeighbourService {
31
32     private Neo4jDBManager manager=new Neo4jDBManager();
33
34
35     public List<Neighbour> getAllNeighbours(long graphId, long nodeId) throws JsonProcessingException {
36         if (graphId < 0) {
37             throw new ForbiddenException("Illegal graph id: " + graphId);
38         }
39         if (nodeId < 0) {
40             throw new ForbiddenException("Illegal node id: " + nodeId);
41         }
42
43         Map<Long, Neighbour> neighbours = manager.getNeighbours(graphId, nodeId);
44         return new ArrayList<Neighbour>(neighbours.values());
45     }
46
47     public Neighbour getNeighbour(long graphId, long nodeId, long neighbourId) throws JsonProcessingException {
48         if (graphId < 0) {
49             throw new ForbiddenException("Illegal graph id: " + graphId);
50         }
51         if (nodeId < 0) {
52             throw new ForbiddenException("Illegal node id: " + nodeId);
53         }
54         if (neighbourId < 0) {
55             throw new ForbiddenException("Illegal neighbour id: " + neighbourId);
56         }
57
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);
62         }
63         return neighbour;
64     }
65
66     public Neighbour addNeighbour(long graphId, long nodeId, Neighbour neighbour) throws JsonParseException, JsonMappingException, JAXBException, IOException {
67         if (graphId < 0) {
68             throw new ForbiddenException("Illegal graph id: " + graphId);
69         }
70         if (nodeId < 0) {
71             throw new ForbiddenException("Illegal node id: " + nodeId);
72         }
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);
78         return out;
79     }
80
81     public Neighbour updateNeighbour(long graphId, long nodeId, Neighbour neighbour) throws JAXBException, IOException {
82         if (graphId < 0) {
83             throw new ForbiddenException("Illegal graph id: " + graphId);
84         }
85         if (nodeId < 0) {
86             throw new ForbiddenException("Illegal node id: " + nodeId);
87         }
88         if (neighbour.getId() < 0) {
89             throw new ForbiddenException("Illegal neighbour id: " + nodeId);
90         }
91         Graph graph=manager.getGraph(graphId);
92         Node node=manager.getNodeById(nodeId, graphId);
93         if (node == null) {
94             throw new DataNotFoundException("Node with id " + nodeId + " not found in graph with id " + graphId);
95         }
96
97         validateNeighbour(graph, node, neighbour);
98         Neighbour n=manager.updateNeighbour(graphId, nodeId, neighbour);
99
100         return n;
101     }
102
103     public Neighbour removeNeighbour(long graphId, long nodeId, long neighbourId) {
104         if (graphId < 0) {
105             throw new ForbiddenException("Illegal graph id: " + graphId);
106         }
107         if (nodeId < 0) {
108             throw new ForbiddenException("Illegal node id: " + nodeId);
109         }
110         if (neighbourId < 0) {
111             throw new ForbiddenException("Illegal neighbour id: " + nodeId);
112         }
113
114         Neighbour n=manager.deleteNeighbour(graphId, nodeId, neighbourId);
115         return n;
116     }
117
118     public static void validateNeighbour(Graph graph, Node node, Neighbour neighbour) throws JsonProcessingException {
119         if (graph == null)
120             throw new BadRequestException("Neighbour validation failed: cannot validate null graph");
121         if (node == null)
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");
125
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");
130
131         //Node nodeFound = graph.searchNodeByName(neighbour.getName());
132
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() + "'");
137
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() + "'");
142     }
143 }