1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 }