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.verigraph.service;
11 import java.io.IOException;
12 import java.util.List;
13 import javax.xml.bind.JAXBException;
14 import com.fasterxml.jackson.core.JsonParseException;
15 import com.fasterxml.jackson.core.JsonProcessingException;
16 import com.fasterxml.jackson.databind.JsonMappingException;
17 import it.polito.neo4j.manager.Neo4jDBManager;
18 import it.polito.neo4j.exceptions.MyInvalidIdException;
19 import it.polito.neo4j.exceptions.MyNotFoundException;
20 import it.polito.verigraph.exception.ForbiddenException;
21 import it.polito.verigraph.model.Graph;
22 import it.polito.verigraph.model.Node;
24 public class GraphService {
26 private Neo4jDBManager manager= new Neo4jDBManager();
27 public static VerigraphLogger vlogger = VerigraphLogger.getVerigraphlogger();
29 public GraphService() {}
31 public List<Graph> getAllGraphs() throws JsonProcessingException, MyNotFoundException {
33 result=manager.getGraphs();
34 for(Graph g : result){
40 public Graph getGraph(long id) throws JsonParseException, JsonMappingException, JAXBException, IOException {
42 throw new ForbiddenException("Illegal graph id: " + id);
44 Graph localGraph=manager.getGraph(id);
45 validateGraph(localGraph);
49 public Graph updateGraph(Graph graph) throws JAXBException, JsonParseException, JsonMappingException, IOException, MyInvalidIdException {
50 if (graph.getId() < 0) {
51 throw new ForbiddenException("Illegal graph id: " + graph.getId());
54 Graph localGraph=new Graph();
55 localGraph=manager.updateGraph(graph);
56 vlogger.logger.info("Graph updated");
57 validateGraph(localGraph);
62 public void removeGraph(long id) {
65 throw new ForbiddenException("Illegal graph id: " + id);
67 manager.deleteGraph(id);
70 public Graph addGraph(Graph graph) throws JAXBException, JsonParseException, JsonMappingException, IOException, MyInvalidIdException {
72 Graph g=manager.addGraph(graph);
77 public static void validateGraph(Graph graph) throws JsonProcessingException {
78 for (Node node : graph.getNodes().values()) {
79 NodeService.validateNode(graph, node);