Search in sources :

Example 41 with PathName

use of com.revolsys.io.PathName in project com.revolsys.open by revolsys.

the class FileGdbRecordStore method getRecordDefinition.

public RecordDefinitionImpl getRecordDefinition(final PathName schemaName, final String path, final String tableDefinition) {
    synchronized (this.apiSync) {
        synchronized (API_SYNC) {
            try {
                final XmlProcessor parser = new EsriGdbXmlParser();
                final DETable deTable = parser.process(tableDefinition);
                final String tableName = deTable.getName();
                final PathName typePath = PathName.newPathName(schemaName.newChild(tableName));
                final RecordStoreSchema schema = getSchema(schemaName);
                final RecordDefinitionImpl recordDefinition = new RecordDefinitionImpl(schema, typePath);
                recordDefinition.setPolygonRingDirection(ClockDirection.CLOCKWISE);
                String lengthFieldName = null;
                String areaFieldName = null;
                if (deTable instanceof DEFeatureClass) {
                    final DEFeatureClass featureClass = (DEFeatureClass) deTable;
                    lengthFieldName = featureClass.getLengthFieldName();
                    final LengthFieldName lengthFieldNameProperty = new LengthFieldName(lengthFieldName);
                    lengthFieldNameProperty.setRecordDefinition(recordDefinition);
                    areaFieldName = featureClass.getAreaFieldName();
                    final LengthFieldName areaFieldNameProperty = new LengthFieldName(areaFieldName);
                    areaFieldNameProperty.setRecordDefinition(recordDefinition);
                }
                for (final Field field : deTable.getFields()) {
                    final String fieldName = field.getName();
                    AbstractFileGdbFieldDefinition fieldDefinition = null;
                    if (fieldName.equals(lengthFieldName)) {
                        fieldDefinition = new LengthFieldDefinition(field);
                    } else if (fieldName.equals(areaFieldName)) {
                        fieldDefinition = new AreaFieldDefinition(field);
                    } else {
                        final FieldType type = field.getType();
                        final Constructor<? extends AbstractFileGdbFieldDefinition> fieldConstructor = ESRI_FIELD_TYPE_FIELD_DEFINITION_MAP.get(type);
                        if (fieldConstructor != null) {
                            try {
                                fieldDefinition = JavaBeanUtil.invokeConstructor(fieldConstructor, field);
                            } catch (final Throwable e) {
                                Logs.error(this, tableDefinition);
                                throw new RuntimeException("Error creating field for " + typePath + "." + field.getName() + " : " + field.getType(), e);
                            }
                        } else {
                            Logs.error(this, "Unsupported field type " + fieldName + ":" + type);
                        }
                    }
                    if (fieldDefinition != null) {
                        final Domain domain = field.getDomain();
                        if (domain != null) {
                            CodeTable codeTable = getCodeTable(domain.getDomainName() + "_ID");
                            if (codeTable == null) {
                                codeTable = new FileGdbDomainCodeTable(this, domain);
                                addCodeTable(codeTable);
                            }
                            fieldDefinition.setCodeTable(codeTable);
                        }
                        fieldDefinition.setRecordStore(this);
                        recordDefinition.addField(fieldDefinition);
                        if (fieldDefinition instanceof GlobalIdFieldDefinition) {
                            recordDefinition.setIdFieldName(fieldName);
                        }
                    }
                }
                final String oidFieldName = deTable.getOIDFieldName();
                recordDefinition.setProperty(EsriGeodatabaseXmlConstants.ESRI_OBJECT_ID_FIELD_NAME, oidFieldName);
                if (deTable instanceof DEFeatureClass) {
                    final DEFeatureClass featureClass = (DEFeatureClass) deTable;
                    final String shapeFieldName = featureClass.getShapeFieldName();
                    recordDefinition.setGeometryFieldName(shapeFieldName);
                }
                for (final Index index : deTable.getIndexes()) {
                    if (index.getName().endsWith("_PK")) {
                        for (final Field field : index.getFields()) {
                            final String fieldName = field.getName();
                            recordDefinition.setIdFieldName(fieldName);
                        }
                    }
                }
                addRecordDefinitionProperties(recordDefinition);
                if (recordDefinition.getIdFieldIndex() == -1) {
                    recordDefinition.setIdFieldName(deTable.getOIDFieldName());
                }
                this.catalogPathByPath.put(typePath, deTable.getCatalogPath());
                return recordDefinition;
            } catch (final RuntimeException e) {
                Logs.debug(this, tableDefinition);
                throw e;
            }
        }
    }
}
Also used : CodeTable(com.revolsys.record.code.CodeTable) FileGdbDomainCodeTable(com.revolsys.gis.esri.gdb.file.capi.FileGdbDomainCodeTable) RecordStoreSchema(com.revolsys.record.schema.RecordStoreSchema) EsriGdbXmlParser(com.revolsys.record.io.format.esri.gdb.xml.model.EsriGdbXmlParser) XmlProcessor(com.revolsys.record.io.format.xml.XmlProcessor) LengthFieldName(com.revolsys.record.property.LengthFieldName) Constructor(java.lang.reflect.Constructor) RecordDefinitionImpl(com.revolsys.record.schema.RecordDefinitionImpl) DEFeatureClass(com.revolsys.record.io.format.esri.gdb.xml.model.DEFeatureClass) FileGdbDomainCodeTable(com.revolsys.gis.esri.gdb.file.capi.FileGdbDomainCodeTable) Index(com.revolsys.record.io.format.esri.gdb.xml.model.Index) VectorOfWString(com.revolsys.gis.esri.gdb.file.capi.swig.VectorOfWString) GlobalIdFieldDefinition(com.revolsys.gis.esri.gdb.file.capi.type.GlobalIdFieldDefinition) AreaFieldDefinition(com.revolsys.gis.esri.gdb.file.capi.type.AreaFieldDefinition) FieldType(com.revolsys.record.io.format.esri.gdb.xml.model.enums.FieldType) Field(com.revolsys.record.io.format.esri.gdb.xml.model.Field) AbstractFileGdbFieldDefinition(com.revolsys.gis.esri.gdb.file.capi.type.AbstractFileGdbFieldDefinition) LengthFieldDefinition(com.revolsys.gis.esri.gdb.file.capi.type.LengthFieldDefinition) PathName(com.revolsys.io.PathName) Domain(com.revolsys.record.io.format.esri.gdb.xml.model.Domain) DETable(com.revolsys.record.io.format.esri.gdb.xml.model.DETable)

