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.upgrade.util;
016    
017    import com.liferay.portal.kernel.dao.jdbc.DataAccess;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.upgrade.UpgradeProcess;
021    import com.liferay.portal.kernel.util.StringUtil;
022    import com.liferay.portal.kernel.xml.Document;
023    import com.liferay.portal.kernel.xml.Element;
024    import com.liferay.portal.kernel.xml.UnsecureSAXReaderUtil;
025    
026    import java.io.InputStream;
027    
028    import java.sql.DatabaseMetaData;
029    import java.sql.ResultSet;
030    import java.sql.SQLException;
031    
032    import java.util.List;
033    
034    /**
035     * @author Shuyang Zhou
036     */
037    public class UpgradeMVCCVersion extends UpgradeProcess {
038    
039            public void upgradeMVCCVersion(
040                            DatabaseMetaData databaseMetaData, String tableName)
041                    throws Exception {
042    
043                    for (String excludeTableName : getExcludedTableNames()) {
044                            if (StringUtil.equalsIgnoreCase(excludeTableName, tableName)) {
045                                    return;
046                            }
047                    }
048    
049                    tableName = normalizeName(tableName, databaseMetaData);
050    
051                    ResultSet tableResultSet = databaseMetaData.getTables(
052                            null, null, tableName, null);
053    
054                    try {
055                            if (!tableResultSet.next()) {
056                                    _log.error("Table " + tableName + " does not exist");
057    
058                                    return;
059                            }
060    
061                            ResultSet columnResultSet = databaseMetaData.getColumns(
062                                    null, null, tableName,
063                                    normalizeName("mvccVersion", databaseMetaData));
064    
065                            try {
066                                    if (columnResultSet.next()) {
067                                            return;
068                                    }
069    
070                                    runSQL(
071                                            "alter table " + tableName +
072                                                    " add mvccVersion LONG default 0 not null");
073    
074                                    if (_log.isDebugEnabled()) {
075                                            _log.debug(
076                                                    "Added column mvccVersion to table " + tableName);
077                                    }
078                            }
079                            finally {
080                                    DataAccess.cleanUp(columnResultSet);
081                            }
082                    }
083                    finally {
084                            DataAccess.cleanUp(tableResultSet);
085                    }
086            }
087    
088            @Override
089            protected void doUpgrade() throws Exception {
090                    DatabaseMetaData databaseMetaData = connection.getMetaData();
091    
092                    List<Element> classElements = getClassElements();
093    
094                    for (Element classElement : classElements) {
095                            if (classElement.element("version") == null) {
096                                    continue;
097                            }
098    
099                            upgradeMVCCVersion(databaseMetaData, classElement);
100                    }
101    
102                    String[] moduleTableNames = getModuleTableNames();
103    
104                    for (String moduleTableName : moduleTableNames) {
105                            upgradeMVCCVersion(databaseMetaData, moduleTableName);
106                    }
107            }
108    
109            protected List<Element> getClassElements() throws Exception {
110                    Thread currentThread = Thread.currentThread();
111    
112                    ClassLoader classLoader = currentThread.getContextClassLoader();
113    
114                    InputStream inputStream = classLoader.getResourceAsStream(
115                            "META-INF/portal-hbm.xml");
116    
117                    Document document = UnsecureSAXReaderUtil.read(inputStream);
118    
119                    Element rootElement = document.getRootElement();
120    
121                    return rootElement.elements("class");
122            }
123    
124            protected String[] getExcludedTableNames() {
125                    return new String[0];
126            }
127    
128            protected String[] getModuleTableNames() {
129                    return new String[] {"BackgroundTask", "Lock_"};
130            }
131    
132            protected String normalizeName(
133                            String name, DatabaseMetaData databaseMetaData)
134                    throws SQLException {
135    
136                    if (databaseMetaData.storesLowerCaseIdentifiers()) {
137                            return StringUtil.toLowerCase(name);
138                    }
139    
140                    if (databaseMetaData.storesUpperCaseIdentifiers()) {
141                            return StringUtil.toUpperCase(name);
142                    }
143    
144                    return name;
145            }
146    
147            protected void upgradeMVCCVersion(
148                            DatabaseMetaData databaseMetaData, Element classElement)
149                    throws Exception {
150    
151                    String tableName = classElement.attributeValue("table");
152    
153                    upgradeMVCCVersion(databaseMetaData, tableName);
154            }
155    
156            private static final Log _log = LogFactoryUtil.getLog(
157                    UpgradeMVCCVersion.class);
158    
159    }