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_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="ResourceCodeDependencyManager.java.html"><b><i>View Source</i></b>
25   * </a>
26   *
27   * @author Brian Wing Shun Chan
28   */
29  public class ResourceCodeDependencyManager extends DependencyManager {
30  
31      public void update(
32              long oldPrimaryKeyValue, Object[] oldColumnValues,
33              Object[] oldExtraColumnValues, long newPrimaryKeyValue,
34              Object[] newColumnValues, Object[] newExtraColumnValues)
35          throws Exception {
36  
37          long codeId = newPrimaryKeyValue;
38  
39          long resourceId = getResourceId(codeId);
40  
41          deleteDuplicateData("Resource_", codeId);
42  
43          if (resourceId > 0) {
44              DependencyManager resourceDependencyManager =
45                  new ResourceDependencyManager();
46  
47              resourceDependencyManager.setPrimaryKeyName("resourceId");
48  
49              resourceDependencyManager.update(resourceId);
50          }
51      }
52  
53      protected long getResourceId(long codeId) throws Exception {
54          Connection con = null;
55          PreparedStatement ps = null;
56          ResultSet rs = null;
57  
58          try {
59              con = DataAccess.getConnection();
60  
61              ps = con.prepareStatement(
62                  "select resourceId from Resource_ where codeId = ?");
63  
64              ps.setLong(1, codeId);
65  
66              rs = ps.executeQuery();
67  
68              while (rs.next()) {
69                  long resourceId = rs.getLong("resourceId");
70  
71                  return resourceId;
72              }
73          }
74          finally {
75              DataAccess.cleanUp(con, ps, rs);
76          }
77  
78          return 0;
79      }
80  
81  }