summaryrefslogtreecommitdiffstats
path: root/verigraph/src/it/polito/neo4j/manager/Neo4jDBManager.java
blob: 40789c8d106c10a7bf29ce8adc5ffe3cecdc6a2e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/*******************************************************************************
 * Copyright (c) 2017 Politecnico di Torino and others.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Apache License, Version 2.0
 * which accompanies this distribution, and is available at
 * http://www.apache.org/licenses/LICENSE-2.0
 *******************************************************************************/
package it.polito.neo4j.manager;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Logger;

import javax.ws.rs.BadRequestException;
import javax.ws.rs.ForbiddenException;
import javax.ws.rs.NotFoundException;
import javax.ws.rs.PathParam;
import javax.ws.rs.QueryParam;
import javax.xml.bind.JAXBException;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;

import it.polito.neo4j.exceptions.DuplicateNodeException;
import it.polito.neo4j.exceptions.MyInvalidObjectException;
import it.polito.neo4j.exceptions.MyNotFoundException;
import it.polito.neo4j.jaxb.ObjectFactory;
import it.polito.neo4j.jaxb.Paths;
import it.polito.neo4j.translator.GraphToNeo4j;
import it.polito.neo4j.translator.Neo4jToGraph;
import it.polito.neo4j.exceptions.MyInvalidDirectionException;
import it.polito.neo4j.exceptions.MyInvalidIdException;
import it.polito.verigraph.exception.DataNotFoundException;
import it.polito.verigraph.model.Configuration;
import it.polito.verigraph.model.Graph;
import it.polito.verigraph.model.Neighbour;
import it.polito.verigraph.model.Node;
import it.polito.verigraph.service.VerigraphLogger;

public class Neo4jDBManager {

    public static Neo4jLibrary lib = Neo4jLibrary.getNeo4jLibrary();
    //static ObjectFactory obFactory = new ObjectFactory();

    public static VerigraphLogger vlogger = VerigraphLogger.getVerigraphlogger();


    public it.polito.verigraph.model.Graph updateGraph(it.polito.verigraph.model.Graph graph) throws JAXBException, JsonParseException, JsonMappingException, IOException, MyInvalidIdException{

        it.polito.neo4j.jaxb.Graph graph_xml=GraphToNeo4j.generateObject(graph);
        it.polito.neo4j.jaxb.Graph graphReturned;
        long graphId;

        try{
            graphId = graph.getId();
            graphReturned = lib.updateGraph(graph_xml, graphId);
            it.polito.verigraph.model.Graph g=Neo4jToGraph.generateGraph(graphReturned);
            return g;

        }
        catch(MyNotFoundException e1){
            vlogger.logger.info("error update 1");
            throw new NotFoundException();
        }

        catch(DuplicateNodeException e3){
            vlogger.logger.info("error update 2");
            throw new BadRequestException(e3.getMessage());
        }
        catch(MyInvalidObjectException e4){
            vlogger.logger.info("error update 3");
            throw new BadRequestException(e4.getMessage());
        }
    }

    public void deleteGraph(long id){

        try{
            lib.deleteGraph(id);
        }
        catch(MyNotFoundException e1){
            throw new NotFoundException();
        }
    }

    public it.polito.verigraph.model.Graph addGraph(it.polito.verigraph.model.Graph graph) throws JAXBException, JsonParseException, JsonMappingException, IOException, MyInvalidIdException {

        it.polito.neo4j.jaxb.Graph graph_xml=GraphToNeo4j.generateObject(graph);
        it.polito.neo4j.jaxb.Graph graphReturned;
        it.polito.neo4j.jaxb.Node node;

        try{
            graphReturned = lib.createGraph(graph_xml);
            it.polito.verigraph.model.Graph g=Neo4jToGraph.generateGraph(graphReturned);
            return g;

        }
        catch(MyNotFoundException e){
            e.printStackTrace();
            throw new NotFoundException();

        }
    }
    public it.polito.verigraph.model.Graph getGraph(long graphId) throws JAXBException, JsonParseException, JsonMappingException, IOException {

        it.polito.neo4j.jaxb.Graph graphReturned;

        try{
            graphReturned = lib.getGraph(graphId);

            it.polito.verigraph.model.Graph g=Neo4jToGraph.generateGraph(graphReturned);

            return g;

        }
        catch(MyNotFoundException e){
            e.printStackTrace();
            throw new NotFoundException();
        }

    }

