Search in sources :

Example 1 with Geospatial

use of org.apache.olingo.commons.api.edm.geo.Geospatial 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 Geospatial

use of org.apache.olingo.commons.api.edm.geo.Geospatial in project teiid by teiid.

the class AtomGeoValueSerializer method serialize.

public void serialize(final XMLStreamWriter writer, final Geospatial value) throws XMLStreamException {
    switch(value.getEdmPrimitiveTypeKind()) {
        case GeographyPoint:
        case GeometryPoint:
            writer.writeStartElement(Constants.PREFIX_GML, Constants.ELEM_POINT, Constants.NS_GML);
            writeSrsName(writer, value);
            points(writer, Collections.singleton((Point) value).iterator(), false);
            writer.writeEndElement();
            break;
        case GeometryMultiPoint:
        case GeographyMultiPoint:
            writer.writeStartElement(Constants.PREFIX_GML, Constants.ELEM_MULTIPOINT, Constants.NS_GML);
            writeSrsName(writer, value);
            if (!((MultiPoint) value).isEmpty()) {
                writer.writeStartElement(Constants.PREFIX_GML, Constants.ELEM_POINTMEMBERS, Constants.NS_GML);
                points(writer, ((MultiPoint) value).iterator(), true);
                writer.writeEndElement();
            }
            writer.writeEndElement();
            break;
        case GeometryLineString:
        case GeographyLineString:
            writer.writeStartElement(Constants.PREFIX_GML, Constants.ELEM_LINESTRING, Constants.NS_GML);
            writeSrsName(writer, value);
            lineStrings(writer, Collections.singleton((LineString) value).iterator(), false);
            writer.writeEndElement();
            break;
        case GeometryMultiLineString:
        case GeographyMultiLineString:
            writer.writeStartElement(Constants.PREFIX_GML, Constants.ELEM_MULTILINESTRING, Constants.NS_GML);
            writeSrsName(writer, value);
            if (!((MultiLineString) value).isEmpty()) {
                writer.writeStartElement(Constants.PREFIX_GML, Constants.ELEM_LINESTRINGMEMBERS, Constants.NS_GML);
                lineStrings(writer, ((MultiLineString) value).iterator(), true);
                writer.writeEndElement();
            }
            writer.writeEndElement();
            break;
        case GeographyPolygon:
        case GeometryPolygon:
            writer.writeStartElement(Constants.PREFIX_GML, Constants.ELEM_POLYGON, Constants.NS_GML);
            writeSrsName(writer, value);
            polygons(writer, Collections.singleton(((Polygon) value)).iterator(), false);
            writer.writeEndElement();
            break;
        case GeographyMultiPolygon:
        case GeometryMultiPolygon:
            writer.writeStartElement(Constants.PREFIX_GML, Constants.ELEM_MULTIPOLYGON, Constants.NS_GML);
            writeSrsName(writer, value);
            if (!((MultiPolygon) value).isEmpty()) {
                writer.writeStartElement(Constants.PREFIX_GML, Constants.ELEM_SURFACEMEMBERS, Constants.NS_GML);
                polygons(writer, ((MultiPolygon) value).iterator(), true);
                writer.writeEndElement();
            }
            writer.writeEndElement();
            break;
        case GeographyCollection:
        case GeometryCollection:
            writer.writeStartElement(Constants.PREFIX_GML, Constants.ELEM_GEOCOLLECTION, Constants.NS_GML);
            writeSrsName(writer, value);
            if (!((GeospatialCollection) value).isEmpty()) {
                writer.writeStartElement(Constants.PREFIX_GML, Constants.ELEM_GEOMEMBERS, Constants.NS_GML);
                for (Geospatial geospatial : ((GeospatialCollection) value)) {
                    serialize(writer, geospatial);
                }
                writer.writeEndElement();
            }
            writer.writeEndElement();
            break;
        default:
    }
}
Also used : Geospatial(org.apache.olingo.commons.api.edm.geo.Geospatial) Polygon(org.apache.olingo.commons.api.edm.geo.Polygon) MultiPolygon(org.apache.olingo.commons.api.edm.geo.MultiPolygon)

Aggregations

Geospatial (org.apache.olingo.commons.api.edm.geo.Geospatial)2 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 XMLOutputFactory (javax.xml.stream.XMLOutputFactory)1 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)1 MultiPolygon (org.apache.olingo.commons.api.edm.geo.MultiPolygon)1 Polygon (org.apache.olingo.commons.api.edm.geo.Polygon)1 EdmDate (org.apache.olingo.commons.core.edm.primitivetype.EdmDate)1 EdmString (org.apache.olingo.commons.core.edm.primitivetype.EdmString)1 GeometryInputSource (org.teiid.GeometryInputSource)1 TeiidException (org.teiid.core.TeiidException)1 TranslatorException (org.teiid.translator.TranslatorException)1