use of com.revolsys.record.io.RecordReader in project com.revolsys.open by revolsys.
the class CopyRecords method run.
@Override
public void run() {
try {
final Query query = this.sourceRecordStore.newQuery(this.typePath);
query.setOrderBy(this.orderBy);
try (final RecordReader reader = this.sourceRecordStore.getRecords(query);
final RecordWriter targetWriter = this.targetRecordStore.newRecordWriter()) {
final RecordDefinition targetRecordDefinition = this.targetRecordStore.getRecordDefinition(this.typePath);
if (targetRecordDefinition == null) {
Logs.error(this, "Cannot find target table: " + this.typePath);
} else {
if (this.hasSequence) {
final String idFieldName = targetRecordDefinition.getIdFieldName();
Identifier maxId = this.targetRecordStore.newPrimaryIdentifier(this.typePath);
for (final Record sourceRecord : reader) {
final Record targetRecord = this.targetRecordStore.newRecord(this.typePath, sourceRecord);
final Identifier sourceId = sourceRecord.getIdentifier(idFieldName);
while (CompareUtil.compare(maxId, sourceId) < 0) {
maxId = this.targetRecordStore.newPrimaryIdentifier(this.typePath);
}
targetWriter.write(targetRecord);
}
} else {
for (final Record sourceRecord : reader) {
final Record targetRecord = this.targetRecordStore.newRecord(this.typePath, sourceRecord);
targetWriter.write(targetRecord);
}
}
}
}
} catch (final Throwable e) {
throw new RuntimeException("Unable to copy records for " + this.typePath, e);
}
}
use of com.revolsys.record.io.RecordReader in project com.revolsys.open by revolsys.
the class EsriCoordinateSystemsLoader method projected.
private void projected() {
final Map<ByteArray, Map<Integer, ProjectedCoordinateSystem>> csBymd5 = new LinkedHashMap<>();
try (RecordReader reader = RecordReader.newRecordReader(this.mainPath + "data/esri/esriProjectedCs.tsv");
final ChannelWriter writer = ChannelWriter.newChannelWriter(this.mainPath + "resources/CoordinateSystems/esri/Projected.cs")) {
for (final Record record : reader) {
final int id = record.getInteger("ID");
final String wkt = record.getString("WKT");
final ProjectedCoordinateSystem coordinateSystem = WktCsParser.read(wkt);
final byte[] digest = coordinateSystem.md5Digest();
Maps.addToMap(Maps::newTree, csBymd5, new ByteArray(digest), id, coordinateSystem);
final String csName = coordinateSystem.getCoordinateSystemName();
final GeographicCoordinateSystem geographicCoordinateSystem = coordinateSystem.getGeographicCoordinateSystem();
final String geographicCoordinateSystemName = geographicCoordinateSystem.getCoordinateSystemName();
final int geographicCoordinateSystemId = this.geographicIdByName.getOrDefault(geographicCoordinateSystemName, 0);
if (geographicCoordinateSystemId == 0) {
System.out.println(wkt);
}
final String projectionName = coordinateSystem.getCoordinateOperationMethod().getName();
final Map<ParameterName, ParameterValue> parameterValues = coordinateSystem.getParameterValues();
final LinearUnit linearUnit = coordinateSystem.getLinearUnit();
final String unitName = linearUnit.getName();
final double conversionFactor = linearUnit.getConversionFactor();
writer.putInt(id);
writer.putStringUtf8ByteCount(csName);
writer.putInt(geographicCoordinateSystemId);
writer.putStringUtf8ByteCount(projectionName);
writeParameters(writer, parameterValues);
writer.putStringUtf8ByteCount(unitName);
writer.putDouble(conversionFactor);
}
}
writeDigestFile(csBymd5, "Projected");
}
use of com.revolsys.record.io.RecordReader in project com.revolsys.open by revolsys.
the class AbstractRecordLayer method copyRecordsToClipboard.
public void copyRecordsToClipboard(final List<LayerRecord> records) {
if (!records.isEmpty()) {
final RecordDefinition recordDefinition = getRecordDefinition();
final List<Record> copies = new ArrayList<>();
for (final LayerRecord record : records) {
final ArrayRecord recordCopy = new ArrayRecord(recordDefinition, record);
copies.add(recordCopy);
}
final RecordReader reader = new ListRecordReader(recordDefinition, copies);
final RecordReaderTransferable transferable = new RecordReaderTransferable(reader);
ClipboardUtil.setContents(transferable);
}
}
use of com.revolsys.record.io.RecordReader in project com.revolsys.open by revolsys.
the class AbstractRecordLayer method pasteRecords.
public void pasteRecords() {
final List<LayerRecord> newRecords = new ArrayList<>();
try (BaseCloseable eventsEnabled = eventsDisabled()) {
RecordReader reader = ClipboardUtil.getContents(RecordReaderTransferable.DATA_OBJECT_READER_FLAVOR);
if (reader == null) {
final String string = ClipboardUtil.getContents(DataFlavor.stringFlavor);
if (Property.hasValue(string)) {
if (string.contains("\t")) {
final Resource tsvResource = new ByteArrayResource("t.tsv", string);
reader = RecordReader.newRecordReader(tsvResource);
} else {
final Resource resource = new ByteArrayResource("t.csv", string);
reader = RecordReader.newRecordReader(resource);
}
}
}
if (reader != null) {
for (final Record sourceRecord : reader) {
final Map<String, Object> newValues = getPasteNewValues(sourceRecord);
if (!newValues.isEmpty()) {
final LayerRecord newRecord = newLayerRecord(newValues);
if (newRecord != null) {
newRecords.add(newRecord);
}
}
}
}
} catch (final Throwable e) {
LoggingEventPanel.showDialog(getMapPanel(), "Unexpected error pasting records", e);
return;
}
RecordValidationDialog.validateRecords("Pasting Records", this, newRecords, (validator) -> {
// Success
// Save the valid records
final List<LayerRecord> validRecords = validator.getValidRecords();
if (!validRecords.isEmpty()) {
saveChanges(validRecords);
addSelectedRecords(validRecords);
zoomToRecords(validRecords);
showRecordsTable(RecordLayerTableModel.MODE_RECORDS_SELECTED);
firePropertyChange(RECORDS_INSERTED, null, validRecords);
}
// Delete any invalid records
final List<LayerRecord> invalidRecords = validator.getInvalidRecords();
if (!invalidRecords.isEmpty()) {
deleteRecords(invalidRecords);
}
}, (validator) -> {
// Cancel, delete all the records
deleteRecords(newRecords);
});
}
use of com.revolsys.record.io.RecordReader in project com.revolsys.open by revolsys.
the class RecordStoreLayer method forEachRecordInternal.
@Override
protected void forEachRecordInternal(Query query, final Consumer<? super LayerRecord> consumer) {
if (isExists()) {
try {
final RecordStore recordStore = getRecordStore();
if (recordStore != null && query != null) {
final Predicate<Record> filter = query.getWhereCondition();
final Map<? extends CharSequence, Boolean> orderBy = query.getOrderBy();
final List<LayerRecord> changedRecords = new ArrayList<>();
changedRecords.addAll(getRecordsNew());
changedRecords.addAll(getRecordsModified());
Records.filterAndSort(changedRecords, filter, orderBy);
final Iterator<LayerRecord> changedIterator = changedRecords.iterator();
LayerRecord currentChangedRecord = Iterators.next(changedIterator);
final RecordDefinition internalRecordDefinition = getInternalRecordDefinition();
query = query.newQuery(internalRecordDefinition);
final Comparator<Record> comparator = Records.newComparatorOrderBy(orderBy);
try (final BaseCloseable booleanValueCloseable = eventsDisabled();
Transaction transaction = recordStore.newTransaction(Propagation.REQUIRES_NEW);
final RecordReader reader = newRecordStoreRecordReader(query)) {
for (LayerRecord record : reader.<LayerRecord>i()) {
boolean write = true;
final Identifier identifier = getId(record);
if (identifier == null) {
record = newProxyLayerRecordNoId(record);
} else {
final LayerRecord cachedRecord = this.recordsByIdentifier.get(identifier);
if (cachedRecord != null) {
record = cachedRecord;
if (record.isChanged() || isDeleted(record)) {
write = false;
}
}
}
if (!isDeleted(record) && write) {
while (currentChangedRecord != null && comparator.compare(currentChangedRecord, record) < 0) {
consumer.accept(currentChangedRecord);
currentChangedRecord = Iterators.next(changedIterator);
}
consumer.accept(record);
}
}
while (currentChangedRecord != null) {
consumer.accept(currentChangedRecord);
currentChangedRecord = Iterators.next(changedIterator);
}
}
}
} catch (final CancellationException e) {
} catch (final RuntimeException e) {
Logs.error(this, "Error executing query: " + query, e);
throw e;
}
}
}
Aggregations