1 package eu
.etaxonomy
.taxeditor
.remoting
.cache
;
3 import java
.lang
.reflect
.Field
;
4 import java
.lang
.reflect
.Method
;
5 import java
.util
.ArrayList
;
6 import java
.util
.Iterator
;
9 import net
.sf
.ehcache
.Cache
;
10 import net
.sf
.ehcache
.Element
;
12 import org
.apache
.log4j
.Logger
;
13 import org
.hibernate
.cfg
.Configuration
;
14 import org
.hibernate
.mapping
.PersistentClass
;
15 import org
.hibernate
.mapping
.Property
;
16 import org
.hibernate
.property
.Getter
;
19 public class CdmModelCacher
{
22 private static final Logger logger
= Logger
.getLogger(CdmModelCacher
.class);
24 private final List
<CdmModelFieldPropertyFromClass
> cmgmfcList
= new ArrayList
<CdmModelFieldPropertyFromClass
>();
26 public void cacheGetters() {
28 Configuration configuration
= new Configuration().configure("/eu/etaxonomy/cdm/mappings/hibernate.cfg.xml");
29 configuration
.buildMappings();
30 Iterator
<PersistentClass
> classMappingIterator
= configuration
.getClassMappings();
32 Cache cache
= CdmRemoteCacheManager
.getInstance().getCdmModelGetMethodsCache();
35 while(classMappingIterator
.hasNext()) {
36 PersistentClass persistentClass
= classMappingIterator
.next();
37 Class mappedClass
= persistentClass
.getMappedClass();
38 String mappedClassName
= mappedClass
.getName();
40 CdmModelFieldPropertyFromClass cmgmfc
= new CdmModelFieldPropertyFromClass(mappedClassName
);
41 Iterator propertyIt
= persistentClass
.getPropertyIterator();
43 logger
.info("Adding class : " + mappedClassName
+ " to cache");
45 while(propertyIt
.hasNext())
47 Property property
= (Property
)propertyIt
.next();
48 Getter getter
= property
.getGetter(mappedClass
);
49 if(getter
!= null && getter
.getMember() != null) {
50 Field field
= (Field
)getter
.getMember();
51 String getMethod
= getMethodNameFromFieldName(field
.getName(), field
.getType().getName());
52 logger
.info(" - getMethod : " + getMethod
+ " for type " + field
.getType().getName());
53 cmgmfc
.addGetMethods(getMethod
);
56 cache
.put(new Element(mappedClassName
, cmgmfc
));
62 public void cacheGetterFields() {
64 Configuration configuration
= new Configuration().configure("/eu/etaxonomy/cdm/mappings/hibernate.cfg.xml");
65 configuration
.buildMappings();
66 Iterator
<PersistentClass
> classMappingIterator
= configuration
.getClassMappings();
68 Cache cache
= CdmRemoteCacheManager
.getInstance().getCdmModelGetMethodsCache();
71 while(classMappingIterator
.hasNext()) {
72 PersistentClass persistentClass
= classMappingIterator
.next();
73 Class mappedClass
= persistentClass
.getMappedClass();
74 String mappedClassName
= mappedClass
.getName();
76 CdmModelFieldPropertyFromClass cmgmfc
= new CdmModelFieldPropertyFromClass(mappedClassName
);
77 addGetters(persistentClass
, cmgmfc
);
78 logger
.info("Adding class : " + mappedClassName
+ " to cache");
80 cache
.put(new Element(mappedClassName
, cmgmfc
));
86 private void addGetters(PersistentClass persistentClass
, CdmModelFieldPropertyFromClass cmgmfc
) {
87 if (persistentClass
!= null) {
88 Iterator propertyIt
= persistentClass
.getPropertyIterator();
90 while(propertyIt
.hasNext())
92 Property property
= (Property
)propertyIt
.next();
93 Getter getter
= property
.getGetter(persistentClass
.getMappedClass());
94 if(getter
!= null && getter
.getMember() != null) {
95 Field field
= (Field
)getter
.getMember();
96 //String getMethod = getMethodNameFromFieldName(field.getName(), field.getType().getName());
97 logger
.info(" - contains field '" + field
.getName() + "' of type '" + field
.getType().getName() + "'");
98 cmgmfc
.addGetMethods(field
.getName());
101 addGetters(persistentClass
.getSuperclass(), cmgmfc
);
105 public void checkGetterMethods() {
107 Configuration configuration
= new Configuration().configure("/eu/etaxonomy/cdm/mappings/hibernate.cfg.xml");
108 configuration
.buildMappings();
109 Iterator
<PersistentClass
> classMappingIterator
= configuration
.getClassMappings();
111 Cache cache
= CdmRemoteCacheManager
.getInstance().getCdmModelGetMethodsCache();
114 while(classMappingIterator
.hasNext()) {
115 PersistentClass persistentClass
= classMappingIterator
.next();
116 Class mappedClass
= persistentClass
.getMappedClass();
117 String mappedClassName
= mappedClass
.getName();
119 Iterator propertyIt
= persistentClass
.getPropertyIterator();
121 Method
[] methods
= mappedClass
.getMethods();
123 while(propertyIt
.hasNext())
125 Property property
= (Property
)propertyIt
.next();
126 Getter getter
= property
.getGetter(mappedClass
);
127 if(getter
!= null && getter
.getMember() != null) {
128 Field field
= (Field
)getter
.getMember();
129 String getMethod
= getMethodNameFromFieldName(field
.getName(), field
.getType().getName());
131 boolean foundMethod
= false;
132 for(Method method
: methods
) {
133 if(method
.getName().equals(getMethod
)) {
139 logger
.info("Inferred method " + getMethod
+ " does not exist in class " + mappedClassName
);
140 //throw new CdmClientCacheException("Inferred method " + getMethod + " does not exist in class " + mappedClassName);
150 public List
<CdmModelFieldPropertyFromClass
> getCdmModelGetMethodFromClassList() {
152 Configuration configuration
= new Configuration().configure("/eu/etaxonomy/cdm/mappings/hibernate.cfg.xml");
153 configuration
.buildMappings();
154 Iterator
<PersistentClass
> classMappingIterator
= configuration
.getClassMappings();
156 while(classMappingIterator
.hasNext()) {
157 PersistentClass persistentClass
= classMappingIterator
.next();
158 Class mappedClass
= persistentClass
.getMappedClass();
159 String mappedClassName
= mappedClass
.getName();
161 CdmModelFieldPropertyFromClass cmgmfc
= new CdmModelFieldPropertyFromClass(mappedClassName
);
162 Iterator propertyIt
= persistentClass
.getPropertyIterator();
164 while(propertyIt
.hasNext())
166 Property property
= (Property
)propertyIt
.next();
167 Getter getter
= property
.getGetter(mappedClass
);
168 if(getter
!= null && getter
.getMember() != null) {
169 Field field
= (Field
)getter
.getMember();
170 String getMethod
= getMethodNameFromFieldName(getter
.getMember().getName(),field
.getType().getName());
171 cmgmfc
.addGetMethods(getMethod
);
174 cmgmfcList
.add(cmgmfc
);
180 public static String
getMethodNameFromFieldName(String fieldName
, String type
) {
181 String prefix
= type
!= null && type
.toLowerCase().endsWith("boolean") ?
"is" : "get";
182 String getMethod
= prefix
+ fieldName
.substring(0, 1).toUpperCase() + fieldName
.substring(1);