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;
}
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:
}
}
Aggregations