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.2-5.2.0");
69              _buildSQLFile(sqlDir, "update-5.1.7-5.2.7");
70              _buildSQLFile(sqlDir, "update-5.1.8-5.2.8");
71              _buildSQLFile(sqlDir, "update-5.2.0-5.2.1");
72              _buildSQLFile(sqlDir, "update-5.2.2-5.2.3");
73              _buildSQLFile(sqlDir, "update-5.2.4-5.2.5");
74              _buildSQLFile(sqlDir, "update-5.2.5-5.2.6");
75              _buildSQLFile(sqlDir, "update-5.2.6-5.2.7");
76              _buildSQLFile(sqlDir, "update-5.2.7-5.2.8");
77  
78              _buildCreateFile(sqlDir);
79          }
80          catch (Exception e) {
81              e.printStackTrace();
82          }
83      }
84  
85      private void _buildCreateFile(String sqlDir) throws IOException {
86          for (int i = 0; i < _databaseTypes.length; i++) {
87              String databaseType = _databaseTypes[i];
88  
89              if (databaseType.equals(DB.TYPE_HYPERSONIC) ||
90                  databaseType.equals(DB.TYPE_INTERBASE) ||
91                  databaseType.equals(DB.TYPE_JDATASTORE) ||
92                  databaseType.equals(DB.TYPE_SAP)) {
93  
94                  continue;
95              }
96  
97              DB db = DBFactoryUtil.getDB(_databaseTypes[i]);
98  
99              if (db != null) {
100                 db.buildCreateFile(sqlDir, _databaseName);
101             }
102         }
103     }
104 
105     private void _buildSQLFile(String sqlDir, String fileName)
106         throws IOException {
107 
108         if (!FileUtil.exists(sqlDir + "/" + fileName + ".sql")) {
109             return;
110         }
111 
112         for (int i = 0; i < _databaseTypes.length; i++) {
113             DB db = DBFactoryUtil.getDB(_databaseTypes[i]);
114 
115             if (db != null) {
116                 db.buildSQLFile(sqlDir, fileName);
117             }
118         }
119     }
120 
121     private String _databaseName;
122     private String[] _databaseTypes;
123 
124 }