1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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.v5_1_5.util;
24  
25  import com.liferay.portal.kernel.dao.jdbc.DataAccess;
26  
27  import java.sql.Connection;
28  import java.sql.PreparedStatement;
29  
30  /**
31   * <a href="DependencyManager.java.html"><b><i>View Source</i></b></a>
32   *
33   * @author Brian Wing Shun Chan
34   */
35  public abstract class DependencyManager {
36  
37      public void setColumns(Object[][] columns) {
38          this.columns = columns;
39      }
40  
41      public void setExtraColumns(Object[][] extraColumns) {
42          this.extraColumns = extraColumns;
43      }
44  
45      public void setPrimaryKeyName(String primaryKeyName) {
46          this.primaryKeyName = primaryKeyName;
47      }
48  
49      public void setTableName(String tableName) {
50          this.tableName = tableName;
51      }
52  
53      public void update(long newPrimaryKeyValue) throws Exception {
54          update(0, null, null, newPrimaryKeyValue, null, null);
55      }
56  
57      public abstract void update(
58              long oldPrimaryKeyValue, Object[] oldColumnValues,
59              Object[] oldExtraColumnValues, long newPrimaryKeyValue,
60              Object[] newColumnValues, Object[] newExtraColumnValues)
61          throws Exception;
62  
63      protected void deleteDuplicateData(String tableName, long primaryKeyValue)
64          throws Exception {
65  
66          deleteDuplicateData(tableName, primaryKeyName, primaryKeyValue);
67      }
68  
69      protected void deleteDuplicateData(
70              String tableName, String columnName, long columnValue)
71          throws Exception {
72  
73          Connection con = null;
74          PreparedStatement ps = null;
75  
76          try {
77              con = DataAccess.getConnection();
78  
79              StringBuilder sb = new StringBuilder();
80  
81              sb.append("delete from ");
82              sb.append(tableName);
83              sb.append(" where ");
84              sb.append(columnName);
85              sb.append(" = ?");
86  
87              String sql = sb.toString();
88  
89              ps = con.prepareStatement(sql);
90  
91              ps.setLong(1, columnValue);
92  
93              ps.executeUpdate();
94          }
95          finally {
96              DataAccess.cleanUp(con, ps);
97          }
98      }
99  
100     protected void updateDuplicateData(
101             String tableName, long oldPrimaryKeyValue, long newPrimaryKeyValue)
102         throws Exception {
103 
104         updateDuplicateData(
105             tableName, primaryKeyName, oldPrimaryKeyValue, newPrimaryKeyValue);
106     }
107 
108     protected void updateDuplicateData(
109             String tableName, String columnName, long oldColumnValue,
110             long newColumnValue)
111         throws Exception {
112 
113         Connection con = null;
114         PreparedStatement ps = null;
115 
116         try {
117             con = DataAccess.getConnection();
118 
119             StringBuilder sb = new StringBuilder();
120 
121             sb.append("update ");
122             sb.append(tableName);
123             sb.append(" set ");
124             sb.append(columnName);
125             sb.append(" = ? where ");
126             sb.append(columnName);
127             sb.append(" = ?");
128 
129             String sql = sb.toString();
130 
131             ps = con.prepareStatement(sql);
132 
133             ps.setLong(1, newColumnValue);
134             ps.setLong(2, oldColumnValue);
135 
136             ps.executeUpdate();
137         }
138         finally {
139             DataAccess.cleanUp(con, ps);
140         }
141     }
142 
143     protected Object[][] columns;
144     protected Object[][] extraColumns;
145     protected String primaryKeyName;
146     protected String tableName;
147 
148 }