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