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