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.dao.db.DB;
26  import com.liferay.portal.kernel.dao.db.DBFactoryUtil;
27  import com.liferay.portal.kernel.util.FileUtil;
28  import com.liferay.portal.kernel.util.StringUtil;
29  import com.liferay.portal.util.InitUtil;
30  
31  import java.io.IOException;
32  
33  /**
34   * <a href="DBBuilder.java.html"><b><i>View Source</i></b></a>
35   *
36   * @author Brian Wing Shun Chan
37   * @author Charles May
38   * @author Alexander Chow
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], DB.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              _buildSQLFile("update-5.2.2-5.2.3");
77              _buildSQLFile("update-5.2.4-5.2.5");
78              _buildSQLFile("update-5.2.5-5.2.6");
79              _buildSQLFile("update-5.2.6-5.2.7");
80  
81              _buildCreateFile();
82          }
83          catch (Exception e) {
84              e.printStackTrace();
85          }
86      }
87  
88      private void _buildCreateFile() 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                 db.buildCreateFile(_databaseName);
104             }
105         }
106     }
107 
108     private void _buildSQLFile(String fileName) throws IOException {
109         if (!FileUtil.exists("../sql/" + fileName + ".sql")) {
110             return;
111         }
112 
113         for (int i = 0; i < _databaseTypes.length; i++) {
114             DB db = DBFactoryUtil.getDB(_databaseTypes[i]);
115 
116             if (db != null) {
117                 db.buildSQLFile(fileName);
118             }
119         }
120     }
121 
122     private String _databaseName;
123     private String[] _databaseTypes;
124 
125 }