1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.kernel.upgrade;
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.exception.SystemException;
20  import com.liferay.portal.kernel.log.Log;
21  import com.liferay.portal.kernel.log.LogFactoryUtil;
22  
23  import java.io.IOException;
24  
25  import java.sql.SQLException;
26  
27  import javax.naming.NamingException;
28  
29  /**
30   * <a href="UpgradeProcess.java.html"><b><i>View Source</i></b></a>
31   *
32   * @author Brian Wing Shun Chan
33   * @author Alexander Chow
34   */
35  public abstract class UpgradeProcess {
36  
37      public UpgradeProcess() {
38      }
39  
40      public int getThreshold() {
41  
42          // This upgrade process will only run if the build number is larger than
43          // the returned threshold value. Return 0 to always run this upgrade
44          // process.
45  
46          return 0;
47      }
48  
49      public long increment() throws SystemException {
50          DB db = DBFactoryUtil.getDB();
51  
52          return db.increment();
53      }
54  
55      public boolean isSupportsAlterColumnName() {
56          DB db = DBFactoryUtil.getDB();
57  
58          return db.isSupportsAlterColumnName();
59      }
60  
61      public boolean isSupportsAlterColumnType() {
62          DB db = DBFactoryUtil.getDB();
63  
64          return db.isSupportsAlterColumnType();
65      }
66  
67      public boolean isSupportsStringCaseSensitiveQuery() {
68          DB db = DBFactoryUtil.getDB();
69  
70          return db.isSupportsStringCaseSensitiveQuery();
71      }
72  
73      public boolean isSupportsUpdateWithInnerJoin() {
74          DB db = DBFactoryUtil.getDB();
75  
76          return db.isSupportsUpdateWithInnerJoin();
77      }
78  
79      public void runSQL(String template) throws IOException, SQLException {
80          DB db = DBFactoryUtil.getDB();
81  
82          db.runSQL(template);
83      }
84  
85      public void runSQL(String[] templates) throws IOException, SQLException {
86          DB db = DBFactoryUtil.getDB();
87  
88          db.runSQL(templates);
89      }
90  
91      public void runSQLTemplate(String path)
92          throws IOException, NamingException, SQLException {
93  
94          DB db = DBFactoryUtil.getDB();
95  
96          db.runSQLTemplate(path);
97      }
98  
99      public void runSQLTemplate(String path, boolean failOnError)
100         throws IOException, NamingException, SQLException {
101 
102         DB db = DBFactoryUtil.getDB();
103 
104         db.runSQLTemplate(path, failOnError);
105     }
106 
107     public void upgrade() throws UpgradeException {
108         try {
109             if (_log.isInfoEnabled()) {
110                 _log.info("Upgrading " + getClass().getName());
111             }
112 
113             doUpgrade();
114         }
115         catch (Exception e) {
116             throw new UpgradeException(e);
117         }
118     }
119 
120     public void upgrade(Class<?> upgradeProcessClass)
121         throws UpgradeException {
122 
123         UpgradeProcess upgradeProcess = null;
124 
125         try {
126             upgradeProcess = (UpgradeProcess)upgradeProcessClass.newInstance();
127         }
128         catch (Exception e) {
129             throw new UpgradeException(e);
130         }
131 
132         upgradeProcess.upgrade();
133     }
134 
135     public void upgrade(UpgradeProcess upgradeProcess)
136         throws UpgradeException {
137 
138         upgradeProcess.upgrade();
139     }
140 
141     protected void doUpgrade() throws Exception {
142     }
143 
144     private static Log _log = LogFactoryUtil.getLog(UpgradeProcess.class);
145 
146 }