001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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, "tables");
058                            _buildSQLFile(sqlDir, "update-4.2.0-4.3.0");
059                            _buildSQLFile(sqlDir, "update-4.3.0-4.3.1");
060                            _buildSQLFile(sqlDir, "update-4.3.1-4.3.2");
061                            _buildSQLFile(sqlDir, "update-4.3.2-4.3.3");
062                            _buildSQLFile(sqlDir, "update-4.3.3-4.3.4");
063                            _buildSQLFile(sqlDir, "update-4.3.6-4.4.0");
064                            _buildSQLFile(sqlDir, "update-4.4.0-5.0.0");
065                            _buildSQLFile(sqlDir, "update-5.0.1-5.1.0");
066                            _buildSQLFile(sqlDir, "update-5.1.1-5.1.2");
067                            _buildSQLFile(sqlDir, "update-5.1.2-5.2.0");
068                            _buildSQLFile(sqlDir, "update-5.2.0-5.2.1");
069                            _buildSQLFile(sqlDir, "update-5.2.2-5.2.3");
070                            _buildSQLFile(sqlDir, "update-5.2.3-6.0.0");
071                            _buildSQLFile(sqlDir, "update-5.2.5-6.0.0");
072                            _buildSQLFile(sqlDir, "update-5.2.7-6.0.0");
073                            _buildSQLFile(sqlDir, "update-5.2.8-6.0.5");
074                            _buildSQLFile(sqlDir, "update-6.0.0-6.0.1");
075                            _buildSQLFile(sqlDir, "update-6.0.1-6.0.2");
076                            _buildSQLFile(sqlDir, "update-6.0.2-6.0.3");
077                            _buildSQLFile(sqlDir, "update-6.0.4-6.0.5");
078                            _buildSQLFile(sqlDir, "update-6.0.5-6.0.6");
079    
080                            _buildCreateFile(sqlDir);
081                    }
082                    catch (Exception e) {
083                            e.printStackTrace();
084                    }
085            }
086    
087            private void _buildCreateFile(String sqlDir) throws IOException {
088                    for (int i = 0; i < _databaseTypes.length; i++) {
089                            String databaseType = _databaseTypes[i];
090    
091                            if (databaseType.equals(DB.TYPE_HYPERSONIC) ||
092                                    databaseType.equals(DB.TYPE_INTERBASE) ||
093                                    databaseType.equals(DB.TYPE_JDATASTORE) ||
094                                    databaseType.equals(DB.TYPE_SAP)) {
095    
096                                    continue;
097                            }
098    
099                            DB db = DBFactoryUtil.getDB(_databaseTypes[i]);
100    
101                            if (db != null) {
102                                    if (sqlDir.equals("../sql")) {
103                                            db.buildCreateFile(sqlDir, _databaseName);
104                                    }
105                                    else {
106                                            db.buildCreateFile(sqlDir, _databaseName, DB.POPULATED);
107                                    }
108                            }
109                    }
110            }
111    
112            private void _buildSQLFile(String sqlDir, String fileName)
113                    throws IOException {
114    
115                    if (!FileUtil.exists(sqlDir + "/" + fileName + ".sql")) {
116                            return;
117                    }
118    
119                    for (int i = 0; i < _databaseTypes.length; i++) {
120                            DB db = DBFactoryUtil.getDB(_databaseTypes[i]);
121    
122                            if (db != null) {
123                                    db.buildSQLFile(sqlDir, fileName);
124                            }
125                    }
126            }
127    
128            private String _databaseName;
129            private String[] _databaseTypes;
130    
131    }