Example 42 with PathName

use of com.revolsys.io.PathName in project com.revolsys.open by revolsys.

the class FileGdbRecordStore method updateRecord.

void updateRecord(final Table table, final Record record) {
    final Object objectId = record.getValue("OBJECTID");
    if (objectId == null) {
        insertRecord(table, record);
    } else {
        final RecordDefinition sourceRecordDefinition = record.getRecordDefinition();
        final RecordDefinition recordDefinition = getRecordDefinition(sourceRecordDefinition);
        validateRequired(record, recordDefinition);
        final PathName typePath = sourceRecordDefinition.getPathName();
        final String whereClause = "OBJECTID=" + objectId;
        try (final FileGdbEnumRowsIterator rows = search(typePath, table, "*", whereClause, false)) {
            for (final Row row : rows) {
                try {
                    for (final FieldDefinition field : recordDefinition.getFields()) {
                        final String name = field.getName();
                        try {
                            final Object value = record.getValue(name);
                            final AbstractFileGdbFieldDefinition esriField = (AbstractFileGdbFieldDefinition) field;
                            esriField.setUpdateValue(record, row, value);
                        } catch (final Throwable e) {
                            throw new ObjectPropertyException(record, name, e);
                        }
                    }
                    updateRow(typePath, table, row);
                    record.setState(RecordState.PERSISTED);
                    addStatistic("Update", record);
                } catch (final ObjectException e) {
                    if (e.getObject() == record) {
                        throw e;
                    } else {
                        throw new ObjectException(record, e);
                    }
                } catch (final Throwable e) {
                    throw new ObjectException(record, e);
                }
            }
        }
    }
}
Also used : ObjectPropertyException(com.revolsys.beans.ObjectPropertyException) AbstractFileGdbFieldDefinition(com.revolsys.gis.esri.gdb.file.capi.type.AbstractFileGdbFieldDefinition) OidFieldDefinition(com.revolsys.gis.esri.gdb.file.capi.type.OidFieldDefinition) DoubleFieldDefinition(com.revolsys.gis.esri.gdb.file.capi.type.DoubleFieldDefinition) FloatFieldDefinition(com.revolsys.gis.esri.gdb.file.capi.type.FloatFieldDefinition) AreaFieldDefinition(com.revolsys.gis.esri.gdb.file.capi.type.AreaFieldDefinition) StringFieldDefinition(com.revolsys.gis.esri.gdb.file.capi.type.StringFieldDefinition) AbstractFileGdbFieldDefinition(com.revolsys.gis.esri.gdb.file.capi.type.AbstractFileGdbFieldDefinition) FieldDefinition(com.revolsys.record.schema.FieldDefinition) GlobalIdFieldDefinition(com.revolsys.gis.esri.gdb.file.capi.type.GlobalIdFieldDefinition) IntegerFieldDefinition(com.revolsys.gis.esri.gdb.file.capi.type.IntegerFieldDefinition) GuidFieldDefinition(com.revolsys.gis.esri.gdb.file.capi.type.GuidFieldDefinition) XmlFieldDefinition(com.revolsys.gis.esri.gdb.file.capi.type.XmlFieldDefinition) ShortFieldDefinition(com.revolsys.gis.esri.gdb.file.capi.type.ShortFieldDefinition) LengthFieldDefinition(com.revolsys.gis.esri.gdb.file.capi.type.LengthFieldDefinition) BinaryFieldDefinition(com.revolsys.gis.esri.gdb.file.capi.type.BinaryFieldDefinition) DateFieldDefinition(com.revolsys.gis.esri.gdb.file.capi.type.DateFieldDefinition) GeometryFieldDefinition(com.revolsys.gis.esri.gdb.file.capi.type.GeometryFieldDefinition) PathName(com.revolsys.io.PathName) VectorOfWString(com.revolsys.gis.esri.gdb.file.capi.swig.VectorOfWString) Row(com.revolsys.gis.esri.gdb.file.capi.swig.Row) ObjectException(com.revolsys.beans.ObjectException) RecordDefinition(com.revolsys.record.schema.RecordDefinition)

