001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.upgrade.v5_1_5.util;
016    
017    import com.liferay.portal.kernel.dao.jdbc.DataAccess;
018    
019    import java.sql.Connection;
020    import java.sql.PreparedStatement;
021    
022    /**
023     * @author Brian Wing Shun Chan
024     */
025    public abstract class DependencyManager {
026    
027            public void setColumns(Object[][] columns) {
028                    this.columns = columns;
029            }
030    
031            public void setExtraColumns(Object[][] extraColumns) {
032                    this.extraColumns = extraColumns;
033            }
034    
035            public void setPrimaryKeyName(String primaryKeyName) {
036                    this.primaryKeyName = primaryKeyName;
037            }
038    
039            public void setTableName(String tableName) {
040                    this.tableName = tableName;
041            }
042    
043            public void update(long newPrimaryKeyValue) throws Exception {
044                    update(0, null, null, newPrimaryKeyValue, null, null);
045            }
046    
047            public abstract void update(
048                            long oldPrimaryKeyValue, Object[] oldColumnValues,
049                            Object[] oldExtraColumnValues, long newPrimaryKeyValue,
050                            Object[] newColumnValues, Object[] newExtraColumnValues)
051                    throws Exception;
052    
053            protected void deleteDuplicateData(String tableName, long primaryKeyValue)
054                    throws Exception {
055    
056                    deleteDuplicateData(tableName, primaryKeyName, primaryKeyValue);
057            }
058    
059            protected void deleteDuplicateData(
060                            String tableName, String columnName, long columnValue)
061                    throws Exception {
062    
063                    Connection con = null;
064                    PreparedStatement ps = null;
065    
066                    try {
067                            con = DataAccess.getUpgradeOptimizedConnection();
068    
069                            StringBuilder sb = new StringBuilder();
070    
071                            sb.append("delete from ");
072                            sb.append(tableName);
073                            sb.append(" where ");
074                            sb.append(columnName);
075                            sb.append(" = ?");
076    
077                            String sql = sb.toString();
078    
079                            ps = con.prepareStatement(sql);
080    
081                            ps.setLong(1, columnValue);
082    
083                            ps.executeUpdate();
084                    }
085                    finally {
086                            DataAccess.cleanUp(con, ps);
087                    }
088            }
089    
090            protected void updateDuplicateData(
091                            String tableName, long oldPrimaryKeyValue, long newPrimaryKeyValue)
092                    throws Exception {
093    
094                    updateDuplicateData(
095                            tableName, primaryKeyName, oldPrimaryKeyValue, newPrimaryKeyValue);
096            }
097    
098            protected void updateDuplicateData(
099                            String tableName, String columnName, long oldColumnValue,
100                            long newColumnValue)
101                    throws Exception {
102    
103                    Connection con = null;
104                    PreparedStatement ps = null;
105    
106                    try {
107                            con = DataAccess.getUpgradeOptimizedConnection();
108    
109                            StringBuilder sb = new StringBuilder();
110    
111                            sb.append("update ");
112                            sb.append(tableName);
113                            sb.append(" set ");
114                            sb.append(columnName);
115                            sb.append(" = ? where ");
116                            sb.append(columnName);
117                            sb.append(" = ?");
118    
119                            String sql = sb.toString();
120    
121                            ps = con.prepareStatement(sql);
122    
123                            ps.setLong(1, newColumnValue);
124                            ps.setLong(2, oldColumnValue);
125    
126                            ps.executeUpdate();
127                    }
128                    finally {
129                            DataAccess.cleanUp(con, ps);
130                    }
131            }
132    
133            protected Object[][] columns;
134            protected Object[][] extraColumns;
135            protected String primaryKeyName;
136            protected String tableName;
137    
138    }