1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.upgrade.v5_1_0;
24  
25  import com.liferay.portal.kernel.dao.jdbc.DataAccess;
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  /**
33   * <a href="UpgradeMessageBoards.java.html"><b><i>View Source</i></b></a>
34   *
35   * @author Brian Wing Shun Chan
36   */
37  public class UpgradeMessageBoards extends UpgradeProcess {
38  
39      protected void doUpgrade() throws Exception {
40  
41          // LEP-5761
42  
43          while (getMessageIdsCount() > 0) {
44              updateMessage();
45          }
46      }
47  
48      protected long getMessageIdsCount() throws Exception {
49          StringBuilder sb = new StringBuilder();
50  
51          sb.append("select count(*) from ");
52          sb.append("MBMessage childMessage ");
53          sb.append("inner join MBMessage parentMessage on ");
54          sb.append("childMessage.parentMessageId = parentMessage.messageId ");
55          sb.append("where parentMessage.categoryId != childMessage.categoryId ");
56          sb.append("or parentMessage.threadId != childMessage.threadId");
57  
58          String sql = sb.toString();
59  
60          Connection con = null;
61          PreparedStatement ps = null;
62          ResultSet rs = null;
63  
64          try {
65              con = DataAccess.getConnection();
66  
67              ps = con.prepareStatement(sql);
68  
69              rs = ps.executeQuery();
70  
71              while (rs.next()) {
72                  return rs.getLong(1);
73              }
74  
75              return 0;
76          }
77          finally {
78              DataAccess.cleanUp(con, ps, rs);
79          }
80      }
81  
82      protected void updateMessage() throws Exception {
83          StringBuilder sb = new StringBuilder();
84  
85          sb.append("select childMessage.messageId, parentMessage.categoryId, ");
86          sb.append("parentMessage.threadId ");
87          sb.append("from MBMessage childMessage ");
88          sb.append("inner join MBMessage parentMessage on ");
89          sb.append("childMessage.parentMessageId = parentMessage.messageId ");
90          sb.append("where parentMessage.categoryId != childMessage.categoryId ");
91          sb.append("or parentMessage.threadId != childMessage.threadId");
92  
93          String sql = sb.toString();
94  
95          Connection con = null;
96          PreparedStatement ps = null;
97          ResultSet rs = null;
98  
99          try {
100             con = DataAccess.getConnection();
101 
102             ps = con.prepareStatement(sql);
103 
104             rs = ps.executeQuery();
105 
106             while (rs.next()) {
107                 long messageId = rs.getLong(1);
108                 long categoryId = rs.getLong(2);
109                 long threadId = rs.getLong(3);
110 
111                 runSQL(
112                     "update MBMessage set categoryId = " + categoryId +
113                         ", threadId = " + threadId + " where messageId = " +
114                             messageId);
115             }
116         }
117         finally {
118             DataAccess.cleanUp(con, ps, rs);
119         }
120     }
121 
122 }