Search in sources :

Example 1 with GeometryInputSource

use of org.teiid.GeometryInputSource in project teiid by teiid.

the class ODataTypeManager method convertToTeiidRuntimeType.

/**
 * @param type
 * @param value
 * @param odataType type hint if the value could be a string containing a literal value of another type
 * @return
 * @throws TeiidException
 */
public static Object convertToTeiidRuntimeType(Class<?> type, Object value, String odataType) throws TeiidException {
    if (value == null) {
        return null;
    }
    if (type.isAssignableFrom(value.getClass())) {
        return value;
    }
    if (value instanceof UUID) {
        return value.toString();
    }
    if (type.isArray() && value instanceof List<?>) {
        List<?> list = (List<?>) value;
        Class<?> expectedArrayComponentType = type.getComponentType();
        Object array = Array.newInstance(type.getComponentType(), list.size());
        for (int i = 0; i < list.size(); i++) {
            Object arrayItem = convertToTeiidRuntimeType(expectedArrayComponentType, list.get(i), null);
            Array.set(array, i, arrayItem);
        }
        return array;
    }
    if (odataType != null && value instanceof String) {
        try {
            value = ODataTypeManager.parseLiteral(odataType, (String) value);
        } catch (TeiidException e) {
            throw new TranslatorException(e);
        }
    }
    if (value instanceof Geospatial && type == DataTypeManager.DefaultDataClasses.GEOMETRY) {
        final Geospatial val = (Geospatial) value;
        // if (val.getDimension() == Dimension.GEOMETRY) {
        return new GeometryInputSource() {

            @Override
            public Reader getGml() throws Exception {
                AtomGeoValueSerializer serializer = new AtomGeoValueSerializer();
                XMLOutputFactory factory = XMLOutputFactory.newInstance();
                StringWriter sw = new StringWriter();
                final XMLStreamWriter writer = factory.createXMLStreamWriter(sw);
                serializer.serialize(writer, val);
                writer.close();
                return new StringReader(sw.toString());
            }

            @Override
            public Integer getSrid() {
                String srid = val.getSrid().toString();
                try {
                    return Integer.parseInt(srid);
                } catch (NumberFormatException e) {
                    return null;
                }
            }
        };
    // }
    }
    if (value instanceof Calendar) {
        Calendar calender = (Calendar) value;
        if (type.isAssignableFrom(java.sql.Time.class)) {
            calender.set(Calendar.YEAR, 1970);
            calender.set(Calendar.MONTH, Calendar.JANUARY);
            calender.set(Calendar.DAY_OF_MONTH, 1);
            calender.set(Calendar.MILLISECOND, 0);
            return new Time(calender.getTimeInMillis());
        } else if (type.isAssignableFrom(java.sql.Date.class)) {
            calender.set(Calendar.HOUR_OF_DAY, 0);
            calender.set(Calendar.MINUTE, 0);
            calender.set(Calendar.SECOND, 0);
            calender.set(Calendar.MILLISECOND, 0);
            return new java.sql.Date(calender.getTimeInMillis());
        } else if (type.isAssignableFrom(java.sql.Timestamp.class)) {
            return new Timestamp(calender.getTimeInMillis());
        }
    }
    Transform transform = DataTypeManager.getTransform(value.getClass(), type);
    if (transform != null) {
        try {
            value = transform.transform(value, type);
        } catch (TransformationException e) {
            throw new TeiidException(e);
        }
    }
    return value;
}
Also used : XMLOutputFactory(javax.xml.stream.XMLOutputFactory) GeometryInputSource(org.teiid.GeometryInputSource) Time(java.sql.Time) EdmString(org.apache.olingo.commons.core.edm.primitivetype.EdmString) Timestamp(java.sql.Timestamp) StringWriter(java.io.StringWriter) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) StringReader(java.io.StringReader) List(java.util.List) UUID(java.util.UUID) Calendar(java.util.Calendar) Geospatial(org.apache.olingo.commons.api.edm.geo.Geospatial) EdmDate(org.apache.olingo.commons.core.edm.primitivetype.EdmDate) Date(java.sql.Date) TeiidException(org.teiid.core.TeiidException) Date(java.sql.Date) TranslatorException(org.teiid.translator.TranslatorException)

Example 2 with GeometryInputSource

use of org.teiid.GeometryInputSource in project teiid by teiid.

the class TestODataQueryExecution method testGeometry.

