Package org.tamacat.dao.rdb.internal

Source Code of org.tamacat.dao.rdb.internal.ORMapper

/*
* Copyright (c) 2008, TamaCat.org
* All rights reserved.
*/
package org.tamacat.dao.rdb.internal;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;

import org.tamacat.dao.DaoException;
import org.tamacat.dao.rdb.ORMappingSupport;
import org.tamacat.dao.rdb.RdbColumnMetaData;
import org.tamacat.dao.rdb.RdbDataType;
import org.tamacat.dao.rdb.util.MappingUtils;
import org.tamacat.di.DI;
import org.tamacat.di.DIContainerException;
import org.tamacat.util.ClassUtils;

public class ORMapper<T extends ORMappingSupport> {

    private String xml = "orm.xml";

    private String name;

    private T data;
    private Class<T> type;

    public ORMapper() {}

    public ORMapper(T data) {
        this.data = data;
    }

    public void setPrototype(String name) {
        this.name = name;
    }

    public void setPrototype(T protptype) {
        this.data = protptype;
    }

    public void setPrototype(Class<T> type) {
        this.type = type;
    }

    public void setMappingXml(String xml) {
        this.xml = xml;
    }

    private T createPrototype() {
        T o = null;
        if (name != null) {
            T obj = DI.configure(xml).getBean(name, type);
            if (obj == null) {
              throw new DIContainerException(name + " is not found.[" + xml + "]");
            }
        }
        if (o == null && type != null) {
            o = ClassUtils.newInstance(type);
        }
        return o;
    }

    public T getMappedObject() {
        if (data != null) {
            return data;
        } else {
            return createPrototype();
        }
    }

    public ORMapper<T> mapping(Collection<RdbColumnMetaData> columns, ResultSet rs) {
        data = createPrototype();
        try {
            int index = 1;
            for (RdbColumnMetaData column : columns) {
                RdbDataType type = column.getType();
                data.mapping(column, MappingUtils.mapping(type, rs, index));
                index++;
            }
        } catch (SQLException e) {
            throw new DaoException(e);
        }
        return this;
    }
}
TOP

Related Classes of org.tamacat.dao.rdb.internal.ORMapper

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.