1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.upgrade.v5_1_5.util;
16  
17  import com.liferay.portal.kernel.dao.jdbc.DataAccess;
18  
19  import java.sql.Connection;
20  import java.sql.PreparedStatement;
21  
22  /**
23   * <a href="DependencyManager.java.html"><b><i>View Source</i></b></a>
24   *
25   * @author Brian Wing Shun Chan
26   */
27  public abstract class DependencyManager {
28  
29      public void setColumns(Object[][] columns) {
30          this.columns = columns;
31      }
32  
33      public void setExtraColumns(Object[][] extraColumns) {
34          this.extraColumns = extraColumns;
35      }
36  
37      public void setPrimaryKeyName(String primaryKeyName) {
38          this.primaryKeyName = primaryKeyName;
39      }
40  
41      public void setTableName(String tableName) {
42          this.tableName = tableName;
43      }
44  
45      public void update(long newPrimaryKeyValue) throws Exception {
46          update(0, null, null, newPrimaryKeyValue, null, null);
47      }
48  
49      public abstract void update(
50              long oldPrimaryKeyValue, Object[] oldColumnValues,
51              Object[] oldExtraColumnValues, long newPrimaryKeyValue,
52              Object[] newColumnValues, Object[] newExtraColumnValues)
53          throws Exception;
54  
55      protected void deleteDuplicateData(String tableName, long primaryKeyValue)
56          throws Exception {
57  
58          deleteDuplicateData(tableName, primaryKeyName, primaryKeyValue);
59      }
60  
61      protected void deleteDuplicateData(
62              String tableName, String columnName, long columnValue)
63          throws Exception {
64  
65          Connection con = null;
66          PreparedStatement ps = null;
67  
68          try {
69              con = DataAccess.getConnection();
70  
71              StringBuilder sb = new StringBuilder();
72  
73              sb.append("delete from ");
74              sb.append(tableName);
75              sb.append(" where ");
76              sb.append(columnName);
77              sb.append(" = ?");
78  
79              String sql = sb.toString();
80  
81              ps = con.prepareStatement(sql);
82  
83              ps.setLong(1, columnValue);
84  
85              ps.executeUpdate();
86          }
87          finally {
88              DataAccess.cleanUp(con, ps);
89          }
90      }
91  
92      protected void updateDuplicateData(
93              String tableName, long oldPrimaryKeyValue, long newPrimaryKeyValue)
94          throws Exception {
95  
96          updateDuplicateData(
97              tableName, primaryKeyName, oldPrimaryKeyValue, newPrimaryKeyValue);
98      }
99  
100     protected void updateDuplicateData(
101             String tableName, String columnName, long oldColumnValue,
102             long newColumnValue)
103         throws Exception {
104 
105         Connection con = null;
106         PreparedStatement ps = null;
107 
108         try {
109             con = DataAccess.getConnection();
110 
111             StringBuilder sb = new StringBuilder();
112 
113             sb.append("update ");
114             sb.append(tableName);
115             sb.append(" set ");
116             sb.append(columnName);
117             sb.append(" = ? where ");
118             sb.append(columnName);
119             sb.append(" = ?");
120 
121             String sql = sb.toString();
122 
123             ps = con.prepareStatement(sql);
124 
125             ps.setLong(1, newColumnValue);
126             ps.setLong(2, oldColumnValue);
127 
128             ps.executeUpdate();
129         }
130         finally {
131             DataAccess.cleanUp(con, ps);
132         }
133     }
134 
135     protected Object[][] columns;
136     protected Object[][] extraColumns;
137     protected String primaryKeyName;
138     protected String tableName;
139 
140 }