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 *******************************************************************************/
10 package it.polito.escape.verify.service;
12 import java.util.ArrayList;
13 import java.util.List;
16 import it.polito.escape.verify.database.DatabaseClass;
17 import it.polito.escape.verify.exception.DataNotFoundException;
18 import it.polito.escape.verify.exception.ForbiddenException;
19 import it.polito.escape.verify.model.Graph;
20 import it.polito.escape.verify.model.Neighbour;
21 import it.polito.escape.verify.model.Node;
23 public class GraphService {
25 private Map<Long, Graph> graphs = DatabaseClass.getInstance().getGraphs();
27 public GraphService() {
31 public List<Graph> getAllGraphs() {
32 return new ArrayList<Graph>(graphs.values());
35 public Graph getGraph(long id) {
37 throw new ForbiddenException("Illegal graph id: " + id);
39 Graph graph = graphs.get(id);
41 throw new DataNotFoundException("Graph with id " + id + " not found");
46 public Graph updateGraph(Graph graph) {
47 if (graph.getId() <= 0) {
48 throw new ForbiddenException("Illegal graph id: " + graph.getId());
50 Graph localGraph = graphs.get(graph.getId());
51 if (localGraph == null) {
52 throw new DataNotFoundException("Graph with id " + graph.getId() + " not found");
57 // int numberOfNodes = 0;
58 // for (Node node : graph.getNodes().values()) {
60 // node.setId(++numberOfNodes);
62 // int numberOfNodeNeighbours = 0;
63 // for (Neighbour neighbour : node.getNeighbours().values()) {
64 // neighbour.setId(++numberOfNodeNeighbours);
68 for (Map.Entry<Long, Node> nodeEntry : graph.getNodes().entrySet()){
69 nodeEntry.getValue().setId(nodeEntry.getKey());
71 for (Map.Entry<Long, Neighbour> neighbourEntry : nodeEntry.getValue().getNeighbours().entrySet()){
72 neighbourEntry.getValue().setId(neighbourEntry.getKey());
77 graphs.put(graph.getId(), graph);
78 DatabaseClass.persistDatabase();
83 public Graph removeGraph(long id) {
85 throw new ForbiddenException("Illegal graph id: " + id);
88 return graphs.remove(id);
92 public Graph addGraph(Graph graph) {
96 graph.setId(DatabaseClass.getInstance().getNumberOfGraphs() + 1);
98 // int numberOfNodes = 0;
99 // for (Node node : graph.getNodes().values()) {
101 // node.setId(++numberOfNodes);
103 // int numberOfNodeNeighbours = 0;
104 // for (Neighbour neighbour : node.getNeighbours().values()) {
105 // neighbour.setId(++numberOfNodeNeighbours);
109 for (Map.Entry<Long, Node> nodeEntry : graph.getNodes().entrySet()){
110 nodeEntry.getValue().setId(nodeEntry.getKey());
112 for (Map.Entry<Long, Neighbour> neighbourEntry : nodeEntry.getValue().getNeighbours().entrySet()){
113 neighbourEntry.getValue().setId(neighbourEntry.getKey());
118 graphs.put(graph.getId(), graph);
119 DatabaseClass.persistDatabase();
124 public static void validateGraph(Graph graph) {
125 for (Node node : graph.getNodes().values()) {
126 NodeService.validateNode(graph, node);