View Javadoc

1   /*
2    * Copyright 2005 Jukka Zitting <jz@yukatan.fi>
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package net.sf.exorcist.midgard;
17  
18  import java.sql.Connection;
19  import java.sql.PreparedStatement;
20  import java.sql.ResultSet;
21  import java.sql.SQLException;
22  import java.util.Date;
23  import java.util.HashMap;
24  import java.util.Map;
25  
26  import org.apache.commons.codec.digest.DigestUtils;
27  
28  public class GUIDResolver {
29  
30      private Map ids;
31      private Map guids;
32  
33      public GUIDResolver(Connection connection) throws SQLException {
34          ids = new HashMap();
35          guids = new HashMap();
36  
37          String sql =
38              "SELECT realm, id, guid FROM repligard WHERE action != 'delete'";
39          PreparedStatement ps = connection.prepareStatement(sql);
40          try {
41              ResultSet rs = ps.executeQuery();
42              try {
43                  while (rs.next()) {
44                      String realm = rs.getString(1);
45                      int id = rs.getInt(2);
46                      String guid = rs.getString(3);
47                      ids.put(realm + ":" + id, guid);
48                      guids.put(guid, new Integer(id));
49                  }
50              } finally {
51                  rs.close();
52              }
53          } finally {
54              ps.close();
55          }
56      }
57  
58      public String findGUID(String table, int id) {
59          String key = table + ":" + id;
60          String guid = (String) ids.get(key);
61          if (guid == null) {
62              guid = DigestUtils.md5Hex("exorcist:" + key);
63              ids.put(key, guid);
64              guids.put(guid, new Integer(id));
65          }
66          return guid;
67      }
68  
69      public int resolveGUID(String guid) {
70          if (guid == null || guid.length() == 0) {
71              return 0;
72          }
73          Integer id = (Integer) guids.get(guid);
74          if (id != null) {
75              return id.intValue();
76          } else {
77              return 0;
78          }
79      }
80  
81  }