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

com.infomaximum.database.schema.dbstruct.DBField Maven / Gradle / Ivy

The newest version!
package com.infomaximum.database.schema.dbstruct;

import com.infomaximum.database.exception.SchemaException;
import net.minidev.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Serializable;

public class DBField extends DBObject {

    private final static Logger log = LoggerFactory.getLogger(DBField.class);
    private static final String JSON_PROP_NAME = "name";
    private static final String JSON_PROP_TYPE = "type";
    private static final String JSON_PROP_FOREIGN_TABLE_ID = "foreign_table_id";

    private String name;
    private Class type;
    private Integer foreignTableId;

    DBField(int id, String name, Class type, Integer foreignTableId) {
        super(id);
        this.name = name;
        this.type = type;
        this.foreignTableId = foreignTableId;
    }

    public String getName() {
        return name;
    }

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

    public Class getType() {
        return type;
    }

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

    public Integer getForeignTableId() {
        return foreignTableId;
    }

    public void setForeignTableId(Integer foreignTableId) {
        this.foreignTableId = foreignTableId;
    }

    public boolean isForeignKey() {
        return foreignTableId != null;
    }

    static DBField fromJson(JSONObject source) throws SchemaException {
        return new DBField(
                JsonUtils.getValue(JSON_PROP_ID, Integer.class, source),
                JsonUtils.getValue(JSON_PROP_NAME, String.class, source),
                resolve(JsonUtils.getValue(JSON_PROP_TYPE, String.class, source)),
                JsonUtils.getValueOrDefault(JSON_PROP_FOREIGN_TABLE_ID, Integer.class, source, null)
        );
    }

    @Override
    JSONObject toJson() {
        JSONObject object = new JSONObject();
        object.put(JSON_PROP_ID, getId());
        object.put(JSON_PROP_NAME, name);
        object.put(JSON_PROP_TYPE, type.getName());
        object.put(JSON_PROP_FOREIGN_TABLE_ID, foreignTableId);
        return object;
    }

    private static Class resolve(String type) throws SchemaException {
        try {
            return (Class) Class.forName(type);
        } catch (ClassNotFoundException e) {
            log.warn("Class not found for type: " + type);
            return String.class;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy