001
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
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.getConnection();
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.getConnection();
073
074 StringBundler sb = new StringBundler(4);
075
076 sb.append("select messageFlag.messageId from MBMessageFlag ");
077 sb.append("messageFlag inner join MBMessage message on ");
078 sb.append("messageFlag.messageId = message.messageId where ");
079 sb.append("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("messageFlag.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.getConnection();
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.getConnection();
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 StringBundler sb = new StringBundler(4);
140
141 sb.append("select messageFlag.threadId from MBMessageFlag ");
142 sb.append("messageFlag inner join MBMessage message on ");
143 sb.append("messageFlag.messageId = message.messageId where ");
144 sb.append("message.parentMessageId = 0 and flag = 3");
145
146 ps = con.prepareStatement(sb.toString());
147
148 rs = ps.executeQuery();
149
150 while (rs.next()) {
151 long threadId = rs.getLong("messageFlag.threadId");
152
153 updateThreadQuestion(threadId, true);
154 }
155 }
156 finally {
157 DataAccess.cleanUp(con, ps, rs);
158 }
159 }
160
161 protected void updateThreadFlag() throws Exception {
162 Connection con = null;
163 PreparedStatement ps = null;
164 ResultSet rs = null;
165
166 try {
167 con = DataAccess.getConnection();
168
169 ps = con.prepareStatement(
170 "select userId, threadId, modifiedDate from MBMessageFlag " +
171 "where flag = 1");
172
173 rs = ps.executeQuery();
174
175 while (rs.next()) {
176 long userId = rs.getLong("userId");
177 long threadId = rs.getLong("threadId");
178 Timestamp modifiedDate = rs.getTimestamp("modifiedDate");
179
180 addThreadFlag(increment(), userId, threadId, modifiedDate);
181 }
182 }
183 finally {
184 DataAccess.cleanUp(con, ps, rs);
185 }
186
187 runSQL("drop table MBMessageFlag");
188 }
189
190 protected void updateThreadQuestion(long threadId, boolean question)
191 throws Exception {
192
193 Connection con = null;
194 PreparedStatement ps = null;
195
196 try {
197 con = DataAccess.getConnection();
198
199 ps = con.prepareStatement(
200 "update MBThread set question = ? where threadId =" + threadId);
201
202 ps.setBoolean(1, question);
203
204 ps.executeUpdate();
205 }
206 finally {
207 DataAccess.cleanUp(con, ps);
208 }
209 }
210
211 }