public static String javaType(EdmType type) { // TODO support complex type keys? assert (type.isSimple()); String javaType = null; if (EdmSimpleType.INT64 == type) { javaType = "Long"; } else if (EdmSimpleType.INT32 == type) { javaType = "Integer"; } else if (EdmSimpleType.INT16 == type) { javaType = "Integer"; } else if (EdmSimpleType.STRING == type) { javaType = "String"; } else if (EdmSimpleType.DATETIME == type) { javaType = "java.util.Date"; } else if (EdmSimpleType.TIME == type) { javaType = "java.util.Date"; } else if (EdmSimpleType.DECIMAL == type) { javaType = "java.math.BigDecimal"; } else if (EdmSimpleType.SINGLE == type) { javaType = "Float"; } else if (EdmSimpleType.DOUBLE == type) { javaType = "Double"; } else if (EdmSimpleType.BOOLEAN == type) { javaType = "Boolean"; } else if (EdmSimpleType.GUID == type) { javaType = "String"; } else if (EdmSimpleType.BINARY == type) { javaType = "String"; } else { // TODO support types other than Long and String throw new RuntimeException( "Entity property type " + type.getFullyQualifiedTypeName() + " not supported"); } return javaType; }
private void writeProperty( StreamWriter writer, EntityMetadata entityMetadata, EntityProperty property) { String elementText = entityMetadata.getPropertyValueAsString(property); writer.startElement(new QName(d, property.getName(), "d")); EdmType type = MetadataOData4j.termValueToEdmType( entityMetadata.getTermValue(property.getFullyQualifiedName(), TermValueType.TERM_NAME)); boolean isNullable = entityMetadata.isPropertyNullable(property.getFullyQualifiedName()); // Append Type Attribute if (!type.equals(EdmSimpleType.STRING)) { writer.writeAttribute(new QName(m, "type", "m"), type.getFullyQualifiedTypeName()); } // Append Null attribute if (isNullable && (elementText.isEmpty()) && !type.equals(EdmSimpleType.STRING)) { writer.writeAttribute(new QName(m, "null", "m"), "true"); } // Write the property text if (type.equals(EdmSimpleType.DATETIME) && !elementText.isEmpty()) { // Write dates in UTC format SimpleDateFormat formatUTC = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); formatUTC.setTimeZone(TimeZone.getTimeZone("UTC")); writer.writeElementText(formatUTC.format((Date) property.getValue())); } else if (elementText != null) { writer.writeElementText(elementText); } writer.endElement(); }
@SuppressWarnings("unchecked") protected void writeProperty( XMLWriter2 writer, String name, EdmType type, Object value, boolean isDocumentElement, boolean writeType) { writer.startElement(new QName2(d, name, "d")); if (isDocumentElement) { writer.writeNamespace("m", m); writer.writeNamespace("d", d); } String sValue = null; if (!type.isSimple()) { if (writeType) { String typename = type.getFullyQualifiedTypeName(); if (value instanceof OCollection) { EdmCollectionType collectionType = (EdmCollectionType) type; typename = "Bag(" + collectionType.getItemType().getFullyQualifiedTypeName() + ")"; } writer.writeAttribute(new QName2(m, "type", "m"), typename); } // complex or collection if (value instanceof OCollection) { writeCollection(writer, name, (OCollection<? extends OObject>) value); } else if (value instanceof OComplexObject) { writeProperties(writer, ((OComplexObject) value).getProperties()); } else { // deprecated form of a complex object. List<OProperty<?>> complexProperties = (List<OProperty<?>>) value; if (complexProperties != null) { writeProperties(writer, complexProperties); } } } else { // simple // write the type attribute if requested and not a string if (writeType && type != EdmSimpleType.STRING) { writer.writeAttribute(new QName2(m, "type", "m"), type.getFullyQualifiedTypeName()); } // now write the value if (type == EdmSimpleType.INT32) { if (value != null) { sValue = value.toString(); } } else if (type == EdmSimpleType.INT16) { if (value != null) { sValue = value.toString(); } } else if (type == EdmSimpleType.INT64) { if (value != null) { sValue = value.toString(); } } else if (type == EdmSimpleType.BOOLEAN) { if (value != null) { sValue = value.toString(); } } else if (type == EdmSimpleType.BYTE) { if (value != null) { sValue = value.toString(); } } else if (type == EdmSimpleType.SBYTE) { if (value != null) { sValue = value.toString(); } } else if (type == EdmSimpleType.DECIMAL) { if (value != null) { sValue = ((BigDecimal) value).toPlainString(); } } else if (type == EdmSimpleType.SINGLE) { if (value != null) { sValue = value.toString(); } } else if (type == EdmSimpleType.DOUBLE) { if (value != null) { sValue = value.toString(); } } else if (type == EdmSimpleType.STRING) { if (value != null) { sValue = value.toString(); } } else if (type == EdmSimpleType.DATETIME) { if (value != null) sValue = InternalUtil.formatDateTimeForXml((LocalDateTime) value); } else if (type == EdmSimpleType.BINARY) { byte[] bValue = (byte[]) value; if (value != null) { sValue = Base64.encodeBase64String(bValue); } } else if (type == EdmSimpleType.GUID) { if (value != null) { sValue = value.toString(); } } else if (type == EdmSimpleType.TIME) { if (value != null) { sValue = InternalUtil.formatTimeForXml((LocalTime) value); } } else if (type == EdmSimpleType.DATETIMEOFFSET) { // Edm.DateTimeOffset '-'? yyyy '-' mm '-' dd 'T' hh ':' mm // ':' ss ('.' s+)? (zzzzzz)? if (value != null) { sValue = InternalUtil.formatDateTimeOffsetForXml((DateTime) value); } } else { throw new UnsupportedOperationException("Implement " + type); } } if (value == null) { writer.writeAttribute(new QName2(m, "null", "m"), "true"); } else if (sValue != null) { writer.writeText(sValue); } writer.endElement(name); }
@Override public BaseResponse executeCall(String sql, List<SQLParam> parameters, EdmType returnType) { ConnectionImpl connection = null; try { LogManager.logDetail(LogConstants.CTX_ODATA, "Teiid-Query:", sql); // $NON-NLS-1$ connection = getConnection(); final CallableStatementImpl stmt = connection.prepareCall(sql); int i = 1; if (returnType != null && returnType.isSimple()) { stmt.registerOutParameter( i++, JDBCSQLTypeInfo.getSQLType( ODataTypeManager.teiidType(returnType.getFullyQualifiedTypeName()))); } if (!parameters.isEmpty()) { for (SQLParam param : parameters) { stmt.setObject(i++, param.value, param.sqlType); } } boolean results = stmt.execute(); if (results) { final ResultSet rs = stmt.getResultSet(); OCollection.Builder resultRows = OCollections.newBuilder( (EdmComplexType) ((EdmCollectionType) returnType).getItemType()); while (rs.next()) { int idx = 1; List<OProperty<?>> row = new ArrayList<OProperty<?>>(); Iterator<EdmProperty> props = ((EdmComplexType) ((EdmCollectionType) returnType).getItemType()) .getProperties() .iterator(); while (props.hasNext()) { EdmProperty prop = props.next(); row.add( buildPropery( prop.getName(), prop.getType(), rs.getObject(idx++), invalidCharacterReplacement)); } OComplexObject erow = OComplexObjects.create( (EdmComplexType) ((EdmCollectionType) returnType).getItemType(), row); resultRows.add(erow); } String collectionName = returnType.getFullyQualifiedTypeName(); collectionName = collectionName.replace("(", "_"); // $NON-NLS-1$ //$NON-NLS-2$ collectionName = collectionName.replace(")", "_"); // $NON-NLS-1$ //$NON-NLS-2$ return Responses.collection(resultRows.build(), null, null, null, collectionName); } if (returnType != null) { Object result = stmt.getObject(1); OProperty prop = buildPropery("return", returnType, result, invalidCharacterReplacement); // $NON-NLS-1$ return Responses.property(prop); } return null; } catch (Exception e) { throw new ServerErrorException(e.getMessage(), e); } finally { if (connection != null) { try { connection.close(); } catch (SQLException e) { } } } }