summaryrefslogtreecommitdiffstats
path: root/verigraph/src/it/polito/verigraph/service/GraphService.java
blob: 7e92281e94ceb84a71f5108a25003ac8fd399838 (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
/*******************************************************************************
 * 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.verigraph.service;

import java.io.IOException;
import java.util.List;
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.manager.Neo4jDBManager;
import it.polito.neo4j.exceptions.MyInvalidIdException;
import it.polito.neo4j.exceptions.MyNotFoundException;
import it.polito.verigraph.exception.ForbiddenException;
import it.polito.verigraph.model.Graph;
import it.polito.verigraph.model.Node;

public class GraphService {

    private Neo4jDBManager manager= new Neo4jDBManager();
    public static VerigraphLogger vlogger = VerigraphLogger.getVerigraphlogger();

    public GraphService() {}

    public List<Graph> getAllGraphs() throws JsonProcessingException, MyNotFoundException {
        List<Graph> result;
        result=manager.getGraphs();
        for(Graph g : result){
            validateGraph(g);
        }
        return result;
    }

    public Graph getGraph(long id) throws JsonParseException, JsonMappingException, JAXBException, IOException {
        if (id < 0) {
            throw new ForbiddenException("Illegal graph id: " + id);
        }
        Graph localGraph=manager.getGraph(id);
        validateGraph(localGraph);
        return localGraph;
    }

    public Graph updateGraph(Graph graph) throws JAXBException, JsonParseException, JsonMappingException, IOException, MyInvalidIdException {
        if (graph.getId() < 0) {
            throw new ForbiddenException("Illegal graph id: " + graph.getId());
        }
        validateGraph(graph);
        Graph localGraph=new Graph();
        localGraph=manager.updateGraph(graph);
        vlogger.logger.info("Graph updated");
        validateGraph(localGraph);
        return localGraph;
    }


    public void removeGraph(long id) {

        if (id < 0) {
            throw new ForbiddenException("Illegal graph id: " + id);
        }
        manager.deleteGraph(id);
    }

    public Graph addGraph(Graph graph) throws JAXBException, JsonParseException, JsonMappingException, IOException, MyInvalidIdException {
        validateGraph(graph);
        Graph g=manager.addGraph(graph);
        validateGraph(g);
        return g;
    }

    public static void validateGraph(Graph graph) throws JsonProcessingException {
        for (Node node : graph.getNodes().values()) {
            NodeService.validateNode(graph, node);
        }
    }
}