001    /**
002     * Copyright (c) 2000-2012 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            @Override
031            protected void doUpgrade() throws Exception {
032    
033                    // LEP-5761
034    
035                    while (getMessageIdsCount() > 0) {
036                            updateMessage();
037                    }
038            }
039    
040            protected long getMessageIdsCount() throws Exception {
041                    Connection con = null;
042                    PreparedStatement ps = null;
043                    ResultSet rs = null;
044    
045                    try {
046                            con = DataAccess.getConnection();
047    
048                            StringBundler sb = new StringBundler(7);
049    
050                            sb.append("select count(*) from ");
051                            sb.append("MBMessage childMessage ");
052                            sb.append("inner join MBMessage parentMessage on ");
053                            sb.append("childMessage.parentMessageId = ");
054                            sb.append("parentMessage.messageId where ");
055                            sb.append("parentMessage.categoryId != childMessage.categoryId ");
056                            sb.append("or parentMessage.threadId != childMessage.threadId");
057    
058                            String sql = sb.toString();
059    
060                            ps = con.prepareStatement(sql);
061    
062                            rs = ps.executeQuery();
063    
064                            while (rs.next()) {
065                                    return rs.getLong(1);
066                            }
067    
068                            return 0;
069                    }
070                    finally {
071                            DataAccess.cleanUp(con, ps, rs);
072                    }
073            }
074    
075            protected void updateMessage() throws Exception {
076                    Connection con = null;
077                    PreparedStatement ps = null;
078                    ResultSet rs = null;
079    
080                    try {
081                            con = DataAccess.getConnection();
082    
083                            StringBundler sb = new StringBundler(8);
084    
085                            sb.append("select childMessage.messageId, ");
086                            sb.append("parentMessage.categoryId, parentMessage.threadId ");
087                            sb.append("from MBMessage childMessage ");
088                            sb.append("inner join MBMessage parentMessage on ");
089                            sb.append("childMessage.parentMessageId = ");
090                            sb.append("parentMessage.messageId where ");
091                            sb.append("parentMessage.categoryId != childMessage.categoryId ");
092                            sb.append("or parentMessage.threadId != childMessage.threadId");
093    
094                            String sql = sb.toString();
095    
096                            ps = con.prepareStatement(sql);
097    
098                            rs = ps.executeQuery();
099    
100                            while (rs.next()) {
101                                    long messageId = rs.getLong(1);
102                                    long categoryId = rs.getLong(2);
103                                    long threadId = rs.getLong(3);
104    
105                                    runSQL(
106                                            "update MBMessage set categoryId = " + categoryId +
107                                                    ", threadId = " + threadId + " where messageId = " +
108                                                            messageId);
109                            }
110                    }
111                    finally {
112                            DataAccess.cleanUp(con, ps, rs);
113                    }
114            }
115    
116    }