Search in sources :

Example 1 with UserColumn

use of mil.nga.geopackage.user.UserColumn in project hale by halestudio.

the class GeopackageInstanceWriter method populateRow.

private void populateRow(UserCoreRow<?, ?> row, Instance instance, SimpleLog log) {
    for (String column : row.getColumnNames()) {
        Object value = new InstanceAccessor(instance).findChildren(column).value();
        if (value != null) {
            UserColumn col = row.getColumn(column);
            if (col.isPrimaryKey()) {
                // it seems for primary key we can't set a value
                continue;
            }
            // any value processing
            try {
                switch(col.getDataType()) {
                    case DATE:
                        // convert to LocalDate, then to String
                        LocalDate ld = ConversionUtil.getAs(value, LocalDate.class);
                        value = ld.toString();
                        break;
                    case DATETIME:
                        // convert to Instant, then to String
                        Instant inst = ConversionUtil.getAs(value, Instant.class);
                        value = inst.toString();
                        break;
                    case INT:
                    case INTEGER:
                    case MEDIUMINT:
                    case SMALLINT:
                    case TINYINT:
                    case REAL:
                    case FLOAT:
                    case DOUBLE:
                    case TEXT:
                        // generic conversion to data type
                        value = ConversionUtil.getAs(value, col.getDataType().getClassType());
                        break;
                    default:
                        // use as-is
                        break;
                }
            } catch (ConversionException e) {
                log.error("Could not convert value to column type", e);
            }
            row.setValue(column, value);
        }
    }
}
Also used : UserColumn(mil.nga.geopackage.user.UserColumn) ConversionException(org.springframework.core.convert.ConversionException) Instant(java.time.Instant) InstanceAccessor(eu.esdihumboldt.hale.common.instance.groovy.InstanceAccessor) MultiLineString(org.locationtech.jts.geom.MultiLineString) LineString(org.locationtech.jts.geom.LineString) LocalDate(java.time.LocalDate)

Example 2 with UserColumn

use of mil.nga.geopackage.user.UserColumn in project hale by halestudio.

the class GeopackageSchemaBuilder method buildType.

private TypeDefinition buildType(UserDao<?, ?, ?, ?> dao, GeometryColumns geomColumns, DefaultSchema schema) {
    QName name = new QName(defaultNamespace, dao.getTableName());
    DefaultTypeDefinition type = new DefaultTypeDefinition(name);
    type.setConstraint(MappableFlag.ENABLED);
    type.setConstraint(MappingRelevantFlag.ENABLED);
    type.setConstraint(HasValueFlag.DISABLED);
    type.setConstraint(AbstractFlag.DISABLED);
    type.setConstraint(Binding.get(Instance.class));
    UserTable<? extends UserColumn> table = dao.getTable();
    // set primary key constraint
    UserColumn pkColumn = table.getPkColumn();
    if (pkColumn != null) {
        type.setConstraint(new PrimaryKey(Collections.singletonList(new QName(pkColumn.getName()))));
    }
    for (String columnName : table.getColumnNames()) {
        UserColumn column = table.getColumn(columnName);
        QName propertyName = new QName(columnName);
        // determine property type
        TypeDefinition propertyType = getOrCreatePropertyType(column, geomColumns, schema);
        DefaultPropertyDefinition property = new DefaultPropertyDefinition(propertyName, type, propertyType);
        property.setConstraint(Cardinality.CC_EXACTLY_ONCE);
        if (column.isNotNull()) {
            property.setConstraint(NillableFlag.DISABLED);
        } else {
            property.setConstraint(NillableFlag.ENABLED);
        }
    }
    return type;
}
Also used : DefaultTypeDefinition(eu.esdihumboldt.hale.common.schema.model.impl.DefaultTypeDefinition) UserColumn(mil.nga.geopackage.user.UserColumn) DefaultPropertyDefinition(eu.esdihumboldt.hale.common.schema.model.impl.DefaultPropertyDefinition) Instance(eu.esdihumboldt.hale.common.instance.model.Instance) QName(javax.xml.namespace.QName) PrimaryKey(eu.esdihumboldt.hale.common.schema.model.constraint.type.PrimaryKey) MultiLineString(org.locationtech.jts.geom.MultiLineString) LineString(org.locationtech.jts.geom.LineString) DefaultTypeDefinition(eu.esdihumboldt.hale.common.schema.model.impl.DefaultTypeDefinition) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition)

Aggregations

UserColumn (mil.nga.geopackage.user.UserColumn)2 LineString (org.locationtech.jts.geom.LineString)2 MultiLineString (org.locationtech.jts.geom.MultiLineString)2 InstanceAccessor (eu.esdihumboldt.hale.common.instance.groovy.InstanceAccessor)1 Instance (eu.esdihumboldt.hale.common.instance.model.Instance)1 TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)1 PrimaryKey (eu.esdihumboldt.hale.common.schema.model.constraint.type.PrimaryKey)1 DefaultPropertyDefinition (eu.esdihumboldt.hale.common.schema.model.impl.DefaultPropertyDefinition)1 DefaultTypeDefinition (eu.esdihumboldt.hale.common.schema.model.impl.DefaultTypeDefinition)1 Instant (java.time.Instant)1 LocalDate (java.time.LocalDate)1 QName (javax.xml.namespace.QName)1 ConversionException (org.springframework.core.convert.ConversionException)1