package foundation.data.meta.field;
|
|
import java.util.HashSet;
|
import java.util.Set;
|
|
import foundation.data.entity.Entity;
|
import foundation.translator.ITranslator;
|
import foundation.translator.ValueLength;
|
import foundation.translator.ValueType;
|
|
|
public class Field {
|
|
private static Set<Integer> sizableType;
|
|
private int indexNo;
|
private String name;
|
private String originalName;
|
private int sqlType;
|
private ValueType type;
|
private ValueLength length;
|
private boolean nullable;
|
private String remark;
|
private ITranslator valueTranslator;
|
private FieldExtension extension;
|
private FieldWriter writer;
|
|
static {
|
sizableType = new HashSet<Integer>();
|
}
|
|
public Field(String name) {
|
this.name = name;
|
this.originalName = name;
|
this.nullable = true;
|
}
|
|
public void load(IFieldReader fieldReader) throws Exception {
|
setType(fieldReader.getFieldType());
|
setLength(fieldReader.getFieldLength());
|
setNullable(fieldReader.getNullable());
|
|
remark = fieldReader.getRemark();
|
}
|
|
public Entity toEntity(Entity entity) {
|
entity.set("field", name);
|
|
if (entity.isAbsentValue("label_chinese")) {
|
entity.set("label_chinese", remark);
|
}
|
|
if (entity.isAbsentValue("label_english")) {
|
entity.set("label_english", name);
|
}
|
|
return entity;
|
}
|
|
public FieldWriter getWriter() {
|
if (writer != null) {
|
return writer;
|
}
|
|
synchronized (this) {
|
if (writer != null) {
|
return writer;
|
}
|
|
writer = FieldWriter.newInstance(name, indexNo, valueTranslator, extension);
|
}
|
|
return writer;
|
}
|
|
public void initLengthByType() {
|
this.length = type.getDefaultLength();
|
}
|
|
public void setType(ValueType value) {
|
this.type = value;
|
this.sqlType = value.toSQLType();
|
}
|
|
public void setType(int value) {
|
this.sqlType = value;
|
this.type = ValueType.parse(value);
|
}
|
|
public void setType(String value) {
|
this.type = ValueType.parse(value);
|
this.sqlType = type.toSQLType();
|
}
|
|
public void setIndexNo(int value) {
|
this.indexNo = value;
|
}
|
|
public void setLength(int value) {
|
this.length = new ValueLength(value);
|
}
|
|
public void setLength(ValueLength length) {
|
this.length = length;
|
}
|
|
public void setLength(String value) {
|
this.length = ValueLength.getInstance(value);
|
}
|
|
public void setNullable(boolean nullable) {
|
this.nullable = nullable;
|
}
|
|
public void setTranslator(ITranslator translator) {
|
this.valueTranslator = translator;
|
}
|
|
public String getName() {
|
return this.name;
|
}
|
|
public String getOriginalName() {
|
return originalName;
|
}
|
|
public void setOriginalName(String originalName) {
|
this.originalName = originalName;
|
}
|
|
public ValueType getType() {
|
return this.type;
|
}
|
|
public int getSQLType() {
|
return this.sqlType;
|
}
|
|
public String getDBType() {
|
return type.toDBType();
|
}
|
|
public String getTypeSQLSegment() {
|
String result = getDBType();
|
|
if (sizableType.contains(this.sqlType)) {
|
result = result + length.toSQLSegment();
|
}
|
|
return result;
|
}
|
|
public boolean isNullable() {
|
return this.nullable;
|
}
|
|
public String getNullableSQLSegment() {
|
return !this.nullable ? "NOT NULL" : "NULL";
|
}
|
|
public int getIndexNo() {
|
return this.indexNo;
|
}
|
|
public int getSqlType() {
|
return sqlType;
|
}
|
|
public FieldExtension getExtention() {
|
return extension;
|
}
|
|
public void setExtension(FieldExtension extension) {
|
this.extension = extension;
|
}
|
|
public ITranslator getValueTranslator() {
|
return valueTranslator;
|
}
|
|
public String getLabelChinese() {
|
if (extension == null) {
|
return name;
|
}
|
|
return extension.getLabelChinese();
|
}
|
|
public String getLabelEnglish() {
|
if (extension == null) {
|
return name;
|
}
|
|
return extension.getLabelEnglish();
|
}
|
|
public boolean hasDefaultValue() {
|
if (extension == null) {
|
return false;
|
}
|
|
return extension.hasDefaultValue();
|
}
|
|
public String getDefaultValue() {
|
if (extension == null) {
|
return null;
|
}
|
|
return extension.getDefaultValue();
|
}
|
|
public boolean hasTranslator() {
|
if (extension == null) {
|
return false;
|
}
|
|
return extension.hasTranslator();
|
}
|
|
public boolean hasValidateRegex() {
|
if (extension == null) {
|
return false;
|
}
|
|
return extension.hasValidateRegex();
|
}
|
|
public String getValidateRegex() {
|
if (extension == null) {
|
return null;
|
}
|
|
return extension.getValidateRegex();
|
}
|
|
public boolean isExportable() {
|
if (extension == null) {
|
return true;
|
}
|
|
return extension.isExportable();
|
}
|
|
public boolean isImportable() {
|
if (extension == null) {
|
return true;
|
}
|
|
return extension.isImportable();
|
}
|
|
public boolean isNumber() {
|
return type.isNumber();
|
}
|
|
public boolean isDate() {
|
return type.isDate();
|
}
|
|
public String getRemark() {
|
return remark;
|
}
|
|
public String toString() {
|
StringBuilder result = new StringBuilder();
|
result.append("fieldName=").append(this.name).append(",");
|
result.append("dataType=").append(this.type).append(",");
|
result.append("length=").append(this.length);
|
return result.toString();
|
}
|
|
public void toString(StringBuilder result) {
|
result.append("fieldName=").append(this.name).append(",");
|
result.append("dataType=").append(this.type).append(",");
|
result.append("length=").append(this.length);
|
}
|
|
}
|