001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.upgrade.util;
022:
023: import com.liferay.portal.kernel.util.StringMaker;
024: import com.liferay.portal.kernel.util.Validator;
025: import com.liferay.portal.tools.comparator.ColumnsComparator;
026: import com.liferay.portal.upgrade.StagnantRowException;
027:
028: import java.sql.PreparedStatement;
029: import java.sql.ResultSet;
030:
031: import java.util.ArrayList;
032: import java.util.Arrays;
033: import java.util.List;
034:
035: /**
036: * <a href="DefaultUpgradeTableImpl.java.html"><b><i>View Source</i></b></a>
037: *
038: * @author Alexander Chow
039: * @author Bruno Farache
040: *
041: */
042: public class DefaultUpgradeTableImpl extends BaseUpgradeTableImpl
043: implements UpgradeTable {
044:
045: public DefaultUpgradeTableImpl(String tableName, Object[][] columns) {
046: this (tableName, columns, null);
047: }
048:
049: public DefaultUpgradeTableImpl(String tableName,
050: Object[][] columns, UpgradeColumn upgradeColumn1) {
051:
052: this (tableName, columns, upgradeColumn1, null);
053: }
054:
055: public DefaultUpgradeTableImpl(String tableName,
056: Object[][] columns, UpgradeColumn upgradeColumn1,
057: UpgradeColumn upgradeColumn2) {
058:
059: this (tableName, columns, upgradeColumn1, upgradeColumn2, null);
060: }
061:
062: public DefaultUpgradeTableImpl(String tableName,
063: Object[][] columns, UpgradeColumn upgradeColumn1,
064: UpgradeColumn upgradeColumn2, UpgradeColumn upgradeColumn3) {
065:
066: this (tableName, columns, upgradeColumn1, upgradeColumn2,
067: upgradeColumn3, null);
068: }
069:
070: public DefaultUpgradeTableImpl(String tableName,
071: Object[][] columns, UpgradeColumn upgradeColumn1,
072: UpgradeColumn upgradeColumn2, UpgradeColumn upgradeColumn3,
073: UpgradeColumn upgradeColumn4) {
074:
075: this (tableName, columns, upgradeColumn1, upgradeColumn2,
076: upgradeColumn3, upgradeColumn4, null);
077: }
078:
079: public DefaultUpgradeTableImpl(String tableName,
080: Object[][] columns, UpgradeColumn upgradeColumn1,
081: UpgradeColumn upgradeColumn2, UpgradeColumn upgradeColumn3,
082: UpgradeColumn upgradeColumn4, UpgradeColumn upgradeColumn5) {
083:
084: this (tableName, columns, upgradeColumn1, upgradeColumn2,
085: upgradeColumn3, upgradeColumn4, upgradeColumn5, null);
086: }
087:
088: public DefaultUpgradeTableImpl(String tableName,
089: Object[][] columns, UpgradeColumn upgradeColumn1,
090: UpgradeColumn upgradeColumn2, UpgradeColumn upgradeColumn3,
091: UpgradeColumn upgradeColumn4, UpgradeColumn upgradeColumn5,
092: UpgradeColumn upgradeColumn6) {
093:
094: this (tableName, columns, upgradeColumn1, upgradeColumn2,
095: upgradeColumn3, upgradeColumn4, upgradeColumn5,
096: upgradeColumn6, null);
097: }
098:
099: public DefaultUpgradeTableImpl(String tableName,
100: Object[][] columns, UpgradeColumn upgradeColumn1,
101: UpgradeColumn upgradeColumn2, UpgradeColumn upgradeColumn3,
102: UpgradeColumn upgradeColumn4, UpgradeColumn upgradeColumn5,
103: UpgradeColumn upgradeColumn6, UpgradeColumn upgradeColumn7) {
104:
105: this (tableName, columns, upgradeColumn1, upgradeColumn2,
106: upgradeColumn3, upgradeColumn4, upgradeColumn5,
107: upgradeColumn6, upgradeColumn7, null);
108: }
109:
110: public DefaultUpgradeTableImpl(String tableName,
111: Object[][] columns, UpgradeColumn upgradeColumn1,
112: UpgradeColumn upgradeColumn2, UpgradeColumn upgradeColumn3,
113: UpgradeColumn upgradeColumn4, UpgradeColumn upgradeColumn5,
114: UpgradeColumn upgradeColumn6, UpgradeColumn upgradeColumn7,
115: UpgradeColumn upgradeColumn8) {
116:
117: this (tableName, columns, upgradeColumn1, upgradeColumn2,
118: upgradeColumn3, upgradeColumn4, upgradeColumn5,
119: upgradeColumn6, upgradeColumn7, upgradeColumn8, null);
120: }
121:
122: public DefaultUpgradeTableImpl(String tableName,
123: Object[][] columns, UpgradeColumn upgradeColumn1,
124: UpgradeColumn upgradeColumn2, UpgradeColumn upgradeColumn3,
125: UpgradeColumn upgradeColumn4, UpgradeColumn upgradeColumn5,
126: UpgradeColumn upgradeColumn6, UpgradeColumn upgradeColumn7,
127: UpgradeColumn upgradeColumn8, UpgradeColumn upgradeColumn9) {
128:
129: this (tableName, columns, upgradeColumn1, upgradeColumn2,
130: upgradeColumn3, upgradeColumn4, upgradeColumn5,
131: upgradeColumn6, upgradeColumn7, upgradeColumn8,
132: upgradeColumn9, null);
133: }
134:
135: public DefaultUpgradeTableImpl(String tableName,
136: Object[][] columns, UpgradeColumn upgradeColumn1,
137: UpgradeColumn upgradeColumn2, UpgradeColumn upgradeColumn3,
138: UpgradeColumn upgradeColumn4, UpgradeColumn upgradeColumn5,
139: UpgradeColumn upgradeColumn6, UpgradeColumn upgradeColumn7,
140: UpgradeColumn upgradeColumn8, UpgradeColumn upgradeColumn9,
141: UpgradeColumn upgradeColumn10) {
142:
143: super (tableName);
144:
145: // Sort the column names to ensure they're sorted based on the
146: // constructor's list of columns to upgrade. This is needed if you
147: // use TempUpgradeColumnImpl and need to ensure a column's temporary
148: // value is populated in the correct order.
149:
150: columns = (Object[][]) columns.clone();
151:
152: List sortedColumnNames = new ArrayList();
153:
154: getSortedColumnName(sortedColumnNames, upgradeColumn1);
155: getSortedColumnName(sortedColumnNames, upgradeColumn2);
156: getSortedColumnName(sortedColumnNames, upgradeColumn3);
157: getSortedColumnName(sortedColumnNames, upgradeColumn4);
158: getSortedColumnName(sortedColumnNames, upgradeColumn5);
159: getSortedColumnName(sortedColumnNames, upgradeColumn6);
160: getSortedColumnName(sortedColumnNames, upgradeColumn7);
161: getSortedColumnName(sortedColumnNames, upgradeColumn8);
162: getSortedColumnName(sortedColumnNames, upgradeColumn9);
163: getSortedColumnName(sortedColumnNames, upgradeColumn10);
164:
165: if (sortedColumnNames.size() > 0) {
166: Arrays.sort(columns, new ColumnsComparator(
167: sortedColumnNames));
168: }
169:
170: setColumns(columns);
171:
172: _upgradeColumns = new UpgradeColumn[columns.length];
173:
174: prepareUpgradeColumns(upgradeColumn1);
175: prepareUpgradeColumns(upgradeColumn2);
176: prepareUpgradeColumns(upgradeColumn3);
177: prepareUpgradeColumns(upgradeColumn4);
178: prepareUpgradeColumns(upgradeColumn5);
179: prepareUpgradeColumns(upgradeColumn6);
180: prepareUpgradeColumns(upgradeColumn7);
181: prepareUpgradeColumns(upgradeColumn8);
182: prepareUpgradeColumns(upgradeColumn9);
183: prepareUpgradeColumns(upgradeColumn10);
184: }
185:
186: public String getExportedData(ResultSet rs) throws Exception {
187: StringMaker sm = new StringMaker();
188:
189: Object[][] columns = getColumns();
190:
191: for (int i = 0; i < columns.length; i++) {
192: boolean last = false;
193:
194: if ((i + 1) == columns.length) {
195: last = true;
196: }
197:
198: if (_upgradeColumns[i] == null) {
199: appendColumn(sm, rs, (String) columns[i][0],
200: (Integer) columns[i][1], last);
201: } else {
202: try {
203: Integer columnType = _upgradeColumns[i]
204: .getOldColumnType((Integer) columns[i][1]);
205:
206: Object oldValue = getValue(rs,
207: (String) columns[i][0], columnType);
208:
209: _upgradeColumns[i].setOldValue(oldValue);
210:
211: Object newValue = _upgradeColumns[i]
212: .getNewValue(oldValue);
213:
214: _upgradeColumns[i].setNewValue(newValue);
215:
216: appendColumn(sm, newValue, last);
217: } catch (StagnantRowException sre) {
218: _upgradeColumns[i].setNewValue(null);
219:
220: throw new StagnantRowException("Column "
221: + columns[i][0] + " with value "
222: + sre.getMessage(), sre);
223: }
224: }
225: }
226:
227: return sm.toString();
228: }
229:
230: public void setColumn(PreparedStatement ps, int index,
231: Integer type, String value) throws Exception {
232:
233: if (_upgradeColumns[index] != null) {
234: if (getCreateSQL() == null) {
235: type = _upgradeColumns[index].getOldColumnType(type);
236: } else {
237: type = _upgradeColumns[index].getNewColumnType(type);
238: }
239: }
240:
241: super .setColumn(ps, index, type, value);
242: }
243:
244: protected void getSortedColumnName(List sortedColumnNames,
245: UpgradeColumn upgradeColumn) {
246:
247: if (upgradeColumn == null) {
248: return;
249: }
250:
251: String name = upgradeColumn.getName();
252:
253: if (Validator.isNotNull(name)) {
254: sortedColumnNames.add(name);
255: }
256: }
257:
258: protected void prepareUpgradeColumns(UpgradeColumn upgradeColumn) {
259: if (upgradeColumn == null) {
260: return;
261: }
262:
263: Object[][] columns = getColumns();
264:
265: for (int i = 0; i < columns.length; i++) {
266: String name = (String) columns[i][0];
267:
268: if (upgradeColumn.isApplicable(name)) {
269: _upgradeColumns[i] = upgradeColumn;
270: }
271: }
272: }
273:
274: private UpgradeColumn[] _upgradeColumns;
275:
276: }
|