1 package eu
.etaxonomy
.cdm
.api
.cache
;
3 import java
.util
.ArrayList
;
4 import java
.util
.Iterator
;
7 import net
.sf
.ehcache
.Cache
;
8 import net
.sf
.ehcache
.Element
;
10 import org
.apache
.log4j
.Logger
;
11 import org
.hibernate
.cfg
.Configuration
;
12 import org
.hibernate
.mapping
.PersistentClass
;
13 import org
.hibernate
.mapping
.Property
;
14 import org
.hibernate
.property
.Getter
;
17 public class CdmModelCacher
{
20 private static final Logger logger
= Logger
.getLogger(CdmModelCacher
.class);
22 private List
<CdmModelGetMethodFromClass
> cmgmfcList
= new ArrayList
<CdmModelGetMethodFromClass
>();
24 public void cacheGetters() {
26 Configuration configuration
= new Configuration().configure("/eu/etaxonomy/cdm/mappings/hibernate.cfg.xml");
27 configuration
.buildMappings();
28 Iterator
<PersistentClass
> classMappingIterator
= configuration
.getClassMappings();
30 Cache cache
= CdmRemoteCacheManager
.getInstance().getCdmModelGetMethodsCache();
33 while(classMappingIterator
.hasNext()) {
34 PersistentClass persistentClass
= classMappingIterator
.next();
35 Class mappedClass
= persistentClass
.getMappedClass();
36 String mappedClassName
= mappedClass
.getName();
38 CdmModelGetMethodFromClass cmgmfc
= new CdmModelGetMethodFromClass(mappedClassName
);
39 Iterator propertyIt
= persistentClass
.getPropertyIterator();
41 logger
.info("Adding class : " + mappedClassName
+ " to cache");
42 while(propertyIt
.hasNext())
44 Property property
= (Property
)propertyIt
.next();
45 Getter getter
= property
.getGetter(mappedClass
);
46 if(getter
!= null && getter
.getMember() != null) {
47 String getMethod
= getMethodNameFromFieldName(getter
.getMember().getName());
48 logger
.info(" - getMethod : " + getMethod
);
49 cmgmfc
.addGetMethods(getMethod
);
52 cache
.put(new Element(mappedClassName
, cmgmfc
));
58 public List
<CdmModelGetMethodFromClass
> getCdmModelGetMethodFromClassList() {
60 Configuration configuration
= new Configuration().configure("/eu/etaxonomy/cdm/mappings/hibernate.cfg.xml");
61 configuration
.buildMappings();
62 Iterator
<PersistentClass
> classMappingIterator
= configuration
.getClassMappings();
64 while(classMappingIterator
.hasNext()) {
65 PersistentClass persistentClass
= classMappingIterator
.next();
66 Class mappedClass
= persistentClass
.getMappedClass();
67 String mappedClassName
= mappedClass
.getName();
69 CdmModelGetMethodFromClass cmgmfc
= new CdmModelGetMethodFromClass(mappedClassName
);
70 Iterator propertyIt
= persistentClass
.getPropertyIterator();
72 while(propertyIt
.hasNext())
74 Property property
= (Property
)propertyIt
.next();
75 Getter getter
= property
.getGetter(mappedClass
);
76 if(getter
!= null && getter
.getMember() != null) {
77 String getMethod
= getMethodNameFromFieldName(getter
.getMember().getName());
78 cmgmfc
.addGetMethods(getMethod
);
81 cmgmfcList
.add(cmgmfc
);
87 public static String
getMethodNameFromFieldName(String fieldName
) {
88 return "get" + fieldName
.substring(0, 1).toUpperCase() + fieldName
.substring(1);