1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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   */
40  public class DBBuilder {
41  
42      public static void main(String[] args) {
43          InitUtil.initWithSpring();
44  
45          if (args.length == 1) {
46              new DBBuilder(args[0], DBUtil.TYPE_ALL);
47          }
48          else if (args.length == 2) {
49              new DBBuilder(args[0], StringUtil.split(args[1]));
50          }
51          else {
52              throw new IllegalArgumentException();
53          }
54      }
55  
56      public DBBuilder(String databaseName, String[] databaseTypes) {
57          try {
58              _databaseName = databaseName;
59              _databaseTypes = databaseTypes;
60  
61              _buildSQLFile("portal");
62              _buildSQLFile("portal-minimal");
63              _buildSQLFile("indexes");
64              _buildSQLFile("sequences");
65              _buildSQLFile("update-4.2.0-4.3.0");
66              _buildSQLFile("update-4.3.0-4.3.1");
67              _buildSQLFile("update-4.3.1-4.3.2");
68              _buildSQLFile("update-4.3.2-4.3.3");
69              _buildSQLFile("update-4.3.3-4.3.4");
70              _buildSQLFile("update-4.3.6-4.4.0");
71              _buildSQLFile("update-4.4.0-5.0.0");
72              _buildSQLFile("update-5.0.1-5.1.0");
73              _buildSQLFile("update-5.1.1-5.1.2");
74              _buildSQLFile("update-5.1.2-5.2.0");
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 type) {
118         return DBUtil.getInstance(type);
119     }
120 
121     private String _databaseName;
122     private String[] _databaseTypes;
123 
124 }