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.kernel.upgrade;
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.log.Log;
28  import com.liferay.portal.kernel.log.LogFactoryUtil;
29  import com.liferay.portal.kernel.util.InstancePool;
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 boolean isSupportsAlterColumnName() {
58          DB db = DBFactoryUtil.getDB();
59  
60          return db.isSupportsAlterColumnName();
61      }
62  
63      public boolean isSupportsAlterColumnType() {
64          DB db = DBFactoryUtil.getDB();
65  
66          return db.isSupportsAlterColumnType();
67      }
68  
69      public boolean isSupportsStringCaseSensitiveQuery() {
70          DB db = DBFactoryUtil.getDB();
71  
72          return db.isSupportsStringCaseSensitiveQuery();
73      }
74  
75      public boolean isSupportsUpdateWithInnerJoin() {
76          DB db = DBFactoryUtil.getDB();
77  
78          return db.isSupportsUpdateWithInnerJoin();
79      }
80  
81      public void runSQL(String template) throws IOException, SQLException {
82          DB db = DBFactoryUtil.getDB();
83  
84          db.runSQL(template);
85      }
86  
87      public void runSQL(String[] templates) throws IOException, SQLException {
88          DB db = DBFactoryUtil.getDB();
89  
90          db.runSQL(templates);
91      }
92  
93      public void runSQLTemplate(String path)
94          throws IOException, NamingException, SQLException {
95  
96          DB db = DBFactoryUtil.getDB();
97  
98          db.runSQLTemplate(path);
99      }
100 
101     public void runSQLTemplate(String path, boolean failOnError)
102         throws IOException, NamingException, SQLException {
103 
104         DB db = DBFactoryUtil.getDB();
105 
106         db.runSQLTemplate(path, failOnError);
107     }
108 
109     public void upgrade() throws UpgradeException {
110         try {
111             if (_log.isInfoEnabled()) {
112                 _log.info("Upgrading " + getClass().getName());
113             }
114 
115             doUpgrade();
116         }
117         catch (Exception e) {
118             throw new UpgradeException(e);
119         }
120     }
121 
122     public void upgrade(Class<?> upgradeProcessClass)
123         throws UpgradeException {
124 
125         UpgradeProcess upgradeProcess = (UpgradeProcess)InstancePool.get(
126             upgradeProcessClass.getName());
127 
128         upgradeProcess.upgrade();
129     }
130 
131     public void upgrade(UpgradeProcess upgradeProcess)
132         throws UpgradeException {
133 
134         upgradeProcess.upgrade();
135     }
136 
137     protected void doUpgrade() throws Exception {
138     }
139 
140     private static Log _log = LogFactoryUtil.getLog(UpgradeProcess.class);
141 
142 }