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.upgrade.StagnantRowException;
018    import com.liferay.portal.kernel.upgrade.util.UpgradeColumn;
019    import com.liferay.portal.kernel.upgrade.util.UpgradeTable;
020    import com.liferay.portal.kernel.util.Validator;
021    import com.liferay.portal.tools.comparator.ColumnsComparator;
022    
023    import java.sql.Connection;
024    import java.sql.PreparedStatement;
025    import java.sql.ResultSet;
026    
027    import java.util.ArrayList;
028    import java.util.Arrays;
029    import java.util.List;
030    
031    /**
032     * @author Alexander Chow
033     * @author Bruno Farache
034     */
035    public class DefaultUpgradeTableImpl
036            extends BaseUpgradeTableImpl implements UpgradeTable {
037    
038            @Override
039            public void copyTable(
040                            Connection sourceConnection, Connection targetConnection)
041                    throws Exception {
042    
043                    updateTable(sourceConnection, targetConnection, false);
044            }
045    
046            @Override
047            public String getExportedData(ResultSet rs) throws Exception {
048                    StringBuilder sb = new StringBuilder();
049    
050                    Object[][] columns = getColumns();
051    
052                    for (int i = 0; i < columns.length; i++) {
053                            boolean last = false;
054    
055                            if ((i + 1) == columns.length) {
056                                    last = true;
057                            }
058    
059                            if (_upgradeColumns[i] == null) {
060                                    appendColumn(
061                                            sb, rs, (String)columns[i][0], (Integer)columns[i][1],
062                                            last);
063                            }
064                            else {
065                                    try {
066                                            Integer columnType = _upgradeColumns[i].getOldColumnType(
067                                                    (Integer)columns[i][1]);
068    
069                                            Object oldValue = getValue(
070                                                    rs, (String)columns[i][0], columnType);
071    
072                                            _upgradeColumns[i].setOldValue(oldValue);
073    
074                                            Object newValue = _upgradeColumns[i].getNewValue(oldValue);
075    
076                                            _upgradeColumns[i].setNewValue(newValue);
077    
078                                            appendColumn(sb, newValue, last);
079                                    }
080                                    catch (StagnantRowException sre) {
081                                            _upgradeColumns[i].setNewValue(null);
082    
083                                            throw new StagnantRowException(
084                                                    "Column " + columns[i][0] + " with value " +
085                                                            sre.getMessage(),
086                                                    sre);
087                                    }
088                            }
089                    }
090    
091                    return sb.toString();
092            }
093    
094            @Override
095            public void setColumn(
096                            PreparedStatement ps, int index, Integer type, String value)
097                    throws Exception {
098    
099                    if (_upgradeColumns[index] != null) {
100                            if (getCreateSQL() == null) {
101                                    type = _upgradeColumns[index].getOldColumnType(type);
102                            }
103                            else {
104                                    type = _upgradeColumns[index].getNewColumnType(type);
105                            }
106                    }
107    
108                    super.setColumn(ps, index, type, value);
109            }
110    
111            protected DefaultUpgradeTableImpl(
112                    String tableName, Object[][] columns, UpgradeColumn... upgradeColumns) {
113    
114                    super(tableName);
115    
116                    // Sort the column names to ensure they're sorted based on the
117                    // constructor's list of columns to upgrade. This is needed if you use
118                    // TempUpgradeColumnImpl and need to ensure a column's temporary value
119                    // is populated in the correct order.
120    
121                    columns = columns.clone();
122    
123                    List<String> sortedColumnNames = new ArrayList<>();
124    
125                    for (UpgradeColumn upgradeColumn : upgradeColumns) {
126                            getSortedColumnName(sortedColumnNames, upgradeColumn);
127                    }
128    
129                    if (!sortedColumnNames.isEmpty()) {
130                            Arrays.sort(columns, new ColumnsComparator(sortedColumnNames));
131                    }
132    
133                    setColumns(columns);
134    
135                    _upgradeColumns = new UpgradeColumn[columns.length];
136    
137                    for (UpgradeColumn upgradeColumn : upgradeColumns) {
138                            prepareUpgradeColumns(upgradeColumn);
139                    }
140            }
141    
142            protected void getSortedColumnName(
143                    List<String> sortedColumnNames, UpgradeColumn upgradeColumn) {
144    
145                    if (upgradeColumn == null) {
146                            return;
147                    }
148    
149                    String name = upgradeColumn.getName();
150    
151                    if (Validator.isNotNull(name)) {
152                            sortedColumnNames.add(name);
153                    }
154            }
155    
156            protected void prepareUpgradeColumns(UpgradeColumn upgradeColumn) {
157                    if (upgradeColumn == null) {
158                            return;
159                    }
160    
161                    Object[][] columns = getColumns();
162    
163                    for (int i = 0; i < columns.length; i++) {
164                            String name = (String)columns[i][0];
165    
166                            if (upgradeColumn.isApplicable(name)) {
167                                    _upgradeColumns[i] = upgradeColumn;
168                            }
169                    }
170            }
171    
172            private final UpgradeColumn[] _upgradeColumns;
173    
174    }