001: package com.bm.introspectors.relations;
002:
003: import java.lang.reflect.ParameterizedType;
004: import java.util.HashMap;
005: import java.util.HashSet;
006: import java.util.Map;
007: import java.util.Set;
008:
009: import com.bm.introspectors.Property;
010:
011: /**
012: * The problems in finding releations using introspectors is to avaid cyclic
013: * dependencies. This singelton is used as such a store of properties
014: * (OneToMany, OneToOne,...) to avoid such dependencies
015: *
016: * @author Daniel Wiese
017: *
018: */
019: public final class GlobalRelationStore {
020:
021: private static final GlobalRelationStore singelton = new GlobalRelationStore();
022:
023: private Map<Class, Set<Property>> store = new HashMap<Class, Set<Property>>();
024:
025: private GlobalRelationStore() {
026: // singelton constructor
027: }
028:
029: /**
030: * Returns the singelton instance.
031: *
032: * @return - the singelton instance
033: */
034: public static GlobalRelationStore getStore() {
035: return singelton;
036: }
037:
038: /**
039: * Save a property wich represents a relation in a class (bean).
040: *
041: * @param forClass -
042: * the entity bean class
043: * @param toSave -
044: * the property to save
045: */
046: public void put(Class forClass, Property toSave) {
047: if (store.containsKey(forClass)) {
048: final Set<Property> tmp = store.get(forClass);
049: tmp.add(toSave);
050: } else {
051: final Set<Property> tmp = new HashSet<Property>();
052: tmp.add(toSave);
053: store.put(forClass, tmp);
054: }
055: }
056:
057: /**
058: * Returns the property based on the class and the name.
059: *
060: * @param forClass -
061: * the class where the property is in
062: * @param propertyName -
063: * the name of the property
064: * @return - the property or null (if not found)
065: */
066: public Property getProperty(Class forClass, String propertyName) {
067:
068: if (store.containsKey(forClass)) {
069: final Set<Property> tmp = store.get(forClass);
070: for (Property akt : tmp) {
071: if (akt.getName().equals(propertyName)) {
072: return akt;
073: }
074: }
075: }
076:
077: return null;
078: }
079:
080: /**
081: * Returns the releation properties for special class (entity bean).
082: *
083: * @param forClass -
084: * the class where the property is in
085: * @param forType -
086: * the type of the property
087: * @return - the property or null (if not found)
088: */
089: public Property getProperty(Class forClass, Class forType) {
090:
091: if (store.containsKey(forClass)) {
092: final Set<Property> tmp = store.get(forClass);
093: for (Property akt : tmp) {
094: // e.g clollections> we are searching the typed
095: if (akt.getType().equals(forType)
096: || this .isGenericTypeEqual(akt, forType)) {
097: return akt;
098: }
099: }
100: }
101:
102: return null;
103: }
104:
105: private boolean isGenericTypeEqual(Property aktProperty, Class clazz) {
106: if (aktProperty.getGenericType() instanceof ParameterizedType) {
107: final ParameterizedType type = (ParameterizedType) aktProperty
108: .getGenericType();
109: if (type.getActualTypeArguments().length == 1) {
110: Class<Object> ty = (Class<Object>) type
111: .getActualTypeArguments()[0];
112: if (ty.equals(clazz)) {
113: return true;
114: }
115: }
116: }
117: return false;
118: }
119:
120: }
|