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

com.wadpam.gaelic.converter.BaseConverter Maven / Gradle / Ivy

/*
 * INSERT COPYRIGHT HERE
 */

package com.wadpam.gaelic.converter;

import com.wadpam.gaelic.json.JBaseObject;
import com.wadpam.gaelic.json.JCursorPage;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.Map;

/**
 *
 * @author sosandstrom
 */
public abstract class BaseConverter {
    
    protected final Class jsonClass;
    protected final Class domainClass;

    public BaseConverter(Class jsonClass, Class domainClass) {
        this.jsonClass = jsonClass;
        this.domainClass = domainClass;
    }

    // --------------- Converter methods --------------------------
    
    public J convertDomain(T from) {
        if (null == from) {
            return null;
        }
        J to = createJson();
        convertDomain(from, to);
        return to;
    }
    
    public T convertJson(J from) {
        if (null == from) {
            return null;
        }
        T to = createDomain();
        convertJson(from, to);
        return to;
    }

    public J convertMap(Map from) {
        if (null == from) {
            return null;
        }
        J to = createJson();
        convertJMap(from, to);
        return to;
    }

    public abstract void convertDomain(T from, J to);
    public abstract void convertJson(J from, T to);
    
    /**
     * Override to get upsertBatch to work!
     * @param from is a LinkedHashMap created by Jackson
     */
    public void convertJMap(Map from, J to) {
        throw new UnsupportedOperationException("Override in subclass!"); 
    }
    
    public J createJson() {
        try {
            return (J) jsonClass.newInstance();
        } catch (InstantiationException ex) {
        } catch (IllegalAccessException ex) {
        }
        return null;
    }
    
    public T createDomain() {
        try {
            return (T) domainClass.newInstance();
        } catch (InstantiationException ex) {
        } catch (IllegalAccessException ex) {
        }
        return null;
    }
    
    public static void convertJCreatedUpdated(Map from, JBaseObject to) {
        if (null == from || null == to) {
            return;
        }

        to.setCreatedBy((String)from.get("createdBy"));
        to.setCreatedDate((Long)from.get("createdDate"));
        to.setUpdatedBy((String)from.get("updatedBy"));
        to.setUpdatedDate((Long)from.get("updatedDate"));
    }
    
    // Convert iterable
    public Collection convertDomains(Iterable from) {
        if (null == from)
            return new ArrayList();

        final Collection returnValue = new ArrayList();

        J to;
        for (T o : from) {
            to = convertDomain(o);
            returnValue.add(to);
        }
        
        return returnValue;
    }

    public ArrayList convertJSONs(Iterable from) {
        final ArrayList returnValue = new ArrayList();
        if (null == from) {
            return returnValue;
        }

        T to;
        for (J o : from) {
            to = convertJson(o);
            returnValue.add(to);
        }
        
        return returnValue;
    }

    public JCursorPage convertDomainPage(JCursorPage from) {
        final JCursorPage to = new JCursorPage();
        
        to.setPageSize(from.getItems().size());
        to.setCursorKey(from.getCursorKey());
        to.setTotalSize(from.getTotalSize());
        to.setItems(convertDomains(from.getItems()));
        
        return to;
    }

    public JCursorPage convertJsonPage(JCursorPage from) {
        final JCursorPage to = new JCursorPage();
        
        to.setPageSize(from.getItems().size());
        to.setCursorKey(from.getCursorKey());
        to.setTotalSize(from.getTotalSize());
        to.setItems(convertJSONs(from.getItems()));
        
        return to;
    }

    protected Collection getInnerParameterNames() {
        return Collections.EMPTY_LIST;
    }

    public static Long toLong(Date from) {
        if (null == from) {
            return null;
        }
        return from.getTime();
    }

    public static Long toLong(String from) {
        if (null == from) {
            return null;
        }
        return Long.parseLong(from);
    }

    public static Date toDate(Long from) {
        if (null == from) {
            return null;
        }
        return new Date(from);
    }

    public static String toString(Long from) {
        if (null == from) {
            return null;
        }
        return Long.toString(from);
    }

    public static Collection toLongs(Collection from) {
        if (null == from) {
            return null;
        }

        final Collection to = new ArrayList();

        for(String s : from) {
            try {
                to.add(Long.parseLong(s));
            }
            catch (NumberFormatException sometimes) {
                to.add(null);
            }
        }

        return to;
    }

    public static Collection toString(Collection from) {
        if (null == from) {
            return null;
        }

        final Collection to = new ArrayList(from.size());

        for(Long l : from) {
            to.add(l.toString());
        }

        return to;
    }
    
    
    
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy