All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.sippnex.firemaw.FiremawProcessor Maven / Gradle / Ivy

There is a newer version: 1.6.0
Show newest version
package com.sippnex.firemaw;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class FiremawProcessor implements IFiremawProcessor {

    public FiremawDto serialize(Object object) {

        try {

            FiremawDto firemawEntity = new FiremawDto();

            // Process class field annotations, including all super classes
            Class currentClass = object.getClass();
            do {
                List currentPropertyList = new ArrayList();
                for(Field field: currentClass.getDeclaredFields()) {
                    FiremawPropertyClass property = this.serializeField(object, field);
                    if(property != null) {
                        currentPropertyList.add(property);
                    }
                }
                firemawEntity.addPropertyRange(0, currentPropertyList);
                currentClass = currentClass.getSuperclass();
            } while(currentClass != null);

            return firemawEntity;

        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }

    }


    public Object deserialize(FiremawDto firemawDto, Class clazz) {

        try {

            Object object = clazz.newInstance();
            List firemawPropertyClasses = firemawDto.getProperties();

            // Process class field annotations, including all super classes
            Class currentClass = clazz;
            do {
                List currentPropertyList = new ArrayList();
                for(Field field: currentClass.getDeclaredFields()) {
                    Object value = this.deserializeField(firemawDto, field);
                    if(value != null) {
                        field.set(object, value);
                    }
                }
                currentClass = currentClass.getSuperclass();
            } while(currentClass != null);

            return object;

        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }

    }

    private FiremawPropertyClass serializeField(Object object, Field field) throws Exception {

        field.setAccessible(true);
        FiremawProperty firemawProperty = field.getAnnotation(FiremawProperty.class);

        if(firemawProperty != null) {

            if(firemawProperty.type() == FiremawType.SubEntity) {

                Class clazz = field.get(object).getClass();
                FiremawDto firemawDto = this.serialize(field.get(object));
                return new FiremawPropertyClass(firemawProperty, firemawDto);

            } else if(firemawProperty.type() == FiremawType.SubEntityList) {

                List subEntityList = (List)field.get(object);
                List firemawDtoList = new ArrayList();
                for(int i=0; i firemawDtoList = (List)firemawPropertyClass.getValue();
                    List subEntityList = new ArrayList();
                    for(int i=0; i) clazz, (String)firemawPropertyClass.getValue());

                } else if(firemawProperty.type() == FiremawType.NumberField) {

                    Class clazz = field.getType();
                    if(field.getType() == Long.class && firemawPropertyClass.getValue() instanceof Integer) {
                        return new Long((Integer)firemawPropertyClass.getValue());
                    } else {
                        return firemawPropertyClass.getValue();
                    }

                } else {

                    return firemawPropertyClass.getValue();

                }
            }
        }
        return null;
    }

}