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.v6_0_12_to_6_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    import java.sql.Timestamp;
025    
026    /**
027     * @author Shuyang Zhou
028     */
029    public class UpgradeMessageBoards extends UpgradeProcess {
030    
031            protected void addThreadFlag(
032                            long threadFlagId, long userId, long threadId,
033                            Timestamp modifiedDate)
034                    throws Exception {
035    
036                    Connection con = null;
037                    PreparedStatement ps = null;
038                    ResultSet rs = null;
039    
040                    try {
041                            con = DataAccess.getUpgradeOptimizedConnection();
042    
043                            ps = con.prepareStatement(
044                                    "insert into MBThreadFlag (threadFlagId, userId, " +
045                                            "modifiedDate, threadId) values (?, ?, ?, ?)");
046    
047                            ps.setLong(1, threadFlagId);
048                            ps.setLong(2, userId);
049                            ps.setTimestamp(3, modifiedDate);
050                            ps.setLong(4, threadId);
051    
052                            ps.executeUpdate();
053                    }
054                    finally {
055                            DataAccess.cleanUp(con, ps, rs);
056                    }
057            }
058    
059            @Override
060            protected void doUpgrade() throws Exception {
061                    updateMessage();
062                    updateThread();
063                    updateThreadFlag();
064            }
065    
066            protected void updateMessage() throws Exception {
067                    Connection con = null;
068                    PreparedStatement ps = null;
069                    ResultSet rs = null;
070    
071                    try {
072                            con = DataAccess.getUpgradeOptimizedConnection();
073    
074                            StringBundler sb = new StringBundler(4);
075    
076                            sb.append("select messageFlag.messageId as messageId from ");
077                            sb.append("MBMessageFlag messageFlag inner join MBMessage ");
078                            sb.append("message on messageFlag.messageId = message.messageId ");
079                            sb.append("where message.parentMessageId != 0 and flag = 3");
080    
081                            String sql = sb.toString();
082    
083                            ps = con.prepareStatement(sql);
084    
085                            rs = ps.executeQuery();
086    
087                            while (rs.next()) {
088                                    long messageId = rs.getLong("messageId");
089    
090                                    updateMessageAnswer(messageId, true);
091                            }
092                    }
093                    finally {
094                            DataAccess.cleanUp(con, ps, rs);
095                    }
096            }
097    
098            protected void updateMessageAnswer(long messageId, boolean answer)
099                    throws Exception {
100    
101                    Connection con = null;
102                    PreparedStatement ps = null;
103    
104                    try {
105                            con = DataAccess.getUpgradeOptimizedConnection();
106    
107                            ps = con.prepareStatement(
108                                    "update MBMessage set answer = ? where messageId = " +
109                                            messageId);
110    
111                            ps.setBoolean(1, answer);
112    
113                            ps.executeUpdate();
114                    }
115                    finally {
116                            DataAccess.cleanUp(con, ps);
117                    }
118            }
119    
120            protected void updateThread() throws Exception {
121                    Connection con = null;
122                    PreparedStatement ps = null;
123                    ResultSet rs = null;
124    
125                    try {
126                            con = DataAccess.getUpgradeOptimizedConnection();
127    
128                            ps = con.prepareStatement(
129                                    "select threadId from MBMessageFlag where flag = 2");
130    
131                            rs = ps.executeQuery();
132    
133                            while (rs.next()) {
134                                    long threadId = rs.getLong("threadId");
135    
136                                    updateThreadQuestion(threadId, true);
137                            }
138                    }
139                    finally {
140                            DataAccess.cleanUp(con, ps, rs);
141                    }
142    
143                    try {
144                            con = DataAccess.getUpgradeOptimizedConnection();
145    
146                            StringBundler sb = new StringBundler(4);
147    
148                            sb.append("select messageFlag.threadId as threadId from ");
149                            sb.append("MBMessageFlag messageFlag inner join MBMessage ");
150                            sb.append("message on messageFlag.messageId = message.messageId ");
151                            sb.append("where message.parentMessageId = 0 and flag = 3");
152    
153                            ps = con.prepareStatement(sb.toString());
154    
155                            rs = ps.executeQuery();
156    
157                            while (rs.next()) {
158                                    long threadId = rs.getLong("threadId");
159    
160                                    updateThreadQuestion(threadId, true);
161                            }
162                    }
163                    finally {
164                            DataAccess.cleanUp(con, ps, rs);
165                    }
166            }
167    
168            protected void updateThreadFlag() throws Exception {
169                    Connection con = null;
170                    PreparedStatement ps = null;
171                    ResultSet rs = null;
172    
173                    try {
174                            con = DataAccess.getUpgradeOptimizedConnection();
175    
176                            ps = con.prepareStatement(
177                                    "select userId, threadId, modifiedDate from MBMessageFlag " +
178                                            "where flag = 1");
179    
180                            rs = ps.executeQuery();
181    
182                            while (rs.next()) {
183                                    long userId = rs.getLong("userId");
184                                    long threadId = rs.getLong("threadId");
185                                    Timestamp modifiedDate = rs.getTimestamp("modifiedDate");
186    
187                                    addThreadFlag(increment(), userId, threadId, modifiedDate);
188                            }
189                    }
190                    finally {
191                            DataAccess.cleanUp(con, ps, rs);
192                    }
193    
194                    runSQL("drop table MBMessageFlag");
195            }
196    
197            protected void updateThreadQuestion(long threadId, boolean question)
198                    throws Exception {
199    
200                    Connection con = null;
201                    PreparedStatement ps = null;
202    
203                    try {
204                            con = DataAccess.getUpgradeOptimizedConnection();
205    
206                            ps = con.prepareStatement(
207                                    "update MBThread set question = ? where threadId =" + threadId);
208    
209                            ps.setBoolean(1, question);
210    
211                            ps.executeUpdate();
212                    }
213                    finally {
214                            DataAccess.cleanUp(con, ps);
215                    }
216            }
217    
218    }