001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.upgrade.v5_2_3.util;
016    
017    import com.liferay.portal.kernel.dao.jdbc.DataAccess;
018    import com.liferay.portal.kernel.util.StringBundler;
019    
020    import java.sql.Connection;
021    import java.sql.PreparedStatement;
022    
023    /**
024     * @author Brian Wing Shun Chan
025     */
026    public abstract class DependencyManager {
027    
028            public void setColumns(Object[][] columns) {
029                    this.columns = columns;
030            }
031    
032            public void setExtraColumns(Object[][] extraColumns) {
033                    this.extraColumns = extraColumns;
034            }
035    
036            public void setPrimaryKeyName(String primaryKeyName) {
037                    this.primaryKeyName = primaryKeyName;
038            }
039    
040            public void setTableName(String tableName) {
041                    this.tableName = tableName;
042            }
043    
044            public void update(long newPrimaryKeyValue) throws Exception {
045                    update(0, null, null, newPrimaryKeyValue, null, null);
046            }
047    
048            public abstract void update(
049                            long oldPrimaryKeyValue, Object[] oldColumnValues,
050                            Object[] oldExtraColumnValues, long newPrimaryKeyValue,
051                            Object[] newColumnValues, Object[] newExtraColumnValues)
052                    throws Exception;
053    
054            protected void deleteDuplicateData(String tableName, long primaryKeyValue)
055                    throws Exception {
056    
057                    deleteDuplicateData(tableName, primaryKeyName, primaryKeyValue);
058            }
059    
060            protected void deleteDuplicateData(
061                            String tableName, String columnName, long columnValue)
062                    throws Exception {
063    
064                    Connection con = null;
065                    PreparedStatement ps = null;
066    
067                    try {
068                            con = DataAccess.getConnection();
069    
070                            StringBundler sb = new StringBundler(5);
071    
072                            sb.append("delete from ");
073                            sb.append(tableName);
074                            sb.append(" where ");
075                            sb.append(columnName);
076                            sb.append(" = ?");
077    
078                            String sql = sb.toString();
079    
080                            ps = con.prepareStatement(sql);
081    
082                            ps.setLong(1, columnValue);
083    
084                            ps.executeUpdate();
085                    }
086                    finally {
087                            DataAccess.cleanUp(con, ps);
088                    }
089            }
090    
091            protected void updateDuplicateData(
092                            String tableName, long oldPrimaryKeyValue, long newPrimaryKeyValue)
093                    throws Exception {
094    
095                    updateDuplicateData(
096                            tableName, primaryKeyName, oldPrimaryKeyValue, newPrimaryKeyValue);
097            }
098    
099            protected void updateDuplicateData(
100                            String tableName, String columnName, long oldColumnValue,
101                            long newColumnValue)
102                    throws Exception {
103    
104                    Connection con = null;
105                    PreparedStatement ps = null;
106    
107                    try {
108                            con = DataAccess.getConnection();
109    
110                            StringBundler sb = new StringBundler(7);
111    
112                            sb.append("update ");
113                            sb.append(tableName);
114                            sb.append(" set ");
115                            sb.append(columnName);
116                            sb.append(" = ? where ");
117                            sb.append(columnName);
118                            sb.append(" = ?");
119    
120                            String sql = sb.toString();
121    
122                            ps = con.prepareStatement(sql);
123    
124                            ps.setLong(1, newColumnValue);
125                            ps.setLong(2, oldColumnValue);
126    
127                            ps.executeUpdate();
128                    }
129                    finally {
130                            DataAccess.cleanUp(con, ps);
131                    }
132            }
133    
134            protected Object[][] columns;
135            protected Object[][] extraColumns;
136            protected String primaryKeyName;
137            protected String tableName;
138    
139    }