1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.upgrade.util;
24  
25  import com.liferay.portal.kernel.upgrade.StagnantRowException;
26  import com.liferay.portal.kernel.upgrade.util.UpgradeColumn;
27  import com.liferay.portal.kernel.upgrade.util.UpgradeTable;
28  import com.liferay.portal.kernel.util.Validator;
29  import com.liferay.portal.tools.comparator.ColumnsComparator;
30  
31  import java.sql.PreparedStatement;
32  import java.sql.ResultSet;
33  
34  import java.util.ArrayList;
35  import java.util.Arrays;
36  import java.util.List;
37  
38  /**
39   * <a href="DefaultUpgradeTableImpl.java.html"><b><i>View Source</i></b></a>
40   *
41   * @author Alexander Chow
42   * @author Bruno Farache
43   */
44  public class DefaultUpgradeTableImpl
45      extends BaseUpgradeTableImpl implements UpgradeTable {
46  
47      public String getExportedData(ResultSet rs) throws Exception {
48          StringBuilder sb = new StringBuilder();
49  
50          Object[][] columns = getColumns();
51  
52          for (int i = 0; i < columns.length; i++) {
53              boolean last = false;
54  
55              if ((i + 1) == columns.length) {
56                  last = true;
57              }
58  
59              if (_upgradeColumns[i] == null) {
60                  appendColumn(
61                      sb, rs, (String)columns[i][0], (Integer)columns[i][1],
62                      last);
63              }
64              else {
65                  try {
66                      Integer columnType = _upgradeColumns[i].getOldColumnType(
67                          (Integer)columns[i][1]);
68  
69                      Object oldValue = getValue(
70                          rs, (String)columns[i][0], columnType);
71  
72                      _upgradeColumns[i].setOldValue(oldValue);
73  
74                      Object newValue = _upgradeColumns[i].getNewValue(oldValue);
75  
76                      _upgradeColumns[i].setNewValue(newValue);
77  
78                      appendColumn(sb, newValue, last);
79                  }
80                  catch (StagnantRowException sre) {
81                      _upgradeColumns[i].setNewValue(null);
82  
83                      throw new StagnantRowException(
84                          "Column " + columns[i][0] + " with value " +
85                              sre.getMessage(),
86                          sre);
87                  }
88              }
89          }
90  
91          return sb.toString();
92      }
93  
94      public void setColumn(
95              PreparedStatement ps, int index, Integer type, String value)
96          throws Exception {
97  
98          if (_upgradeColumns[index] != null) {
99              if (getCreateSQL() == null) {
100                 type = _upgradeColumns[index].getOldColumnType(type);
101             }
102             else {
103                 type = _upgradeColumns[index].getNewColumnType(type);
104             }
105         }
106 
107         super.setColumn(ps, index, type, value);
108     }
109 
110     protected DefaultUpgradeTableImpl(
111         String tableName, Object[][] columns, UpgradeColumn... upgradeColumns) {
112 
113         super(tableName);
114 
115         // Sort the column names to ensure they're sorted based on the
116         // constructor's list of columns to upgrade. This is needed if you
117         // use TempUpgradeColumnImpl and need to ensure a column's temporary
118         // value is populated in the correct order.
119 
120         columns = columns.clone();
121 
122         List<String> sortedColumnNames = new ArrayList<String>();
123 
124         for (UpgradeColumn upgradeColumn : upgradeColumns) {
125             getSortedColumnName(sortedColumnNames, upgradeColumn);
126         }
127 
128         if (sortedColumnNames.size() > 0) {
129             Arrays.sort(columns, new ColumnsComparator(sortedColumnNames));
130         }
131 
132         setColumns(columns);
133 
134         _upgradeColumns = new UpgradeColumn[columns.length];
135 
136         for (UpgradeColumn upgradeColumn : upgradeColumns) {
137             prepareUpgradeColumns(upgradeColumn);
138         }
139     }
140 
141     protected void getSortedColumnName(
142         List<String> sortedColumnNames, UpgradeColumn upgradeColumn) {
143 
144         if (upgradeColumn == null) {
145             return;
146         }
147 
148         String name = upgradeColumn.getName();
149 
150         if (Validator.isNotNull(name)) {
151             sortedColumnNames.add(name);
152         }
153     }
154 
155     protected void prepareUpgradeColumns(UpgradeColumn upgradeColumn) {
156         if (upgradeColumn == null) {
157             return;
158         }
159 
160         Object[][] columns = getColumns();
161 
162         for (int i = 0; i < columns.length; i++) {
163             String name = (String)columns[i][0];
164 
165             if (upgradeColumn.isApplicable(name)) {
166                 _upgradeColumns[i] = upgradeColumn;
167             }
168         }
169     }
170 
171     private UpgradeColumn[] _upgradeColumns;
172 
173 }