Project

General

Profile

Download (5.26 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
	public static String HB_CONFIG_FILE_PATH= "/eu/etaxonomy/cdm/mappings/hibernate.cfg.xml";
25

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

    
28

    
29
	public static Configuration buildConfiguration() {
30
	    Configuration configuration = new Configuration().configure(HB_CONFIG_FILE_PATH);
31
        configuration.buildMappings();
32
        return configuration;
33
	}
34

    
35
	public void cacheGetterFields(Cache cache) {
36

    
37
	    Configuration configuration = buildConfiguration();
38
		Iterator<PersistentClass> classMappingIterator =   configuration.getClassMappings();
39

    
40
		cache.removeAll();
41

    
42
		while(classMappingIterator.hasNext()) {
43
			PersistentClass persistentClass = classMappingIterator.next();
44
			Class mappedClass = persistentClass.getMappedClass();
45
			String mappedClassName = mappedClass.getName();
46

    
47
			CdmModelFieldPropertyFromClass cmgmfc = new CdmModelFieldPropertyFromClass(mappedClassName);
48
			//logger.info("Adding class : " + mappedClassName + " to cache");
49
			addGetters(persistentClass, cmgmfc);
50
			cache.put(new Element(mappedClassName, cmgmfc));
51
		}
52

    
53
	}
54

    
55
	private void addGetters(PersistentClass persistentClass, CdmModelFieldPropertyFromClass cmgmfc) {
56
	    if (persistentClass != null) {
57
	        Iterator propertyIt = persistentClass.getPropertyIterator();
58

    
59
	        while(propertyIt.hasNext())
60
	        {
61
	            Property property = (Property)propertyIt.next();
62
	            Getter getter = property.getGetter(persistentClass.getMappedClass());
63
	            if(getter != null && getter.getMember() != null) {
64
	                Field field = (Field)getter.getMember();
65

    
66
	                //logger.info(" - contains field '" + field.getName() + "' of type '" + field.getType().getName() + "'");
67
	                cmgmfc.addGetMethods(field.getName());
68
	            }
69
	        }
70
	        addGetters(persistentClass.getSuperclass(), cmgmfc);
71
	    }
72
	}
73

    
74
	public void checkGetterMethods() {
75

    
76
		Configuration configuration = new Configuration().configure(HB_CONFIG_FILE_PATH);
77
		configuration.buildMappings();
78
		Iterator<PersistentClass> classMappingIterator =   configuration.getClassMappings();
79

    
80
		Cache cache = CdmRemoteCacheManager.getInstance().getCdmModelGetMethodsCache();
81
		cache.removeAll();
82

    
83
		while(classMappingIterator.hasNext()) {
84
			PersistentClass persistentClass = classMappingIterator.next();
85
			Class mappedClass = persistentClass.getMappedClass();
86
			String mappedClassName = mappedClass.getName();
87

    
88
			Iterator propertyIt = persistentClass.getPropertyIterator();
89

    
90
			Method[] methods = mappedClass.getMethods();
91

    
92
			while(propertyIt.hasNext())
93
			{
94
				Property property = (Property)propertyIt.next();
95
				Getter getter = property.getGetter(mappedClass);
96
				if(getter != null && getter.getMember() != null) {
97
					Field field = (Field)getter.getMember();
98
					String getMethod = getMethodNameFromFieldName(field.getName(), field.getType().getName());
99

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

    
114

    
115
		}
116

    
117
	}
118

    
119
	public List<CdmModelFieldPropertyFromClass> getCdmModelGetMethodFromClassList() {
120
		cmgmfcList.clear();
121
		Configuration configuration = new Configuration().configure(HB_CONFIG_FILE_PATH);
122
		configuration.buildMappings();
123
		Iterator<PersistentClass> classMappingIterator =   configuration.getClassMappings();
124

    
125
		while(classMappingIterator.hasNext()) {
126
			PersistentClass persistentClass = classMappingIterator.next();
127
			Class mappedClass = persistentClass.getMappedClass();
128
			String mappedClassName = mappedClass.getName();
129

    
130
			CdmModelFieldPropertyFromClass cmgmfc = new CdmModelFieldPropertyFromClass(mappedClassName);
131
			Iterator propertyIt = persistentClass.getPropertyIterator();
132

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

    
147
	}
148

    
149
	public static String getMethodNameFromFieldName(String fieldName, String type) {
150
		String prefix = type != null && type.toLowerCase().endsWith("boolean") ? "is" : "get";
151
		String getMethod =  prefix + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
152
		return getMethod;
153
	}
154

    
155

    
156

    
157

    
158

    
159
}
(4-4/9)