This repository was archived by the owner on Mar 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTransitDataServer.java
More file actions
346 lines (254 loc) · 10.2 KB
/
TransitDataServer.java
File metadata and controls
346 lines (254 loc) · 10.2 KB
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
// Simple REST webserver for transit data
//import java.io.IOException;
//import java.io.OutputStream;
import java.net.InetSocketAddress;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
import java.util.*;
import java.io.*;
import java.nio.file.*;
import java.sql.*;
import org.json.simple.*;
import org.mariadb.jdbc.Driver;
public class TransitDataServer {
public static Connection getSQLConnection() throws SQLException {
String sql_string = "jdbc:mariadb://localhost:3306/";
String sql_user = "root";
// Set-up SQL Database Connection
Properties connectionProps = new Properties();
connectionProps.put("user", sql_user);
return DriverManager.getConnection(sql_string, connectionProps);
}
public static void main(String[] args) throws Exception {
String data_dir = "gtfs";
int port = 8200;
Class.forName("org.mariadb.jdbc.Driver");
Connection sql_conn = getSQLConnection();
System.out.println("Connected to database");
// Get data
getData(data_dir, sql_conn);
System.out.println("Data loaded");
// Start Server
HttpServer server = HttpServer.create(new InetSocketAddress(port), 0);
// Server contexts
server.createContext("/data", new DataHandler());
server.setExecutor(null);
server.start();
}
static class DataHandler implements HttpHandler {
@Override
public void handle(HttpExchange t) throws IOException {
String data_type = t.getRequestURI().getPath().substring(6).trim();
String response;
try {
response = getDataHandler(data_type);
} catch (SQLException e) {
response = sqlError(e);
}
t.sendResponseHeaders(200, response.length());
OutputStream os = t.getResponseBody();
os.write(response.getBytes());
os.close();
}
}
public static String getDataHandler(String data_type) throws SQLException {
Connection sql_conn = getSQLConnection();
Statement sql_stmt = sql_conn.createStatement();
if (data_type.equals("agency")) {
return getData_Agency(sql_stmt);
} else if (data_type.equals("all-data")) {
return getData_All(sql_stmt);
} else {
String agency;
if (data_type.startsWith("routes")) {
agency = data_type.substring(7);
return getData_Routes(sql_stmt, agency);
} else if (data_type.startsWith("shape-list")) {
agency = data_type.substring(11);
return getData_ShapeList(sql_stmt, agency);
} else if (data_type.startsWith("shapes")) {
agency = data_type.substring(7);
return getData_Shapes(sql_stmt, agency);
} else {
return "Data could not be found for type: " + data_type;
}
}
}
public static String getData_Agency(Statement sql_stmt) throws SQLException {
System.out.print("Getting agency data...");
List<String> agency_names = new ArrayList<String>();
for (String agency : getDBNames(sql_stmt))
if (!agency.equals("mysql") && !agency.endsWith("schema"))
agency_names.add(agency);
System.out.println("success");
return JSONValue.toJSONString(agency_names);
}
public static String getData_Routes(Statement sql_stmt, String agency) throws SQLException {
System.out.print("Getting route data for " + agency + "...");
Map<String, String> routes = new TreeMap<String, String>();
ResultSet route_table = getTable(sql_stmt, agency, "routes");
while (route_table.next())
routes.put(route_table.getString("route_id"), route_table.getString("route_long_name"));
System.out.println("success");
return new JSONObject(routes).toString();
}
public static String getData_ShapeList(Statement sql_stmt, String agency) throws SQLException {
System.out.print("Getting shape list for " + agency + "...");
List<String> shapes = new ArrayList<String>();
ResultSet shape_table = getTable(sql_stmt, agency, "shapes");
while (shape_table.next()) {
String id = shape_table.getString("shape_id");
if (!shapes.contains(id))
shapes.add(shape_table.getString("shape_id"));
}
System.out.println("success");
return JSONValue.toJSONString(shapes);
}
public static String getData_Shapes(Statement sql_stmt, String agency) throws SQLException {
System.out.println("Getting shapes for " + agency);
Map<String, List<List<String>>> shapes = new TreeMap<String, List<List<String>>>();
JSONArray shape_list = (JSONArray)JSONValue.parse(getData_ShapeList(sql_stmt, agency));
for (Object shape_object : shape_list) {
String shape = (String)shape_object;
ResultSet result = sql_stmt.executeQuery("SELECT * FROM " + agency + ".shapes WHERE shape_id='" + shape + "';");
List<List<String>> points = new ArrayList<List<String>>();
while (result.next()) {
List<String> point = new ArrayList<String>();
point.add(result.getString("shape_pt_lat"));
point.add(result.getString("shape_pt_lon"));
points.add(point);
}
shapes.put(shape, points);
}
return JSONValue.toJSONString(shapes);
}
public static String getData_All(Statement sql_stmt) throws SQLException {
JSONObject data = new JSONObject();
for (String agency : getDBNames(sql_stmt))
data.put(agency, (JSONObject)JSONValue.parse(getData_Shapes(sql_stmt, agency)));
return JSONValue.toJSONString(data);
}
public static String sqlError(SQLException e) {
return "SQL ERROR:\n:" + e.getMessage();
}
// Data Import Section ---------------------------------------------------------------//
private static void getData(String data_dir, Connection sql_conn) throws SQLException {
File folder = new File(data_dir);
File[] agency_folders = folder.listFiles();
Statement sql_stmt = sql_conn.createStatement();
ResultSet sql_result;
// Get current database names
List<String> databases = getDBNames(sql_stmt);
String[] lines = null;
for (File file : folder.listFiles())
if (file.isDirectory() && !file.getName().startsWith(".")) {
//System.out.println("In dir: " + file.getName());
File agency = new File(file.getAbsolutePath() + "/agency.txt");
lines = readFile(agency);
String[] headers = lines[0].split(",", -1);
String agency_name = lines[1].split(",", -1)[Arrays.asList(headers).indexOf("agency_name")].replace(' ','_').replace('-','_');
System.out.println("Processing agency: " + agency_name);
// Create SQL Database with agency name
if (!databases.contains(agency_name))
sql_stmt.executeUpdate("CREATE DATABASE " + agency_name + ";");
// Select Database
sql_stmt.executeUpdate("USE " + agency_name + ";");
for (File table : file.listFiles()) {
// Check for existing table name
String table_name = stripExtension(table.getName());
String query = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_SCHEMA='" + agency_name + "';";
sql_result = sql_stmt.executeQuery(query);
List<String> table_names = new ArrayList<String>();
System.out.println("\tTable: " + table_name);
while (sql_result.next())
table_names.add(sql_result.getString("TABLE_NAME"));
// Delete table if it exists
if (table_names.contains(table_name))
sql_stmt.executeUpdate("DROP TABLE " + table_name + ";");
// Create new table
lines = readFile(table);
headers = lines[0].split(",");
String create_table_query = "CREATE TABLE " + table_name + " (\n";
for (int i = 0; i < headers.length; i++) {
String header = headers[i];
// if (header.endsWith("id"))
// create_table_query += header + "\tINT\tNOT NULL";
// else
create_table_query += header + "\tVARCHAR (100)";
if (i != headers.length - 1)
create_table_query += ",\n";
}
create_table_query += ");";
sql_stmt.executeUpdate(create_table_query);
// Add data to table
for (int j = 1; j < lines.length; j++) {
if (lines.length > 1000 && j % 1000 == 0)
updateProgress((double)j / lines.length);
String row = createRow(lines[j].replace("'", "").split(",", -1));
query = "INSERT INTO " + table_name + "\n VALUES " + row + ";";
try {
sql_stmt.executeUpdate(query);
} catch (SQLException e) {
for (int k = 0; k < 120; k++)
System.out.print(" ");
System.out.print("\r");
System.out.print("\tSQL Error: Line: " + j + "\n\t" + e.getMessage());
}
}
System.out.print("\r");
for (int k = 0; k < 120; k++)
System.out.print(" ");
System.out.print("\r");
}
}
return;
}
private static void updateProgress(double progressPercentage) {
final int width = 100;
System.out.print("\rProgress: [");
int i = 0;
for (; i <= (int)(progressPercentage*width); i++)
System.out.print(".");
for (; i < width; i++)
System.out.print(" ");
System.out.print("] " + (int)(progressPercentage*100) + "%");
}
private static String stripExtension(String filename) {
if (filename == null) return null;
int pos = filename.lastIndexOf(".");
if (pos == -1) return filename;
return filename.substring(0, pos);
}
private static String createRow(String[] array) {
if (array == null || array.length == 0) return null;
String result = "('" + array[0] + "'";;
for (int i = 1; i < array.length; i++)
result += ",'" + array[i] + "'";
return result + ")";
}
private static String[] readFile(File file) {
Object[] file_data = null;
try {
file_data = Files.lines(file.toPath()).toArray();
} catch (IOException e) {
System.out.println("Error: " + e.getMessage());
return null;
}
return Arrays.copyOf(file_data, file_data.length, String[].class);
}
// Database Interaction Section ----------------------------------------------------//
private static List<String> getDBNames(Statement sql_stmt) throws SQLException {
ResultSet sql_result = sql_stmt.executeQuery("SHOW DATABASES;");
List<String> databases = new ArrayList<String>();
while(sql_result.next()) {
String db = sql_result.getString("Database");
if (!db.endsWith("schema") && !db.equals("mysql"))
databases.add(sql_result.getString("Database"));
}
return databases;
}
private static ResultSet getTable(Statement sql_stmt, String db, String table) throws SQLException {
return sql_stmt.executeQuery("SELECT * FROM " + db + "." + table + ";");
}
}