Project

General

Profile

Download (6.42 KB) Statistics
| Branch: | Tag: | Revision:
1
package eu.etaxonomy.cdm.api.cache;
2

    
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;
8
import java.util.List;
9

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

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

    
19

    
20
public class CdmModelCacher {
21

    
22

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

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

    
27
	public void cacheGetters() {
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
			Iterator propertyIt = persistentClass.getPropertyIterator();	   
43

    
44
			logger.info("Adding class : " + mappedClassName + " to cache");
45
						
46
			while(propertyIt.hasNext())
47
			{
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);
55
				}
56
			}
57
			cache.put(new Element(mappedClassName, cmgmfc));
58

    
59
		}
60
		cache.flush();
61
	}
62
	
63
	public void cacheGetterFields() {
64

    
65
		Configuration configuration = new Configuration().configure("/eu/etaxonomy/cdm/mappings/hibernate.cfg.xml");
66
		configuration.buildMappings();
67
		Iterator<PersistentClass> classMappingIterator =   configuration.getClassMappings();	  	  	  
68

    
69
		Cache cache = CdmRemoteCacheManager.getInstance().getCdmModelGetMethodsCache();
70
		cache.removeAll();
71

    
72
		while(classMappingIterator.hasNext()) {
73
			PersistentClass persistentClass = classMappingIterator.next();
74
			Class mappedClass = persistentClass.getMappedClass();
75
			String mappedClassName = mappedClass.getName();
76

    
77
			CdmModelFieldPropertyFromClass cmgmfc = new CdmModelFieldPropertyFromClass(mappedClassName);
78
			Iterator propertyIt = persistentClass.getPropertyIterator();	   
79

    
80
			logger.info("Adding class : " + mappedClassName + " to cache");
81
						
82
			while(propertyIt.hasNext())
83
			{
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());
91
				}
92
			}
93
			cache.put(new Element(mappedClassName, cmgmfc));
94

    
95
		}
96
		cache.flush();
97
	}
98
	
99
	public void checkGetterMethods() {
100

    
101
		Configuration configuration = new Configuration().configure("/eu/etaxonomy/cdm/mappings/hibernate.cfg.xml");
102
		configuration.buildMappings();
103
		Iterator<PersistentClass> classMappingIterator =   configuration.getClassMappings();	  	  	  
104

    
105
		Cache cache = CdmRemoteCacheManager.getInstance().getCdmModelGetMethodsCache();
106
		cache.removeAll();
107

    
108
		while(classMappingIterator.hasNext()) {
109
			PersistentClass persistentClass = classMappingIterator.next();
110
			Class mappedClass = persistentClass.getMappedClass();
111
			String mappedClassName = mappedClass.getName();
112
			
113
			Iterator propertyIt = persistentClass.getPropertyIterator();	   
114
			
115
			Method[] methods = mappedClass.getMethods();
116

    
117
			while(propertyIt.hasNext())
118
			{
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());					
124
					
125
					boolean foundMethod = false;
126
					for(Method method : methods) {
127
						if(method.getName().equals(getMethod)) {
128
							foundMethod = true;
129
							break;
130
						}
131
					}
132
					if(!foundMethod) {
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);
135
					}
136
				}
137
			}
138
			
139

    
140
		}
141
		
142
	}
143

    
144
	public List<CdmModelFieldPropertyFromClass> getCdmModelGetMethodFromClassList() {
145
		cmgmfcList.clear();
146
		Configuration configuration = new Configuration().configure("/eu/etaxonomy/cdm/mappings/hibernate.cfg.xml");
147
		configuration.buildMappings();
148
		Iterator<PersistentClass> classMappingIterator =   configuration.getClassMappings();	  	  	  					 
149

    
150
		while(classMappingIterator.hasNext()) {
151
			PersistentClass persistentClass = classMappingIterator.next();
152
			Class mappedClass = persistentClass.getMappedClass();
153
			String mappedClassName = mappedClass.getName();
154

    
155
			CdmModelFieldPropertyFromClass cmgmfc = new CdmModelFieldPropertyFromClass(mappedClassName);
156
			Iterator propertyIt = persistentClass.getPropertyIterator();	   
157

    
158
			while(propertyIt.hasNext())
159
			{
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);
166
				}
167
			}
168
			cmgmfcList.add(cmgmfc);
169
		}
170
		return cmgmfcList;
171

    
172
	}
173

    
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);
177
		return getMethod;
178
	}
179
	
180

    
181

    
182

    
183

    
184
}
(4-4/9)