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.BadRequestException;
18 import it.polito.escape.verify.exception.DataNotFoundException;
19 import it.polito.escape.verify.exception.ForbiddenException;
20 import it.polito.escape.verify.model.Graph;
21 import it.polito.escape.verify.model.Neighbour;
22 import it.polito.escape.verify.model.Node;
24 public class NeighbourService {
26 private Map<Long, Graph> graphs = DatabaseClass.getInstance().getGraphs();
28 public List<Neighbour> getAllNeighbours(long graphId, long nodeId) {
30 throw new ForbiddenException("Illegal graph id: " + graphId);
33 throw new ForbiddenException("Illegal node id: " + nodeId);
35 Graph graph = graphs.get(graphId);
37 throw new DataNotFoundException("Graph with id " + graphId + " not found");
38 Map<Long, Node> nodes = graph.getNodes();
39 Node node = nodes.get(nodeId);
41 throw new DataNotFoundException("Node with id " + nodeId + " not found in graph with id " + graphId);
42 Map<Long, Neighbour> neighbours = node.getNeighbours();
43 return new ArrayList<Neighbour>(neighbours.values());
46 public Neighbour getNeighbour(long graphId, long nodeId, long neighbourId) {
48 throw new ForbiddenException("Illegal graph id: " + graphId);
51 throw new ForbiddenException("Illegal node id: " + nodeId);
53 if (neighbourId <= 0) {
54 throw new ForbiddenException("Illegal neighbour id: " + neighbourId);
56 Graph graph = graphs.get(graphId);
58 throw new DataNotFoundException("Graph with id " + graphId + " not found");
59 Map<Long, Node> nodes = graph.getNodes();
60 Node node = nodes.get(nodeId);
62 throw new DataNotFoundException("Node with id " + nodeId + " not found in graph with id " + graphId);
64 Map<Long, Neighbour> neighbours = node.getNeighbours();
65 Neighbour neighbour = neighbours.get(neighbourId);
66 if (neighbour == null) {
67 throw new DataNotFoundException("Neighbour with id " + neighbourId + " not found for node with id " + nodeId
68 + " in graph with id " + graphId);
73 public Neighbour addNeighbour(long graphId, long nodeId, Neighbour neighbour) {
75 throw new ForbiddenException("Illegal graph id: " + graphId);
78 throw new ForbiddenException("Illegal node id: " + nodeId);
80 Graph graph = graphs.get(graphId);
82 throw new DataNotFoundException("Graph with id " + graphId + " not found");
83 Map<Long, Node> nodes = graph.getNodes();
84 Node node = nodes.get(nodeId);
86 throw new DataNotFoundException("Node with id " + nodeId + " not found in graph with id " + graphId);
88 Map<Long, Neighbour> neighbours = node.getNeighbours();
90 validateNeighbour(graph, node, neighbour);
93 neighbour.setId(neighbours.size() + 1);
94 neighbours.put(neighbour.getId(), neighbour);
95 DatabaseClass.persistDatabase();
100 public Neighbour updateNeighbour(long graphId, long nodeId, Neighbour neighbour) {
102 throw new ForbiddenException("Illegal graph id: " + graphId);
105 throw new ForbiddenException("Illegal node id: " + nodeId);
107 if (neighbour.getId() <= 0) {
108 throw new ForbiddenException("Illegal neighbour id: " + nodeId);
110 Graph graph = graphs.get(graphId);
112 throw new DataNotFoundException("Graph with id " + graphId + " not found");
113 Map<Long, Node> nodes = graph.getNodes();
114 Node node = nodes.get(nodeId);
116 throw new DataNotFoundException("Node with id " + nodeId + " not found in graph with id " + graphId);
118 Map<Long, Neighbour> neighbours = node.getNeighbours();
119 Neighbour currentNeighbour = neighbours.get(neighbour.getId());
120 if (currentNeighbour == null) {
121 throw new DataNotFoundException("Neighbour with id " + neighbour.getId() + " not found for node with id "
122 + nodeId + " in graph with id " + graphId);
125 validateNeighbour(graph, node, neighbour);
127 synchronized (this) {
128 neighbours.put(neighbour.getId(), neighbour);
129 DatabaseClass.persistDatabase();
134 public Neighbour removeNeighbour(long graphId, long nodeId, long neighbourId) {
136 throw new ForbiddenException("Illegal graph id: " + graphId);
139 throw new ForbiddenException("Illegal node id: " + nodeId);
141 if (neighbourId <= 0) {
142 throw new ForbiddenException("Illegal neighbour id: " + nodeId);
144 Graph graph = graphs.get(graphId);
146 throw new DataNotFoundException("Graph with id " + graphId + " not found");
147 Map<Long, Node> nodes = graph.getNodes();
148 Node node = nodes.get(nodeId);
150 throw new DataNotFoundException("Node with id " + nodeId + " not found in graph with id " + graphId);
152 Map<Long, Neighbour> neighbours = node.getNeighbours();
155 return neighbours.remove(neighbourId);
159 public static void validateNeighbour(Graph graph, Node node, Neighbour neighbour) {
161 throw new BadRequestException("Neighbour validation failed: cannot validate null graph");
163 throw new BadRequestException("Neighbour validation failed: cannot validate null node");
164 if (neighbour == null)
165 throw new BadRequestException("Neighbour validation failed: cannot validate null neighbour");
167 if (neighbour.getName() == null)
168 throw new BadRequestException("Neighbour validation failed: neighbour 'name' field cannot be null");
169 if (neighbour.getName().equals(""))
170 throw new BadRequestException("Neighbour validation failed: neighbour 'name' field cannot be an empty string");
172 Node nodeFound = graph.searchNodeByName(neighbour.getName());
173 if ((nodeFound == null) || (nodeFound.getName().equals(node.getName())))
174 throw new BadRequestException("Neighbour validation failed: '" + neighbour.getName()
175 + "' is not a valid name for a neighbour of node '" + node.getName() + "'");
177 Neighbour neighbourFound = node.searchNeighbourByName(neighbour.getName());
178 if ((neighbourFound != null) && (neighbourFound.equals(neighbour) == false))
179 throw new BadRequestException("Neighbour validation failed: node '" + node.getName()
180 + "' already has a neighbour named '" + neighbour.getName() + "'");