update verigraph
[parser.git] / verigraph / src / it / polito / neo4j / manager / Neo4jDBManager.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.neo4j.manager;
10
11 import java.io.IOException;
12 import java.util.ArrayList;
13 import java.util.HashMap;
14 import java.util.List;
15 import java.util.Map;
16 import java.util.Set;
17 import java.util.logging.Logger;
18
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;
25
26 import com.fasterxml.jackson.core.JsonParseException;
27 import com.fasterxml.jackson.core.JsonProcessingException;
28 import com.fasterxml.jackson.databind.JsonMappingException;
29
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;
45
46 public class Neo4jDBManager {
47
48     public static Neo4jLibrary lib = Neo4jLibrary.getNeo4jLibrary();
49     //static ObjectFactory obFactory = new ObjectFactory();
50
51     public static VerigraphLogger vlogger = VerigraphLogger.getVerigraphlogger();
52
53
54     public it.polito.verigraph.model.Graph updateGraph(it.polito.verigraph.model.Graph graph) throws JAXBException, JsonParseException, JsonMappingException, IOException, MyInvalidIdException{
55
56         it.polito.neo4j.jaxb.Graph graph_xml=GraphToNeo4j.generateObject(graph);
57         it.polito.neo4j.jaxb.Graph graphReturned;
58         long graphId;
59
60         try{
61             graphId = graph.getId();
62             graphReturned = lib.updateGraph(graph_xml, graphId);
63             it.polito.verigraph.model.Graph g=Neo4jToGraph.generateGraph(graphReturned);
64             return g;
65
66         }
67         catch(MyNotFoundException e1){
68             vlogger.logger.info("error update 1");
69             throw new NotFoundException();
70         }
71
72         catch(DuplicateNodeException e3){
73             vlogger.logger.info("error update 2");
74             throw new BadRequestException(e3.getMessage());
75         }
76         catch(MyInvalidObjectException e4){
77             vlogger.logger.info("error update 3");
78             throw new BadRequestException(e4.getMessage());
79         }
80     }
81
82     public void deleteGraph(long id){
83
84         try{
85             lib.deleteGraph(id);
86         }
87         catch(MyNotFoundException e1){
88             throw new NotFoundException();
89         }
90     }
91
92     public it.polito.verigraph.model.Graph addGraph(it.polito.verigraph.model.Graph graph) throws JAXBException, JsonParseException, JsonMappingException, IOException, MyInvalidIdException {
93
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;
97
98         try{
99             graphReturned = lib.createGraph(graph_xml);
100             it.polito.verigraph.model.Graph g=Neo4jToGraph.generateGraph(graphReturned);
101             return g;
102
103         }
104         catch(MyNotFoundException e){
105             e.printStackTrace();
106             throw new NotFoundException();
107
108         }
109     }
110     public it.polito.verigraph.model.Graph getGraph(long graphId) throws JAXBException, JsonParseException, JsonMappingException, IOException {
111
112         it.polito.neo4j.jaxb.Graph graphReturned;
113
114         try{
115             graphReturned = lib.getGraph(graphId);
116
117             it.polito.verigraph.model.Graph g=Neo4jToGraph.generateGraph(graphReturned);
118
119             return g;
120
121         }
122         catch(MyNotFoundException e){
123             e.printStackTrace();
124             throw new NotFoundException();
125         }
126
127     }
128
129     public List<it.polito.verigraph.model.Graph> getGraphs() throws JsonProcessingException, MyNotFoundException {
130
131         List<it.polito.verigraph.model.Graph> graphsReturned=new ArrayList<it.polito.verigraph.model.Graph>();
132         it.polito.neo4j.jaxb.Graphs graphs;
133
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);
138         }
139
140         return graphsReturned;
141
142     }
143
144     public it.polito.verigraph.model.Neighbour addNeighbours(long graphId, long nodeId, it.polito.verigraph.model.Neighbour neighbour) throws JsonProcessingException {
145
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;
149         try{
150             neighReturned = lib.createNeighbour(neighbour_xml, graphId, nodeId);
151             n=Neo4jToGraph.NeighbourToVerigraph(neighReturned);
152             return n;
153         }
154         catch(MyNotFoundException e2){
155             e2.printStackTrace();
156             throw new NotFoundException();
157         }
158     }
159
160     public Neighbour deleteNeighbour(long graphId, long nodeId, long neighbourId) {
161
162         try{
163             it.polito.neo4j.jaxb.Neighbour n=lib.getNeighbour(graphId, nodeId, neighbourId);
164             if(n==null){
165                 throw new DataNotFoundException("Neighbour validation failed: '"+ neighbourId
166                         + "' is not a valid id for a neighbour of node '" + nodeId + "'" + "' of graph '" + graphId + "'");
167             }else{
168                 lib.deleteNeighbour(graphId, nodeId, neighbourId);
169                 return Neo4jToGraph.NeighbourToVerigraph(n);
170             }
171         }
172         catch(MyNotFoundException e1){
173             e1.printStackTrace();
174             throw new NotFoundException();
175         }catch (JsonProcessingException e) {
176             throw new NotFoundException("jsonprocessing node");
177         }
178
179     }
180
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;
184         Neighbour r;
185         try{
186             nodeReturned=lib.updateNeighbour(neighbour_xml, graphId, nodeId, neighbour_xml.getId());
187             r=Neo4jToGraph.NeighbourToVerigraph(lib.getNeighbour(graphId, nodeReturned.getId(), neighbour_xml.getId()));
188             return r;
189         }
190         catch(MyNotFoundException e2){
191             throw new NotFoundException();
192         } catch (MyInvalidObjectException e) {
193             e.printStackTrace();
194         }
195         return null;
196
197     }
198
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();
203         try
204         {
205
206             nodeReturned = lib.createNode(node_xml, graphId);
207             node_v=Neo4jToGraph.NodeToVerigraph(nodeReturned);
208         }
209         catch(MyNotFoundException e1){
210             e1.printStackTrace();
211             throw new NotFoundException();
212         }
213
214         catch(DuplicateNodeException e3){
215             e3.printStackTrace();
216             throw new BadRequestException();
217         }
218         return node_v;
219     }
220
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;
224         Node node_v;
225         try{
226             nodeReturned = lib.updateNode(node_xml, graphId, id);
227             node_v=Neo4jToGraph.NodeToVerigraph(nodeReturned);
228             return node_v;
229         }
230         catch(MyNotFoundException e2){
231             e2.printStackTrace();
232             throw new NotFoundException();
233         }
234         catch(MyInvalidObjectException e3){
235             e3.printStackTrace();
236             throw new BadRequestException(e3.getMessage());
237         }
238
239     }
240
241     public Node deleteNode(long graphId, long nodeId) {
242
243         try{
244             it.polito.neo4j.jaxb.Node n=lib.getNodeById(nodeId, graphId);
245             lib.deleteNode(graphId, nodeId);
246             return Neo4jToGraph.NodeToVerigraph(n);
247         }
248         catch(MyNotFoundException e1){
249             throw new NotFoundException("graph or node not found");
250         } catch (JsonProcessingException e) {
251             throw new NotFoundException("jsonprocessing node");
252         }
253     }
254
255     public Paths getPath(long graphId, String source, String destination, String direction) throws MyInvalidDirectionException {
256         it.polito.neo4j.jaxb.Paths paths=(new ObjectFactory()).createPaths();
257         try{
258             if(source == null || destination == null || direction == null)
259                 throw new DataNotFoundException("Missing query parameters");
260             paths =  lib.findAllPathsBetweenTwoNodes(graphId, source, destination,direction);
261         }
262         catch(MyNotFoundException e1){
263             e1.printStackTrace();
264             throw new NotFoundException();
265
266         }
267
268         return paths;
269     }
270
271     public Map<Long, Node> getNodes(long graphId) throws JsonProcessingException {
272         Map<Long, Node> nodes=new HashMap<Long, Node>();
273         try
274         {
275             Set<it.polito.neo4j.jaxb.Node> set= lib.getNodes(graphId);
276             nodes=Neo4jToGraph.NodesToVerigraph(set);
277         }
278         catch(MyNotFoundException e1){
279             e1.printStackTrace();
280             throw new NotFoundException();
281         }
282         return nodes;
283
284     }
285
286     public Map<Long, Neighbour> getNeighbours(long graphId, long nodeId) throws JsonProcessingException {
287         Map<Long, Neighbour> neighbours=new HashMap<Long, Neighbour>();
288         try
289         {
290             Set<it.polito.neo4j.jaxb.Neighbour> set= lib.getNeighbours(graphId, nodeId);
291             neighbours=Neo4jToGraph.NeighboursToVerigraph(set);
292             return neighbours;
293         }
294         catch(MyNotFoundException e1){
295             e1.printStackTrace();
296             throw new NotFoundException();
297         }
298
299     }
300
301     public Node getNodeByName(long graphId, String name) throws JsonProcessingException {
302
303         it.polito.verigraph.model.Node node;
304         try
305         {
306             it.polito.neo4j.jaxb.Node set= lib.getNodeByName(name, graphId);
307             if(set==null)
308                 return null;
309             else{
310                 node=Neo4jToGraph.NodeToVerigraph(set);
311                 return node;
312             }
313         }
314         catch(MyNotFoundException e1){
315             e1.printStackTrace();
316             throw new NotFoundException();
317         }
318
319     }
320
321     public Node getNodeById(long nodeId, long graphId) throws JsonProcessingException {
322         it.polito.verigraph.model.Node node;
323         try
324         {
325             it.polito.neo4j.jaxb.Node set= lib.getNodeById(nodeId, graphId);
326             if(set==null)
327                 return null;
328             else{
329                 node=Neo4jToGraph.NodeToVerigraph(set);
330                 return node;
331             }
332         }
333         catch(MyNotFoundException e1){
334             e1.printStackTrace();
335             throw new NotFoundException();
336         }
337     }
338
339     public Neighbour getNeighbour(long graphId, long nodeId, long neighbourId) throws JsonProcessingException {
340         try
341         {
342             it.polito.neo4j.jaxb.Neighbour set= lib.getNeighbour(graphId, nodeId, neighbourId);
343             if(set!=null)
344                 return Neo4jToGraph.NeighbourToVerigraph(set);
345             else
346                 return null;
347         }
348         catch(MyNotFoundException e1){
349             e1.printStackTrace();
350             throw new NotFoundException();
351         }
352     }
353
354     public void checkGraph(long graphId) {
355
356         try
357         {
358             lib.checkGraph(graphId);
359         }
360         catch(MyNotFoundException e1){
361             e1.printStackTrace();
362             throw new NotFoundException();
363         }
364     }
365
366     public Configuration updateConfiguration(long nodeId, long graphId, Configuration nodeConfiguration, Node node) throws JsonParseException, JsonMappingException, IOException, MyInvalidIdException {
367
368         try{
369
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);
373             return r;
374         }
375         catch(MyNotFoundException e2){
376             e2.printStackTrace();
377             throw new NotFoundException();
378         }
379     }
380
381 }