1 package eu
.etaxonomy
.cdm
.api
.cache
;
3 import java
.lang
.reflect
.Field
;
4 import java
.lang
.reflect
.Method
;
5 import java
.util
.ArrayList
;
6 import java
.util
.Arrays
;
7 import java
.util
.Iterator
;
10 import net
.sf
.ehcache
.Cache
;
11 import net
.sf
.ehcache
.Element
;
13 import org
.apache
.log4j
.Logger
;
14 import org
.hibernate
.cfg
.Configuration
;
15 import org
.hibernate
.mapping
.PersistentClass
;
16 import org
.hibernate
.mapping
.Property
;
17 import org
.hibernate
.property
.Getter
;
20 public class CdmModelCacher
{
23 private static final Logger logger
= Logger
.getLogger(CdmModelCacher
.class);
25 private List
<CdmModelFieldPropertyFromClass
> cmgmfcList
= new ArrayList
<CdmModelFieldPropertyFromClass
>();
27 public void cacheGetters() {
29 Configuration configuration
= new Configuration().configure("/eu/etaxonomy/cdm/mappings/hibernate.cfg.xml");
30 configuration
.buildMappings();
31 Iterator
<PersistentClass
> classMappingIterator
= configuration
.getClassMappings();
33 Cache cache
= CdmRemoteCacheManager
.getInstance().getCdmModelGetMethodsCache();
36 while(classMappingIterator
.hasNext()) {
37 PersistentClass persistentClass
= classMappingIterator
.next();
38 Class mappedClass
= persistentClass
.getMappedClass();
39 String mappedClassName
= mappedClass
.getName();
41 CdmModelFieldPropertyFromClass cmgmfc
= new CdmModelFieldPropertyFromClass(mappedClassName
);
42 Iterator propertyIt
= persistentClass
.getPropertyIterator();
44 logger
.info("Adding class : " + mappedClassName
+ " to cache");
46 while(propertyIt
.hasNext())
48 Property property
= (Property
)propertyIt
.next();
49 Getter getter
= property
.getGetter(mappedClass
);
50 if(getter
!= null && getter
.getMember() != null) {
51 Field field
= (Field
)getter
.getMember();
52 String getMethod
= getMethodNameFromFieldName(field
.getName(), field
.getType().getName());
53 logger
.info(" - getMethod : " + getMethod
+ " for type " + field
.getType().getName());
54 cmgmfc
.addGetMethods(getMethod
);
57 cache
.put(new Element(mappedClassName
, cmgmfc
));
63 public void cacheGetterFields() {
65 Configuration configuration
= new Configuration().configure("/eu/etaxonomy/cdm/mappings/hibernate.cfg.xml");
66 configuration
.buildMappings();
67 Iterator
<PersistentClass
> classMappingIterator
= configuration
.getClassMappings();
69 Cache cache
= CdmRemoteCacheManager
.getInstance().getCdmModelGetMethodsCache();
72 while(classMappingIterator
.hasNext()) {
73 PersistentClass persistentClass
= classMappingIterator
.next();
74 Class mappedClass
= persistentClass
.getMappedClass();
75 String mappedClassName
= mappedClass
.getName();
77 CdmModelFieldPropertyFromClass cmgmfc
= new CdmModelFieldPropertyFromClass(mappedClassName
);
78 Iterator propertyIt
= persistentClass
.getPropertyIterator();
80 logger
.info("Adding class : " + mappedClassName
+ " to cache");
82 while(propertyIt
.hasNext())
84 Property property
= (Property
)propertyIt
.next();
85 Getter getter
= property
.getGetter(mappedClass
);
86 if(getter
!= null && getter
.getMember() != null) {
87 Field field
= (Field
)getter
.getMember();
88 //String getMethod = getMethodNameFromFieldName(field.getName(), field.getType().getName());
89 logger
.info(" - contains field '" + field
.getName() + "' of type '" + field
.getType().getName() + "'");
90 cmgmfc
.addGetMethods(field
.getName());
93 cache
.put(new Element(mappedClassName
, cmgmfc
));
99 public void checkGetterMethods() {
101 Configuration configuration
= new Configuration().configure("/eu/etaxonomy/cdm/mappings/hibernate.cfg.xml");
102 configuration
.buildMappings();
103 Iterator
<PersistentClass
> classMappingIterator
= configuration
.getClassMappings();
105 Cache cache
= CdmRemoteCacheManager
.getInstance().getCdmModelGetMethodsCache();
108 while(classMappingIterator
.hasNext()) {
109 PersistentClass persistentClass
= classMappingIterator
.next();
110 Class mappedClass
= persistentClass
.getMappedClass();
111 String mappedClassName
= mappedClass
.getName();
113 Iterator propertyIt
= persistentClass
.getPropertyIterator();
115 Method
[] methods
= mappedClass
.getMethods();
117 while(propertyIt
.hasNext())
119 Property property
= (Property
)propertyIt
.next();
120 Getter getter
= property
.getGetter(mappedClass
);
121 if(getter
!= null && getter
.getMember() != null) {
122 Field field
= (Field
)getter
.getMember();
123 String getMethod
= getMethodNameFromFieldName(field
.getName(), field
.getType().getName());
125 boolean foundMethod
= false;
126 for(Method method
: methods
) {
127 if(method
.getName().equals(getMethod
)) {
133 logger
.info("Inferred method " + getMethod
+ " does not exist in class " + mappedClassName
);
134 //throw new CdmClientCacheException("Inferred method " + getMethod + " does not exist in class " + mappedClassName);
144 public List
<CdmModelFieldPropertyFromClass
> getCdmModelGetMethodFromClassList() {
146 Configuration configuration
= new Configuration().configure("/eu/etaxonomy/cdm/mappings/hibernate.cfg.xml");
147 configuration
.buildMappings();
148 Iterator
<PersistentClass
> classMappingIterator
= configuration
.getClassMappings();
150 while(classMappingIterator
.hasNext()) {
151 PersistentClass persistentClass
= classMappingIterator
.next();
152 Class mappedClass
= persistentClass
.getMappedClass();
153 String mappedClassName
= mappedClass
.getName();
155 CdmModelFieldPropertyFromClass cmgmfc
= new CdmModelFieldPropertyFromClass(mappedClassName
);
156 Iterator propertyIt
= persistentClass
.getPropertyIterator();
158 while(propertyIt
.hasNext())
160 Property property
= (Property
)propertyIt
.next();
161 Getter getter
= property
.getGetter(mappedClass
);
162 if(getter
!= null && getter
.getMember() != null) {
163 Field field
= (Field
)getter
.getMember();
164 String getMethod
= getMethodNameFromFieldName(getter
.getMember().getName(),field
.getType().getName());
165 cmgmfc
.addGetMethods(getMethod
);
168 cmgmfcList
.add(cmgmfc
);
174 public static String
getMethodNameFromFieldName(String fieldName
, String type
) {
175 String prefix
= type
!= null && type
.toLowerCase().endsWith("boolean") ?
"is" : "get";
176 String getMethod
= prefix
+ fieldName
.substring(0, 1).toUpperCase() + fieldName
.substring(1);