Search in sources :

Example 1 with Identifier

use of com.revolsys.identifier.Identifier in project com.revolsys.open by revolsys.

the class AbstractCodeTable method getIdentifier.

@Override
public Identifier getIdentifier(final List<Object> values, final boolean loadMissing) {
    if (values.size() == 1) {
        final Object id = values.get(0);
        if (id == null) {
            return null;
        } else {
            final Identifier cachedId = this.idIdCache.get(id);
            if (cachedId != null) {
                return cachedId;
            } else {
                String lowerId = id.toString();
                if (!this.caseSensitive) {
                    lowerId = lowerId.toLowerCase();
                }
                if (this.stringIdMap.containsKey(lowerId)) {
                    return this.stringIdMap.get(lowerId);
                }
            }
        }
    }
    processValues(values);
    Identifier id = getIdByValue(values);
    if (id == null && loadMissing) {
        synchronized (this) {
            id = loadId(values, true);
            if (id != null && !this.idValueCache.containsKey(id)) {
                addValue(id, values);
            }
        }
    }
    return id;
}
Also used : Identifier(com.revolsys.identifier.Identifier) SingleIdentifier(com.revolsys.identifier.SingleIdentifier)

Example 2 with Identifier

use of com.revolsys.identifier.Identifier in project com.revolsys.open by revolsys.

the class CodeTableProperty method addValue.

public void addValue(final Record code) {
    final String idFieldName = getIdFieldName();
    final Identifier id = code.getIdentifier(idFieldName);
    if (id == null) {
        throw new NullPointerException(idFieldName + "=null for " + code);
    } else {
        final List<Object> values = new ArrayList<>();
        for (final String fieldName : this.valueFieldNames) {
            Object value = code.getValue(fieldName);
            if (value instanceof SingleIdentifier) {
                final SingleIdentifier identifier = (SingleIdentifier) value;
                value = identifier.getValue(0);
            }
            if (value == null) {
                if (!this.allowNullValues) {
                    throw new NullPointerException(this.valueFieldNames + "=null for " + code);
                }
            }
            values.add(value);
        }
        addValue(id, values);
    }
}
Also used : Identifier(com.revolsys.identifier.Identifier) SingleIdentifier(com.revolsys.identifier.SingleIdentifier) ListIdentifier(com.revolsys.identifier.ListIdentifier) SingleIdentifier(com.revolsys.identifier.SingleIdentifier) ArrayList(java.util.ArrayList)

Example 3 with Identifier

use of com.revolsys.identifier.Identifier in project com.revolsys.open by revolsys.

the class CodeTableProperty method loadId.

@Override
protected synchronized Identifier loadId(final List<Object> values, final boolean createId) {
    if (this.loadAll && !this.loadMissingCodes && !isEmpty()) {
        return null;
    }
    Identifier id = null;
    if (createId && this.loadAll && !isLoaded()) {
        loadAll();
        id = getIdentifier(values, false);
    } else {
        final Query query = new Query(this.typePath);
        final And and = new And();
        if (!values.isEmpty()) {
            int i = 0;
            for (final String fieldName : this.valueFieldNames) {
                final Object value = values.get(i);
                if (value == null) {
                    and.and(Q.isNull(fieldName));
                } else {
                    final FieldDefinition fieldDefinition = this.recordDefinition.getField(fieldName);
                    and.and(Q.equal(fieldDefinition, value));
                }
                i++;
            }
        }
        query.setWhereCondition(and);
        final Reader<Record> reader = this.recordStore.getRecords(query);
        try {
            final List<Record> codes = reader.toList();
            if (codes.size() > 0) {
                final CategoryLabelCountMap statistics = this.recordStore.getStatistics();
                if (statistics != null) {
                    statistics.getLabelCountMap("query").addCount(this.typePath, -codes.size());
                }
                addValues(codes);
            }
            id = getIdByValue(values);
            Property.firePropertyChange(this, "valuesChanged", false, true);
        } finally {
            reader.close();
        }
    }
    if (createId && id == null) {
        return newIdentifier(values);
    } else {
        return id;
    }
}
Also used : Identifier(com.revolsys.identifier.Identifier) SingleIdentifier(com.revolsys.identifier.SingleIdentifier) ListIdentifier(com.revolsys.identifier.ListIdentifier) Query(com.revolsys.record.query.Query) CategoryLabelCountMap(com.revolsys.util.count.CategoryLabelCountMap) And(com.revolsys.record.query.And) FieldDefinition(com.revolsys.record.schema.FieldDefinition) Record(com.revolsys.record.Record)

Example 4 with Identifier

use of com.revolsys.identifier.Identifier in project com.revolsys.open by revolsys.

the class CodeTableProperty method loadValues.

@Override
protected List<Object> loadValues(final Object id) {
    if (this.loadAll && !isLoaded()) {
        loadAll();
    } else {
        try {
            final Record code;
            if (id instanceof Identifier) {
                final Identifier identifier = (Identifier) id;
                code = this.recordStore.getRecord(this.typePath, identifier);
            } else {
                code = this.recordStore.getRecord(this.typePath, id);
            }
            if (code != null) {
                addValue(code);
            }
        } catch (final Throwable e) {
            return null;
        }
    }
    return getValueById(id);
}
Also used : Identifier(com.revolsys.identifier.Identifier) SingleIdentifier(com.revolsys.identifier.SingleIdentifier) ListIdentifier(com.revolsys.identifier.ListIdentifier) Record(com.revolsys.record.Record)

Example 5 with Identifier

use of com.revolsys.identifier.Identifier in project com.revolsys.open by revolsys.

the class SimpleCodeTable method newCodeTable.

public static CodeTable newCodeTable(final String name, final Resource resource) {
    final SimpleCodeTable codeTable = new SimpleCodeTable(name);
    try (final RecordReader reader = RecordReader.newRecordReader(resource)) {
        for (final Record record : reader) {
            final Identifier id = record.getIdentifier(0);
            final List<Object> values = new ArrayList<>();
            final int fieldCount = record.getRecordDefinition().getFieldCount();
            for (int i = 1; i < fieldCount; i++) {
                final Object value = record.getValue(i);
                values.add(value);
            }
            codeTable.addValue(id, values);
        }
    }
    return codeTable;
}
Also used : Identifier(com.revolsys.identifier.Identifier) RecordReader(com.revolsys.record.io.RecordReader) ArrayList(java.util.ArrayList) Record(com.revolsys.record.Record)

Aggregations

Identifier (com.revolsys.identifier.Identifier)56 Record (com.revolsys.record.Record)17 ArrayList (java.util.ArrayList)12 RecordDefinition (com.revolsys.record.schema.RecordDefinition)11 SingleIdentifier (com.revolsys.identifier.SingleIdentifier)8 ListIdentifier (com.revolsys.identifier.ListIdentifier)7 TypedIdentifier (com.revolsys.identifier.TypedIdentifier)6 CodeTable (com.revolsys.record.code.CodeTable)6 RecordReader (com.revolsys.record.io.RecordReader)5 RecordStore (com.revolsys.record.schema.RecordStore)5 Transaction (com.revolsys.transaction.Transaction)5 List (java.util.List)5 Geometry (com.revolsys.geometry.model.Geometry)4 BaseCloseable (com.revolsys.io.BaseCloseable)4 PathName (com.revolsys.io.PathName)4 ArrayRecord (com.revolsys.record.ArrayRecord)4 Query (com.revolsys.record.query.Query)4 OsmElement (com.revolsys.record.io.format.openstreetmap.model.OsmElement)3 FieldDefinition (com.revolsys.record.schema.FieldDefinition)3 CompoundCoordinateSystem (com.revolsys.geometry.cs.CompoundCoordinateSystem)2