Example 43 with PathName

use of com.revolsys.io.PathName in project com.revolsys.open by revolsys.

the class FileGdbRecordStore method newIterator.

@Override
public AbstractIterator<Record> newIterator(final Query query, final Map<String, Object> properties) {
    PathName typePath = query.getTypePath();
    RecordDefinition recordDefinition = query.getRecordDefinition();
    if (recordDefinition == null) {
        recordDefinition = getRecordDefinition(typePath);
        if (recordDefinition == null) {
            throw new IllegalArgumentException("Type name does not exist " + typePath);
        }
    } else {
        typePath = recordDefinition.getPathName();
    }
    final String catalogPath = getCatalogPath(typePath);
    final BoundingBox boundingBox = QueryValue.getBoundingBox(query);
    final Map<? extends CharSequence, Boolean> orderBy = query.getOrderBy();
    final StringBuilder whereClause = getWhereClause(query);
    StringBuilder sql = new StringBuilder();
    if (orderBy.isEmpty() || boundingBox != null) {
        if (!orderBy.isEmpty()) {
            Logs.error(this, "Unable to sort on " + catalogPath + " " + orderBy.keySet() + " as the ESRI library can't sort with a bounding box query");
        }
        sql = whereClause;
    } else {
        sql.append("SELECT ");
        final List<String> fieldNames = query.getFieldNames();
        if (fieldNames.isEmpty()) {
            StringBuilders.append(sql, recordDefinition.getFieldNames());
        } else {
            StringBuilders.append(sql, fieldNames);
        }
        sql.append(" FROM ");
        sql.append(JdbcUtils.getTableName(catalogPath));
        if (whereClause.length() > 0) {
            sql.append(" WHERE ");
            sql.append(whereClause);
        }
        boolean first = true;
        for (final Entry<? extends CharSequence, Boolean> entry : orderBy.entrySet()) {
            final CharSequence fieldName = entry.getKey();
            final DataType dataType = recordDefinition.getFieldType(fieldName);
            if (dataType != null && !Geometry.class.isAssignableFrom(dataType.getJavaClass())) {
                if (first) {
                    sql.append(" ORDER BY ");
                    first = false;
                } else {
                    sql.append(", ");
                }
                if (fieldName instanceof FieldDefinition) {
                    final FieldDefinition field = (FieldDefinition) fieldName;
                    field.appendColumnName(sql);
                } else {
                    sql.append(fieldName);
                }
                final Boolean ascending = entry.getValue();
                if (!ascending) {
                    sql.append(" DESC");
                }
            } else {
                Logs.error(this, "Unable to sort on " + recordDefinition.getPath() + "." + fieldName + " as the ESRI library can't sort on " + dataType + " columns");
            }
        }
    }
    final FileGdbQueryIterator iterator = new FileGdbQueryIterator(this, catalogPath, sql.toString(), boundingBox, query, query.getOffset(), query.getLimit());
    iterator.setStatistics(query.getStatistics());
    return iterator;
}
Also used : OidFieldDefinition(com.revolsys.gis.esri.gdb.file.capi.type.OidFieldDefinition) DoubleFieldDefinition(com.revolsys.gis.esri.gdb.file.capi.type.DoubleFieldDefinition) FloatFieldDefinition(com.revolsys.gis.esri.gdb.file.capi.type.FloatFieldDefinition) AreaFieldDefinition(com.revolsys.gis.esri.gdb.file.capi.type.AreaFieldDefinition) StringFieldDefinition(com.revolsys.gis.esri.gdb.file.capi.type.StringFieldDefinition) AbstractFileGdbFieldDefinition(com.revolsys.gis.esri.gdb.file.capi.type.AbstractFileGdbFieldDefinition) FieldDefinition(com.revolsys.record.schema.FieldDefinition) GlobalIdFieldDefinition(com.revolsys.gis.esri.gdb.file.capi.type.GlobalIdFieldDefinition) IntegerFieldDefinition(com.revolsys.gis.esri.gdb.file.capi.type.IntegerFieldDefinition) GuidFieldDefinition(com.revolsys.gis.esri.gdb.file.capi.type.GuidFieldDefinition) XmlFieldDefinition(com.revolsys.gis.esri.gdb.file.capi.type.XmlFieldDefinition) ShortFieldDefinition(com.revolsys.gis.esri.gdb.file.capi.type.ShortFieldDefinition) LengthFieldDefinition(com.revolsys.gis.esri.gdb.file.capi.type.LengthFieldDefinition) BinaryFieldDefinition(com.revolsys.gis.esri.gdb.file.capi.type.BinaryFieldDefinition) DateFieldDefinition(com.revolsys.gis.esri.gdb.file.capi.type.DateFieldDefinition) GeometryFieldDefinition(com.revolsys.gis.esri.gdb.file.capi.type.GeometryFieldDefinition) VectorOfWString(com.revolsys.gis.esri.gdb.file.capi.swig.VectorOfWString) RecordDefinition(com.revolsys.record.schema.RecordDefinition) BoundingBox(com.revolsys.geometry.model.BoundingBox) DataType(com.revolsys.datatype.DataType) PathName(com.revolsys.io.PathName)

