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