001    /**
002     * Copyright (c) 2000-present 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.DBManagerUtil;
019    import com.liferay.portal.kernel.dao.db.DBType;
020    import com.liferay.portal.kernel.util.FileUtil;
021    import com.liferay.portal.kernel.util.StringUtil;
022    
023    import java.io.IOException;
024    
025    import java.util.Map;
026    
027    /**
028     * @author Brian Wing Shun Chan
029     * @author Charles May
030     * @author Alexander Chow
031     * @author Raymond Aug??
032     */
033    public class DBBuilder {
034    
035            public static void main(String[] args) throws Exception {
036                    ToolDependencies.wireBasic();
037    
038                    Map<String, String> arguments = ArgumentsUtil.parseArguments(args);
039    
040                    String databaseName = arguments.get("db.database.name");
041    
042                    String databaseTypesString = arguments.get("db.database.types");
043    
044                    DBType[] dbTypes = DBType.values();
045    
046                    if (databaseTypesString != null) {
047                            String[] databaseTypeValues = StringUtil.split(databaseTypesString);
048    
049                            dbTypes = new DBType[databaseTypeValues.length];
050    
051                            for (int i = 0; i < dbTypes.length; i++) {
052                                    dbTypes[i] = DBType.valueOf(
053                                            StringUtil.toUpperCase(databaseTypeValues[i]));
054                            }
055                    }
056    
057                    String sqlDir = arguments.get("db.sql.dir");
058    
059                    try {
060                            new DBBuilder(databaseName, dbTypes, sqlDir);
061                    }
062                    catch (Exception e) {
063                            ArgumentsUtil.processMainException(arguments, e);
064                    }
065            }
066    
067            public DBBuilder(String databaseName, DBType[] dbTypes, String sqlDir)
068                    throws Exception {
069    
070                    _databaseName = databaseName;
071                    _dbTypes = dbTypes;
072    
073                    if (!sqlDir.endsWith("/META-INF/sql") &&
074                            !sqlDir.endsWith("/WEB-INF/sql")) {
075    
076                            _buildSQLFile(sqlDir, "portal");
077                            _buildSQLFile(sqlDir, "portal-tables");
078                    }
079                    else {
080                            _buildSQLFile(sqlDir, "tables");
081                    }
082    
083                    _buildSQLFile(sqlDir, "indexes");
084                    _buildSQLFile(sqlDir, "sequences");
085                    _buildSQLFile(sqlDir, "update-5.0.1-5.1.0");
086                    _buildSQLFile(sqlDir, "update-5.1.1-5.1.2");
087                    _buildSQLFile(sqlDir, "update-5.1.2-5.2.0");
088                    _buildSQLFile(sqlDir, "update-5.2.0-5.2.1");
089                    _buildSQLFile(sqlDir, "update-5.2.2-5.2.3");
090                    _buildSQLFile(sqlDir, "update-5.2.3-6.0.0");
091                    _buildSQLFile(sqlDir, "update-5.2.5-6.0.0");
092                    _buildSQLFile(sqlDir, "update-5.2.7-6.0.0");
093                    _buildSQLFile(sqlDir, "update-5.2.8-6.0.5");
094                    _buildSQLFile(sqlDir, "update-6.0.0-6.0.1");
095                    _buildSQLFile(sqlDir, "update-6.0.1-6.0.2");
096                    _buildSQLFile(sqlDir, "update-6.0.2-6.0.3");
097                    _buildSQLFile(sqlDir, "update-6.0.4-6.0.5");
098                    _buildSQLFile(sqlDir, "update-6.0.5-6.0.6");
099                    _buildSQLFile(sqlDir, "update-6.0.6-6.1.0");
100                    _buildSQLFile(sqlDir, "update-6.0.12-6.1.0");
101                    _buildSQLFile(sqlDir, "update-6.1.0-6.1.1");
102                    _buildSQLFile(sqlDir, "update-6.1.1-6.2.0");
103    
104                    _buildCreateFile(sqlDir);
105            }
106    
107            private void _buildCreateFile(String sqlDir) throws IOException {
108                    for (DBType dbType : _dbTypes) {
109                            if (dbType == DBType.HYPERSONIC) {
110                                    continue;
111                            }
112    
113                            DB db = DBManagerUtil.getDB(dbType, null);
114    
115                            if (db != null) {
116                                    if (!sqlDir.endsWith("/WEB-INF/sql")) {
117                                            db.buildCreateFile(sqlDir, _databaseName);
118                                    }
119                                    else {
120                                            db.buildCreateFile(sqlDir, _databaseName, DB.DEFAULT);
121                                    }
122                            }
123                    }
124            }
125    
126            private void _buildSQLFile(String sqlDir, String fileName)
127                    throws IOException {
128    
129                    if (!FileUtil.exists(sqlDir + "/" + fileName + ".sql")) {
130                            return;
131                    }
132    
133                    for (DBType dbType : _dbTypes) {
134                            DB db = DBManagerUtil.getDB(dbType, null);
135    
136                            if (db != null) {
137                                    db.buildSQLFile(sqlDir, fileName);
138                            }
139                    }
140            }
141    
142            private final String _databaseName;
143            private final DBType[] _dbTypes;
144    
145    }