001    /**
002     * Copyright (c) 2000-2011 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    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    
088                            _buildCreateFile(sqlDir);
089                    }
090                    catch (Exception e) {
091                            e.printStackTrace();
092                    }
093            }
094    
095            private void _buildCreateFile(String sqlDir) throws IOException {
096                    for (int i = 0; i < _databaseTypes.length; i++) {
097                            String databaseType = _databaseTypes[i];
098    
099                            if (databaseType.equals(DB.TYPE_HYPERSONIC) ||
100                                    databaseType.equals(DB.TYPE_INTERBASE) ||
101                                    databaseType.equals(DB.TYPE_JDATASTORE) ||
102                                    databaseType.equals(DB.TYPE_SAP)) {
103    
104                                    continue;
105                            }
106    
107                            DB db = DBFactoryUtil.getDB(_databaseTypes[i]);
108    
109                            if (db != null) {
110                                    if (!sqlDir.endsWith("/WEB-INF/sql")) {
111                                            db.buildCreateFile(sqlDir, _databaseName);
112                                    }
113                                    else {
114                                            db.buildCreateFile(sqlDir, _databaseName, DB.POPULATED);
115                                    }
116                            }
117                    }
118            }
119    
120            private void _buildSQLFile(String sqlDir, String fileName)
121                    throws IOException {
122    
123                    if (!FileUtil.exists(sqlDir + "/" + fileName + ".sql")) {
124                            return;
125                    }
126    
127                    for (int i = 0; i < _databaseTypes.length; i++) {
128                            DB db = DBFactoryUtil.getDB(_databaseTypes[i]);
129    
130                            if (db != null) {
131                                    db.buildSQLFile(sqlDir, fileName);
132                            }
133                    }
134            }
135    
136            private String _databaseName;
137            private String[] _databaseTypes;
138    
139    }