001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.tools.samplesqlbuilder;
016    
017    import com.liferay.portal.kernel.dao.jdbc.DataAccess;
018    import com.liferay.portal.kernel.util.SortedProperties;
019    import com.liferay.portal.tools.DBLoader;
020    import com.liferay.portal.util.InitUtil;
021    
022    import java.io.FileReader;
023    import java.io.IOException;
024    import java.io.Reader;
025    
026    import java.sql.Connection;
027    import java.sql.DriverManager;
028    import java.sql.Statement;
029    
030    import java.util.Properties;
031    
032    /**
033     * @author Tina Tian
034     * @author Shuyang Zhou
035     */
036    public class TestSampleSQLBuilder {
037    
038            public static void main(String[] args) {
039                    InitUtil.initWithSpring();
040    
041                    Reader reader = null;
042    
043                    try {
044                            Properties properties = new SortedProperties();
045    
046                            reader = new FileReader(args[0]);
047    
048                            properties.load(reader);
049    
050                            DataFactory dataFactory = new DataFactory(properties);
051    
052                            new SampleSQLBuilder(properties, dataFactory);
053    
054                            String sqlDir = properties.getProperty("sql.dir");
055                            String outputDir = properties.getProperty("sample.sql.output.dir");
056    
057                            loadHypersonic(sqlDir, outputDir);
058                    }
059                    catch (Exception e) {
060                            e.printStackTrace();
061                    }
062                    finally {
063                            if (reader != null) {
064                                    try {
065                                            reader.close();
066                                    }
067                                    catch (IOException ioe) {
068                                            ioe.printStackTrace();
069                                    }
070                            }
071                    }
072            }
073    
074            protected static void loadHypersonic(String sqlDir, String outputDir)
075                    throws Exception {
076    
077                    Class.forName("org.hsqldb.jdbcDriver");
078    
079                    Connection connection = null;
080                    Statement statement = null;
081    
082                    try {
083                            connection = DriverManager.getConnection(
084                                    "jdbc:hsqldb:mem:testSampleSQLBuilderDB;shutdown=true", "sa",
085                                    "");
086    
087                            DBLoader.loadHypersonic(
088                                    connection, sqlDir + "/portal/portal-hypersonic.sql");
089                            DBLoader.loadHypersonic(
090                                    connection, sqlDir + "/indexes/indexes-hypersonic.sql");
091                            DBLoader.loadHypersonic(
092                                    connection, outputDir + "/sample-hypersonic.sql");
093    
094                            statement = connection.createStatement();
095    
096                            statement.execute("SHUTDOWN COMPACT");
097                    }
098                    finally {
099                            DataAccess.cleanUp(connection, statement);
100                    }
101            }
102    
103    }