1
19
20 package com.liferay.portal.upgrade.v5_1_0;
21
22 import com.liferay.portal.kernel.dao.jdbc.DataAccess;
23 import com.liferay.portal.kernel.log.Log;
24 import com.liferay.portal.kernel.log.LogFactoryUtil;
25 import com.liferay.portal.upgrade.UpgradeException;
26 import com.liferay.portal.upgrade.UpgradeProcess;
27
28 import java.sql.Connection;
29 import java.sql.PreparedStatement;
30 import java.sql.ResultSet;
31
32
38 public class UpgradeMessageBoards extends UpgradeProcess {
39
40 public void upgrade() throws UpgradeException {
41 _log.info("Upgrading");
42
43 try {
44 doUpgrade();
45 }
46 catch (Exception e) {
47 throw new UpgradeException(e);
48 }
49 }
50
51 protected void doUpgrade() throws Exception {
52
53
55 while (getMessageIdsCount() > 0) {
56 updateMessage();
57 }
58 }
59
60 protected long getMessageIdsCount() throws Exception {
61 StringBuilder sb = new StringBuilder();
62
63 sb.append("select count(*) from ");
64 sb.append("MBMessage childMessage ");
65 sb.append("inner join MBMessage parentMessage on ");
66 sb.append("childMessage.parentMessageId = parentMessage.messageId ");
67 sb.append("where parentMessage.categoryId != childMessage.categoryId ");
68 sb.append("or parentMessage.threadId != childMessage.threadId");
69
70 String sql = sb.toString();
71
72 Connection con = null;
73 PreparedStatement ps = null;
74 ResultSet rs = null;
75
76 try {
77 con = DataAccess.getConnection();
78
79 ps = con.prepareStatement(sql);
80
81 rs = ps.executeQuery();
82
83 while (rs.next()) {
84 return rs.getLong(1);
85 }
86
87 return 0;
88 }
89 finally {
90 DataAccess.cleanUp(con, ps, rs);
91 }
92 }
93
94 protected void updateMessage() throws Exception {
95 StringBuilder sb = new StringBuilder();
96
97 sb.append("select childMessage.messageId, parentMessage.categoryId, ");
98 sb.append("parentMessage.threadId ");
99 sb.append("from MBMessage childMessage ");
100 sb.append("inner join MBMessage parentMessage on ");
101 sb.append("childMessage.parentMessageId = parentMessage.messageId ");
102 sb.append("where parentMessage.categoryId != childMessage.categoryId ");
103 sb.append("or parentMessage.threadId != childMessage.threadId");
104
105 String sql = sb.toString();
106
107 Connection con = null;
108 PreparedStatement ps = null;
109 ResultSet rs = null;
110
111 try {
112 con = DataAccess.getConnection();
113
114 ps = con.prepareStatement(sql);
115
116 rs = ps.executeQuery();
117
118 while (rs.next()) {
119 long messageId = rs.getLong(1);
120 long categoryId = rs.getLong(2);
121 long threadId = rs.getLong(3);
122
123 runSQL(
124 "update MBMessage set categoryId = " + categoryId +
125 ", threadId = " + threadId + " where messageId = " +
126 messageId);
127 }
128 }
129 finally {
130 DataAccess.cleanUp(con, ps, rs);
131 }
132 }
133
134 private static Log _log = LogFactoryUtil.getLog(UpgradeMessageBoards.class);
135
136 }