Project

General

Profile

Download (5.29 KB) Statistics
| Branch: | Tag: | Revision:
1
package eu.etaxonomy.taxeditor.remoting.cache;
2

    
3
import java.lang.reflect.Field;
4
import java.lang.reflect.Method;
5
import java.util.ArrayList;
6
import java.util.Iterator;
7
import java.util.List;
8

    
9
import net.sf.ehcache.Cache;
10
import net.sf.ehcache.Element;
11

    
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;
17

    
18

    
19
public class CdmModelCacher {
20

    
21

    
22
	private static final Logger logger = Logger.getLogger(CdmModelCacher.class);
23

    
24
	private final List<CdmModelFieldPropertyFromClass> cmgmfcList = new ArrayList<CdmModelFieldPropertyFromClass>();
25

    
26

    
27
	public void cacheGetterFields() {
28

    
29
		Configuration configuration = new Configuration().configure("/eu/etaxonomy/cdm/mappings/hibernate.cfg.xml");
30
		configuration.buildMappings();
31
		Iterator<PersistentClass> classMappingIterator =   configuration.getClassMappings();
32

    
33
		Cache cache = CdmRemoteCacheManager.getInstance().getCdmModelGetMethodsCache();
34
		cache.removeAll();
35

    
36
		while(classMappingIterator.hasNext()) {
37
			PersistentClass persistentClass = classMappingIterator.next();
38
			Class mappedClass = persistentClass.getMappedClass();
39
			String mappedClassName = mappedClass.getName();
40

    
41
			CdmModelFieldPropertyFromClass cmgmfc = new CdmModelFieldPropertyFromClass(mappedClassName);
42
			logger.info("Adding class : " + mappedClassName + " to cache");
43
			addGetters(persistentClass, cmgmfc);
44
			cache.put(new Element(mappedClassName, cmgmfc));
45

    
46
		}
47
		//cache.flush();
48
	}
49

    
50
	private void addGetters(PersistentClass persistentClass, CdmModelFieldPropertyFromClass cmgmfc) {
51
	    if (persistentClass != null) {
52
	        Iterator propertyIt = persistentClass.getPropertyIterator();
53

    
54
	        while(propertyIt.hasNext())
55
	        {
56
	            Property property = (Property)propertyIt.next();
57
	            Getter getter = property.getGetter(persistentClass.getMappedClass());
58
	            if(getter != null && getter.getMember() != null) {
59
	                Field field = (Field)getter.getMember();
60
	                //String getMethod = getMethodNameFromFieldName(field.getName(), field.getType().getName());
61
	                logger.info(" - contains field '" + field.getName() + "' of type '" + field.getType().getName() + "'");
62
	                cmgmfc.addGetMethods(field.getName());
63
	            }
64
	        }
65
	        addGetters(persistentClass.getSuperclass(), cmgmfc);
66
	    }
67
	}
68

    
69
	public void checkGetterMethods() {
70

    
71
		Configuration configuration = new Configuration().configure("/eu/etaxonomy/cdm/mappings/hibernate.cfg.xml");
72
		configuration.buildMappings();
73
		Iterator<PersistentClass> classMappingIterator =   configuration.getClassMappings();
74

    
75
		Cache cache = CdmRemoteCacheManager.getInstance().getCdmModelGetMethodsCache();
76
		cache.removeAll();
77

    
78
		while(classMappingIterator.hasNext()) {
79
			PersistentClass persistentClass = classMappingIterator.next();
80
			Class mappedClass = persistentClass.getMappedClass();
81
			String mappedClassName = mappedClass.getName();
82

    
83
			Iterator propertyIt = persistentClass.getPropertyIterator();
84

    
85
			Method[] methods = mappedClass.getMethods();
86

    
87
			while(propertyIt.hasNext())
88
			{
89
				Property property = (Property)propertyIt.next();
90
				Getter getter = property.getGetter(mappedClass);
91
				if(getter != null && getter.getMember() != null) {
92
					Field field = (Field)getter.getMember();
93
					String getMethod = getMethodNameFromFieldName(field.getName(), field.getType().getName());
94

    
95
					boolean foundMethod = false;
96
					for(Method method : methods) {
97
						if(method.getName().equals(getMethod)) {
98
							foundMethod = true;
99
							break;
100
						}
101
					}
102
					if(!foundMethod) {
103
						logger.info("Inferred method " + getMethod + " does not exist in class " + mappedClassName);
104
						//throw new CdmClientCacheException("Inferred method " + getMethod + " does not exist in class " + mappedClassName);
105
					}
106
				}
107
			}
108

    
109

    
110
		}
111

    
112
	}
113

    
114
	public List<CdmModelFieldPropertyFromClass> getCdmModelGetMethodFromClassList() {
115
		cmgmfcList.clear();
116
		Configuration configuration = new Configuration().configure("/eu/etaxonomy/cdm/mappings/hibernate.cfg.xml");
117
		configuration.buildMappings();
118
		Iterator<PersistentClass> classMappingIterator =   configuration.getClassMappings();
119

    
120
		while(classMappingIterator.hasNext()) {
121
			PersistentClass persistentClass = classMappingIterator.next();
122
			Class mappedClass = persistentClass.getMappedClass();
123
			String mappedClassName = mappedClass.getName();
124

    
125
			CdmModelFieldPropertyFromClass cmgmfc = new CdmModelFieldPropertyFromClass(mappedClassName);
126
			Iterator propertyIt = persistentClass.getPropertyIterator();
127

    
128
			while(propertyIt.hasNext())
129
			{
130
				Property property = (Property)propertyIt.next();
131
				Getter getter = property.getGetter(mappedClass);
132
				if(getter != null && getter.getMember() != null) {
133
					Field field = (Field)getter.getMember();
134
					String getMethod = getMethodNameFromFieldName(getter.getMember().getName(),field.getType().getName());
135
					cmgmfc.addGetMethods(getMethod);
136
				}
137
			}
138
			cmgmfcList.add(cmgmfc);
139
		}
140
		return cmgmfcList;
141

    
142
	}
143

    
144
	public static String getMethodNameFromFieldName(String fieldName, String type) {
145
		String prefix = type != null && type.toLowerCase().endsWith("boolean") ? "is" : "get";
146
		String getMethod =  prefix + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
147
		return getMethod;
148
	}
149

    
150

    
151

    
152

    
153

    
154
}
(4-4/9)