1 /*
2 * Copyright (c) 2010 Ralph Jones
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22 package com.totalchange.osopendata.generator;
23
24 import java.io.File;
25 import java.io.IOException;
26 import java.sql.Connection;
27 import java.sql.DriverManager;
28 import java.sql.SQLException;
29 import java.sql.Statement;
30 import java.util.logging.Logger;
31
32 public final class Populate {
33 private static final Logger logger = Logger.getLogger(Populate.class
34 .getName());
35
36 public static void populate(String db, String codePointDir,
37 String locatorDir) throws ClassNotFoundException, SQLException,
38 IOException {
39 logger.info("Creating connection to database " + db);
40
41 Class.forName("org.hsqldb.jdbcDriver");
42 Connection conn = DriverManager.getConnection("jdbc:hsqldb:file:" + db
43 + ";shutdown=true;hsqldb.default_table_type=cached", "SA", "");
44 conn.setAutoCommit(true);
45
46 try {
47 // The code point data is expected to be extracted AS IS from the
48 // OS download
49 File codePointRoot = new File(codePointDir);
50 File csvRoot = new File(codePointRoot, "data" + File.separator
51 + "CSV");
52 File codeListFile = new File(codePointRoot, "doc" + File.separator
53 + "codelist.txt");
54 PopulateCodepoint.populate(conn, csvRoot, codeListFile);
55
56 // The locator data is expected to be in the same structure as was
57 // provided by the OS
58 File locatorRoot = new File(locatorDir);
59 File dataDir = new File(locatorRoot, "data");
60 PopulateLocator.populate(conn, dataDir);
61
62 Statement st = conn.createStatement();
63 try {
64 logger.info("Closing and compacting database");
65 st.execute("SHUTDOWN COMPACT");
66 } finally {
67 st.close();
68 }
69 } finally {
70 conn.close();
71 }
72
73 logger.info("Database populated and available here: "
74 + new File(db).getAbsolutePath());
75 }
76
77 public static void main(String[] args) throws Exception {
78 populate("osopendata", "C:\\Documents and Settings\\ralphj\\Desktop\\"
79 + "Misc\\OS OpenData\\Code-Point Open",
80 "C:\\Documents and Settings\\ralphj\\Desktop\\Misc\\"
81 + "OS OpenData\\OS Locator");
82 }
83 }