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              _buildSQLFile("update-5.2.0-5.2.1");
76  
77              _buildCreateFile();
78          }
79          catch (Exception e) {
80              e.printStackTrace();
81          }
82      }
83  
84      private void _buildCreateFile() throws IOException {
85          for (int i = 0; i < _databaseTypes.length; i++) {
86              String databaseType = _databaseTypes[i];
87  
88              if (databaseType.equals(DBUtil.TYPE_HYPERSONIC) ||
89                  databaseType.equals(DBUtil.TYPE_INTERBASE) ||
90                  databaseType.equals(DBUtil.TYPE_JDATASTORE) ||
91                  databaseType.equals(DBUtil.TYPE_SAP)) {
92  
93                  continue;
94              }
95  
96              DBUtil dbUtil = _getDBUtil(_databaseTypes[i]);
97  
98              if (dbUtil != null) {
99                  dbUtil.buildCreateFile(_databaseName);
100             }
101         }
102     }
103 
104     private void _buildSQLFile(String fileName) throws IOException {
105         if (!FileUtil.exists("../sql/" + fileName + ".sql")) {
106             return;
107         }
108 
109         for (int i = 0; i < _databaseTypes.length; i++) {
110             DBUtil dbUtil = _getDBUtil(_databaseTypes[i]);
111 
112             if (dbUtil != null) {
113                 dbUtil.buildSQLFile(fileName);
114             }
115         }
116     }
117 
118     private DBUtil _getDBUtil(String type) {
119         return DBUtil.getInstance(type);
120     }
121 
122     private String _databaseName;
123     private String[] _databaseTypes;
124 
125 }