1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.tools;
21  
22  import com.liferay.portal.kernel.util.FileUtil;
23  import com.liferay.portal.kernel.util.StringUtil;
24  import com.liferay.portal.tools.sql.DBUtil;
25  import com.liferay.portal.util.InitUtil;
26  
27  import java.io.IOException;
28  
29  /**
30   * <a href="DBBuilder.java.html"><b><i>View Source</i></b></a>
31   *
32   * @author Brian Wing Shun Chan
33   * @author Charles May
34   * @author Alexander Chow
35   *
36   */
37  public class DBBuilder {
38  
39      public static void main(String[] args) {
40          InitUtil.initWithSpring();
41  
42          if (args.length == 1) {
43              new DBBuilder(args[0], DBUtil.TYPE_ALL);
44          }
45          else if (args.length == 2) {
46              new DBBuilder(args[0], StringUtil.split(args[1]));
47          }
48          else {
49              throw new IllegalArgumentException();
50          }
51      }
52  
53      public DBBuilder(String databaseName, String[] databaseTypes) {
54          try {
55              _databaseName = databaseName;
56              _databaseTypes = databaseTypes;
57  
58              _buildSQLFile("portal");
59              _buildSQLFile("portal-minimal");
60              _buildSQLFile("indexes");
61              _buildSQLFile("sequences");
62              _buildSQLFile("update-4.2.0-4.3.0");
63              _buildSQLFile("update-4.3.0-4.3.1");
64              _buildSQLFile("update-4.3.1-4.3.2");
65              _buildSQLFile("update-4.3.2-4.3.3");
66              _buildSQLFile("update-4.3.3-4.3.4");
67              _buildSQLFile("update-4.3.6-4.4.0");
68              _buildSQLFile("update-4.4.0-5.0.0");
69              _buildSQLFile("update-5.0.1-5.1.0");
70              _buildSQLFile("update-5.1.1-5.1.2");
71              _buildSQLFile("update-5.1.2-5.2.0");
72              _buildSQLFile("update-5.2.0-5.2.1");
73              _buildSQLFile("update-5.2.2-5.2.3");
74  
75              _buildCreateFile();
76          }
77          catch (Exception e) {
78              e.printStackTrace();
79          }
80      }
81  
82      private void _buildCreateFile() throws IOException {
83          for (int i = 0; i < _databaseTypes.length; i++) {
84              String databaseType = _databaseTypes[i];
85  
86              if (databaseType.equals(DBUtil.TYPE_HYPERSONIC) ||
87                  databaseType.equals(DBUtil.TYPE_INTERBASE) ||
88                  databaseType.equals(DBUtil.TYPE_JDATASTORE) ||
89                  databaseType.equals(DBUtil.TYPE_SAP)) {
90  
91                  continue;
92              }
93  
94              DBUtil dbUtil = _getDBUtil(_databaseTypes[i]);
95  
96              if (dbUtil != null) {
97                  dbUtil.buildCreateFile(_databaseName);
98              }
99          }
100     }
101 
102     private void _buildSQLFile(String fileName) throws IOException {
103         if (!FileUtil.exists("../sql/" + fileName + ".sql")) {
104             return;
105         }
106 
107         for (int i = 0; i < _databaseTypes.length; i++) {
108             DBUtil dbUtil = _getDBUtil(_databaseTypes[i]);
109 
110             if (dbUtil != null) {
111                 dbUtil.buildSQLFile(fileName);
112             }
113         }
114     }
115 
116     private DBUtil _getDBUtil(String type) {
117         return DBUtil.getInstance(type);
118     }
119 
120     private String _databaseName;
121     private String[] _databaseTypes;
122 
123 }