1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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_2_3;
24  
25  import com.liferay.portal.kernel.dao.jdbc.DataAccess;
26  import com.liferay.portal.kernel.log.Log;
27  import com.liferay.portal.kernel.log.LogFactoryUtil;
28  import com.liferay.portal.upgrade.UpgradeException;
29  import com.liferay.portal.upgrade.UpgradeProcess;
30  import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
31  import com.liferay.portlet.messageboards.service.MBThreadLocalServiceUtil;
32  
33  import java.sql.Connection;
34  import java.sql.PreparedStatement;
35  import java.sql.ResultSet;
36  
37  /**
38   * <a href="UpgradeMessageBoards.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Brian Wing Shun Chan
41   *
42   */
43  public class UpgradeMessageBoards extends UpgradeProcess {
44  
45      public void upgrade() throws UpgradeException {
46          _log.info("Upgrading");
47  
48          try {
49              doUpgrade();
50          }
51          catch (Exception e) {
52              throw new UpgradeException(e);
53          }
54      }
55  
56      protected void doUpgrade() throws Exception {
57          updateGroupId();
58          updateMessageClassNameId();
59          updateMessageFlagThreadId();
60          updateMessagePriority();
61      }
62  
63      protected void updateGroupId() throws Exception {
64          Connection con = null;
65          PreparedStatement ps = null;
66          ResultSet rs = null;
67  
68          try {
69              con = DataAccess.getConnection();
70  
71              ps = con.prepareStatement(
72                  "select categoryId, groupId from MBCategory");
73  
74              rs = ps.executeQuery();
75  
76              while (rs.next()) {
77                  long categoryId = rs.getLong("categoryId");
78                  long groupId = rs.getLong("groupId");
79  
80                  int threadCount =
81                      MBThreadLocalServiceUtil.getCategoryThreadsCount(
82                          categoryId);
83                  int messageCount =
84                      MBMessageLocalServiceUtil.getCategoryMessagesCount(
85                          categoryId);
86  
87                  runSQL(
88                      "update MBCategory set threadCount = " + threadCount +
89                          ", messageCount = " + messageCount +
90                              " where categoryId = " + categoryId);
91                  runSQL(
92                      "update MBMessage set groupId = " + groupId +
93                          " where categoryId = " + categoryId);
94                  runSQL(
95                      "update MBThread set groupId = " + groupId +
96                          " where categoryId = " + categoryId);
97              }
98          }
99          finally {
100             DataAccess.cleanUp(con, ps, rs);
101         }
102     }
103 
104     protected void updateMessageClassNameId() throws Exception {
105         Connection con = null;
106         PreparedStatement ps = null;
107         ResultSet rs = null;
108 
109         try {
110             con = DataAccess.getConnection();
111 
112             ps = con.prepareStatement(
113                 "select classNameId, classPK, threadId from MBDiscussion");
114 
115             rs = ps.executeQuery();
116 
117             while (rs.next()) {
118                 long classNameId = rs.getLong("classNameId");
119                 long classPK = rs.getLong("classPK");
120                 long threadId = rs.getLong("threadId");
121 
122                 runSQL(
123                     "update MBMessage set classNameId = " + classNameId +
124                         ", classPK = " + classPK + " where threadId = " +
125                             threadId);
126             }
127         }
128         finally {
129             DataAccess.cleanUp(con, ps, rs);
130         }
131     }
132 
133     protected void updateMessageFlagThreadId() throws Exception {
134         if (isSupportsUpdateWithInnerJoin()) {
135             StringBuilder sb = new StringBuilder();
136 
137             sb.append("update MBMessageFlag inner join MBMessage on ");
138             sb.append("MBMessage.messageId = MBMessageFlag.messageId set ");
139             sb.append("MBMessageFlag.threadId = MBMessage.threadId");
140 
141             runSQL(sb.toString());
142         }
143         else {
144             Connection con = null;
145             PreparedStatement ps = null;
146             ResultSet rs = null;
147 
148             try {
149                 con = DataAccess.getConnection();
150 
151                 ps = con.prepareStatement(
152                     "select messageId, threadId from MBMessage");
153 
154                 rs = ps.executeQuery();
155 
156                 while (rs.next()) {
157                     long messageId = rs.getLong("messageId");
158                     long threadId = rs.getLong("threadId");
159 
160                     runSQL(
161                         "update MBMessageFlag set threadId = " + threadId +
162                             " where messageId = " + messageId);
163                 }
164             }
165             finally {
166                 DataAccess.cleanUp(con, ps, rs);
167             }
168         }
169     }
170 
171     protected void updateMessagePriority() throws Exception {
172         if (isSupportsUpdateWithInnerJoin()) {
173             StringBuilder sb = new StringBuilder();
174 
175             sb.append("update MBMessage inner join MBThread on ");
176             sb.append("MBThread.threadId = MBMessage.threadId set ");
177             sb.append("MBMessage.priority = MBThread.priority");
178 
179             runSQL(sb.toString());
180         }
181         else {
182             Connection con = null;
183             PreparedStatement ps = null;
184             ResultSet rs = null;
185 
186             try {
187                 con = DataAccess.getConnection();
188 
189                 ps = con.prepareStatement(
190                     "select threadId, priority from MBThread");
191 
192                 rs = ps.executeQuery();
193 
194                 while (rs.next()) {
195                     long threadId = rs.getLong("threadId");
196                     double priority = rs.getDouble("priority");
197 
198                     runSQL(
199                         "update MBMessage set priority = " + priority +
200                             " where threadId = " + threadId);
201                 }
202             }
203             finally {
204                 DataAccess.cleanUp(con, ps, rs);
205             }
206         }
207     }
208 
209     private static Log _log = LogFactoryUtil.getLog(UpgradeMessageBoards.class);
210 
211 }