blob: b34eb086825494f8b4c1bd0b5af83def659e22ed (
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
|
/*******************************************************************************
* 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.escape.verify.service;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import it.polito.escape.verify.database.DatabaseClass;
import it.polito.escape.verify.exception.DataNotFoundException;
import it.polito.escape.verify.exception.ForbiddenException;
import it.polito.escape.verify.model.Graph;
import it.polito.escape.verify.model.Neighbour;
import it.polito.escape.verify.model.Node;
public class GraphService {
private Map<Long, Graph> graphs = DatabaseClass.getInstance().getGraphs();
public GraphService() {
}
public List<Graph> getAllGraphs() {
return new ArrayList<Graph>(graphs.values());
}
public Graph getGraph(long id) {
if (id <= 0) {
throw new ForbiddenException("Illegal graph id: " + id);
}
Graph graph = graphs.get(id);
if (graph == null) {
throw new DataNotFoundException("Graph with id " + id + " not found");
}
return graph;
}
public Graph updateGraph(Graph graph) {
if (graph.getId() <= 0) {
throw new ForbiddenException("Illegal graph id: " + graph.getId());
}
Graph localGraph = graphs.get(graph.getId());
if (localGraph == null) {
throw new DataNotFoundException("Graph with id " + graph.getId() + " not found");
}
validateGraph(graph);
// int numberOfNodes = 0;
// for (Node node : graph.getNodes().values()) {
//
// node.setId(++numberOfNodes);
//
// int numberOfNodeNeighbours = 0;
// for (Neighbour neighbour : node.getNeighbours().values()) {
// neighbour.setId(++numberOfNodeNeighbours);
// }
// }
for (Map.Entry<Long, Node> nodeEntry : graph.getNodes().entrySet()){
nodeEntry.getValue().setId(nodeEntry.getKey());
for (Map.Entry<Long, Neighbour> neighbourEntry : nodeEntry.getValue().getNeighbours().entrySet()){
neighbourEntry.getValue().setId(neighbourEntry.getKey());
}
}
synchronized(this){
graphs.put(graph.getId(), graph);
DatabaseClass.persistDatabase();
return graph;
}
}
public Graph removeGraph(long id) {
if (id <= 0) {
throw new ForbiddenException("Illegal graph id: " + id);
}
synchronized(this){
return graphs.remove(id);
}
}
public Graph addGraph(Graph graph) {
validateGraph(graph);
synchronized (this) {
graph.setId(DatabaseClass.getInstance().getNumberOfGraphs() + 1);
}
// int numberOfNodes = 0;
// for (Node node : graph.getNodes().values()) {
//
// node.setId(++numberOfNodes);
//
// int numberOfNodeNeighbours = 0;
// for (Neighbour neighbour : node.getNeighbours().values()) {
// neighbour.setId(++numberOfNodeNeighbours);
// }
// }
for (Map.Entry<Long, Node> nodeEntry : graph.getNodes().entrySet()){
nodeEntry.getValue().setId(nodeEntry.getKey());
for (Map.Entry<Long, Neighbour> neighbourEntry : nodeEntry.getValue().getNeighbours().entrySet()){
neighbourEntry.getValue().setId(neighbourEntry.getKey());
}
}
synchronized(this){
graphs.put(graph.getId(), graph);
DatabaseClass.persistDatabase();
return graph;
}
}
public static void validateGraph(Graph graph) {
for (Node node : graph.getNodes().values()) {
NodeService.validateNode(graph, node);
}
}
}
|