/*
 * Copyright 2006-2011 the Seasar Foundation and the Others.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */
package org.seasar.codegen.dbms;

import org.seasar.codegen.LanguageDataTypeSelectHelper;
import org.seasar.codegen.element.DataType;
import org.seasar.codegen.element.FieldSetting;
import org.seasar.codegen.exception.InternalGenerateException;

public class DB2 implements Dbms {

    private LanguageDataTypeSelectHelper langSelectUtil;

    public String convDBTypeToDataType(String typeName) {
        return typeName;
    }

    public DataType selectBestDataType(FieldSetting fieldSetting)
            throws InternalGenerateException {
        String typeName = fieldSetting.getTypeName();
        fieldSetting.setRdbType(fieldSetting.getTypeName());
        DataType dataType = null;
        if (typeName.startsWith("BIGINT")) {
            dataType = getAnyDataType(fieldSetting, "long");
        } else if (typeName.startsWith("BLOB")) {
            dataType = getAnyDataType(fieldSetting, "byte[]");
        } else if (typeName.startsWith("CHAR")) {
            // dataType = getAnyDataType(fieldSetting,"char");
            dataType = getAnyDataType(fieldSetting, "String");
        } else if (typeName.startsWith("CLOB")) {
            dataType = getAnyDataType(fieldSetting, "String");
        } else if (typeName.startsWith("DOUBLE")) {
            dataType = getAnyDataType(fieldSetting, "BigDecimal");
        } else if (typeName.startsWith("DOUBLE PRECISION")) {
            dataType = getAnyDataType(fieldSetting, "BigDecimal");
        } else if (typeName.startsWith("FLOAT")) {
            dataType = getAnyDataType(fieldSetting, "BigDecimal");
        } else if (typeName.startsWith("INTEGER")) {
            dataType = getAnyDataType(fieldSetting, "int");
        } else if (typeName.startsWith("LONG VARCHAR")) {
            dataType = getAnyDataType(fieldSetting, "String");
        } else if (typeName.startsWith("REAL")) {
            dataType = getAnyDataType(fieldSetting, "BigDecimal");
        } else if (typeName.startsWith("SMALLINT")) {
            dataType = getAnyDataType(fieldSetting, "short");
        } else if (typeName.startsWith("VARCHAR")) {
            dataType = getAnyDataType(fieldSetting, "String");
        } else if (typeName.startsWith("TIMESTAMP")) {
            dataType = getAnyDataType(fieldSetting, "Timestamp");
        } else if (typeName.startsWith("TIME")) {
            dataType = getAnyDataType(fieldSetting, "Time");
        } else if (typeName.startsWith("DATE")) {
            dataType = getAnyDataType(fieldSetting, "Date");
        } else if (typeName.startsWith("DECIMAL")) {
            dataType = getDECIMAL(fieldSetting);
        } else if (typeName.startsWith("NUMERIC")) {
            dataType = getDECIMAL(fieldSetting);
        } else {
            throw new InternalGenerateException(fieldSetting.getTypeName());
        }

        return dataType;
    }

    protected DataType getDECIMAL(FieldSetting fieldSetting) {
        String dataType = null;
        if (fieldSetting.getPointNumber() == 0) {
            if (fieldSetting.getColmnSize() == 1) {
                dataType = "boolean";
            } else if (fieldSetting.getColmnSize() <= 9) {
                dataType = "int";
            } else if (fieldSetting.getColmnSize() <= 18) {
                dataType = "long";
            } else {
                dataType = "BigInteger";
            }
        } else {
            // if(fieldSetting.getColmnSize() <= 7) {
            // dataType = "BigDecimal";
            // } else if(fieldSetting.getColmnSize() <= 15) {
            // dataType = "BigDecimal";
            // } else {
            dataType = "BigDecimal";
            // }
        }
        return getAnyDataType(fieldSetting, dataType);
    }

    protected DataType getAnyDataType(FieldSetting fieldSetting,
            String javaDataType) {
        DataType dataType = new DataType(fieldSetting);
        dataType.setLangDataType(langSelectUtil.getDataType(javaDataType,
                fieldSetting.isNotNull(), fieldSetting.getFieldDefault()));
        return dataType;
    }

    public LanguageDataTypeSelectHelper getLangSelectUtil() {
        return langSelectUtil;
    }

    public void setLangSelectUtil(LanguageDataTypeSelectHelper langSelectUtil) {
        this.langSelectUtil = langSelectUtil;
    }

    public String getSuffix() {
        return "db2";
    }

}
