001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.upgrade;
016    
017    import com.liferay.portal.kernel.dao.db.BaseDBProcess;
018    import com.liferay.portal.kernel.dao.db.DB;
019    import com.liferay.portal.kernel.dao.db.DBFactoryUtil;
020    import com.liferay.portal.kernel.dao.jdbc.DataAccess;
021    import com.liferay.portal.kernel.exception.SystemException;
022    import com.liferay.portal.kernel.log.Log;
023    import com.liferay.portal.kernel.log.LogFactoryUtil;
024    import com.liferay.portal.kernel.upgrade.util.UpgradeTable;
025    import com.liferay.portal.kernel.upgrade.util.UpgradeTableFactoryUtil;
026    
027    import java.sql.Connection;
028    import java.sql.DatabaseMetaData;
029    import java.sql.PreparedStatement;
030    import java.sql.ResultSet;
031    import java.sql.ResultSetMetaData;
032    
033    /**
034     * @author Brian Wing Shun Chan
035     * @author Alexander Chow
036     */
037    public abstract class UpgradeProcess extends BaseDBProcess {
038    
039            public UpgradeProcess() {
040            }
041    
042            public int getThreshold() {
043    
044                    // This upgrade process will only run if the build number is larger than
045                    // the returned threshold value. Return 0 to always run this upgrade
046                    // process.
047    
048                    return 0;
049            }
050    
051            public boolean hasTable(String tableName) throws Exception {
052                    Connection con = null;
053                    PreparedStatement ps = null;
054                    ResultSet rs = null;
055    
056                    try {
057                            con = DataAccess.getUpgradeOptimizedConnection();
058    
059                            DatabaseMetaData metadata = con.getMetaData();
060    
061                            rs = metadata.getTables(null, null, tableName, null);
062    
063                            while (rs.next()) {
064                                    return true;
065                            }
066                    }
067                    finally {
068                            DataAccess.cleanUp(con, ps, rs);
069                    }
070    
071                    return false;
072            }
073    
074            public long increment() throws SystemException {
075                    DB db = DBFactoryUtil.getDB();
076    
077                    return db.increment();
078            }
079    
080            public long increment(String name) throws SystemException {
081                    DB db = DBFactoryUtil.getDB();
082    
083                    return db.increment(name);
084            }
085    
086            public boolean isSupportsAlterColumnName() {
087                    DB db = DBFactoryUtil.getDB();
088    
089                    return db.isSupportsAlterColumnName();
090            }
091    
092            public boolean isSupportsAlterColumnType() {
093                    DB db = DBFactoryUtil.getDB();
094    
095                    return db.isSupportsAlterColumnType();
096            }
097    
098            public boolean isSupportsStringCaseSensitiveQuery() {
099                    DB db = DBFactoryUtil.getDB();
100    
101                    return db.isSupportsStringCaseSensitiveQuery();
102            }
103    
104            public boolean isSupportsUpdateWithInnerJoin() {
105                    DB db = DBFactoryUtil.getDB();
106    
107                    return db.isSupportsUpdateWithInnerJoin();
108            }
109    
110            public boolean tableHasColumn(String tableName, String columnName)
111                    throws Exception {
112    
113                    Connection con = null;
114                    PreparedStatement ps = null;
115                    ResultSet rs = null;
116    
117                    try {
118                            con = DataAccess.getUpgradeOptimizedConnection();
119    
120                            ps = con.prepareStatement("select * from " + tableName);
121    
122                            rs = ps.executeQuery();
123    
124                            ResultSetMetaData rsmd = rs.getMetaData();
125    
126                            for (int i = 0; i < rsmd.getColumnCount(); i++) {
127                                    String curColumnName = rsmd.getColumnName(i + 1);
128    
129                                    if (curColumnName.equals(columnName)) {
130                                            return true;
131                                    }
132                            }
133                    }
134                    catch (Exception e) {
135                    }
136                    finally {
137                            DataAccess.cleanUp(con, ps, rs);
138                    }
139    
140                    return false;
141            }
142    
143            public boolean tableHasData(String tableName) throws Exception {
144                    Connection con = null;
145                    PreparedStatement ps = null;
146                    ResultSet rs = null;
147    
148                    try {
149                            con = DataAccess.getUpgradeOptimizedConnection();
150    
151                            ps = con.prepareStatement("select count(*) from " + tableName);
152    
153                            rs = ps.executeQuery();
154    
155                            while (rs.next()) {
156                                    int count = rs.getInt(1);
157    
158                                    if (count > 0) {
159                                            return true;
160                                    }
161                            }
162                    }
163                    catch (Exception e) {
164                    }
165                    finally {
166                            DataAccess.cleanUp(con, ps, rs);
167                    }
168    
169                    return false;
170            }
171    
172            public void upgrade() throws UpgradeException {
173                    try {
174                            if (_log.isInfoEnabled()) {
175                                    _log.info("Upgrading " + getClass().getName());
176                            }
177    
178                            doUpgrade();
179                    }
180                    catch (Exception e) {
181                            throw new UpgradeException(e);
182                    }
183            }
184    
185            public void upgrade(Class<?> upgradeProcessClass) throws UpgradeException {
186                    UpgradeProcess upgradeProcess = null;
187    
188                    try {
189                            upgradeProcess = (UpgradeProcess)upgradeProcessClass.newInstance();
190                    }
191                    catch (Exception e) {
192                            throw new UpgradeException(e);
193                    }
194    
195                    upgradeProcess.upgrade();
196            }
197    
198            public void upgrade(UpgradeProcess upgradeProcess) throws UpgradeException {
199                    upgradeProcess.upgrade();
200            }
201    
202            protected void doUpgrade() throws Exception {
203            }
204    
205            protected void upgradeTable(String tableName, Object[][] tableColumns)
206                    throws Exception {
207    
208                    UpgradeTable upgradeTable = UpgradeTableFactoryUtil.getUpgradeTable(
209                            tableName, tableColumns);
210    
211                    upgradeTable.updateTable();
212            }
213    
214            protected void upgradeTable(
215                            String tableName, Object[][] tableColumns, String sqlCreate,
216                            String[] sqlAddIndexes)
217                    throws Exception {
218    
219                    UpgradeTable upgradeTable = UpgradeTableFactoryUtil.getUpgradeTable(
220                            tableName, tableColumns);
221    
222                    upgradeTable.setCreateSQL(sqlCreate);
223                    upgradeTable.setIndexesSQL(sqlAddIndexes);
224    
225                    upgradeTable.updateTable();
226            }
227    
228            private static Log _log = LogFactoryUtil.getLog(UpgradeProcess.class);
229    
230    }