001    /**
002     * Copyright (c) 2000-2010 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;
016    
017    import com.liferay.portal.kernel.dao.db.DB;
018    import com.liferay.portal.kernel.dao.db.DBFactoryUtil;
019    import com.liferay.portal.kernel.util.FileUtil;
020    import com.liferay.portal.kernel.util.StringUtil;
021    import com.liferay.portal.util.InitUtil;
022    
023    import java.io.IOException;
024    
025    /**
026     * @author Brian Wing Shun Chan
027     * @author Charles May
028     * @author Alexander Chow
029     */
030    public class DBBuilder {
031    
032            public static void main(String[] args) {
033                    InitUtil.initWithSpring();
034    
035                    if (args.length == 1) {
036                            new DBBuilder(args[0], DB.TYPE_ALL);
037                    }
038                    else if (args.length == 2) {
039                            new DBBuilder(args[0], StringUtil.split(args[1]));
040                    }
041                    else {
042                            throw new IllegalArgumentException();
043                    }
044            }
045    
046            public DBBuilder(String databaseName, String[] databaseTypes) {
047                    try {
048                            _databaseName = databaseName;
049                            _databaseTypes = databaseTypes;
050    
051                            String sqlDir = System.getProperty("sql.dir", "../sql");
052    
053                            _buildSQLFile(sqlDir, "portal");
054                            _buildSQLFile(sqlDir, "portal-minimal");
055                            _buildSQLFile(sqlDir, "indexes");
056                            _buildSQLFile(sqlDir, "sequences");
057                            _buildSQLFile(sqlDir, "update-4.2.0-4.3.0");
058                            _buildSQLFile(sqlDir, "update-4.3.0-4.3.1");
059                            _buildSQLFile(sqlDir, "update-4.3.1-4.3.2");
060                            _buildSQLFile(sqlDir, "update-4.3.2-4.3.3");
061                            _buildSQLFile(sqlDir, "update-4.3.3-4.3.4");
062                            _buildSQLFile(sqlDir, "update-4.3.6-4.4.0");
063                            _buildSQLFile(sqlDir, "update-4.4.0-5.0.0");
064                            _buildSQLFile(sqlDir, "update-5.0.1-5.1.0");
065                            _buildSQLFile(sqlDir, "update-5.1.1-5.1.2");
066                            _buildSQLFile(sqlDir, "update-5.1.2-5.2.0");
067                            _buildSQLFile(sqlDir, "update-5.2.0-5.2.1");
068                            _buildSQLFile(sqlDir, "update-5.2.2-5.2.3");
069                            _buildSQLFile(sqlDir, "update-5.2.3-6.0.0");
070                            _buildSQLFile(sqlDir, "update-5.2.5-6.0.0");
071                            _buildSQLFile(sqlDir, "update-5.2.7-6.0.0");
072                            _buildSQLFile(sqlDir, "update-5.2.8-6.0.3");
073                            _buildSQLFile(sqlDir, "update-6.0.0-6.0.1");
074                            _buildSQLFile(sqlDir, "update-6.0.1-6.0.2");
075                            _buildSQLFile(sqlDir, "update-6.0.2-6.0.3");
076    
077                            _buildCreateFile(sqlDir);
078                    }
079                    catch (Exception e) {
080                            e.printStackTrace();
081                    }
082            }
083    
084            private void _buildCreateFile(String sqlDir) throws IOException {
085                    for (int i = 0; i < _databaseTypes.length; i++) {
086                            String databaseType = _databaseTypes[i];
087    
088                            if (databaseType.equals(DB.TYPE_HYPERSONIC) ||
089                                    databaseType.equals(DB.TYPE_INTERBASE) ||
090                                    databaseType.equals(DB.TYPE_JDATASTORE) ||
091                                    databaseType.equals(DB.TYPE_SAP)) {
092    
093                                    continue;
094                            }
095    
096                            DB db = DBFactoryUtil.getDB(_databaseTypes[i]);
097    
098                            if (db != null) {
099                                    db.buildCreateFile(sqlDir, _databaseName);
100                            }
101                    }
102            }
103    
104            private void _buildSQLFile(String sqlDir, String fileName)
105                    throws IOException {
106    
107                    if (!FileUtil.exists(sqlDir + "/" + fileName + ".sql")) {
108                            return;
109                    }
110    
111                    for (int i = 0; i < _databaseTypes.length; i++) {
112                            DB db = DBFactoryUtil.getDB(_databaseTypes[i]);
113    
114                            if (db != null) {
115                                    db.buildSQLFile(sqlDir, fileName);
116                            }
117                    }
118            }
119    
120            private String _databaseName;
121            private String[] _databaseTypes;
122    
123    }