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_1_0;
016    
017    import com.liferay.portal.kernel.dao.jdbc.DataAccess;
018    import com.liferay.portal.kernel.upgrade.UpgradeProcess;
019    import com.liferay.portal.kernel.util.StringBundler;
020    
021    import java.sql.Connection;
022    import java.sql.PreparedStatement;
023    import java.sql.ResultSet;
024    
025    /**
026     * @author Brian Wing Shun Chan
027     */
028    public class UpgradeMessageBoards extends UpgradeProcess {
029    
030            protected void doUpgrade() throws Exception {
031    
032                    // LEP-5761
033    
034                    while (getMessageIdsCount() > 0) {
035                            updateMessage();
036                    }
037            }
038    
039            protected long getMessageIdsCount() throws Exception {
040                    StringBundler sb = new StringBundler(6);
041    
042                    sb.append("select count(*) from ");
043                    sb.append("MBMessage childMessage ");
044                    sb.append("inner join MBMessage parentMessage on ");
045                    sb.append("childMessage.parentMessageId = parentMessage.messageId ");
046                    sb.append("where parentMessage.categoryId != childMessage.categoryId ");
047                    sb.append("or parentMessage.threadId != childMessage.threadId");
048    
049                    String sql = sb.toString();
050    
051                    Connection con = null;
052                    PreparedStatement ps = null;
053                    ResultSet rs = null;
054    
055                    try {
056                            con = DataAccess.getConnection();
057    
058                            ps = con.prepareStatement(sql);
059    
060                            rs = ps.executeQuery();
061    
062                            while (rs.next()) {
063                                    return rs.getLong(1);
064                            }
065    
066                            return 0;
067                    }
068                    finally {
069                            DataAccess.cleanUp(con, ps, rs);
070                    }
071            }
072    
073            protected void updateMessage() throws Exception {
074                    StringBundler sb = new StringBundler(7);
075    
076                    sb.append("select childMessage.messageId, parentMessage.categoryId, ");
077                    sb.append("parentMessage.threadId ");
078                    sb.append("from MBMessage childMessage ");
079                    sb.append("inner join MBMessage parentMessage on ");
080                    sb.append("childMessage.parentMessageId = parentMessage.messageId ");
081                    sb.append("where parentMessage.categoryId != childMessage.categoryId ");
082                    sb.append("or parentMessage.threadId != childMessage.threadId");
083    
084                    String sql = sb.toString();
085    
086                    Connection con = null;
087                    PreparedStatement ps = null;
088                    ResultSet rs = null;
089    
090                    try {
091                            con = DataAccess.getConnection();
092    
093                            ps = con.prepareStatement(sql);
094    
095                            rs = ps.executeQuery();
096    
097                            while (rs.next()) {
098                                    long messageId = rs.getLong(1);
099                                    long categoryId = rs.getLong(2);
100                                    long threadId = rs.getLong(3);
101    
102                                    runSQL(
103                                            "update MBMessage set categoryId = " + categoryId +
104                                                    ", threadId = " + threadId + " where messageId = " +
105                                                            messageId);
106                            }
107                    }
108                    finally {
109                            DataAccess.cleanUp(con, ps, rs);
110                    }
111            }
112    
113    }