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.kernel.upgrade;
24  
25  import com.liferay.portal.SystemException;
26  import com.liferay.portal.kernel.dao.db.DB;
27  import com.liferay.portal.kernel.dao.db.DBFactoryUtil;
28  import com.liferay.portal.kernel.log.Log;
29  import com.liferay.portal.kernel.log.LogFactoryUtil;
30  
31  import java.io.IOException;
32  
33  import java.sql.SQLException;
34  
35  import javax.naming.NamingException;
36  
37  /**
38   * <a href="UpgradeProcess.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Brian Wing Shun Chan
41   * @author Alexander Chow
42   */
43  public abstract class UpgradeProcess {
44  
45      public UpgradeProcess() {
46      }
47  
48      public int getThreshold() {
49  
50          // This upgrade process will only run if the build number is larger than
51          // the returned threshold value. Return 0 to always run this upgrade
52          // process.
53  
54          return 0;
55      }
56  
57      public long increment() throws SystemException {
58          DB db = DBFactoryUtil.getDB();
59  
60          return db.increment();
61      }
62  
63      public boolean isSupportsAlterColumnName() {
64          DB db = DBFactoryUtil.getDB();
65  
66          return db.isSupportsAlterColumnName();
67      }
68  
69      public boolean isSupportsAlterColumnType() {
70          DB db = DBFactoryUtil.getDB();
71  
72          return db.isSupportsAlterColumnType();
73      }
74  
75      public boolean isSupportsStringCaseSensitiveQuery() {
76          DB db = DBFactoryUtil.getDB();
77  
78          return db.isSupportsStringCaseSensitiveQuery();
79      }
80  
81      public boolean isSupportsUpdateWithInnerJoin() {
82          DB db = DBFactoryUtil.getDB();
83  
84          return db.isSupportsUpdateWithInnerJoin();
85      }
86  
87      public void runSQL(String template) throws IOException, SQLException {
88          DB db = DBFactoryUtil.getDB();
89  
90          db.runSQL(template);
91      }
92  
93      public void runSQL(String[] templates) throws IOException, SQLException {
94          DB db = DBFactoryUtil.getDB();
95  
96          db.runSQL(templates);
97      }
98  
99      public void runSQLTemplate(String path)
100         throws IOException, NamingException, SQLException {
101 
102         DB db = DBFactoryUtil.getDB();
103 
104         db.runSQLTemplate(path);
105     }
106 
107     public void runSQLTemplate(String path, boolean failOnError)
108         throws IOException, NamingException, SQLException {
109 
110         DB db = DBFactoryUtil.getDB();
111 
112         db.runSQLTemplate(path, failOnError);
113     }
114 
115     public void upgrade() throws UpgradeException {
116         try {
117             if (_log.isInfoEnabled()) {
118                 _log.info("Upgrading " + getClass().getName());
119             }
120 
121             doUpgrade();
122         }
123         catch (Exception e) {
124             throw new UpgradeException(e);
125         }
126     }
127 
128     public void upgrade(Class<?> upgradeProcessClass)
129         throws UpgradeException {
130 
131         UpgradeProcess upgradeProcess = null;
132 
133         try {
134             upgradeProcess = (UpgradeProcess)upgradeProcessClass.newInstance();
135         }
136         catch (Exception e) {
137             throw new UpgradeException(e);
138         }
139 
140         upgradeProcess.upgrade();
141     }
142 
143     public void upgrade(UpgradeProcess upgradeProcess)
144         throws UpgradeException {
145 
146         upgradeProcess.upgrade();
147     }
148 
149     protected void doUpgrade() throws Exception {
150     }
151 
152     private static Log _log = LogFactoryUtil.getLog(UpgradeProcess.class);
153 
154 }