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.StringBundler;
021    
022    import java.io.IOException;
023    
024    import java.sql.SQLException;
025    
026    /**
027     * @author Brian Wing Shun Chan
028     */
029    public abstract class UpgradeCompanyId extends UpgradeProcess {
030    
031            @Override
032            protected void doUpgrade() throws Exception {
033                    for (TableUpdater tableUpdater : getTableUpdaters()) {
034                            if (hasColumn(tableUpdater.getTableName(), "companyId")) {
035                                    if (_log.isInfoEnabled()) {
036                                            _log.info("Skipping table " + tableUpdater.getTableName());
037                                    }
038    
039                                    continue;
040                            }
041    
042                            if (_log.isInfoEnabled()) {
043                                    _log.info(
044                                            "Adding column companyId to table " +
045                                                    tableUpdater.getTableName());
046                            }
047    
048                            runSQL(
049                                    "alter table " + tableUpdater.getTableName() +
050                                            " add companyId LONG");
051    
052                            tableUpdater.update();
053                    }
054            }
055    
056            protected abstract TableUpdater[] getTableUpdaters();
057    
058            protected class TableUpdater {
059    
060                    public TableUpdater(
061                            String tableName, String foreignTableName,
062                            String foreignColumnName) {
063    
064                            _tableName = tableName;
065    
066                            _columnName = foreignColumnName;
067    
068                            _foreignNamesArray = new String[][] {
069                                    new String[] {foreignTableName, foreignColumnName}
070                            };
071                    }
072    
073                    public TableUpdater(
074                            String tableName, String columnName, String[][] foreignNamesArray) {
075    
076                            _tableName = tableName;
077                            _columnName = columnName;
078                            _foreignNamesArray = foreignNamesArray;
079                    }
080    
081                    public String getTableName() {
082                            return _tableName;
083                    }
084    
085                    public void update() throws IOException, SQLException {
086                            for (String[] foreignNames : _foreignNamesArray) {
087                                    runSQL(getUpdateSQL(foreignNames[0], foreignNames[1]));
088                            }
089                    }
090    
091                    protected String getSelectSQL(
092                            String foreignTableName, String foreignColumnName) {
093    
094                            StringBundler sb = new StringBundler(10);
095    
096                            sb.append("select max(companyId) from ");
097                            sb.append(foreignTableName);
098                            sb.append(" where ");
099                            sb.append(foreignTableName);
100                            sb.append(".");
101                            sb.append(foreignColumnName);
102                            sb.append(" = ");
103                            sb.append(_tableName);
104                            sb.append(".");
105                            sb.append(_columnName);
106    
107                            return sb.toString();
108                    }
109    
110                    protected String getUpdateSQL(String selectSQL) {
111                            StringBundler sb = new StringBundler(5);
112    
113                            sb.append("update ");
114                            sb.append(_tableName);
115                            sb.append(" set companyId = (");
116                            sb.append(selectSQL);
117                            sb.append(")");
118    
119                            return sb.toString();
120                    }
121    
122                    protected String getUpdateSQL(
123                            String foreignTableName, String foreignColumnName) {
124    
125                            return getUpdateSQL(
126                                    getSelectSQL(foreignTableName, foreignColumnName));
127                    }
128    
129                    private final String _columnName;
130                    private final String[][] _foreignNamesArray;
131                    private final String _tableName;
132    
133            }
134    
135            private static final Log _log = LogFactoryUtil.getLog(
136                    UpgradeCompanyId.class);
137    
138    }