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