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