1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.upgrade.util;
16  
17  import com.liferay.portal.kernel.upgrade.StagnantRowException;
18  import com.liferay.portal.kernel.upgrade.util.UpgradeColumn;
19  import com.liferay.portal.kernel.upgrade.util.UpgradeTable;
20  import com.liferay.portal.kernel.util.Validator;
21  import com.liferay.portal.tools.comparator.ColumnsComparator;
22  
23  import java.sql.PreparedStatement;
24  import java.sql.ResultSet;
25  
26  import java.util.ArrayList;
27  import java.util.Arrays;
28  import java.util.List;
29  
30  /**
31   * <a href="DefaultUpgradeTableImpl.java.html"><b><i>View Source</i></b></a>
32   *
33   * @author Alexander Chow
34   * @author Bruno Farache
35   */
36  public class DefaultUpgradeTableImpl
37      extends BaseUpgradeTableImpl implements UpgradeTable {
38  
39      public String getExportedData(ResultSet rs) throws Exception {
40          StringBuilder sb = new StringBuilder();
41  
42          Object[][] columns = getColumns();
43  
44          for (int i = 0; i < columns.length; i++) {
45              boolean last = false;
46  
47              if ((i + 1) == columns.length) {
48                  last = true;
49              }
50  
51              if (_upgradeColumns[i] == null) {
52                  appendColumn(
53                      sb, rs, (String)columns[i][0], (Integer)columns[i][1],
54                      last);
55              }
56              else {
57                  try {
58                      Integer columnType = _upgradeColumns[i].getOldColumnType(
59                          (Integer)columns[i][1]);
60  
61                      Object oldValue = getValue(
62                          rs, (String)columns[i][0], columnType);
63  
64                      _upgradeColumns[i].setOldValue(oldValue);
65  
66                      Object newValue = _upgradeColumns[i].getNewValue(oldValue);
67  
68                      _upgradeColumns[i].setNewValue(newValue);
69  
70                      appendColumn(sb, newValue, last);
71                  }
72                  catch (StagnantRowException sre) {
73                      _upgradeColumns[i].setNewValue(null);
74  
75                      throw new StagnantRowException(
76                          "Column " + columns[i][0] + " with value " +
77                              sre.getMessage(),
78                          sre);
79                  }
80              }
81          }
82  
83          return sb.toString();
84      }
85  
86      public void setColumn(
87              PreparedStatement ps, int index, Integer type, String value)
88          throws Exception {
89  
90          if (_upgradeColumns[index] != null) {
91              if (getCreateSQL() == null) {
92                  type = _upgradeColumns[index].getOldColumnType(type);
93              }
94              else {
95                  type = _upgradeColumns[index].getNewColumnType(type);
96              }
97          }
98  
99          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
109         // use TempUpgradeColumnImpl and need to ensure a column's temporary
110         // value 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 }