@Test
public void testGeometry() throws Exception {
    String query = "SELECT * FROM Airports_Location";
    String expectedURL = "Airports?$select=IcaoCode,Location";
    FileReader reader = new FileReader(UnitTestUtil.getTestDataFile("airport-locations.json"));
    MetadataFactory mf = TestODataMetadataProcessor.tripPinMetadata();
    ResultSetExecution execution = helpExecute(TestODataMetadataProcessor.tripPinMetadata(), query, ObjectConverterUtil.convertToString(reader), expectedURL);
    List<?> row = execution.next();
    assertEquals("187 Suffolk Ln.", row.get(2));
    assertEquals("xyz", row.get(0));
    GeometryInputSource gis = (GeometryInputSource) row.get(1);
    assertEquals("<gml:Point><gml:pos>-48.23456 20.12345</gml:pos></gml:Point>", ObjectConverterUtil.convertToString(gis.getGml()));
    assertEquals(4326, gis.getSrid().intValue());
    row = execution.next();
    assertEquals("gso", row.get(0));
    gis = (GeometryInputSource) row.get(1);
    assertEquals("<gml:Point><gml:pos>1.0 2.0</gml:pos></gml:Point>", ObjectConverterUtil.convertToString(gis.getGml()));
    assertNull(execution.next());
    reader.close();
}
Also used : ResultSetExecution(org.teiid.translator.ResultSetExecution) MetadataFactory(org.teiid.metadata.MetadataFactory) GeometryInputSource(org.teiid.GeometryInputSource) FileReader(java.io.FileReader) Test(org.junit.Test)

Example 3 with GeometryInputSource

use of org.teiid.GeometryInputSource in project teiid by teiid.

the class ConnectorWorkItem method convertToRuntimeType.

