From 53d83244c1bf36af86e90ce5fe758a369f73563e Mon Sep 17 00:00:00 2001 From: "serena.spinoso" Date: Sat, 25 Feb 2017 12:00:55 +0100 Subject: Add verigraph code base JIRA: PARSER-111 Change-Id: Ie76e14fabbb6c388ebc89d9a15dd3021b25fa892 Signed-off-by: serena.spinoso --- .../polito/escape/verify/service/GraphService.java | 129 +++++++++++++++++++++ 1 file changed, 129 insertions(+) create mode 100644 verigraph/src/main/java/it/polito/escape/verify/service/GraphService.java (limited to 'verigraph/src/main/java/it/polito/escape/verify/service/GraphService.java') diff --git a/verigraph/src/main/java/it/polito/escape/verify/service/GraphService.java b/verigraph/src/main/java/it/polito/escape/verify/service/GraphService.java new file mode 100644 index 0000000..b34eb08 --- /dev/null +++ b/verigraph/src/main/java/it/polito/escape/verify/service/GraphService.java @@ -0,0 +1,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 graphs = DatabaseClass.getInstance().getGraphs(); + + public GraphService() { + + } + + public List getAllGraphs() { + return new ArrayList(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 nodeEntry : graph.getNodes().entrySet()){ + nodeEntry.getValue().setId(nodeEntry.getKey()); + + for (Map.Entry 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 nodeEntry : graph.getNodes().entrySet()){ + nodeEntry.getValue().setId(nodeEntry.getKey()); + + for (Map.Entry 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); + } + } +} -- cgit 1.2.3-korg