Search in sources :

Example 16 with RecordReader

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;
}
Also used : Geometry(com.revolsys.geometry.model.Geometry) BaseCloseable(com.revolsys.io.BaseCloseable) GeometryFactory(com.revolsys.geometry.model.GeometryFactory) RecordReader(com.revolsys.record.io.RecordReader) Record(com.revolsys.record.Record) RecordDefinition(com.revolsys.record.schema.RecordDefinition)

Example 17 with RecordReader

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;
    }
}
Also used : MapPanel(com.revolsys.swing.map.MapPanel) GeometryFactory(com.revolsys.geometry.model.GeometryFactory) FieldDefinition(com.revolsys.record.schema.FieldDefinition) RecordReader(com.revolsys.record.io.RecordReader) ListRecordReader(com.revolsys.record.io.ListRecordReader) ByteArrayResource(com.revolsys.spring.resource.ByteArrayResource) Resource(com.revolsys.spring.resource.Resource) LineString(com.revolsys.geometry.model.LineString) ByteArrayResource(com.revolsys.spring.resource.ByteArrayResource) RecordDefinition(com.revolsys.record.schema.RecordDefinition) Geometry(com.revolsys.geometry.model.Geometry) DataType(com.revolsys.datatype.DataType) ArrayRecord(com.revolsys.record.ArrayRecord) Record(com.revolsys.record.Record)

Example 18 with RecordReader

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);
    }
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) GeometryFactory(com.revolsys.geometry.model.GeometryFactory) InputStream(java.io.InputStream) RecordReader(com.revolsys.record.io.RecordReader) Charset(java.nio.charset.Charset) IOException(java.io.IOException) RecordReaderFactory(com.revolsys.record.io.RecordReaderFactory) HttpMessageNotReadableException(org.springframework.http.converter.HttpMessageNotReadableException) MediaType(org.springframework.http.MediaType) Record(com.revolsys.record.Record) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) InputStreamResource(com.revolsys.spring.resource.InputStreamResource)

Example 19 with RecordReader

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());
    }
}
Also used : Geometry(com.revolsys.geometry.model.Geometry) RecordReader(com.revolsys.record.io.RecordReader) GeometryRecordReader(com.revolsys.record.io.GeometryRecordReader) Record(com.revolsys.record.Record) ArrayRecord(com.revolsys.record.ArrayRecord) GeometryRecordReader(com.revolsys.record.io.GeometryRecordReader) RecordReaderFactory(com.revolsys.record.io.RecordReaderFactory) ClockDirection(com.revolsys.geometry.model.ClockDirection)

Example 20 with RecordReader

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);
}
Also used : ListRecordReader(com.revolsys.record.io.ListRecordReader) RecordReader(com.revolsys.record.io.RecordReader) ListResultPager(com.revolsys.collection.ListResultPager) Record(com.revolsys.record.Record) ArrayRecord(com.revolsys.record.ArrayRecord)

Aggregations

RecordReader (com.revolsys.record.io.RecordReader)43 Record (com.revolsys.record.Record)34 ChannelWriter (com.revolsys.io.channels.ChannelWriter)19 RecordDefinition (com.revolsys.record.schema.RecordDefinition)11 Identifier (com.revolsys.identifier.Identifier)5 ArrayRecord (com.revolsys.record.ArrayRecord)5 ListRecordReader (com.revolsys.record.io.ListRecordReader)5 Query (com.revolsys.record.query.Query)5 HashMap (java.util.HashMap)5 GeometryFactory (com.revolsys.geometry.model.GeometryFactory)4 BaseCloseable (com.revolsys.io.BaseCloseable)4 Transaction (com.revolsys.transaction.Transaction)4 ArrayList (java.util.ArrayList)4 Maps (com.revolsys.collection.map.Maps)3 Ellipsoid (com.revolsys.geometry.cs.Ellipsoid)3 Geometry (com.revolsys.geometry.model.Geometry)3 RecordWriter (com.revolsys.record.io.RecordWriter)3 RecordStore (com.revolsys.record.schema.RecordStore)3 Resource (com.revolsys.spring.resource.Resource)3 LinkedHashMap (java.util.LinkedHashMap)3