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_2_3.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   */
36  public abstract class DependencyManager {
37  
38      public void setColumns(Object[][] columns) {
39          this.columns = columns;
40      }
41  
42      public void setExtraColumns(Object[][] extraColumns) {
43          this.extraColumns = extraColumns;
44      }
45  
46      public void setPrimaryKeyName(String primaryKeyName) {
47          this.primaryKeyName = primaryKeyName;
48      }
49  
50      public void setTableName(String tableName) {
51          this.tableName = tableName;
52      }
53  
54      public void update(long newPrimaryKeyValue) throws Exception {
55          update(0, null, null, newPrimaryKeyValue, null, null);
56      }
57  
58      public abstract void update(
59              long oldPrimaryKeyValue, Object[] oldColumnValues,
60              Object[] oldExtraColumnValues, long newPrimaryKeyValue,
61              Object[] newColumnValues, Object[] newExtraColumnValues)
62          throws Exception;
63  
64      protected void deleteDuplicateData(String tableName, long primaryKeyValue)
65          throws Exception {
66  
67          deleteDuplicateData(tableName, primaryKeyName, primaryKeyValue);
68      }
69  
70      protected void deleteDuplicateData(
71              String tableName, String columnName, long columnValue)
72          throws Exception {
73  
74          Connection con = null;
75          PreparedStatement ps = null;
76  
77          try {
78              con = DataAccess.getConnection();
79  
80              StringBuilder sb = new StringBuilder();
81  
82              sb.append("delete from ");
83              sb.append(tableName);
84              sb.append(" where ");
85              sb.append(columnName);
86              sb.append(" = ?");
87  
88              String sql = sb.toString();
89  
90              ps = con.prepareStatement(sql);
91  
92              ps.setLong(1, columnValue);
93  
94              ps.executeUpdate();
95          }
96          finally {
97              DataAccess.cleanUp(con, ps);
98          }
99      }
100 
101     protected void updateDuplicateData(
102             String tableName, long oldPrimaryKeyValue, long newPrimaryKeyValue)
103         throws Exception {
104 
105         updateDuplicateData(
106             tableName, primaryKeyName, oldPrimaryKeyValue, newPrimaryKeyValue);
107     }
108 
109     protected void updateDuplicateData(
110             String tableName, String columnName, long oldColumnValue,
111             long newColumnValue)
112         throws Exception {
113 
114         Connection con = null;
115         PreparedStatement ps = null;
116 
117         try {
118             con = DataAccess.getConnection();
119 
120             StringBuilder sb = new StringBuilder();
121 
122             sb.append("update ");
123             sb.append(tableName);
124             sb.append(" set ");
125             sb.append(columnName);
126             sb.append(" = ? where ");
127             sb.append(columnName);
128             sb.append(" = ?");
129 
130             String sql = sb.toString();
131 
132             ps = con.prepareStatement(sql);
133 
134             ps.setLong(1, newColumnValue);
135             ps.setLong(2, oldColumnValue);
136 
137             ps.executeUpdate();
138         }
139         finally {
140             DataAccess.cleanUp(con, ps);
141         }
142     }
143 
144     protected Object[][] columns;
145     protected Object[][] extraColumns;
146     protected String primaryKeyName;
147     protected String tableName;
148 
149 }