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.neo4j.manager;
11 import java.io.IOException;
12 import java.util.ArrayList;
13 import java.util.HashMap;
14 import java.util.List;
17 import java.util.logging.Logger;
19 import javax.ws.rs.BadRequestException;
20 import javax.ws.rs.ForbiddenException;
21 import javax.ws.rs.NotFoundException;
22 import javax.ws.rs.PathParam;
23 import javax.ws.rs.QueryParam;
24 import javax.xml.bind.JAXBException;
26 import com.fasterxml.jackson.core.JsonParseException;
27 import com.fasterxml.jackson.core.JsonProcessingException;
28 import com.fasterxml.jackson.databind.JsonMappingException;
30 import it.polito.neo4j.exceptions.DuplicateNodeException;
31 import it.polito.neo4j.exceptions.MyInvalidObjectException;
32 import it.polito.neo4j.exceptions.MyNotFoundException;
33 import it.polito.neo4j.jaxb.ObjectFactory;
34 import it.polito.neo4j.jaxb.Paths;
35 import it.polito.neo4j.translator.GraphToNeo4j;
36 import it.polito.neo4j.translator.Neo4jToGraph;
37 import it.polito.neo4j.exceptions.MyInvalidDirectionException;
38 import it.polito.neo4j.exceptions.MyInvalidIdException;
39 import it.polito.verigraph.exception.DataNotFoundException;
40 import it.polito.verigraph.model.Configuration;
41 import it.polito.verigraph.model.Graph;
42 import it.polito.verigraph.model.Neighbour;
43 import it.polito.verigraph.model.Node;
44 import it.polito.verigraph.service.VerigraphLogger;
46 public class Neo4jDBManager {
48 public static Neo4jLibrary lib = Neo4jLibrary.getNeo4jLibrary();
49 //static ObjectFactory obFactory = new ObjectFactory();
51 public static VerigraphLogger vlogger = VerigraphLogger.getVerigraphlogger();
54 public it.polito.verigraph.model.Graph updateGraph(it.polito.verigraph.model.Graph graph) throws JAXBException, JsonParseException, JsonMappingException, IOException, MyInvalidIdException{
56 it.polito.neo4j.jaxb.Graph graph_xml=GraphToNeo4j.generateObject(graph);
57 it.polito.neo4j.jaxb.Graph graphReturned;
61 graphId = graph.getId();
62 graphReturned = lib.updateGraph(graph_xml, graphId);
63 it.polito.verigraph.model.Graph g=Neo4jToGraph.generateGraph(graphReturned);
67 catch(MyNotFoundException e1){
68 vlogger.logger.info("error update 1");
69 throw new NotFoundException();
72 catch(DuplicateNodeException e3){
73 vlogger.logger.info("error update 2");
74 throw new BadRequestException(e3.getMessage());
76 catch(MyInvalidObjectException e4){
77 vlogger.logger.info("error update 3");
78 throw new BadRequestException(e4.getMessage());
82 public void deleteGraph(long id){
87 catch(MyNotFoundException e1){
88 throw new NotFoundException();
92 public it.polito.verigraph.model.Graph addGraph(it.polito.verigraph.model.Graph graph) throws JAXBException, JsonParseException, JsonMappingException, IOException, MyInvalidIdException {
94 it.polito.neo4j.jaxb.Graph graph_xml=GraphToNeo4j.generateObject(graph);
95 it.polito.neo4j.jaxb.Graph graphReturned;
96 it.polito.neo4j.jaxb.Node node;
99 graphReturned = lib.createGraph(graph_xml);
100 it.polito.verigraph.model.Graph g=Neo4jToGraph.generateGraph(graphReturned);
104 catch(MyNotFoundException e){
106 throw new NotFoundException();
110 public it.polito.verigraph.model.Graph getGraph(long graphId) throws JAXBException, JsonParseException, JsonMappingException, IOException {
112 it.polito.neo4j.jaxb.Graph graphReturned;
115 graphReturned = lib.getGraph(graphId);
117 it.polito.verigraph.model.Graph g=Neo4jToGraph.generateGraph(graphReturned);
122 catch(MyNotFoundException e){
124 throw new NotFoundException();
129 public List<it.polito.verigraph.model.Graph> getGraphs() throws JsonProcessingException, MyNotFoundException {
131 List<it.polito.verigraph.model.Graph> graphsReturned=new ArrayList<it.polito.verigraph.model.Graph>();
132 it.polito.neo4j.jaxb.Graphs graphs;
134 graphs = lib.getGraphs();
135 for(it.polito.neo4j.jaxb.Graph tmp : graphs.getGraph()){
136 it.polito.verigraph.model.Graph g=Neo4jToGraph.generateGraph(tmp);
137 graphsReturned.add(g);
140 return graphsReturned;
144 public it.polito.verigraph.model.Neighbour addNeighbours(long graphId, long nodeId, it.polito.verigraph.model.Neighbour neighbour) throws JsonProcessingException {
146 it.polito.neo4j.jaxb.Neighbour neighbour_xml=GraphToNeo4j.NeighbourToNeo4j(neighbour);
147 it.polito.neo4j.jaxb.Neighbour neighReturned;
148 it.polito.verigraph.model.Neighbour n;
150 neighReturned = lib.createNeighbour(neighbour_xml, graphId, nodeId);
151 n=Neo4jToGraph.NeighbourToVerigraph(neighReturned);
154 catch(MyNotFoundException e2){
155 e2.printStackTrace();
156 throw new NotFoundException();
160 public Neighbour deleteNeighbour(long graphId, long nodeId, long neighbourId) {
163 it.polito.neo4j.jaxb.Neighbour n=lib.getNeighbour(graphId, nodeId, neighbourId);
165 throw new DataNotFoundException("Neighbour validation failed: '"+ neighbourId
166 + "' is not a valid id for a neighbour of node '" + nodeId + "'" + "' of graph '" + graphId + "'");
168 lib.deleteNeighbour(graphId, nodeId, neighbourId);
169 return Neo4jToGraph.NeighbourToVerigraph(n);
172 catch(MyNotFoundException e1){
173 e1.printStackTrace();
174 throw new NotFoundException();
175 }catch (JsonProcessingException e) {
176 throw new NotFoundException("jsonprocessing node");
181 public Neighbour updateNeighbour(long graphId, long nodeId, it.polito.verigraph.model.Neighbour neighbour) throws JsonProcessingException {
182 it.polito.neo4j.jaxb.Neighbour neighbour_xml=GraphToNeo4j.NeighbourToNeo4j(neighbour);
183 it.polito.neo4j.jaxb.Node nodeReturned;
186 nodeReturned=lib.updateNeighbour(neighbour_xml, graphId, nodeId, neighbour_xml.getId());
187 r=Neo4jToGraph.NeighbourToVerigraph(lib.getNeighbour(graphId, nodeReturned.getId(), neighbour_xml.getId()));
190 catch(MyNotFoundException e2){
191 throw new NotFoundException();
192 } catch (MyInvalidObjectException e) {
199 public it.polito.verigraph.model.Node addNode(long graphId, it.polito.verigraph.model.Node node) throws IOException, MyInvalidIdException {
200 it.polito.neo4j.jaxb.Node node_xml=GraphToNeo4j.NodeToNeo4j(node);
201 it.polito.neo4j.jaxb.Node nodeReturned;
202 it.polito.verigraph.model.Node node_v=new it.polito.verigraph.model.Node();
206 nodeReturned = lib.createNode(node_xml, graphId);
207 node_v=Neo4jToGraph.NodeToVerigraph(nodeReturned);
209 catch(MyNotFoundException e1){
210 e1.printStackTrace();
211 throw new NotFoundException();
214 catch(DuplicateNodeException e3){
215 e3.printStackTrace();
216 throw new BadRequestException();
221 public Node updateNode(long graphId, Node node, long id) throws IOException, MyInvalidIdException {
222 it.polito.neo4j.jaxb.Node node_xml=GraphToNeo4j.NodeToNeo4j(node);
223 it.polito.neo4j.jaxb.Node nodeReturned;
226 nodeReturned = lib.updateNode(node_xml, graphId, id);
227 node_v=Neo4jToGraph.NodeToVerigraph(nodeReturned);
230 catch(MyNotFoundException e2){
231 e2.printStackTrace();
232 throw new NotFoundException();
234 catch(MyInvalidObjectException e3){
235 e3.printStackTrace();
236 throw new BadRequestException(e3.getMessage());
241 public Node deleteNode(long graphId, long nodeId) {
244 it.polito.neo4j.jaxb.Node n=lib.getNodeById(nodeId, graphId);
245 lib.deleteNode(graphId, nodeId);
246 return Neo4jToGraph.NodeToVerigraph(n);
248 catch(MyNotFoundException e1){
249 throw new NotFoundException("graph or node not found");
250 } catch (JsonProcessingException e) {
251 throw new NotFoundException("jsonprocessing node");
255 public Paths getPath(long graphId, String source, String destination, String direction) throws MyInvalidDirectionException {
256 it.polito.neo4j.jaxb.Paths paths=(new ObjectFactory()).createPaths();
258 if(source == null || destination == null || direction == null)
259 throw new DataNotFoundException("Missing query parameters");
260 paths = lib.findAllPathsBetweenTwoNodes(graphId, source, destination,direction);
262 catch(MyNotFoundException e1){
263 e1.printStackTrace();
264 throw new NotFoundException();
271 public Map<Long, Node> getNodes(long graphId) throws JsonProcessingException {
272 Map<Long, Node> nodes=new HashMap<Long, Node>();
275 Set<it.polito.neo4j.jaxb.Node> set= lib.getNodes(graphId);
276 nodes=Neo4jToGraph.NodesToVerigraph(set);
278 catch(MyNotFoundException e1){
279 e1.printStackTrace();
280 throw new NotFoundException();
286 public Map<Long, Neighbour> getNeighbours(long graphId, long nodeId) throws JsonProcessingException {
287 Map<Long, Neighbour> neighbours=new HashMap<Long, Neighbour>();
290 Set<it.polito.neo4j.jaxb.Neighbour> set= lib.getNeighbours(graphId, nodeId);
291 neighbours=Neo4jToGraph.NeighboursToVerigraph(set);
294 catch(MyNotFoundException e1){
295 e1.printStackTrace();
296 throw new NotFoundException();
301 public Node getNodeByName(long graphId, String name) throws JsonProcessingException {
303 it.polito.verigraph.model.Node node;
306 it.polito.neo4j.jaxb.Node set= lib.getNodeByName(name, graphId);
310 node=Neo4jToGraph.NodeToVerigraph(set);
314 catch(MyNotFoundException e1){
315 e1.printStackTrace();
316 throw new NotFoundException();
321 public Node getNodeById(long nodeId, long graphId) throws JsonProcessingException {
322 it.polito.verigraph.model.Node node;
325 it.polito.neo4j.jaxb.Node set= lib.getNodeById(nodeId, graphId);
329 node=Neo4jToGraph.NodeToVerigraph(set);
333 catch(MyNotFoundException e1){
334 e1.printStackTrace();
335 throw new NotFoundException();
339 public Neighbour getNeighbour(long graphId, long nodeId, long neighbourId) throws JsonProcessingException {
342 it.polito.neo4j.jaxb.Neighbour set= lib.getNeighbour(graphId, nodeId, neighbourId);
344 return Neo4jToGraph.NeighbourToVerigraph(set);
348 catch(MyNotFoundException e1){
349 e1.printStackTrace();
350 throw new NotFoundException();
354 public void checkGraph(long graphId) {
358 lib.checkGraph(graphId);
360 catch(MyNotFoundException e1){
361 e1.printStackTrace();
362 throw new NotFoundException();
366 public Configuration updateConfiguration(long nodeId, long graphId, Configuration nodeConfiguration, Node node) throws JsonParseException, JsonMappingException, IOException, MyInvalidIdException {
370 it.polito.neo4j.jaxb.Configuration conf=GraphToNeo4j.ConfToNeo4j(nodeConfiguration, node);
371 it.polito.neo4j.jaxb.Configuration c=lib.updateConfiguration(nodeId, graphId, conf);
372 Configuration r=Neo4jToGraph.ConfToVerigraph(c);
375 catch(MyNotFoundException e2){
376 e2.printStackTrace();
377 throw new NotFoundException();