UserDefinedTypeMetaColumn.java :  » Database-ORM » ODAL » com » completex » objective » components » persistency » Java Open Source

Java Open Source » Database ORM » ODAL 
ODAL » com » completex » objective » components » persistency » UserDefinedTypeMetaColumn.java
/**
 *  Objective Database Abstraction Layer (ODAL)
 *  Copyright (c) 2004, The ODAL Development Group
 *  All rights reserved.
 *  For definition of the ODAL Development Group please refer to LICENCE.txt file
 *
 *  Distributable under LGPL license.
 *  See terms of license at gnu.org.
 */
package com.completex.objective.components.persistency;

import com.completex.objective.components.persistency.type.TypeHandler;
import com.completex.objective.util.StringUtil;
import com.completex.objective.util.TypeUtil;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Under construction 
 *
 * @author Gennady Krizhevsky
 */
public class UserDefinedTypeMetaColumn implements ModelConsts {
    protected int columnIndex = -1;
    protected String columnName;
    protected String columnAlias;
    protected int decimalDigits;
    protected int columnSize;

    protected UserDefinedTypeMetaTable table;
    protected ColumnType type;
    protected TypeHandler typeHandler;
    protected int jdbcType;
    protected String typeName;
    protected boolean existInSuperType;
    protected boolean exclude;
    protected boolean transformed;

    public UserDefinedTypeMetaColumn() {
    }

    public UserDefinedTypeMetaColumn(String columnName, UserDefinedTypeMetaTable table) {
        this.columnName = columnName;
        this.table = table;
    }

    public boolean isExistInSuperType() {
        return existInSuperType;
    }

    public void setExistInSuperType(boolean existInSuperType) {
        this.existInSuperType = existInSuperType;
    }

    public int getColumnIndex() {
        return columnIndex;
    }

    public void setColumnIndex(int columnIndex) {
        this.columnIndex = columnIndex;
    }

    public String getColumnName() {
        return columnName;
    }

    public void setColumnName(String columnName) {
        this.columnName = columnName;
    }

    public String getColumnAlias() {
        return columnAlias;
    }

    public void setColumnAlias(String columnAlias) {
        this.columnAlias = columnAlias;
    }

    public int getDecimalDigits() {
        return decimalDigits;
    }

    public void setDecimalDigits(int decimalDigits) {
        this.decimalDigits = decimalDigits;
    }

    public int getColumnSize() {
        return columnSize;
    }

    public void setColumnSize(int columnSize) {
        this.columnSize = columnSize;
    }

    public UserDefinedTypeMetaTable getTable() {
        return table;
    }

    public void setTable(UserDefinedTypeMetaTable table) {
        this.table = table;
    }

    public ColumnType getType() {
        return type;
    }

    public void setType(ColumnType type) {
        this.type = type;
    }

    public TypeHandler getTypeHandler() {
        return typeHandler;
    }

    public void setTypeHandler(TypeHandler typeHandler) {
        this.typeHandler = typeHandler;
    }

    public int getJdbcType() {
        return jdbcType;
    }

    public void setJdbcType(int jdbcType) {
        this.jdbcType = jdbcType;
    }

    public String getTypeName() {
        return typeName;
    }

    public void setTypeName(String typeName) {
        this.typeName = typeName;
    }

    public boolean isExclude() {
        return exclude;
    }

    public void setExclude(boolean exclude) {
        this.exclude = exclude;
    }

    public boolean isTransformed() {
        return transformed;
    }

    public void setTransformed(boolean transformed) {
        this.transformed = transformed;
    }

    static String line(int indent, String line) {
        return StringUtil.indent(indent, line + EOL);
    }

    static String line0(int indent, String line) {
        return StringUtil.indent(indent, line);
    }

    public Map toInternalMap() {
        final Map columnMap = new LinkedHashMap();
        columnMap.put(COLUMN_NAME_TAG, columnName);
        columnMap.put(COLUMN_INDEX_TAG, new Integer(columnIndex));
        columnMap.put(COLUMN_SIZE_TAG, new Integer(columnSize));
        columnMap.put(DECIMAL_DIGITS_TAG, new Integer(decimalDigits));
        columnMap.put(JDBC_TYPE_TAG, new Integer(jdbcType));
        columnMap.put(TYPE_NAME_TAG, typeName);
        columnMap.put(EXIST_IN_SUPER_TYPE_TAG, Boolean.valueOf(existInSuperType));

        return columnMap;
    }

    public void fromInternalMap(Map columnMap) {
        columnName = (String) columnMap.get(COLUMN_NAME_TAG);
        columnIndex = extractInt(columnMap, COLUMN_INDEX_TAG);
        columnSize = extractInt(columnMap, COLUMN_SIZE_TAG);
        decimalDigits = extractInt(columnMap, DECIMAL_DIGITS_TAG);
        jdbcType = extractInt(columnMap, JDBC_TYPE_TAG);
        typeName = (String) columnMap.get(TYPE_NAME_TAG);
        existInSuperType = extractBoolean(columnMap, EXIST_IN_SUPER_TYPE_TAG);
    }

    public Map toExternalMap() {
        final Map columnMap = new LinkedHashMap();
        columnMap.put(COLUMN_NAME_TAG, columnName);
        columnMap.put(COLUMN_ALIAS_TAG, columnAlias == null ? columnName : columnAlias);
        String typeName = ColumnType.contains(type.getName()) ? type.getName() : type.getClass().getName();
        columnMap.put(DATA_TYPE_TAG, typeName);
        columnMap.put(EXCLUDE_TAG, Boolean.valueOf(exclude));
        columnMap.put(TRANSFORMED_TAG, Boolean.valueOf(transformed));
        return columnMap;
    }

    public void fromExternalMap(Map columnMap) {
        columnAlias = (String) columnMap.get(COLUMN_ALIAS_TAG);
        columnName = columnName != null ? columnName : (String) columnMap.get(COLUMN_NAME_TAG);
        String typeValue = (String) columnMap.get(DATA_TYPE_TAG);
        if (ColumnType.contains(typeValue)) {
            type = ColumnType.toColumnType((String) columnMap.get(DATA_TYPE_TAG));
        } else {
            try {
                Object o = Class.forName(typeValue).newInstance();
                type = (ColumnType) o;
            } catch (ClassNotFoundException e) {
                throw new RuntimeException("Class not found: " + typeValue, e);
            } catch (IllegalAccessException e) {
                throw new RuntimeException("IllegalAccessException for class: " + typeValue, e);
            } catch (InstantiationException e) {
                throw new RuntimeException("Cannot instantiate class: " + typeValue, e);
            }
        }
        exclude = extractBoolean(columnMap, EXCLUDE_TAG);
        transformed = extractBoolean(columnMap, TRANSFORMED_TAG);
    }



    private boolean extractBoolean(Map columnMap, String tag) {
        return TypeUtil.extractBoolean(columnMap, tag);
    }

    private int extractInt(Map columnMap, String columnIndexTag) {
        return TypeUtil.extractInt(columnMap, columnIndexTag);
    }

    public String toString() {
        return "{columnName = " + columnName
                + "; columnAlias = " + columnAlias
                + "; columnIndex = " + columnIndex
                + "; type  = " + type
                + "; typeName  = " + typeName
                + "; jdbcType  = " + jdbcType
                + "; columnSize = " + columnSize
                + "; decimalDigits = " + decimalDigits
                + "; exclude = " + exclude
                + "; transformed = " + transformed
                + "}";
    }

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.