Example 44 with PathName

use of com.revolsys.io.PathName in project com.revolsys.open by revolsys.

the class OgrRecordStore method refreshSchemaElements.

@Override
protected synchronized Map<PathName, ? extends RecordStoreSchemaElement> refreshSchemaElements(final RecordStoreSchema schema) {
    final Map<PathName, RecordStoreSchemaElement> elementsByPath = new TreeMap<>();
    if (!isClosed()) {
        final DataSource dataSource = getDataSource();
        if (dataSource != null) {
            for (int layerIndex = 0; layerIndex < dataSource.GetLayerCount(); layerIndex++) {
                final Layer layer = dataSource.GetLayer(layerIndex);
                if (layer != null) {
                    try {
                        final RecordDefinitionImpl recordDefinition = newLayerRecordDefinition(schema, layer);
                        final PathName typePath = recordDefinition.getPathName();
                        final String layerName = layer.GetName();
                        this.layerNameToPathMap.put(layerName.toUpperCase(), typePath);
                        this.pathToLayerNameMap.put(typePath, layerName);
                        elementsByPath.put(typePath, recordDefinition);
                    } finally {
                        layer.delete();
                    }
                }
            }
        }
    }
    return elementsByPath;
}
Also used : RecordDefinitionImpl(com.revolsys.record.schema.RecordDefinitionImpl) PathName(com.revolsys.io.PathName) TreeMap(java.util.TreeMap) Layer(org.gdal.ogr.Layer) RecordStoreSchemaElement(com.revolsys.record.schema.RecordStoreSchemaElement) DataSource(org.gdal.ogr.DataSource)

