1   /**
2    * Copyright (c) 2000-2010 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   *
12   *
13   */
14  
15  package com.liferay.portal.upgrade.v5_2_3.util;
16  
17  import com.liferay.portal.kernel.dao.jdbc.DataAccess;
18  
19  import java.sql.Connection;
20  import java.sql.PreparedStatement;
21  import java.sql.ResultSet;
22  
23  /**
24   * <a href="MBDiscussionDependencyManager.java.html"><b><i>View Source</i></b>
25   * </a>
26   *
27   * @author Brian Wing Shun Chan
28   * @author Amos Fong
29   */
30  public class MBDiscussionDependencyManager extends DependencyManager {
31  
32      public void update(
33              long oldPrimaryKeyValue, Object[] oldColumnValues,
34              Object[] oldExtraColumnValues, long newPrimaryKeyValue,
35              Object[] newColumnValues, Object[] newExtraColumnValues)
36          throws Exception {
37  
38          long threadId = 0;
39  
40          for (int i = 0; i < columns.length; i++) {
41              if (columns[i][0].equals("threadId")) {
42                  threadId = (Long)newColumnValues[i];
43              }
44          }
45  
46          if ((threadId == 0) && (extraColumns != null)) {
47              for (int i = 0; i < extraColumns.length; i++) {
48                  if (extraColumns[i][0].equals("threadId")) {
49                      threadId = (Long)newExtraColumnValues[i];
50                  }
51              }
52          }
53  
54          if (isDuplicateThread(threadId)) {
55              deleteDuplicateData("MBMessage", "threadId", threadId);
56              deleteDuplicateData("MBMessageFlag", "threadId", threadId);
57              deleteDuplicateData("MBThread", "threadId", threadId);
58          }
59      }
60  
61      protected boolean isDuplicateThread(long threadId) throws Exception {
62          Connection con = null;
63          PreparedStatement ps = null;
64          ResultSet rs = null;
65  
66          try {
67              con = DataAccess.getConnection();
68  
69              ps = con.prepareStatement(
70                  "select count(*) from MBDiscussion where threadId = ?");
71  
72              ps.setLong(1, threadId);
73  
74              rs = ps.executeQuery();
75  
76              while (rs.next()) {
77                  long count = rs.getLong(1);
78  
79                  if (count > 0) {
80                      return false;
81                  }
82              }
83  
84              return true;
85          }
86          finally {
87              DataAccess.cleanUp(con, ps, rs);
88          }
89      }
90  
91  }