Skip to content
Snippets Groups Projects
Select Git revision
  • master default
  • ember-ui
2 results

ElasticSerializer.java

Blame
  • user avatar
    Eike Cochu authored
    removed lucene and custom processors
    added debug switch to enable extensive productive logging
    added silent switch to mute all logging
    replaced cmd project slf4j loggers with log4j loggers
    simplified cmd exception structure
    added catching mongo and elasticsearch connection exceptions with explicit error messages
    removed delete cmd
    removed lucene dependency, clashes with elasticsearch lucene 5.0 dependency in executable jar
    deleted unnecessary jardesc
    removed log4j dependency from utils project
    renamed database constants
    added elasticsearch constants
    added database and elasticsearch config convenience functions
    added missing database generic types
    added test command to test connections to database and elasticsearch
    d23a0528
    History
    Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    ElasticSerializer.java 1.99 KiB
    package de.vipra.util;
    
    import java.lang.reflect.Field;
    import java.lang.reflect.InvocationTargetException;
    import java.lang.reflect.Method;
    import java.lang.reflect.Modifier;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.Map.Entry;
    import java.util.Set;
    
    import de.vipra.util.an.ElasticIndex;
    
    public class ElasticSerializer<T> {
    
    	private Set<Entry<String, Field>> fields;
    	private Set<Entry<String, Method>> methods;
    
    	public ElasticSerializer(Class<T> clazz) {
    		Map<String, Field> foundFields = new HashMap<>();
    		Map<String, Method> foundMethods = new HashMap<>();
    
    		for (Field field : clazz.getDeclaredFields()) {
    			if (!Modifier.isStatic(field.getModifiers())) {
    				if (!field.isAccessible())
    					field.setAccessible(true);
    
    				ElasticIndex ei = field.getDeclaredAnnotation(ElasticIndex.class);
    				if (ei == null)
    					continue;
    
    				foundFields.put(ei.value(), field);
    			}
    		}
    
    		for (Method method : clazz.getDeclaredMethods()) {
    			if (!Modifier.isStatic(method.getModifiers()) && method.getParameterCount() == 0) {
    				if (!method.isAccessible())
    					method.setAccessible(true);
    
    				ElasticIndex ei = method.getDeclaredAnnotation(ElasticIndex.class);
    				if (ei == null)
    					continue;
    
    				foundMethods.put(ei.value(), method);
    			}
    		}
    
    		this.fields = foundFields.entrySet();
    		this.methods = foundMethods.entrySet();
    	}
    
    	public Map<String, Object> serialize(T t) {
    		Map<String, Object> values = new HashMap<>();
    		for (Entry<String, Field> e : fields) {
    			try {
    				values.put(e.getKey(), e.getValue().get(t));
    			} catch (IllegalArgumentException | IllegalAccessException e1) {
    				throw new RuntimeException("could not serialize field value " + e.getKey(), e1);
    			}
    		}
    		for (Entry<String, Method> m : methods) {
    			try {
    				values.put(m.getKey(), m.getValue().invoke(t));
    			} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e1) {
    				throw new RuntimeException("could not serialize method value " + m.getKey(), e1);
    			}
    		}
    		return values;
    	}
    
    }