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