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
|
/*******************************************************************************
* 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.BadRequestException;
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 NeighbourService {
private Map<Long, Graph> graphs = DatabaseClass.getInstance().getGraphs();
public List<Neighbour> getAllNeighbours(long graphId, long nodeId) {
if (graphId <= 0) {
throw new ForbiddenException("Illegal graph id: " + graphId);
}
if (nodeId <= 0) {
throw new ForbiddenException("Illegal node id: " + nodeId);
}
Graph graph = graphs.get(graphId);
if (graph == null)
throw new DataNotFoundException("Graph with id " + graphId + " not found");
Map<Long, Node> nodes = graph.getNodes();
Node node = nodes.get(nodeId);
if (node == null)
throw new DataNotFoundException("Node with id " + nodeId + " not found in graph with id " + graphId);
Map<Long, Neighbour> neighbours = node.getNeighbours();
return new ArrayList<Neighbour>(neighbours.values());
}
public Neighbour getNeighbour(long graphId, long nodeId, long neighbourId) {
if (graphId <= 0) {
throw new ForbiddenException("Illegal graph id: " + graphId);
}
if (nodeId <= 0) {
throw new ForbiddenException("Illegal node id: " + nodeId);
}
if (neighbourId <= 0) {
throw new ForbiddenException("Illegal neighbour id: " + neighbourId);
}
Graph graph = graphs.get(graphId);
if (graph == null)
throw new DataNotFoundException("Graph with id " + graphId + " not found");
Map<Long, Node> nodes = graph.getNodes();
Node node = nodes.get(nodeId);
if (node == null) {
throw new DataNotFoundException("Node with id " + nodeId + " not found in graph with id " + graphId);
}
Map<Long, Neighbour> neighbours = node.getNeighbours();
Neighbour neighbour = neighbours.get(neighbourId);
if (neighbour == null) {
throw new DataNotFoundException("Neighbour with id " + neighbourId + " not found for node with id " + nodeId
+ " in graph with id " + graphId);
}
return neighbour;
}
public Neighbour addNeighbour(long graphId, long nodeId, Neighbour neighbour) {
if (graphId <= 0) {
throw new ForbiddenException("Illegal graph id: " + graphId);
}
if (nodeId <= 0) {
throw new ForbiddenException("Illegal node id: " + nodeId);
}
Graph graph = graphs.get(graphId);
if (graph == null)
throw new DataNotFoundException("Graph with id " + graphId + " not found");
Map<Long, Node> nodes = graph.getNodes();
Node node = nodes.get(nodeId);
if (node == null) {
throw new DataNotFoundException("Node with id " + nodeId + " not found in graph with id " + graphId);
}
Map<Long, Neighbour> neighbours = node.getNeighbours();
validateNeighbour(graph, node, neighbour);
synchronized (this) {
neighbour.setId(neighbours.size() + 1);
neighbours.put(neighbour.getId(), neighbour);
DatabaseClass.persistDatabase();
return neighbour;
}
}
public Neighbour updateNeighbour(long graphId, long nodeId, Neighbour neighbour) {
if (graphId <= 0) {
throw new ForbiddenException("Illegal graph id: " + graphId);
}
if (nodeId <= 0) {
throw new ForbiddenException("Illegal node id: " + nodeId);
}
if (neighbour.getId() <= 0) {
throw new ForbiddenException("Illegal neighbour id: " + nodeId);
}
Graph graph = graphs.get(graphId);
if (graph == null)
throw new DataNotFoundException("Graph with id " + graphId + " not found");
Map<Long, Node> nodes = graph.getNodes();
Node node = nodes.get(nodeId);
if (node == null) {
throw new DataNotFoundException("Node with id " + nodeId + " not found in graph with id " + graphId);
}
Map<Long, Neighbour> neighbours = node.getNeighbours();
Neighbour currentNeighbour = neighbours.get(neighbour.getId());
if (currentNeighbour == null) {
throw new DataNotFoundException("Neighbour with id " + neighbour.getId() + " not found for node with id "
+ nodeId + " in graph with id " + graphId);
}
validateNeighbour(graph, node, neighbour);
synchronized (this) {
neighbours.put(neighbour.getId(), neighbour);
DatabaseClass.persistDatabase();
return neighbour;
}
}
public Neighbour removeNeighbour(long graphId, long nodeId, long neighbourId) {
if (graphId <= 0) {
throw new ForbiddenException("Illegal graph id: " + graphId);
}
if (nodeId <= 0) {
throw new ForbiddenException("Illegal node id: " + nodeId);
}
if (neighbourId <= 0) {
throw new ForbiddenException("Illegal neighbour id: " + nodeId);
}
Graph graph = graphs.get(graphId);
if (graph == null)
throw new DataNotFoundException("Graph with id " + graphId + " not found");
Map<Long, Node> nodes = graph.getNodes();
Node node = nodes.get(nodeId);
if (node == null) {
throw new DataNotFoundException("Node with id " + nodeId + " not found in graph with id " + graphId);
}
Map<Long, Neighbour> neighbours = node.getNeighbours();
synchronized(this){
return neighbours.remove(neighbourId);
}
}
public static void validateNeighbour(Graph graph, Node node, Neighbour neighbour) {
if (graph == null)
throw new BadRequestException("Neighbour validation failed: cannot validate null graph");
if (node == null)
throw new BadRequestException("Neighbour validation failed: cannot validate null node");
if (neighbour == null)
throw new BadRequestException("Neighbour validation failed: cannot validate null neighbour");
if (neighbour.getName() == null)
throw new BadRequestException("Neighbour validation failed: neighbour 'name' field cannot be null");
if (neighbour.getName().equals(""))
throw new BadRequestException("Neighbour validation failed: neighbour 'name' field cannot be an empty string");
Node nodeFound = graph.searchNodeByName(neighbour.getName());
if ((nodeFound == null) || (nodeFound.getName().equals(node.getName())))
throw new BadRequestException("Neighbour validation failed: '" + neighbour.getName()
+ "' is not a valid name for a neighbour of node '" + node.getName() + "'");
Neighbour neighbourFound = node.searchNeighbourByName(neighbour.getName());
if ((neighbourFound != null) && (neighbourFound.equals(neighbour) == false))
throw new BadRequestException("Neighbour validation failed: node '" + node.getName()
+ "' already has a neighbour named '" + neighbour.getName() + "'");
}
}
|