0dd85c5c2e081de8153f26cd2827302046d415fa
[taxeditor.git] / eu.etaxonomy.taxeditor.remoting / src / main / java / eu / etaxonomy / cdm / api / cache / CdmModelCacher.java
1 package eu.etaxonomy.cdm.api.cache;
2
3 import java.util.ArrayList;
4 import java.util.Iterator;
5 import java.util.List;
6
7 import net.sf.ehcache.Cache;
8 import net.sf.ehcache.Element;
9
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;
15
16
17 public class CdmModelCacher {
18
19
20 private static final Logger logger = Logger.getLogger(CdmModelCacher.class);
21
22 private List<CdmModelGetMethodFromClass> cmgmfcList = new ArrayList<CdmModelGetMethodFromClass>();
23
24 public void cacheGetters() {
25
26 Configuration configuration = new Configuration().configure("/eu/etaxonomy/cdm/mappings/hibernate.cfg.xml");
27 configuration.buildMappings();
28 Iterator<PersistentClass> classMappingIterator = configuration.getClassMappings();
29
30 Cache cache = CdmRemoteCacheManager.getInstance().getCdmModelGetMethodsCache();
31 cache.removeAll();
32
33 while(classMappingIterator.hasNext()) {
34 PersistentClass persistentClass = classMappingIterator.next();
35 Class mappedClass = persistentClass.getMappedClass();
36 String mappedClassName = mappedClass.getName();
37
38 CdmModelGetMethodFromClass cmgmfc = new CdmModelGetMethodFromClass(mappedClassName);
39 Iterator propertyIt = persistentClass.getPropertyIterator();
40
41 logger.info("Adding class : " + mappedClassName + " to cache");
42 while(propertyIt.hasNext())
43 {
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);
50 }
51 }
52 cache.put(new Element(mappedClassName, cmgmfc));
53
54 }
55 cache.flush();
56 }
57
58 public List<CdmModelGetMethodFromClass> getCdmModelGetMethodFromClassList() {
59 cmgmfcList.clear();
60 Configuration configuration = new Configuration().configure("/eu/etaxonomy/cdm/mappings/hibernate.cfg.xml");
61 configuration.buildMappings();
62 Iterator<PersistentClass> classMappingIterator = configuration.getClassMappings();
63
64 while(classMappingIterator.hasNext()) {
65 PersistentClass persistentClass = classMappingIterator.next();
66 Class mappedClass = persistentClass.getMappedClass();
67 String mappedClassName = mappedClass.getName();
68
69 CdmModelGetMethodFromClass cmgmfc = new CdmModelGetMethodFromClass(mappedClassName);
70 Iterator propertyIt = persistentClass.getPropertyIterator();
71
72 while(propertyIt.hasNext())
73 {
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);
79 }
80 }
81 cmgmfcList.add(cmgmfc);
82 }
83 return cmgmfcList;
84
85 }
86
87 public static String getMethodNameFromFieldName(String fieldName) {
88 return "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
89 }
90
91
92
93 }