use of com.revolsys.record.io.RecordReader in project com.revolsys.open by revolsys.
the class FileRecordLayer method revertDo.
protected boolean revertDo() {
if (this.resource == null) {
return false;
} else {
if (this.resource.exists()) {
try (final RecordReader reader = RecordReader.newRecordReader(this.resource)) {
if (reader == null) {
Logs.error(this, "Cannot find reader for: " + this.resource);
return false;
} else {
final Map<String, Object> properties = getProperties();
reader.setProperties(properties);
final RecordDefinition recordDefinition = reader.getRecordDefinition();
setRecordDefinition(recordDefinition);
if (recordDefinition == null) {
Logs.error(this, "No record definition found for: " + this.url);
return false;
} else {
GeometryFactory geometryFactory = recordDefinition.getGeometryFactory();
clearRecords();
try (BaseCloseable eventsDisabled = eventsDisabled()) {
for (final Record record : reader) {
final Geometry geometry = record.getGeometry();
if (geometry != null) {
if (geometryFactory == null || !geometryFactory.isHasCoordinateSystem()) {
final GeometryFactory geometryFactory2 = geometry.getGeometryFactory();
if (geometryFactory2.isHasCoordinateSystem()) {
setGeometryFactory(geometryFactory2);
geometryFactory = geometryFactory2;
recordDefinition.setGeometryFactory(geometryFactory2);
}
}
}
newRecordInternal(record);
}
}
}
refreshBoundingBox();
initRecordMenu();
setExists(true);
return true;
}
} catch (final RuntimeException e) {
Logs.error(this, "Error reading: " + this.resource, e);
} finally {
refresh();
}
} else {
Logs.error(this, "Cannot find: " + this.url);
}
}
return false;
}
use of com.revolsys.record.io.RecordReader in project com.revolsys.open by revolsys.
the class AbstractRecordLayer method getPasteRecordGeometry.
protected Geometry getPasteRecordGeometry(final LayerRecord record, final boolean alert) {
try {
if (record == null) {
return null;
} else {
final RecordDefinition recordDefinition = getRecordDefinition();
final FieldDefinition geometryField = recordDefinition.getGeometryField();
if (geometryField != null) {
final MapPanel parentComponent = getMapPanel();
Geometry geometry = null;
DataType geometryDataType = null;
Class<?> layerGeometryClass = null;
final GeometryFactory geometryFactory = getGeometryFactory();
geometryDataType = geometryField.getDataType();
layerGeometryClass = geometryDataType.getJavaClass();
RecordReader reader = ClipboardUtil.getContents(RecordReaderTransferable.DATA_OBJECT_READER_FLAVOR);
if (reader == null) {
final String string = ClipboardUtil.getContents(DataFlavor.stringFlavor);
if (Property.hasValue(string)) {
try {
geometry = geometryFactory.geometry(string);
geometry = geometryFactory.geometry(layerGeometryClass, geometry);
if (geometry != null) {
return geometry;
}
} catch (final Throwable e) {
}
final Resource resource = new ByteArrayResource("t.csv", string);
reader = RecordReader.newRecordReader(resource);
} else {
return null;
}
}
if (reader != null) {
try {
for (final Record sourceRecord : reader) {
if (geometry == null) {
final Geometry sourceGeometry = sourceRecord.getGeometry();
if (sourceGeometry == null) {
if (alert) {
JOptionPane.showMessageDialog(parentComponent, "Clipboard does not contain a record with a geometry.", "Paste Geometry", JOptionPane.ERROR_MESSAGE);
}
return null;
}
geometry = geometryFactory.geometry(layerGeometryClass, sourceGeometry);
if (geometry == null) {
if (alert) {
JOptionPane.showMessageDialog(parentComponent, "Clipboard should contain a record with a " + geometryDataType + " not a " + sourceGeometry.getGeometryType() + ".", "Paste Geometry", JOptionPane.ERROR_MESSAGE);
}
return null;
}
} else {
if (alert) {
JOptionPane.showMessageDialog(parentComponent, "Clipboard contains more than one record. Copy a single record.", "Paste Geometry", JOptionPane.ERROR_MESSAGE);
}
return null;
}
}
} finally {
FileUtil.closeSilent(reader);
}
if (geometry == null) {
if (alert) {
JOptionPane.showMessageDialog(parentComponent, "Clipboard does not contain a record with a geometry.", "Paste Geometry", JOptionPane.ERROR_MESSAGE);
}
} else if (geometry.isEmpty()) {
if (alert) {
JOptionPane.showMessageDialog(parentComponent, "Clipboard contains an empty geometry.", "Paste Geometry", JOptionPane.ERROR_MESSAGE);
}
return null;
} else {
return geometry;
}
}
}
return null;
}
} catch (final Throwable t) {
return null;
}
}
use of com.revolsys.record.io.RecordReader in project com.revolsys.open by revolsys.
the class RecordReaderHttpMessageConverter method read.
@Override
public RecordReader read(final Class<? extends RecordReader> clazz, final HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {
try {
final HttpHeaders headers = inputMessage.getHeaders();
final MediaType mediaType = headers.getContentType();
Charset charset = mediaType.getCharSet();
if (charset == null) {
charset = StandardCharsets.UTF_8;
}
final InputStream body = inputMessage.getBody();
final String mediaTypeString = mediaType.getType() + "/" + mediaType.getSubtype();
final RecordReaderFactory readerFactory = IoFactory.factoryByMediaType(RecordReaderFactory.class, mediaTypeString);
if (readerFactory == null) {
throw new HttpMessageNotReadableException("Cannot read data in format" + mediaType);
} else {
final Reader<Record> reader = readerFactory.newRecordReader(new InputStreamResource("recordInput", body));
GeometryFactory factory = this.geometryFactory;
final ServletWebRequest requestAttributes = (ServletWebRequest) RequestContextHolder.getRequestAttributes();
final String srid = requestAttributes.getParameter("srid");
if (srid != null && srid.trim().length() > 0) {
factory = GeometryFactory.floating3d(Integer.parseInt(srid));
}
reader.setProperty(IoConstants.GEOMETRY_FACTORY, factory);
return (RecordReader) reader;
}
} catch (final IOException e) {
throw new HttpMessageNotReadableException("Error reading data", e);
}
}
use of com.revolsys.record.io.RecordReader in project com.revolsys.open by revolsys.
the class RecordIoTestSuite method doRecordReadTest.
private static void doRecordReadTest(final PathResource resource, final ArrayRecord record) {
if (RecordReader.isReadable(resource)) {
final RecordReaderFactory recordReaderFactory = IoFactory.factory(RecordReaderFactory.class, resource);
try (RecordReader recordReader = RecordReader.newRecordReader(resource)) {
final ClockDirection polygonRingDirection = recordReader.getPolygonRingDirection();
final List<Record> records = recordReader.toList();
Assert.assertEquals("Record Count", 1, records.size());
final Record actualRecord = records.get(0);
if (recordReaderFactory.isCustomFieldsSupported() && !(recordReader instanceof GeometryRecordReader)) {
for (final String fieldName : record.getRecordDefinition().getFieldNames()) {
if (!fieldName.equals("GEOMETRY")) {
final Object expectedValue = record.getValue(fieldName);
final Object actualValue = actualRecord.getValue(fieldName);
final boolean equals = DataType.equal(expectedValue, actualValue);
com.revolsys.geometry.util.Assert.equals(fieldName, equals, expectedValue, actualValue);
}
}
}
if (recordReaderFactory.isGeometrySupported()) {
final Geometry expectedGeometry = record.getGeometry();
final Geometry actualGeometry = actualRecord.getGeometry();
assertGeometry(polygonRingDirection, expectedGeometry, actualGeometry);
}
}
} else {
Logs.debug(RecordIoTestSuite.class, "Reading geometry not supported for: " + resource.getFileNameExtension());
}
}
use of com.revolsys.record.io.RecordReader in project com.revolsys.open by revolsys.
the class RecordStore method page.
default ResultPager<Record> page(final Query query) {
final RecordReader results = getRecords(query);
final List<Record> list = results.toList();
return new ListResultPager<>(list);
}
Aggregations