1
14
15 package com.liferay.portal.upgrade.v5_1_0;
16
17 import com.liferay.portal.kernel.dao.jdbc.DataAccess;
18 import com.liferay.portal.kernel.upgrade.UpgradeProcess;
19 import com.liferay.portal.kernel.util.StringBundler;
20
21 import java.sql.Connection;
22 import java.sql.PreparedStatement;
23 import java.sql.ResultSet;
24
25
30 public class UpgradeMessageBoards extends UpgradeProcess {
31
32 protected void doUpgrade() throws Exception {
33
34
36 while (getMessageIdsCount() > 0) {
37 updateMessage();
38 }
39 }
40
41 protected long getMessageIdsCount() throws Exception {
42 StringBundler sb = new StringBundler(6);
43
44 sb.append("select count(*) from ");
45 sb.append("MBMessage childMessage ");
46 sb.append("inner join MBMessage parentMessage on ");
47 sb.append("childMessage.parentMessageId = parentMessage.messageId ");
48 sb.append("where parentMessage.categoryId != childMessage.categoryId ");
49 sb.append("or parentMessage.threadId != childMessage.threadId");
50
51 String sql = sb.toString();
52
53 Connection con = null;
54 PreparedStatement ps = null;
55 ResultSet rs = null;
56
57 try {
58 con = DataAccess.getConnection();
59
60 ps = con.prepareStatement(sql);
61
62 rs = ps.executeQuery();
63
64 while (rs.next()) {
65 return rs.getLong(1);
66 }
67
68 return 0;
69 }
70 finally {
71 DataAccess.cleanUp(con, ps, rs);
72 }
73 }
74
75 protected void updateMessage() throws Exception {
76 StringBundler sb = new StringBundler(7);
77
78 sb.append("select childMessage.messageId, parentMessage.categoryId, ");
79 sb.append("parentMessage.threadId ");
80 sb.append("from MBMessage childMessage ");
81 sb.append("inner join MBMessage parentMessage on ");
82 sb.append("childMessage.parentMessageId = parentMessage.messageId ");
83 sb.append("where parentMessage.categoryId != childMessage.categoryId ");
84 sb.append("or parentMessage.threadId != childMessage.threadId");
85
86 String sql = sb.toString();
87
88 Connection con = null;
89 PreparedStatement ps = null;
90 ResultSet rs = null;
91
92 try {
93 con = DataAccess.getConnection();
94
95 ps = con.prepareStatement(sql);
96
97 rs = ps.executeQuery();
98
99 while (rs.next()) {
100 long messageId = rs.getLong(1);
101 long categoryId = rs.getLong(2);
102 long threadId = rs.getLong(3);
103
104 runSQL(
105 "update MBMessage set categoryId = " + categoryId +
106 ", threadId = " + threadId + " where messageId = " +
107 messageId);
108 }
109 }
110 finally {
111 DataAccess.cleanUp(con, ps, rs);
112 }
113 }
114
115 }