001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.kernel.upgrade;
016    
017    import com.liferay.portal.kernel.dao.db.DB;
018    import com.liferay.portal.kernel.dao.db.DBFactoryUtil;
019    import com.liferay.portal.kernel.dao.jdbc.DataAccess;
020    import com.liferay.portal.kernel.exception.SystemException;
021    import com.liferay.portal.kernel.log.Log;
022    import com.liferay.portal.kernel.log.LogFactoryUtil;
023    
024    import java.io.IOException;
025    
026    import java.sql.Connection;
027    import java.sql.DatabaseMetaData;
028    import java.sql.PreparedStatement;
029    import java.sql.ResultSet;
030    import java.sql.ResultSetMetaData;
031    import java.sql.SQLException;
032    
033    import javax.naming.NamingException;
034    
035    /**
036     * @author Brian Wing Shun Chan
037     * @author Alexander Chow
038     */
039    public abstract class UpgradeProcess {
040    
041            public UpgradeProcess() {
042            }
043    
044            public int getThreshold() {
045    
046                    // This upgrade process will only run if the build number is larger than
047                    // the returned threshold value. Return 0 to always run this upgrade
048                    // process.
049    
050                    return 0;
051            }
052    
053            public boolean hasTable(String tableName) throws Exception {
054                    Connection con = null;
055                    PreparedStatement ps = null;
056                    ResultSet rs = null;
057    
058                    try {
059                            con = DataAccess.getConnection();
060    
061                            DatabaseMetaData metadata = con.getMetaData();
062    
063                            rs = metadata.getTables(null, null, tableName, null);
064    
065                            while (rs.next()) {
066                                    return true;
067                            }
068                    }
069                    finally {
070                            DataAccess.cleanUp(con, ps, rs);
071                    }
072    
073                    return false;
074            }
075    
076            public long increment() throws SystemException {
077                    DB db = DBFactoryUtil.getDB();
078    
079                    return db.increment();
080            }
081    
082            public boolean isSupportsAlterColumnName() {
083                    DB db = DBFactoryUtil.getDB();
084    
085                    return db.isSupportsAlterColumnName();
086            }
087    
088            public boolean isSupportsAlterColumnType() {
089                    DB db = DBFactoryUtil.getDB();
090    
091                    return db.isSupportsAlterColumnType();
092            }
093    
094            public boolean isSupportsStringCaseSensitiveQuery() {
095                    DB db = DBFactoryUtil.getDB();
096    
097                    return db.isSupportsStringCaseSensitiveQuery();
098            }
099    
100            public boolean isSupportsUpdateWithInnerJoin() {
101                    DB db = DBFactoryUtil.getDB();
102    
103                    return db.isSupportsUpdateWithInnerJoin();
104            }
105    
106            public void runSQL(String template) throws IOException, SQLException {
107                    DB db = DBFactoryUtil.getDB();
108    
109                    db.runSQL(template);
110            }
111    
112            public void runSQL(String[] templates) throws IOException, SQLException {
113                    DB db = DBFactoryUtil.getDB();
114    
115                    db.runSQL(templates);
116            }
117    
118            public void runSQLTemplate(String path)
119                    throws IOException, NamingException, SQLException {
120    
121                    DB db = DBFactoryUtil.getDB();
122    
123                    db.runSQLTemplate(path);
124            }
125    
126            public void runSQLTemplate(String path, boolean failOnError)
127                    throws IOException, NamingException, SQLException {
128    
129                    DB db = DBFactoryUtil.getDB();
130    
131                    db.runSQLTemplate(path, failOnError);
132            }
133    
134            public boolean tableHasColumn(String tableName, String columnName)
135                    throws Exception {
136    
137                    Connection con = null;
138                    PreparedStatement ps = null;
139                    ResultSet rs = null;
140    
141                    try {
142                            con = DataAccess.getConnection();
143    
144                            ps = con.prepareStatement("select * from " + tableName);
145    
146                            rs = ps.executeQuery();
147    
148                            ResultSetMetaData rsmd = rs.getMetaData();
149    
150                            for (int i = 0; i < rsmd.getColumnCount(); i++) {
151                                    String curColumnName = rsmd.getColumnName(i + 1);
152    
153                                    if (curColumnName.equals(columnName)) {
154                                            return true;
155                                    }
156                            }
157                    }
158                    catch (Exception e) {
159                    }
160                    finally {
161                            DataAccess.cleanUp(con, ps, rs);
162                    }
163    
164                    return false;
165            }
166    
167            public boolean tableHasData(String tableName) throws Exception {
168                    Connection con = null;
169                    PreparedStatement ps = null;
170                    ResultSet rs = null;
171    
172                    try {
173                            con = DataAccess.getConnection();
174    
175                            ps = con.prepareStatement("select count(*) from " + tableName);
176    
177                            rs = ps.executeQuery();
178    
179                            while (rs.next()) {
180                                    long count = rs.getLong(1);
181    
182                                    if (count > 0) {
183                                            return true;
184                                    }
185                            }
186                    }
187                    catch (Exception e) {
188                    }
189                    finally {
190                            DataAccess.cleanUp(con, ps, rs);
191                    }
192    
193                    return false;
194            }
195    
196            public void upgrade() throws UpgradeException {
197                    try {
198                            if (_log.isInfoEnabled()) {
199                                    _log.info("Upgrading " + getClass().getName());
200                            }
201    
202                            doUpgrade();
203                    }
204                    catch (Exception e) {
205                            throw new UpgradeException(e);
206                    }
207            }
208    
209            public void upgrade(Class<?> upgradeProcessClass)
210                    throws UpgradeException {
211    
212                    UpgradeProcess upgradeProcess = null;
213    
214                    try {
215                            upgradeProcess = (UpgradeProcess)upgradeProcessClass.newInstance();
216                    }
217                    catch (Exception e) {
218                            throw new UpgradeException(e);
219                    }
220    
221                    upgradeProcess.upgrade();
222            }
223    
224            public void upgrade(UpgradeProcess upgradeProcess)
225                    throws UpgradeException {
226    
227                    upgradeProcess.upgrade();
228            }
229    
230            protected void doUpgrade() throws Exception {
231            }
232    
233            private static Log _log = LogFactoryUtil.getLog(UpgradeProcess.class);
234    
235    }