#4932 Remove autowired members, Use serliaised cdm model map
[taxeditor.git] / eu.etaxonomy.taxeditor.cdmlib / src / main / java / eu / etaxonomy / taxeditor / remoting / cache / CdmModelCacher.java
index a0e095d91983504ed6a23b7c271a23c6e0a53b2b..45a3094e6a2322a7bbcde7376d932a443f086f77 100644 (file)
 package eu.etaxonomy.taxeditor.remoting.cache;
 
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
 import java.lang.reflect.Field;
-import java.lang.reflect.Method;
-import java.util.ArrayList;
+import java.net.URISyntaxException;
+import java.net.URL;
+import java.util.HashMap;
 import java.util.Iterator;
-import java.util.List;
+import java.util.Map;
 
 import net.sf.ehcache.Cache;
 import net.sf.ehcache.Element;
 
-import org.apache.log4j.Logger;
+import org.eclipse.core.runtime.FileLocator;
+import org.eclipse.core.runtime.Platform;
 import org.hibernate.cfg.Configuration;
 import org.hibernate.mapping.PersistentClass;
 import org.hibernate.mapping.Property;
 import org.hibernate.property.Getter;
+import org.osgi.framework.Bundle;
 
 
 public class CdmModelCacher {
 
 
-       private static final Logger logger = Logger.getLogger(CdmModelCacher.class);
 
-       public static String HB_CONFIG_FILE_PATH= "/eu/etaxonomy/cdm/mappings/hibernate.cfg.xml";
 
-       private final List<CdmModelFieldPropertyFromClass> cmgmfcList = new ArrayList<CdmModelFieldPropertyFromClass>();
+    public static String HB_CONFIG_FILE_PATH= "/eu/etaxonomy/cdm/mappings/hibernate.cfg.xml";
 
+    public static final String CDM_MAP_SER_FILE_PATH = "resources/cdm.map.ser";
 
-       public static Configuration buildConfiguration() {
-           Configuration configuration = new Configuration().configure(HB_CONFIG_FILE_PATH);
+
+
+
+    public void cacheGetterFields(Cache cache) throws IOException, ClassNotFoundException, URISyntaxException {
+        Map<String, CdmModelFieldPropertyFromClass> modelClassMap = loadModelClassMap();
+
+        cache.removeAll();
+
+        for(Map.Entry<String, CdmModelFieldPropertyFromClass> entry : modelClassMap.entrySet()) {
+            cache.put(new Element(entry.getKey(), entry.getValue()));
+        }
+    }
+
+    public Map<String, CdmModelFieldPropertyFromClass> loadModelClassMap() throws URISyntaxException, IOException, ClassNotFoundException  {
+
+        Bundle bundle = Platform.getBundle("eu.etaxonomy.taxeditor.cdmlib");
+
+        URL modelMapFileURL = bundle.getEntry(CDM_MAP_SER_FILE_PATH);
+        File modelMapFile = new File(FileLocator.resolve(modelMapFileURL).toURI());
+
+        FileInputStream fin = new FileInputStream(modelMapFile);
+        ObjectInputStream ois = new ObjectInputStream(fin);
+        Map<String, CdmModelFieldPropertyFromClass> modelClassMap = (Map<String, CdmModelFieldPropertyFromClass>) ois.readObject();
+        ois.close();
+        return modelClassMap;
+    }
+
+
+    public Map<String, CdmModelFieldPropertyFromClass> generateModelClassMap() {
+
+        Configuration configuration = buildConfiguration(HB_CONFIG_FILE_PATH);
+        Iterator<PersistentClass> classMappingIterator =   configuration.getClassMappings();
+
+        Map<String, CdmModelFieldPropertyFromClass> modelClassMap = new HashMap<String, CdmModelFieldPropertyFromClass>();
+
+        while(classMappingIterator.hasNext()) {
+            PersistentClass persistentClass = classMappingIterator.next();
+            Class mappedClass = persistentClass.getMappedClass();
+            String mappedClassName = mappedClass.getName();
+
+            CdmModelFieldPropertyFromClass cmgmfc = new CdmModelFieldPropertyFromClass(mappedClassName);
+            System.out.println("Adding class : " + mappedClassName + " to cache");
+            addGetters(persistentClass, cmgmfc);
+            modelClassMap.put(mappedClassName, cmgmfc);
+        }
+        return modelClassMap;
+    }
+
+
+    public static Configuration buildConfiguration(String hibernateConfigFilePath) {
+        Configuration configuration = new Configuration().configure(hibernateConfigFilePath);
         configuration.buildMappings();
         return configuration;
-       }
-
-       public void cacheGetterFields(Cache cache) {
-
-           Configuration configuration = buildConfiguration();
-               Iterator<PersistentClass> classMappingIterator =   configuration.getClassMappings();
-
-               cache.removeAll();
-
-               while(classMappingIterator.hasNext()) {
-                       PersistentClass persistentClass = classMappingIterator.next();
-                       Class mappedClass = persistentClass.getMappedClass();
-                       String mappedClassName = mappedClass.getName();
-
-                       CdmModelFieldPropertyFromClass cmgmfc = new CdmModelFieldPropertyFromClass(mappedClassName);
-                       //logger.info("Adding class : " + mappedClassName + " to cache");
-                       addGetters(persistentClass, cmgmfc);
-                       cache.put(new Element(mappedClassName, cmgmfc));
-               }
-
-       }
-
-       private void addGetters(PersistentClass persistentClass, CdmModelFieldPropertyFromClass cmgmfc) {
-           if (persistentClass != null) {
-               Iterator propertyIt = persistentClass.getPropertyIterator();
-
-               while(propertyIt.hasNext())
-               {
-                   Property property = (Property)propertyIt.next();
-                   Getter getter = property.getGetter(persistentClass.getMappedClass());
-                   if(getter != null && getter.getMember() != null) {
-                       Field field = (Field)getter.getMember();
-
-                       //logger.info(" - contains field '" + field.getName() + "' of type '" + field.getType().getName() + "'");
-                       cmgmfc.addGetMethods(field.getName());
-                   }
-               }
-               addGetters(persistentClass.getSuperclass(), cmgmfc);
-           }
-       }
-
-       public void checkGetterMethods() {
-
-               Configuration configuration = new Configuration().configure(HB_CONFIG_FILE_PATH);
-               configuration.buildMappings();
-               Iterator<PersistentClass> classMappingIterator =   configuration.getClassMappings();
-
-               Cache cache = CdmRemoteCacheManager.getInstance().getCdmModelGetMethodsCache();
-               cache.removeAll();
-
-               while(classMappingIterator.hasNext()) {
-                       PersistentClass persistentClass = classMappingIterator.next();
-                       Class mappedClass = persistentClass.getMappedClass();
-                       String mappedClassName = mappedClass.getName();
-
-                       Iterator propertyIt = persistentClass.getPropertyIterator();
-
-                       Method[] methods = mappedClass.getMethods();
-
-                       while(propertyIt.hasNext())
-                       {
-                               Property property = (Property)propertyIt.next();
-                               Getter getter = property.getGetter(mappedClass);
-                               if(getter != null && getter.getMember() != null) {
-                                       Field field = (Field)getter.getMember();
-                                       String getMethod = getMethodNameFromFieldName(field.getName(), field.getType().getName());
-
-                                       boolean foundMethod = false;
-                                       for(Method method : methods) {
-                                               if(method.getName().equals(getMethod)) {
-                                                       foundMethod = true;
-                                                       break;
-                                               }
-                                       }
-                                       if(!foundMethod) {
-                                               logger.info("Inferred method " + getMethod + " does not exist in class " + mappedClassName);
-                                               //throw new CdmClientCacheException("Inferred method " + getMethod + " does not exist in class " + mappedClassName);
-                                       }
-                               }
-                       }
-
-
-               }
-
-       }
-
-       public List<CdmModelFieldPropertyFromClass> getCdmModelGetMethodFromClassList() {
-               cmgmfcList.clear();
-               Configuration configuration = new Configuration().configure(HB_CONFIG_FILE_PATH);
-               configuration.buildMappings();
-               Iterator<PersistentClass> classMappingIterator =   configuration.getClassMappings();
-
-               while(classMappingIterator.hasNext()) {
-                       PersistentClass persistentClass = classMappingIterator.next();
-                       Class mappedClass = persistentClass.getMappedClass();
-                       String mappedClassName = mappedClass.getName();
-
-                       CdmModelFieldPropertyFromClass cmgmfc = new CdmModelFieldPropertyFromClass(mappedClassName);
-                       Iterator propertyIt = persistentClass.getPropertyIterator();
-
-                       while(propertyIt.hasNext())
-                       {
-                               Property property = (Property)propertyIt.next();
-                               Getter getter = property.getGetter(mappedClass);
-                               if(getter != null && getter.getMember() != null) {
-                                       Field field = (Field)getter.getMember();
-                                       String getMethod = getMethodNameFromFieldName(getter.getMember().getName(),field.getType().getName());
-                                       cmgmfc.addGetMethods(getMethod);
-                               }
-                       }
-                       cmgmfcList.add(cmgmfc);
-               }
-               return cmgmfcList;
-
-       }
-
-       public static String getMethodNameFromFieldName(String fieldName, String type) {
-               String prefix = type != null && type.toLowerCase().endsWith("boolean") ? "is" : "get";
-               String getMethod =  prefix + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
-               return getMethod;
-       }
+    }
+
+    private void addGetters(PersistentClass persistentClass, CdmModelFieldPropertyFromClass cmgmfc) {
+        if (persistentClass != null) {
+            Iterator propertyIt = persistentClass.getPropertyIterator();
+
+            while(propertyIt.hasNext())
+            {
+                Property property = (Property)propertyIt.next();
+                Getter getter = property.getGetter(persistentClass.getMappedClass());
+                if(getter != null && getter.getMember() != null) {
+                    Field field = (Field)getter.getMember();
+
+                    //logger.info(" - contains field '" + field.getName() + "' of type '" + field.getType().getName() + "'");
+                    cmgmfc.addGetMethods(field.getName());
+                }
+            }
+            addGetters(persistentClass.getSuperclass(), cmgmfc);
+        }
+    }
+
+
+
+    public static void main(String argv[]) {
+
+        // To re-create the serialised cdm map run,
+        // mvn exec:java -Dexec.mainClass="eu.etaxonomy.taxeditor.remoting.cache.CdmModelCacher"
+        // in the eu.etaxonomy.taxeditor.cdmlib project root dir
+        String CDM_MAP_SER_DIR = "resources/";
+
+        CdmModelCacher cdmModelCacher = new CdmModelCacher();
+        Map<String, CdmModelFieldPropertyFromClass> modelClassMap = cdmModelCacher.generateModelClassMap();
+        try{
 
+            FileOutputStream fout = new FileOutputStream(CDM_MAP_SER_DIR + "cdm.map.ser");
+            ObjectOutputStream oos = new ObjectOutputStream(fout);
+            oos.writeObject(modelClassMap);
+            oos.close();
+            System.out.println("CDM Map serialized");
 
+        }catch(Exception ex){
+            ex.printStackTrace();
+        }
 
+    }
 
 
 }