static Object convertToRuntimeType(BufferManager bm, Object value, Class<?> desiredType, CommandContext context) throws TransformationException {
    if (desiredType != DataTypeManager.DefaultDataClasses.XML || !(value instanceof Source)) {
        if (value instanceof DataSource) {
            final DataSource ds = (DataSource) value;
            try {
                // Teiid uses the datasource interface in a degenerate way that
                // reuses the stream, so we test for that here
                InputStream initial = ds.getInputStream();
                InputStream other = null;
                try {
                    other = ds.getInputStream();
                } catch (IOException e) {
                // likely streaming
                }
                if (other != null && initial != other) {
                    initial.close();
                    other.close();
                    if (value instanceof InputStreamFactory) {
                        return asLob((InputStreamFactory) value, desiredType);
                    }
                    return asLob(new InputStreamFactory() {

                        @Override
                        public InputStream getInputStream() throws IOException {
                            return ds.getInputStream();
                        }
                    }, desiredType);
                }
                // $NON-NLS-1$
                FileStore fs = bm.createFileStore("bytes");
                // TODO: guess at the encoding from the content type
                FileStoreInputStreamFactory fsisf = new FileStoreInputStreamFactory(fs, Streamable.ENCODING);
                SaveOnReadInputStream is = new SaveOnReadInputStream(initial, fsisf);
                if (context != null) {
                    context.addCreatedLob(fsisf);
                }
                return asLob(is.getInputStreamFactory(), desiredType);
            } catch (IOException e) {
                throw new TransformationException(QueryPlugin.Event.TEIID30500, e, e.getMessage());
            }
        }
        if (value instanceof InputStreamFactory) {
            return asLob((InputStreamFactory) value, desiredType);
        }
        if (value instanceof GeometryInputSource) {
            GeometryInputSource gis = (GeometryInputSource) value;
            try {
                InputStream is = gis.getEwkb();
                if (is != null) {
                    return GeometryUtils.geometryFromEwkb(is, gis.getSrid());
                }
            } catch (Exception e) {
                throw new TransformationException(e);
            }
            try {
                Reader r = gis.getGml();
                if (r != null) {
                    return GeometryUtils.geometryFromGml(r, gis.getSrid());
                }
            } catch (Exception e) {
                throw new TransformationException(e);
            }
        }
    }
    if (value instanceof Source) {
        if (!(value instanceof InputStreamFactory)) {
            if (value instanceof StreamSource) {
                StreamSource ss = (StreamSource) value;
                InputStream is = ss.getInputStream();
                Reader r = ss.getReader();
                if (is == null && r != null) {
                    is = new ReaderInputStream(r, Streamable.CHARSET);
                }
                // $NON-NLS-1$
                final FileStore fs = bm.createFileStore("xml");
                final FileStoreInputStreamFactory fsisf = new FileStoreInputStreamFactory(fs, Streamable.ENCODING);
                value = new SaveOnReadInputStream(is, fsisf).getInputStreamFactory();
                if (context != null) {
                    context.addCreatedLob(fsisf);
                }
            } else if (value instanceof StAXSource) {
                // TODO: do this lazily.  if the first access to get the STaXSource, then
                // it's more efficient to let the processing happen against STaX
                StAXSource ss = (StAXSource) value;
                try {
                    // $NON-NLS-1$
                    final FileStore fs = bm.createFileStore("xml");
                    final FileStoreInputStreamFactory fsisf = new FileStoreInputStreamFactory(fs, Streamable.ENCODING);
                    value = new SaveOnReadInputStream(new XMLInputStream(ss, XMLSystemFunctions.getOutputFactory(true)), fsisf).getInputStreamFactory();
                    if (context != null) {
                        context.addCreatedLob(fsisf);
                    }
                } catch (XMLStreamException e) {
                    throw new TransformationException(e);
                }
            } else {
                // maybe dom or some other source we want to get out of memory
                StandardXMLTranslator sxt = new StandardXMLTranslator((Source) value);
                SQLXMLImpl sqlxml;
                try {
                    sqlxml = XMLSystemFunctions.saveToBufferManager(bm, sxt, context);
                } catch (TeiidComponentException e) {
                    throw new TransformationException(e);
                } catch (TeiidProcessingException e) {
                    throw new TransformationException(e);
                }
                return new XMLType(sqlxml);
            }
        }
        return new XMLType(new SQLXMLImpl((InputStreamFactory) value));
    }
    return DataTypeManager.convertToRuntimeType(value, desiredType != DataTypeManager.DefaultDataClasses.OBJECT);
}
Also used : SaveOnReadInputStream(org.teiid.dqp.internal.process.SaveOnReadInputStream) XMLInputStream(org.teiid.util.XMLInputStream) FileStoreInputStreamFactory(org.teiid.common.buffer.FileStoreInputStreamFactory) XMLInputStream(org.teiid.util.XMLInputStream) SaveOnReadInputStream(org.teiid.dqp.internal.process.SaveOnReadInputStream) ReaderInputStream(org.teiid.core.util.ReaderInputStream) InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) GeometryInputSource(org.teiid.GeometryInputSource) Reader(java.io.Reader) IOException(java.io.IOException) StAXSource(javax.xml.transform.stax.StAXSource) FileStoreInputStreamFactory(org.teiid.common.buffer.FileStoreInputStreamFactory) GeometryInputSource(org.teiid.GeometryInputSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) StAXSource(javax.xml.transform.stax.StAXSource) DataSource(javax.activation.DataSource) CollectionTupleSource(org.teiid.query.processor.CollectionTupleSource) ResourceException(javax.resource.ResourceException) TeiidComponentException(org.teiid.core.TeiidComponentException) XMLStreamException(javax.xml.stream.XMLStreamException) TeiidProcessingException(org.teiid.core.TeiidProcessingException) TeiidException(org.teiid.core.TeiidException) IOException(java.io.IOException) DataSource(javax.activation.DataSource) TeiidProcessingException(org.teiid.core.TeiidProcessingException) FileStore(org.teiid.common.buffer.FileStore) ReaderInputStream(org.teiid.core.util.ReaderInputStream) XMLStreamException(javax.xml.stream.XMLStreamException) TeiidComponentException(org.teiid.core.TeiidComponentException)

Aggregations

GeometryInputSource (org.teiid.GeometryInputSource)3 TeiidException (org.teiid.core.TeiidException)2 FileReader (java.io.FileReader)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Reader (java.io.Reader)1 StringReader (java.io.StringReader)1 StringWriter (java.io.StringWriter)1 Date (java.sql.Date)1 Time (java.sql.Time)1 Timestamp (java.sql.Timestamp)1 Calendar (java.util.Calendar)1 List (java.util.List)1 UUID (java.util.UUID)1 DataSource (javax.activation.DataSource)1 ResourceException (javax.resource.ResourceException)1 XMLOutputFactory (javax.xml.stream.XMLOutputFactory)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)1 Source (javax.xml.transform.Source)1