001    /**
002     * Copyright (c) 2000-2013 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_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    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            @Override
030            public void update(
031                            long oldPrimaryKeyValue, Object[] oldColumnValues,
032                            Object[] oldExtraColumnValues, long newPrimaryKeyValue,
033                            Object[] newColumnValues, Object[] newExtraColumnValues)
034                    throws Exception {
035    
036                    long threadId = 0;
037    
038                    for (int i = 0; i < columns.length; i++) {
039                            if (columns[i][0].equals("threadId")) {
040                                    threadId = (Long)newColumnValues[i];
041                            }
042                    }
043    
044                    if ((threadId == 0) && (extraColumns != null)) {
045                            for (int i = 0; i < extraColumns.length; i++) {
046                                    if (extraColumns[i][0].equals("threadId")) {
047                                            threadId = (Long)newExtraColumnValues[i];
048                                    }
049                            }
050                    }
051    
052                    if (isDuplicateThread(threadId)) {
053                            deleteDuplicateData("MBMessage", "threadId", threadId);
054                            deleteDuplicateData("MBMessageFlag", "threadId", threadId);
055                            deleteDuplicateData("MBThread", "threadId", threadId);
056                    }
057            }
058    
059            protected boolean isDuplicateThread(long threadId) throws Exception {
060                    Connection con = null;
061                    PreparedStatement ps = null;
062                    ResultSet rs = null;
063    
064                    try {
065                            con = DataAccess.getUpgradeOptimizedConnection();
066    
067                            ps = con.prepareStatement(
068                                    "select count(*) from MBDiscussion where threadId = ?");
069    
070                            ps.setLong(1, threadId);
071    
072                            rs = ps.executeQuery();
073    
074                            while (rs.next()) {
075                                    int count = rs.getInt(1);
076    
077                                    if (count > 0) {
078                                            return false;
079                                    }
080                            }
081    
082                            return true;
083                    }
084                    finally {
085                            DataAccess.cleanUp(con, ps, rs);
086                    }
087            }
088    
089    }