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          System.exit(0);
48      }
49  
50      public DBBuilder(String databaseName, String[] databaseTypes) {
51          try {
52              _databaseName = databaseName;
53              _databaseTypes = databaseTypes;
54  
55              String sqlDir = System.getProperty("sql.dir", "../sql");
56  
57              _buildSQLFile(sqlDir, "portal");
58              _buildSQLFile(sqlDir, "portal-minimal");
59              _buildSQLFile(sqlDir, "indexes");
60              _buildSQLFile(sqlDir, "sequences");
61              _buildSQLFile(sqlDir, "tables");
62              _buildSQLFile(sqlDir, "update-4.2.0-4.3.0");
63              _buildSQLFile(sqlDir, "update-4.3.0-4.3.1");
64              _buildSQLFile(sqlDir, "update-4.3.1-4.3.2");
65              _buildSQLFile(sqlDir, "update-4.3.2-4.3.3");
66              _buildSQLFile(sqlDir, "update-4.3.3-4.3.4");
67              _buildSQLFile(sqlDir, "update-4.3.6-4.4.0");
68              _buildSQLFile(sqlDir, "update-4.4.0-5.0.0");
69              _buildSQLFile(sqlDir, "update-5.0.1-5.1.0");
70              _buildSQLFile(sqlDir, "update-5.1.1-5.1.2");
71              _buildSQLFile(sqlDir, "update-5.1.2-5.2.0");
72              _buildSQLFile(sqlDir, "update-5.1.7-5.2.7");
73              _buildSQLFile(sqlDir, "update-5.1.8-5.2.8");
74              _buildSQLFile(sqlDir, "update-5.2.0-5.2.1");
75              _buildSQLFile(sqlDir, "update-5.2.2-5.2.3");
76              _buildSQLFile(sqlDir, "update-5.2.4-5.2.5");
77              _buildSQLFile(sqlDir, "update-5.2.5-5.2.6");
78              _buildSQLFile(sqlDir, "update-5.2.6-5.2.7");
79              _buildSQLFile(sqlDir, "update-5.2.7-5.2.8");
80  
81              _buildCreateFile(sqlDir);
82          }
83          catch (Exception e) {
84              e.printStackTrace();
85          }
86      }
87  
88      private void _buildCreateFile(String sqlDir) throws IOException {
89          for (int i = 0; i < _databaseTypes.length; i++) {
90              String databaseType = _databaseTypes[i];
91  
92              if (databaseType.equals(DB.TYPE_HYPERSONIC) ||
93                  databaseType.equals(DB.TYPE_INTERBASE) ||
94                  databaseType.equals(DB.TYPE_JDATASTORE) ||
95                  databaseType.equals(DB.TYPE_SAP)) {
96  
97                  continue;
98              }
99  
100             DB db = DBFactoryUtil.getDB(_databaseTypes[i]);
101 
102             if (db != null) {
103                 if (sqlDir.equals("../sql")) {
104                     db.buildCreateFile(sqlDir, _databaseName);
105                 }
106                 else {
107                     db.buildCreateFile(sqlDir, _databaseName, DB.POPULATED);
108                 }
109             }
110         }
111     }
112 
113     private void _buildSQLFile(String sqlDir, String fileName)
114         throws IOException {
115 
116         if (!FileUtil.exists(sqlDir + "/" + fileName + ".sql")) {
117             return;
118         }
119 
120         for (int i = 0; i < _databaseTypes.length; i++) {
121             DB db = DBFactoryUtil.getDB(_databaseTypes[i]);
122 
123             if (db != null) {
124                 db.buildSQLFile(sqlDir, fileName);
125             }
126         }
127     }
128 
129     private String _databaseName;
130     private String[] _databaseTypes;
131 
132 }