use of com.revolsys.record.Record in project com.revolsys.open by revolsys.
the class AbstractJdbcRecordStore method writeAll.
protected int writeAll(final Iterable<? extends Record> records, final RecordState state) {
int count = 0;
try (Transaction transaction = newTransaction(Propagation.REQUIRED)) {
// rolled back.
try (final RecordWriter writer = newRecordWriter(true)) {
for (final Record record : records) {
write(writer, record, state);
count++;
}
} catch (final RuntimeException e) {
transaction.setRollbackOnly();
throw e;
} catch (final Error e) {
transaction.setRollbackOnly();
throw e;
}
}
return count;
}
use of com.revolsys.record.Record in project com.revolsys.open by revolsys.
the class JdbcRecordWriter method processCurrentBatch.
private void processCurrentBatch(final JdbcRecordDefinition recordDefinition, final Map<JdbcRecordDefinition, String> sqlMap, final PreparedStatement statement, final Map<JdbcRecordDefinition, Integer> batchCountMap, final Map<JdbcRecordDefinition, List<Record>> recordsByType, final boolean hasGeneratedKeys) {
Integer batchCount = batchCountMap.get(recordDefinition);
if (batchCount == null) {
batchCount = 0;
}
try {
Integer typeCount = this.typeCountMap.get(recordDefinition);
if (typeCount == null) {
typeCount = batchCount;
} else {
typeCount += batchCount;
}
this.typeCountMap.put(recordDefinition, typeCount);
statement.executeBatch();
if (hasGeneratedKeys) {
final List<Record> records = recordsByType.remove(recordDefinition);
if (records != null) {
final ResultSet generatedKeyResultSet = statement.getGeneratedKeys();
int recordIndex = 0;
while (generatedKeyResultSet.next()) {
final Record record = records.get(recordIndex++);
int columnIndex = 1;
for (final FieldDefinition idField : recordDefinition.getIdFields()) {
((JdbcFieldDefinition) idField).setFieldValueFromResultSet(generatedKeyResultSet, columnIndex++, record, false);
}
}
}
}
} catch (final SQLException e) {
final String sql = sqlMap.get(recordDefinition);
throw this.connection.getException("Process Batch", sql, e);
} catch (final RuntimeException e) {
LOG.error(sqlMap, e);
throw e;
} finally {
batchCountMap.put(recordDefinition, 0);
}
}
use of com.revolsys.record.Record in project com.revolsys.open by revolsys.
the class DirectoryRecordWriter method getWriter.
private Writer<Record> getWriter(final Record record) {
final RecordDefinition recordDefinition = record.getRecordDefinition();
final String path = recordDefinition.getPath();
Writer<Record> writer = this.writers.get(path);
if (writer == null) {
final File directory = getDirectory(recordDefinition);
directory.mkdirs();
final String fileName = getFileName(recordDefinition);
final File file = new File(directory, fileName + this.nameSuffix + "." + this.fileExtension);
final PathResource resource = new PathResource(file);
writer = RecordWriter.newRecordWriter(recordDefinition, resource);
if (writer == null) {
throw new IllegalArgumentException("Unable to create writer for " + resource);
} else {
final Map<String, Object> properties = getProperties();
writer.setProperties(properties);
final Geometry geometry = record.getGeometry();
if (geometry != null) {
final GeometryFactory geometryFactory = geometry.getGeometryFactory();
setProperty(IoConstants.GEOMETRY_FACTORY, geometryFactory);
}
this.writers.put(path, writer);
RecordDefinition writerRecordDefinition = recordDefinition;
if (writer instanceof AbstractRecordWriter) {
final AbstractRecordWriter recordWriter = (AbstractRecordWriter) writer;
writerRecordDefinition = recordWriter.getRecordDefinition();
if (writerRecordDefinition == null) {
writerRecordDefinition = recordDefinition;
}
}
this.recordDefinitionMap.put(path, writerRecordDefinition);
}
}
return writer;
}
use of com.revolsys.record.Record 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;
}
}
use of com.revolsys.record.Record 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);
}
Aggregations