1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
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  /**
26   * <a href="UpgradeMessageBoards.java.html"><b><i>View Source</i></b></a>
27   *
28   * @author Brian Wing Shun Chan
29   */
30  public class UpgradeMessageBoards extends UpgradeProcess {
31  
32      protected void doUpgrade() throws Exception {
33  
34          // LEP-5761
35  
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 }