    public List<it.polito.verigraph.model.Graph> getGraphs() throws JsonProcessingException, MyNotFoundException {

        List<it.polito.verigraph.model.Graph> graphsReturned=new ArrayList<it.polito.verigraph.model.Graph>();
        it.polito.neo4j.jaxb.Graphs graphs;

        graphs = lib.getGraphs();
        for(it.polito.neo4j.jaxb.Graph tmp : graphs.getGraph()){
            it.polito.verigraph.model.Graph g=Neo4jToGraph.generateGraph(tmp);
            graphsReturned.add(g);
        }

        return graphsReturned;

    }

    public it.polito.verigraph.model.Neighbour addNeighbours(long graphId, long nodeId, it.polito.verigraph.model.Neighbour neighbour) throws JsonProcessingException {

        it.polito.neo4j.jaxb.Neighbour neighbour_xml=GraphToNeo4j.NeighbourToNeo4j(neighbour);
        it.polito.neo4j.jaxb.Neighbour neighReturned;
        it.polito.verigraph.model.Neighbour n;
        try{
            neighReturned = lib.createNeighbour(neighbour_xml, graphId, nodeId);
            n=Neo4jToGraph.NeighbourToVerigraph(neighReturned);
            return n;
        }
        catch(MyNotFoundException e2){
            e2.printStackTrace();
            throw new NotFoundException();
        }
    }

    public Neighbour deleteNeighbour(long graphId, long nodeId, long neighbourId) {

        try{
            it.polito.neo4j.jaxb.Neighbour n=lib.getNeighbour(graphId, nodeId, neighbourId);
            if(n==null){
                throw new DataNotFoundException("Neighbour validation failed: '"+ neighbourId
                        + "' is not a valid id for a neighbour of node '" + nodeId + "'" + "' of graph '" + graphId + "'");
            }else{
                lib.deleteNeighbour(graphId, nodeId, neighbourId);
                return Neo4jToGraph.NeighbourToVerigraph(n);
            }
        }
        catch(MyNotFoundException e1){
            e1.printStackTrace();
            throw new NotFoundException();
        }catch (JsonProcessingException e) {
            throw new NotFoundException("jsonprocessing node");
        }

    }

    public Neighbour updateNeighbour(long graphId, long nodeId, it.polito.verigraph.model.Neighbour neighbour) throws JsonProcessingException {
        it.polito.neo4j.jaxb.Neighbour neighbour_xml=GraphToNeo4j.NeighbourToNeo4j(neighbour);
        it.polito.neo4j.jaxb.Node nodeReturned;
        Neighbour r;
        try{
            nodeReturned=lib.updateNeighbour(neighbour_xml, graphId, nodeId, neighbour_xml.getId());
            r=Neo4jToGraph.NeighbourToVerigraph(lib.getNeighbour(graphId, nodeReturned.getId(), neighbour_xml.getId()));
            return r;
        }
        catch(MyNotFoundException e2){
            throw new NotFoundException();
        } catch (MyInvalidObjectException e) {
            e.printStackTrace();
        }
        return null;

    }

    public it.polito.verigraph.model.Node addNode(long graphId, it.polito.verigraph.model.Node node) throws IOException, MyInvalidIdException {
        it.polito.neo4j.jaxb.Node node_xml=GraphToNeo4j.NodeToNeo4j(node);
        it.polito.neo4j.jaxb.Node nodeReturned;
        it.polito.verigraph.model.Node node_v=new it.polito.verigraph.model.Node();
        try
        {

            nodeReturned = lib.createNode(node_xml, graphId);
            node_v=Neo4jToGraph.NodeToVerigraph(nodeReturned);
        }
        catch(MyNotFoundException e1){
            e1.printStackTrace();
            throw new NotFoundException();
        }

        catch(DuplicateNodeException e3){
            e3.printStackTrace();
            throw new BadRequestException();
        }
        return node_v;
    }

    public Node updateNode(long graphId, Node node, long id) throws IOException, MyInvalidIdException {
        it.polito.neo4j.jaxb.Node node_xml=GraphToNeo4j.NodeToNeo4j(node);
        it.polito.neo4j.jaxb.Node nodeReturned;
        Node node_v;
        try{
            nodeReturned = lib.updateNode(node_xml, graphId, id);
            node_v=Neo4jToGraph.NodeToVerigraph(nodeReturned);
            return node_v;
        }
        catch(MyNotFoundException e2){
            e2.printStackTrace();
            throw new NotFoundException();
        }
        catch(MyInvalidObjectException e3){
            e3.printStackTrace();
            throw new BadRequestException(e3.getMessage());
        }

    }

