01: package com.bm.introspectors.relations;
02:
03: import java.util.HashMap;
04: import java.util.Map;
05: import java.util.Set;
06:
07: import com.bm.introspectors.PrimaryKeyInfo;
08: import com.bm.introspectors.Property;
09:
10: /**
11: * Global store for storing the primary key properties of entity classes.
12: * Such a global store is necessary to avoid cyclic dependencies while processing relations.
13: *
14: * @author Peter Doornbosch
15: */
16: public class GlobalPrimaryKeyStore {
17:
18: private static final GlobalPrimaryKeyStore singleton = new GlobalPrimaryKeyStore();
19:
20: private Map<Class, Map<Property, PrimaryKeyInfo>> store = new HashMap<Class, Map<Property, PrimaryKeyInfo>>();
21:
22: private GlobalPrimaryKeyStore() {
23: // singleton constructor
24: }
25:
26: /**
27: * Returns the singleton instance.
28: *
29: * @return - the singleton instance
30: */
31: public static GlobalPrimaryKeyStore getStore() {
32: return singleton;
33: }
34:
35: /**
36: * Stores primary key info of a given entity class.
37: * @param entityClass the entity class
38: * @param pkFieldInfo the primary key property and field info; usually the map will contain
39: * just one entry, but it might contain more entries in case of a composite primary key.
40: */
41: public void put(Class entityClass,
42: Map<Property, PrimaryKeyInfo> pkFieldInfo) {
43: store.put(entityClass, pkFieldInfo);
44: }
45:
46: /**
47: * Retrieves primary key info.
48: * @param entityClass
49: * @return one or more (in case of a composite primary key) properties that specify the
50: * primary key(s), or null if no primary key info is registered for the given class.
51: */
52: public Set<Property> getPrimaryKeyInfo(Class entityClass) {
53: Map<Property, PrimaryKeyInfo> pkInfo = store.get(entityClass);
54: if (pkInfo == null) {
55: return null;
56: } else {
57: return pkInfo.keySet();
58: }
59: }
60: }
|