1   /**
2    * Copyright (c) 2000-2010 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   *
12   * 
13   */
14  
15  package com.liferay.portal.tools;
16  
17  import com.liferay.portal.kernel.dao.db.DB;
18  import com.liferay.portal.kernel.dao.db.DBFactoryUtil;
19  import com.liferay.portal.kernel.util.FileUtil;
20  import com.liferay.portal.kernel.util.StringUtil;
21  import com.liferay.portal.util.InitUtil;
22  
23  import java.io.IOException;
24  
25  /**
26   * <a href="DBBuilder.java.html"><b><i>View Source</i></b></a>
27   *
28   * @author Brian Wing Shun Chan
29   * @author Charles May
30   * @author Alexander Chow
31   */
32  public class DBBuilder {
33  
34      public static void main(String[] args) {
35          InitUtil.initWithSpring();
36  
37          if (args.length == 1) {
38              new DBBuilder(args[0], DB.TYPE_ALL);
39          }
40          else if (args.length == 2) {
41              new DBBuilder(args[0], StringUtil.split(args[1]));
42          }
43          else {
44              throw new IllegalArgumentException();
45          }
46      }
47  
48      public DBBuilder(String databaseName, String[] databaseTypes) {
49          try {
50              _databaseName = databaseName;
51              _databaseTypes = databaseTypes;
52  
53              String sqlDir = System.getProperty("sql.dir", "../sql");
54  
55              _buildSQLFile(sqlDir, "portal");
56              _buildSQLFile(sqlDir, "portal-minimal");
57              _buildSQLFile(sqlDir, "indexes");
58              _buildSQLFile(sqlDir, "sequences");
59              _buildSQLFile(sqlDir, "update-4.2.0-4.3.0");
60              _buildSQLFile(sqlDir, "update-4.3.0-4.3.1");
61              _buildSQLFile(sqlDir, "update-4.3.1-4.3.2");
62              _buildSQLFile(sqlDir, "update-4.3.2-4.3.3");
63              _buildSQLFile(sqlDir, "update-4.3.3-4.3.4");
64              _buildSQLFile(sqlDir, "update-4.3.6-4.4.0");
65              _buildSQLFile(sqlDir, "update-4.4.0-5.0.0");
66              _buildSQLFile(sqlDir, "update-5.0.1-5.1.0");
67              _buildSQLFile(sqlDir, "update-5.1.1-5.1.2");
68              _buildSQLFile(sqlDir, "update-5.1.4-5.1.5");
69              _buildSQLFile(sqlDir, "update-5.1.6-5.1.7");
70              _buildSQLFile(sqlDir, "update-5.1.7-5.1.8");
71  
72              _buildCreateFile(sqlDir);
73          }
74          catch (Exception e) {
75              e.printStackTrace();
76          }
77      }
78  
79      private void _buildCreateFile(String sqlDir) throws IOException {
80          for (int i = 0; i < _databaseTypes.length; i++) {
81              String databaseType = _databaseTypes[i];
82  
83              if (databaseType.equals(DB.TYPE_HYPERSONIC) ||
84                  databaseType.equals(DB.TYPE_INTERBASE) ||
85                  databaseType.equals(DB.TYPE_JDATASTORE) ||
86                  databaseType.equals(DB.TYPE_SAP)) {
87  
88                  continue;
89              }
90  
91              DB db = DBFactoryUtil.getDB(_databaseTypes[i]);
92  
93              if (db != null) {
94                  db.buildCreateFile(sqlDir, _databaseName);
95              }
96          }
97      }
98  
99      private void _buildSQLFile(String sqlDir, String fileName)
100         throws IOException {
101 
102         if (!FileUtil.exists(sqlDir + "/" + fileName + ".sql")) {
103             return;
104         }
105 
106         for (int i = 0; i < _databaseTypes.length; i++) {
107             DB db = DBFactoryUtil.getDB(_databaseTypes[i]);
108 
109             if (db != null) {
110                 db.buildSQLFile(sqlDir, fileName);
111             }
112         }
113     }
114 
115     private String _databaseName;
116     private String[] _databaseTypes;
117 
118 }