    public Node deleteNode(long graphId, long nodeId) {

        try{
            it.polito.neo4j.jaxb.Node n=lib.getNodeById(nodeId, graphId);
            lib.deleteNode(graphId, nodeId);
            return Neo4jToGraph.NodeToVerigraph(n);
        }
        catch(MyNotFoundException e1){
            throw new NotFoundException("graph or node not found");
        } catch (JsonProcessingException e) {
            throw new NotFoundException("jsonprocessing node");
        }
    }

    public Paths getPath(long graphId, String source, String destination, String direction) throws MyInvalidDirectionException {
        it.polito.neo4j.jaxb.Paths paths=(new ObjectFactory()).createPaths();
        try{
            if(source == null || destination == null || direction == null)
                throw new DataNotFoundException("Missing query parameters");
            paths =  lib.findAllPathsBetweenTwoNodes(graphId, source, destination,direction);
        }
        catch(MyNotFoundException e1){
            e1.printStackTrace();
            throw new NotFoundException();

        }

        return paths;
    }

    public Map<Long, Node> getNodes(long graphId) throws JsonProcessingException {
        Map<Long, Node> nodes=new HashMap<Long, Node>();
        try
        {
            Set<it.polito.neo4j.jaxb.Node> set= lib.getNodes(graphId);
            nodes=Neo4jToGraph.NodesToVerigraph(set);
        }
        catch(MyNotFoundException e1){
            e1.printStackTrace();
            throw new NotFoundException();
        }
        return nodes;

    }

    public Map<Long, Neighbour> getNeighbours(long graphId, long nodeId) throws JsonProcessingException {
        Map<Long, Neighbour> neighbours=new HashMap<Long, Neighbour>();
        try
        {
            Set<it.polito.neo4j.jaxb.Neighbour> set= lib.getNeighbours(graphId, nodeId);
            neighbours=Neo4jToGraph.NeighboursToVerigraph(set);
            return neighbours;
        }
        catch(MyNotFoundException e1){
            e1.printStackTrace();
            throw new NotFoundException();
        }

    }

    public Node getNodeByName(long graphId, String name) throws JsonProcessingException {

        it.polito.verigraph.model.Node node;
        try
        {
            it.polito.neo4j.jaxb.Node set= lib.getNodeByName(name, graphId);
            if(set==null)
                return null;
            else{
                node=Neo4jToGraph.NodeToVerigraph(set);
                return node;
            }
        }
        catch(MyNotFoundException e1){
            e1.printStackTrace();
            throw new NotFoundException();
        }

    }

    public Node getNodeById(long nodeId, long graphId) throws JsonProcessingException {
        it.polito.verigraph.model.Node node;
        try
        {
            it.polito.neo4j.jaxb.Node set= lib.getNodeById(nodeId, graphId);
            if(set==null)
                return null;
            else{
                node=Neo4jToGraph.NodeToVerigraph(set);
                return node;
            }
        }
        catch(MyNotFoundException e1){
            e1.printStackTrace();
            throw new NotFoundException();
        }
    }

    public Neighbour getNeighbour(long graphId, long nodeId, long neighbourId) throws JsonProcessingException {
        try
        {
            it.polito.neo4j.jaxb.Neighbour set= lib.getNeighbour(graphId, nodeId, neighbourId);
            if(set!=null)
                return Neo4jToGraph.NeighbourToVerigraph(set);
            else
                return null;
        }
        catch(MyNotFoundException e1){
            e1.printStackTrace();
            throw new NotFoundException();
        }
    }

    public void checkGraph(long graphId) {

        try
        {
            lib.checkGraph(graphId);
        }
        catch(MyNotFoundException e1){
            e1.printStackTrace();
            throw new NotFoundException();
        }
    }

    public Configuration updateConfiguration(long nodeId, long graphId, Configuration nodeConfiguration, Node node) throws JsonParseException, JsonMappingException, IOException, MyInvalidIdException {

        try{

            it.polito.neo4j.jaxb.Configuration conf=GraphToNeo4j.ConfToNeo4j(nodeConfiguration, node);
            it.polito.neo4j.jaxb.Configuration c=lib.updateConfiguration(nodeId, graphId, conf);
            Configuration r=Neo4jToGraph.ConfToVerigraph(c);
            return r;
        }
        catch(MyNotFoundException e2){
            e2.printStackTrace();
            throw new NotFoundException();
        }
    }

}