Example 45 with PathName

use of com.revolsys.io.PathName in project com.revolsys.open by revolsys.

the class AbstractJdbcRecordStore method refreshSchemaElements.

@Override
protected Map<PathName, ? extends RecordStoreSchemaElement> refreshSchemaElements(final RecordStoreSchema schema) {
    final JdbcRecordStoreSchema jdbcSchema = (JdbcRecordStoreSchema) schema;
    final JdbcRecordStoreSchema rootSchema = getRootSchema();
    final PathName schemaPath = jdbcSchema.getPathName();
    if (jdbcSchema == rootSchema) {
        if (this.usesSchema) {
            final Map<PathName, RecordStoreSchemaElement> schemas = new TreeMap<>();
            final Set<String> databaseSchemaNames = getDatabaseSchemaNames();
            for (final String dbSchemaName : databaseSchemaNames) {
                final PathName childSchemaPath = schemaPath.newChild(dbSchemaName.toUpperCase());
                RecordStoreSchema childSchema = schema.getSchema(childSchemaPath);
                if (childSchema == null) {
                    childSchema = new JdbcRecordStoreSchema(rootSchema, childSchemaPath, dbSchemaName);
                } else {
                    if (childSchema.isInitialized()) {
                        childSchema.refresh();
                    }
                }
                schemas.put(childSchemaPath, childSchema);
            }
            return schemas;
        } else {
            return refreshSchemaElementsDo(jdbcSchema, schemaPath);
        }
    } else {
        return refreshSchemaElementsDo(jdbcSchema, schemaPath);
    }
}
Also used : RecordStoreSchema(com.revolsys.record.schema.RecordStoreSchema) PathName(com.revolsys.io.PathName) TreeMap(java.util.TreeMap) RecordStoreSchemaElement(com.revolsys.record.schema.RecordStoreSchemaElement)

Aggregations

PathName (com.revolsys.io.PathName)64 FieldDefinition (com.revolsys.record.schema.FieldDefinition)15 RecordDefinitionImpl (com.revolsys.record.schema.RecordDefinitionImpl)14 DataType (com.revolsys.datatype.DataType)13 RecordDefinition (com.revolsys.record.schema.RecordDefinition)13 GeometryFactory (com.revolsys.geometry.model.GeometryFactory)12 RecordStoreSchema (com.revolsys.record.schema.RecordStoreSchema)11 VectorOfWString (com.revolsys.gis.esri.gdb.file.capi.swig.VectorOfWString)9 RecordStoreSchemaElement (com.revolsys.record.schema.RecordStoreSchemaElement)7 ArrayList (java.util.ArrayList)7 TreeMap (java.util.TreeMap)7 MapEx (com.revolsys.collection.map.MapEx)5 ResultSet (java.sql.ResultSet)5 Geodatabase (com.revolsys.gis.esri.gdb.file.capi.swig.Geodatabase)4 Identifier (com.revolsys.identifier.Identifier)4 JdbcConnection (com.revolsys.jdbc.JdbcConnection)4 Record (com.revolsys.record.Record)4 File (java.io.File)4 PreparedStatement (java.sql.PreparedStatement)4 Geometry (com.revolsys.geometry.model.Geometry)3