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