001    /**
002     * Copyright (c) 2000-present 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.DBManagerUtil;
020    import com.liferay.portal.kernel.dao.db.DBProcessContext;
021    import com.liferay.portal.kernel.dao.jdbc.DataAccess;
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    import com.liferay.portal.kernel.util.ClassUtil;
027    
028    import java.sql.Connection;
029    
030    /**
031     * @author Brian Wing Shun Chan
032     * @author Alexander Chow
033     */
034    public abstract class UpgradeProcess
035            extends BaseDBProcess implements UpgradeStep {
036    
037            public int getThreshold() {
038    
039                    // This upgrade process will only run if the build number is larger than
040                    // the returned threshold value. Return 0 to always run this upgrade
041                    // process.
042    
043                    return 0;
044            }
045    
046            public void upgrade() throws UpgradeException {
047                    long start = System.currentTimeMillis();
048    
049                    if (_log.isInfoEnabled()) {
050                            _log.info("Upgrading " + ClassUtil.getClassName(this));
051                    }
052    
053                    try (Connection con = DataAccess.getUpgradeOptimizedConnection()) {
054                            connection = con;
055    
056                            doUpgrade();
057                    }
058                    catch (Exception e) {
059                            throw new UpgradeException(e);
060                    }
061                    finally {
062                            connection = null;
063    
064                            if (_log.isInfoEnabled()) {
065                                    _log.info(
066                                            "Completed upgrade process " +
067                                                    ClassUtil.getClassName(this) + " in " +
068                                                            (System.currentTimeMillis() - start) + "ms");
069                            }
070                    }
071            }
072    
073            public void upgrade(Class<?> upgradeProcessClass) throws UpgradeException {
074                    UpgradeProcess upgradeProcess = null;
075    
076                    try {
077                            upgradeProcess = (UpgradeProcess)upgradeProcessClass.newInstance();
078                    }
079                    catch (Exception e) {
080                            throw new UpgradeException(e);
081                    }
082    
083                    upgradeProcess.upgrade();
084            }
085    
086            @Override
087            public void upgrade(DBProcessContext dbProcessContext)
088                    throws UpgradeException {
089    
090                    upgrade();
091            }
092    
093            public void upgrade(UpgradeProcess upgradeProcess) throws UpgradeException {
094                    upgradeProcess.upgrade();
095            }
096    
097            protected abstract void doUpgrade() throws Exception;
098    
099            protected long increment() {
100                    DB db = DBManagerUtil.getDB();
101    
102                    return db.increment();
103            }
104    
105            protected long increment(String name) {
106                    DB db = DBManagerUtil.getDB();
107    
108                    return db.increment(name);
109            }
110    
111            protected boolean isSupportsAlterColumnName() {
112                    DB db = DBManagerUtil.getDB();
113    
114                    return db.isSupportsAlterColumnName();
115            }
116    
117            protected boolean isSupportsAlterColumnType() {
118                    DB db = DBManagerUtil.getDB();
119    
120                    return db.isSupportsAlterColumnType();
121            }
122    
123            protected boolean isSupportsStringCaseSensitiveQuery() {
124                    DB db = DBManagerUtil.getDB();
125    
126                    return db.isSupportsStringCaseSensitiveQuery();
127            }
128    
129            protected boolean isSupportsUpdateWithInnerJoin() {
130                    DB db = DBManagerUtil.getDB();
131    
132                    return db.isSupportsUpdateWithInnerJoin();
133            }
134    
135            protected void upgradeTable(String tableName, Object[][] tableColumns)
136                    throws Exception {
137    
138                    UpgradeTable upgradeTable = UpgradeTableFactoryUtil.getUpgradeTable(
139                            tableName, tableColumns);
140    
141                    upgradeTable.updateTable();
142            }
143    
144            protected void upgradeTable(
145                            String tableName, Object[][] tableColumns, String sqlCreate,
146                            String[] sqlAddIndexes)
147                    throws Exception {
148    
149                    UpgradeTable upgradeTable = UpgradeTableFactoryUtil.getUpgradeTable(
150                            tableName, tableColumns);
151    
152                    upgradeTable.setCreateSQL(sqlCreate);
153                    upgradeTable.setIndexesSQL(sqlAddIndexes);
154    
155                    upgradeTable.updateTable();
156            }
157    
158            private static final Log _log = LogFactoryUtil.getLog(UpgradeProcess.class);
159    
160    }