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.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 UpgradeMVCCVersion extends UpgradeProcess {
039    
040            public void upgradeMVCCVersion(
041                            DatabaseMetaData databaseMetaData, String tableName)
042                    throws Exception {
043    
044                    tableName = normalizeName(tableName, databaseMetaData);
045    
046                    ResultSet tableResultSet = databaseMetaData.getTables(
047                            null, null, tableName, null);
048    
049                    try {
050                            if (!tableResultSet.next()) {
051                                    _log.error("Table " + tableName + " does not exist");
052    
053                                    return;
054                            }
055    
056                            ResultSet columnResultSet = databaseMetaData.getColumns(
057                                    null, null, tableName,
058                                    normalizeName("mvccVersion", databaseMetaData));
059    
060                            try {
061                                    if (columnResultSet.next()) {
062                                            return;
063                                    }
064    
065                                    runSQL(
066                                            "alter table " + tableName +
067                                                    " add mvccVersion LONG default 0");
068    
069                                    if (_log.isDebugEnabled()) {
070                                            _log.debug(
071                                                    "Added column mvccVersion to table " + tableName);
072                                    }
073                            }
074                            finally {
075                                    DataAccess.cleanUp(columnResultSet);
076                            }
077                    }
078                    finally {
079                            DataAccess.cleanUp(tableResultSet);
080                    }
081            }
082    
083            @Override
084            protected void doUpgrade() throws Exception {
085                    Connection connection = null;
086    
087                    try {
088                            connection = DataAccess.getUpgradeOptimizedConnection();
089    
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                    finally {
109                            DataAccess.cleanUp(connection);
110                    }
111            }
112    
113            protected List<Element> getClassElements() throws Exception {
114                    Thread currentThread = Thread.currentThread();
115    
116                    ClassLoader classLoader = currentThread.getContextClassLoader();
117    
118                    InputStream inputStream = classLoader.getResourceAsStream(
119                            "META-INF/portal-hbm.xml");
120    
121                    Document document = UnsecureSAXReaderUtil.read(inputStream);
122    
123                    Element rootElement = document.getRootElement();
124    
125                    return rootElement.elements("class");
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    }