1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
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  /**
33   * <a href="UpgradeMessageBoards.java.html"><b><i>View Source</i></b></a>
34   *
35   * @author Brian Wing Shun Chan
36   *
37   */
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          // LEP-5761
54  
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 }