summaryrefslogtreecommitdiffstats
path: root/verigraph/src/main/java/it/polito/escape/verify/database/DatabaseClass.java
blob: 7837e5156d951fdac95651c92c1fd3934eb3f96b (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
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
/*******************************************************************************
 * 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());
			}
		}
	}
}