diff options
author | serena.spinoso <serena.spinoso@polito.it> | 2017-02-25 12:00:55 +0100 |
---|---|---|
committer | serena.spinoso <serena.spinoso@polito.it> | 2017-02-27 13:39:18 +0100 |
commit | 53d83244c1bf36af86e90ce5fe758a369f73563e (patch) | |
tree | f5780b3033763d6b3cd62c40f5d4a63dfac35f7a /verigraph/src/main/java/it/polito/escape/verify/database | |
parent | f0108f26fb1f62fefbbc4d5484b87241d563737b (diff) |
Add verigraph code base
JIRA: PARSER-111
Change-Id: Ie76e14fabbb6c388ebc89d9a15dd3021b25fa892
Signed-off-by: serena.spinoso <serena.spinoso@polito.it>
Diffstat (limited to 'verigraph/src/main/java/it/polito/escape/verify/database')
-rw-r--r-- | verigraph/src/main/java/it/polito/escape/verify/database/DatabaseClass.java | 175 |
1 files changed, 175 insertions, 0 deletions
diff --git a/verigraph/src/main/java/it/polito/escape/verify/database/DatabaseClass.java b/verigraph/src/main/java/it/polito/escape/verify/database/DatabaseClass.java new file mode 100644 index 0000000..7837e51 --- /dev/null +++ b/verigraph/src/main/java/it/polito/escape/verify/database/DatabaseClass.java @@ -0,0 +1,175 @@ +/******************************************************************************* + * 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.database; + +import java.io.File; +import java.io.IOException; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +import com.fasterxml.jackson.core.JsonGenerationException; +import com.fasterxml.jackson.core.JsonParseException; +import com.fasterxml.jackson.databind.JsonMappingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.type.TypeFactory; + +import it.polito.escape.verify.exception.InternalServerErrorException; +import it.polito.escape.verify.model.Graph; +import it.polito.escape.verify.model.Neighbour; +import it.polito.escape.verify.model.Node; +import it.polito.escape.verify.service.GraphService; + +public class DatabaseClass { + + private static final DatabaseClass instance = new DatabaseClass(); + + private static ConcurrentHashMap<Long, Graph> graphs; + + private static String persistenceFile; + + private static boolean enablePersistence; + + protected DatabaseClass() { + initialize(); + if (enablePersistence) + loadDatabase(); + } + + private void initialize() { + graphs = new ConcurrentHashMap<>(); + enablePersistence = false; + persistenceFile = System.getProperty("catalina.base") + "/webapps/verify/json/" + "database.json"; + } + + private void loadDatabase() { + ObjectMapper mapper = new ObjectMapper(); + + List<Graph> parsedGraphs = null; + try { + File databaseFile = new File(persistenceFile); + parsedGraphs = mapper.readValue(databaseFile, + TypeFactory.defaultInstance().constructCollectionType( List.class, + Graph.class)); + } + catch (JsonParseException e) { + System.out.println("Database not loaded due to a JsonParseException: " + e.getMessage()); + return; + } + catch (JsonMappingException e) { + System.out.println("Database not loaded due to a JsonMappingException: " + e.getMessage()); + return; + } + catch (IOException e) { + //retry changing path + persistenceFile = "src/main/webapp/json/" + "database.json"; + + try { + File databaseFile = new File(persistenceFile); + parsedGraphs = mapper.readValue(databaseFile, + TypeFactory.defaultInstance().constructCollectionType( List.class, + Graph.class)); + } catch (JsonParseException e1) { + System.out.println("Database not loaded due to a JsonParseException: " + e.getMessage()); + return; + } catch (JsonMappingException e1) { + System.out.println("Database not loaded due to a JsonMappingException: " + e.getMessage()); + return; + } catch (IOException e1) { + System.out.println("Database not loaded due to an IOException: " + e.getMessage()); + return; + } + } + + System.out.println("Loading database..."); + + for (Graph graph : parsedGraphs) { + + try { + GraphService.validateGraph(graph); + } + catch (Exception e) { + System.out.println("Invalid database file: at least one graph is invalid!"); + return; + } + + graph.setId(getNumberOfGraphs() + 1); + + 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()); + } + } + + graphs.put(graph.getId(), graph); + } + + System.out.println("Database loaded!"); + System.out.println(graphs.size() + " graphs added"); + } + + public static DatabaseClass getInstance() { + return instance; + } + + public ConcurrentHashMap<Long, Graph> getGraphs() { + return graphs; + } + + public synchronized int getNumberOfGraphs() { + return graphs.size(); + } + + public synchronized int getGraphNumberOfNodes(long graphId) { + Graph graph = graphs.get(graphId); + if (graph == null) + return 0; + Map<Long, Node> nodes = graph.getNodes(); + if (nodes == null) + return 0; + return nodes.size(); + } + + public static void persistDatabase() { + if (!enablePersistence) + return; + ObjectMapper mapper = new ObjectMapper(); + + try { + mapper.writeValue(new File(persistenceFile), graphs); + } + catch (JsonGenerationException e) { + throw new InternalServerErrorException("Unable to persist database due to a JsonGenerationException: " + + e.getMessage()); + } + catch (JsonMappingException e) { + throw new InternalServerErrorException("Unable to persist database due to a JsonMappingException: " + + e.getMessage()); + } + catch (IOException e) { + //retry changing path + persistenceFile = "src/main/webapp/json/" + "database.json"; + try { + mapper.writeValue(new File(persistenceFile), graphs); + } catch (JsonGenerationException e1) { + throw new InternalServerErrorException("Unable to persist database due to a JsonGenerationException: " + + e.getMessage()); + } catch (JsonMappingException e1) { + throw new InternalServerErrorException("Unable to persist database due to a JsonGenerationException: " + + e.getMessage()); + } catch (IOException e1) { + throw new InternalServerErrorException("Unable to persist database due to an IOException: " + + e.getMessage()); + } + } + } +} |