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.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.upgrade.UpgradeProcess;
020    import com.liferay.portal.kernel.util.LoggingTimer;
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    
031    import java.util.List;
032    
033    /**
034     * @author     Shuyang Zhou
035     * @deprecated As of 7.0.0, replaced by {@link
036     *             com.liferay.portal.kernel.upgrade.UpgradeMVCCVersion}
037     */
038    @Deprecated
039    public class UpgradeMVCCVersion extends UpgradeProcess {
040    
041            public void upgradeMVCCVersion(
042                            DatabaseMetaData databaseMetaData, String tableName)
043                    throws Exception {
044    
045                    for (String excludeTableName : getExcludedTableNames()) {
046                            if (StringUtil.equalsIgnoreCase(excludeTableName, tableName)) {
047                                    return;
048                            }
049                    }
050    
051                    tableName = normalizeName(tableName, databaseMetaData);
052    
053                    try (ResultSet tableResultSet = databaseMetaData.getTables(
054                                    null, null, tableName, null)) {
055    
056                            if (!tableResultSet.next()) {
057                                    _log.error("Table " + tableName + " does not exist");
058    
059                                    return;
060                            }
061    
062                            try (ResultSet columnResultSet = databaseMetaData.getColumns(
063                                            null, null, tableName,
064                                            normalizeName("mvccVersion", databaseMetaData))) {
065    
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                    }
080            }
081    
082            @Override
083            protected void doUpgrade() throws Exception {
084                    upgradeClassElementMVCCVersions();
085                    upgradeModuleTableMVCCVersions();
086            }
087    
088            protected List<Element> getClassElements() throws Exception {
089                    Thread currentThread = Thread.currentThread();
090    
091                    ClassLoader classLoader = currentThread.getContextClassLoader();
092    
093                    InputStream inputStream = classLoader.getResourceAsStream(
094                            "META-INF/portal-hbm.xml");
095    
096                    Document document = UnsecureSAXReaderUtil.read(inputStream);
097    
098                    Element rootElement = document.getRootElement();
099    
100                    return rootElement.elements("class");
101            }
102    
103            protected String[] getExcludedTableNames() {
104                    return new String[0];
105            }
106    
107            protected String[] getModuleTableNames() {
108                    return new String[] {"BackgroundTask", "Lock_"};
109            }
110    
111            protected void upgradeClassElementMVCCVersions() throws Exception {
112                    try (LoggingTimer loggingTimer = new LoggingTimer()) {
113                            DatabaseMetaData databaseMetaData = connection.getMetaData();
114    
115                            List<Element> classElements = getClassElements();
116    
117                            for (Element classElement : classElements) {
118                                    if (classElement.element("version") == null) {
119                                            continue;
120                                    }
121    
122                                    upgradeMVCCVersion(databaseMetaData, classElement);
123                            }
124                    }
125            }
126    
127            protected void upgradeModuleTableMVCCVersions() throws Exception {
128                    try (LoggingTimer loggingTimer = new LoggingTimer()) {
129                            DatabaseMetaData databaseMetaData = connection.getMetaData();
130    
131                            String[] moduleTableNames = getModuleTableNames();
132    
133                            for (String moduleTableName : moduleTableNames) {
134                                    upgradeMVCCVersion(databaseMetaData, moduleTableName);
135                            }
136                    }
137            }
138    
139            protected void upgradeMVCCVersion(
140                            DatabaseMetaData databaseMetaData, Element classElement)
141                    throws Exception {
142    
143                    String tableName = classElement.attributeValue("table");
144    
145                    upgradeMVCCVersion(databaseMetaData, tableName);
146            }
147    
148            private static final Log _log = LogFactoryUtil.getLog(
149                    UpgradeMVCCVersion.class);
150    
151    }