${database.allClassCopyright}package ${glPackagePluginBuri};

import java.util.HashMap;
import java.util.Map;

import org.escafe.buri.common.util.ClassDefUtil;
import org.escafe.buri.compiler.util.BuriDataFieldCompilePreprocessor;
import org.escafe.buri.dataaccess.impl.BuriDataAccessFactoryImpl;
import org.escafe.buri.dataaccess.impl.ScriptDataAccessUtilLongKeyImpl;
import org.escafe.buri.oouo.internal.structure.BuriDataFieldType;
import org.seasar.coffee.dataaccess.DataAccessUtil;
import org.seasar.coffee.script.ScriptFactory;

/**
 * @author ${database.classAuthor}
 */
public class ${glFixedBuriDataAccessScriptFactoryImpl} extends BuriDataAccessFactoryImpl {

    // ===================================================================================
    //                                                                           Attribute
    //                                                                           =========
    private BuriDataFieldCompilePreprocessor preprocessor;
    private ScriptFactory scriptFactory;
    private ClassDefUtil classDefUtil;
    private Map<String, DataAccessUtil> classToUtil = new HashMap<String, DataAccessUtil>();

    // ===================================================================================
    //                                                                            Override
    //                                                                            ========
    @SuppressWarnings("unchecked")
    @Override
    public DataAccessUtil getDataAccessUtil(Class tgtClass) {
        String className = classDefUtil.getClassName(tgtClass);
        if (classToUtil.containsKey(className)) {
            return (DataAccessUtil) classToUtil.get(className);
        }
        BuriDataFieldType fieldType = new BuriDataFieldType();
        fieldType.setId(className);
        fieldType = preprocessor.preprocess(fieldType);
        ScriptDataAccessUtilLongKeyImpl dataAccessUtil = createScriptDataAccessUtilLongKeyImpl(fieldType);
        dataAccessUtil.setScriptFactory(scriptFactory);
        classToUtil.put(className, dataAccessUtil);
        return dataAccessUtil;
    }

    protected ScriptDataAccessUtilLongKeyImpl createScriptDataAccessUtilLongKeyImpl(BuriDataFieldType fieldType) {
        return new ${glFixedScriptDataAccessUtilKeyImpl}(fieldType);
    }

    @Override
    public void destroy() {
        dispose();
    }

    @Override
    public void dispose() {
        super.dispose();
        classToUtil.clear();
    }

    // ===================================================================================
    //                                                                            Accessor
    //                                                                            ========
    public BuriDataFieldCompilePreprocessor getPreprocessor() {
        return preprocessor;
    }

    public void setPreprocessor(BuriDataFieldCompilePreprocessor preprocessor) {
        this.preprocessor = preprocessor;
    }

    public ScriptFactory getScriptFactory() {
        return scriptFactory;
    }

    public void setScriptFactory(ScriptFactory scriptFactory) {
        this.scriptFactory = scriptFactory;
    }

    public ClassDefUtil getClassDefUtil() {
        return classDefUtil;
    }

    public void setClassDefUtil(ClassDefUtil classDefUtil) {
        this.classDefUtil = classDefUtil;
    }
}
