001    /**
002     * Copyright (c) 2000-2010 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_2_3.util;
016    
017    import com.liferay.portal.kernel.dao.jdbc.DataAccess;
018    
019    import java.sql.Connection;
020    import java.sql.PreparedStatement;
021    import java.sql.ResultSet;
022    
023    /**
024     * @author Brian Wing Shun Chan
025     * @author Amos Fong
026     */
027    public class MBDiscussionDependencyManager extends DependencyManager {
028    
029            public void update(
030                            long oldPrimaryKeyValue, Object[] oldColumnValues,
031                            Object[] oldExtraColumnValues, long newPrimaryKeyValue,
032                            Object[] newColumnValues, Object[] newExtraColumnValues)
033                    throws Exception {
034    
035                    long threadId = 0;
036    
037                    for (int i = 0; i < columns.length; i++) {
038                            if (columns[i][0].equals("threadId")) {
039                                    threadId = (Long)newColumnValues[i];
040                            }
041                    }
042    
043                    if ((threadId == 0) && (extraColumns != null)) {
044                            for (int i = 0; i < extraColumns.length; i++) {
045                                    if (extraColumns[i][0].equals("threadId")) {
046                                            threadId = (Long)newExtraColumnValues[i];
047                                    }
048                            }
049                    }
050    
051                    if (isDuplicateThread(threadId)) {
052                            deleteDuplicateData("MBMessage", "threadId", threadId);
053                            deleteDuplicateData("MBMessageFlag", "threadId", threadId);
054                            deleteDuplicateData("MBThread", "threadId", threadId);
055                    }
056            }
057    
058            protected boolean isDuplicateThread(long threadId) throws Exception {
059                    Connection con = null;
060                    PreparedStatement ps = null;
061                    ResultSet rs = null;
062    
063                    try {
064                            con = DataAccess.getConnection();
065    
066                            ps = con.prepareStatement(
067                                    "select count(*) from MBDiscussion where threadId = ?");
068    
069                            ps.setLong(1, threadId);
070    
071                            rs = ps.executeQuery();
072    
073                            while (rs.next()) {
074                                    long count = rs.getLong(1);
075    
076                                    if (count > 0) {
077                                            return false;
078                                    }
079                            }
080    
081                            return true;
082                    }
083                    finally {
084                            DataAccess.cleanUp(con, ps, rs);
085                    }
086            }
087    
088    }