use of ch.interlis.ili2c.metamodel.TextType in project ili2db by claeis.
the class PostgisColumnConverter method fromIomArray.
@Override
public Object fromIomArray(ch.interlis.ili2c.metamodel.AttributeDef attr, String[] iomValues, EnumCodeMapper enumTypes) throws SQLException, ConverterException {
java.sql.Array array = null;
ch.interlis.ili2c.metamodel.Type type = attr.getDomainResolvingAliases();
if (attr.isDomainBoolean()) {
Boolean[] values = new Boolean[iomValues.length];
for (int i = 0; i < values.length; i++) {
if (iomValues[i].equals("true")) {
values[i] = true;
} else {
values[i] = false;
}
}
array = conn.createArrayOf("bool", values);
} else if (attr.isDomainIli1Date()) {
java.sql.Date[] values = new java.sql.Date[iomValues.length];
for (int i = 0; i < values.length; i++) {
String iomValue = iomValues[i];
GregorianCalendar gdate = new GregorianCalendar(Integer.parseInt(iomValue.substring(0, 4)), Integer.parseInt(iomValue.substring(4, 6)) - 1, Integer.parseInt(iomValue.substring(6, 8)));
values[i] = new java.sql.Date(gdate.getTimeInMillis());
}
array = conn.createArrayOf("DATE", values);
} else if (attr.isDomainIliUuid()) {
Object[] values = new Object[iomValues.length];
for (int i = 0; i < values.length; i++) {
String iomValue = iomValues[i];
values[i] = fromIomUuid(iomValue);
}
array = conn.createArrayOf("uuid", values);
} else if (attr.isDomainIli2Date()) {
java.sql.Date[] values = new java.sql.Date[iomValues.length];
for (int i = 0; i < values.length; i++) {
String iomValue = iomValues[i];
XMLGregorianCalendar xmldate;
try {
xmldate = javax.xml.datatype.DatatypeFactory.newInstance().newXMLGregorianCalendar(iomValue);
} catch (DatatypeConfigurationException e) {
throw new ConverterException(e);
}
values[i] = new java.sql.Date(xmldate.toGregorianCalendar().getTimeInMillis());
}
array = conn.createArrayOf("DATE", values);
} else if (attr.isDomainIli2DateTime()) {
java.sql.Timestamp[] values = new java.sql.Timestamp[iomValues.length];
for (int i = 0; i < values.length; i++) {
String iomValue = iomValues[i];
XMLGregorianCalendar xmldate;
try {
xmldate = javax.xml.datatype.DatatypeFactory.newInstance().newXMLGregorianCalendar(iomValue);
} catch (DatatypeConfigurationException e) {
throw new ConverterException(e);
}
values[i] = new java.sql.Timestamp(xmldate.toGregorianCalendar().getTimeInMillis());
}
array = conn.createArrayOf("TIMESTAMP", values);
} else if (attr.isDomainIli2Time()) {
java.sql.Time[] values = new java.sql.Time[iomValues.length];
for (int i = 0; i < values.length; i++) {
String iomValue = iomValues[i];
XMLGregorianCalendar xmldate;
try {
xmldate = javax.xml.datatype.DatatypeFactory.newInstance().newXMLGregorianCalendar(iomValue);
} catch (DatatypeConfigurationException e) {
throw new ConverterException(e);
}
values[i] = new java.sql.Time(xmldate.toGregorianCalendar().getTimeInMillis());
}
array = conn.createArrayOf("TIME", values);
} else if (type instanceof EnumerationType) {
if (createEnumColAsItfCode) {
Integer[] values = new Integer[iomValues.length];
for (int i = 0; i < values.length; i++) {
String iomValue = iomValues[i];
int itfCode = Integer.parseInt(enumTypes.mapXtfCode2ItfCode((EnumerationType) type, iomValue));
values[i] = itfCode;
}
array = conn.createArrayOf("INTEGER", values);
} else {
array = conn.createArrayOf("VARCHAR", iomValues);
}
} else if (type instanceof NumericType) {
if (type.isAbstract()) {
} else {
PrecisionDecimal min = ((NumericType) type).getMinimum();
PrecisionDecimal max = ((NumericType) type).getMaximum();
if (min.getAccuracy() > 0) {
Double[] values = new Double[iomValues.length];
for (int i = 0; i < values.length; i++) {
String iomValue = iomValues[i];
double value = Double.parseDouble(iomValue);
values[i] = value;
}
array = conn.createArrayOf("DECIMAL", values);
} else {
Integer[] values = new Integer[iomValues.length];
for (int i = 0; i < values.length; i++) {
String iomValue = iomValues[i];
int value = (int) Math.round(Double.parseDouble(iomValue));
values[i] = value;
}
array = conn.createArrayOf("INTEGER", values);
}
}
} else if (type instanceof TextType) {
array = conn.createArrayOf("VARCHAR", iomValues);
} else if (type instanceof BlackboxType) {
if (((BlackboxType) type).getKind() == BlackboxType.eXML) {
Object[] values = new Object[iomValues.length];
for (int i = 0; i < values.length; i++) {
String iomValue = iomValues[i];
values[i] = fromIomXml(iomValue);
}
array = conn.createArrayOf("xml", values);
} else {
Object[] values = new Object[iomValues.length];
for (int i = 0; i < values.length; i++) {
String iomValue = iomValues[i];
values[i] = fromIomBlob(iomValue);
}
array = conn.createArrayOf("bytea", values);
}
} else {
throw new IllegalArgumentException(attr.getScopedName());
}
return array;
}
use of ch.interlis.ili2c.metamodel.TextType in project ili2db by claeis.
the class FromIliRecordConverter method createSimpleDbCol.
private boolean createSimpleDbCol(DbTable dbTable, Viewable aclass, AttributeDef attr, Type type, Holder<DbColumn> dbCol, Holder<Unit> unitDef, Holder<Boolean> mText, ArrayList<DbColumn> dbColExts) {
if (attr.isDomainBoolean()) {
dbCol.value = new DbColBoolean();
} else if (attr.isDomainIli1Date()) {
dbCol.value = new DbColDate();
} else if (attr.isDomainIliUuid()) {
dbCol.value = new DbColUuid();
} else if (attr.isDomainIli2Date()) {
dbCol.value = new DbColDate();
} else if (attr.isDomainIli2DateTime()) {
dbCol.value = new DbColDateTime();
} else if (attr.isDomainIli2Time()) {
dbCol.value = new DbColTime();
} else if (type instanceof BasketType) {
// skip it; type no longer exists in ili 2.3
dbCol.value = null;
} else if (type instanceof EnumerationType) {
visitedEnumsAttrs.add(attr);
if (createEnumColAsItfCode) {
DbColId ret = new DbColId();
dbCol.value = ret;
} else {
DbColVarchar ret = new DbColVarchar();
ret.setSize(255);
dbCol.value = ret;
}
} else if (type instanceof NumericType) {
if (type.isAbstract()) {
} else {
PrecisionDecimal min = ((NumericType) type).getMinimum();
PrecisionDecimal max = ((NumericType) type).getMaximum();
int minLen = min.toString().length();
int maxLen = max.toString().length();
if (min.toString().startsWith("-")) {
minLen -= 1;
}
if (max.toString().startsWith("-")) {
maxLen -= 1;
}
if (min.getAccuracy() > 0) {
DbColDecimal ret = new DbColDecimal();
int size = Math.max(minLen, maxLen) - 1;
int precision = min.getAccuracy();
// EhiLogger.debug("attr "+ attr.getName()+", maxStr <"+maxStr+">, size "+Integer.toString(size)+", precision "+Integer.toString(precision));
ret.setSize(size);
ret.setPrecision(precision);
if (createNumCheck) {
ret.setMinValue(min.doubleValue());
ret.setMaxValue(max.doubleValue());
}
dbCol.value = ret;
} else {
DbColNumber ret = new DbColNumber();
int size = Math.max(minLen, maxLen);
ret.setSize(size);
if (createNumCheck) {
ret.setMinValue((int) min.doubleValue());
ret.setMaxValue((int) max.doubleValue());
}
dbCol.value = ret;
}
unitDef.value = ((NumericType) type).getUnit();
}
} else if (type instanceof TextType) {
DbColVarchar ret = new DbColVarchar();
if (((TextType) type).getMaxLength() > 0) {
ret.setSize(((TextType) type).getMaxLength());
} else {
ret.setSize(DbColVarchar.UNLIMITED);
}
if (!((TextType) type).isNormalized()) {
mText.value = true;
}
dbCol.value = ret;
} else if (type instanceof BlackboxType) {
if (((BlackboxType) type).getKind() == BlackboxType.eXML) {
DbColXml ret = new DbColXml();
dbCol.value = ret;
} else {
DbColBlob ret = new DbColBlob();
dbCol.value = ret;
}
} else {
return false;
}
return true;
}
use of ch.interlis.ili2c.metamodel.TextType in project ili2db by claeis.
the class PostgisColumnConverter method toIomArray.
@Override
public String[] toIomArray(ch.interlis.ili2c.metamodel.AttributeDef attr, Object sqlArray, EnumCodeMapper enumTypes) throws SQLException, ConverterException {
java.sql.Array array = (java.sql.Array) sqlArray;
String[] ret = null;
ch.interlis.ili2c.metamodel.Type type = attr.getDomainResolvingAliases();
if (attr.isDomainBoolean()) {
Boolean[] values = (Boolean[]) array.getArray();
ret = new String[values.length];
for (int i = 0; i < values.length; i++) {
if (values[i]) {
ret[i] = "true";
} else {
ret[i] = "false";
}
}
} else if (attr.isDomainIli1Date()) {
java.sql.Date[] values = (java.sql.Date[]) array.getArray();
ret = new String[values.length];
for (int i = 0; i < values.length; i++) {
java.text.SimpleDateFormat fmt = new java.text.SimpleDateFormat("yyyyMMdd");
GregorianCalendar date = new GregorianCalendar();
date.setGregorianChange(ToXtfRecordConverter.PURE_GREGORIAN_CALENDAR);
fmt.setCalendar(date);
ret[i] = fmt.format(values[i]);
}
} else if (attr.isDomainIliUuid()) {
java.util.UUID[] values = (java.util.UUID[]) array.getArray();
ret = new String[values.length];
for (int i = 0; i < values.length; i++) {
ret[i] = values[i].toString();
}
} else if (attr.isDomainIli2Date()) {
java.sql.Date[] values = (java.sql.Date[]) array.getArray();
ret = new String[values.length];
for (int i = 0; i < values.length; i++) {
java.text.SimpleDateFormat fmt = new java.text.SimpleDateFormat("yyyy-MM-dd");
GregorianCalendar date = new GregorianCalendar();
date.setGregorianChange(ToXtfRecordConverter.PURE_GREGORIAN_CALENDAR);
fmt.setCalendar(date);
ret[i] = fmt.format(values[i]);
}
} else if (attr.isDomainIli2DateTime()) {
java.sql.Timestamp[] values = (java.sql.Timestamp[]) array.getArray();
ret = new String[values.length];
for (int i = 0; i < values.length; i++) {
// with timezone: yyyy-MM-dd'T'HH:mm:ss.SSSZ
java.text.SimpleDateFormat fmt = new java.text.SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
GregorianCalendar date = new GregorianCalendar();
date.setGregorianChange(ToXtfRecordConverter.PURE_GREGORIAN_CALENDAR);
fmt.setCalendar(date);
ret[i] = fmt.format(values[i]);
}
} else if (attr.isDomainIli2Time()) {
java.sql.Time[] values = (java.sql.Time[]) array.getArray();
ret = new String[values.length];
for (int i = 0; i < values.length; i++) {
java.text.SimpleDateFormat fmt = new java.text.SimpleDateFormat("HH:mm:ss.SSS");
ret[i] = fmt.format(values[i]);
}
} else if (type instanceof EnumerationType) {
if (createEnumColAsItfCode) {
Integer[] values = (Integer[]) array.getArray();
ret = new String[values.length];
for (int i = 0; i < values.length; i++) {
ret[i] = enumTypes.mapItfCode2XtfCode((EnumerationType) type, values[i].toString());
}
} else {
ret = ((String[]) array.getArray());
}
} else if (type instanceof NumericType) {
if (type.isAbstract()) {
} else {
PrecisionDecimal min = ((NumericType) type).getMinimum();
PrecisionDecimal max = ((NumericType) type).getMaximum();
if (min.getAccuracy() > 0) {
BigDecimal[] values = (BigDecimal[]) array.getArray();
ret = new String[values.length];
for (int i = 0; i < values.length; i++) {
ret[i] = values[i].toString();
}
} else {
Integer[] values = (Integer[]) array.getArray();
ret = new String[values.length];
for (int i = 0; i < values.length; i++) {
ret[i] = values[i].toString();
}
}
}
} else if (type instanceof TextType) {
ret = ((String[]) array.getArray());
} else if (type instanceof BlackboxType) {
if (((BlackboxType) type).getKind() == BlackboxType.eXML) {
Object[] values = (Object[]) array.getArray();
ret = new String[values.length];
for (int i = 0; i < values.length; i++) {
ret[i] = toIomXml(values[i]);
}
} else {
Object[] values = (Object[]) array.getArray();
ret = new String[values.length];
for (int i = 0; i < values.length; i++) {
ret[i] = toIomBlob(values[i]);
}
}
} else {
throw new IllegalArgumentException(attr.getScopedName());
}
return ret;
}
Aggregations