public static void write(EdmDataServices services, Writer w) { XMLWriter2 writer = XMLFactoryProvider2.getInstance().newXMLWriterFactory2().createXMLWriter(w); writer.startDocument(); writer.startElement(new QName2(edmx, "Edmx", "edmx")); writer.writeAttribute("Version", "1.0"); writer.writeNamespace("edmx", edmx); writer.writeNamespace("d", d); writer.writeNamespace("m", m); writeExtensionNamespaces(services, writer); writer.startElement(new QName2(edmx, "DataServices", "edmx")); writer.writeAttribute(new QName2(m, "DataServiceVersion", "m"), "1.0"); // Schema for (EdmSchema schema : services.getSchemas()) { writer.startElement(new QName2("Schema"), edm); writer.writeAttribute("Namespace", schema.getNamespace()); writeAnnotationAttributes(schema, writer); writeDocumentation(schema, writer); // ComplexType for (EdmComplexType ect : schema.getComplexTypes()) { writer.startElement(new QName2("ComplexType")); writer.writeAttribute("Name", ect.getName()); if (null != ect.getIsAbstract()) { writer.writeAttribute("Abstract", ect.getIsAbstract().toString()); } writeAnnotationAttributes(ect, writer); writeDocumentation(ect, writer); writeProperties(ect.getProperties(), writer); writeAnnotationElements(ect, writer); writer.endElement("ComplexType"); } // EntityType for (EdmEntityType eet : schema.getEntityTypes()) { writer.startElement(new QName2("EntityType")); writer.writeAttribute("Name", eet.getName()); if (null != eet.getIsAbstract()) { writer.writeAttribute("Abstract", eet.getIsAbstract().toString()); } if (Boolean.TRUE.equals(eet.getHasStream())) { writer.writeAttribute(new QName2(m, "HasStream", "m"), "true"); } // keys only on base types if (eet.isRootType()) { writeAnnotationAttributes(eet, writer); writeDocumentation(eet, writer); writer.startElement(new QName2("Key")); for (String key : eet.getKeys()) { writer.startElement(new QName2("PropertyRef")); writer.writeAttribute("Name", key); writer.endElement("PropertyRef"); } writer.endElement("Key"); } else { writer.writeAttribute("BaseType", eet.getBaseType().getFullyQualifiedTypeName()); writeAnnotationAttributes(eet, writer); writeDocumentation(eet, writer); } writeProperties(eet.getDeclaredProperties(), writer); for (EdmNavigationProperty np : eet.getDeclaredNavigationProperties()) { writer.startElement(new QName2("NavigationProperty")); writer.writeAttribute("Name", np.getName()); writer.writeAttribute("Relationship", np.getRelationship().getFQNamespaceName()); writer.writeAttribute("FromRole", np.getFromRole().getRole()); writer.writeAttribute("ToRole", np.getToRole().getRole()); writeAnnotationAttributes(np, writer); writeDocumentation(np, writer); writeAnnotationElements(np, writer); writer.endElement("NavigationProperty"); } writeAnnotationElements(eet, writer); writer.endElement("EntityType"); } // Association for (EdmAssociation assoc : schema.getAssociations()) { writer.startElement(new QName2("Association")); writer.writeAttribute("Name", assoc.getName()); writeAnnotationAttributes(assoc, writer); writeDocumentation(assoc, writer); writer.startElement(new QName2("End")); writer.writeAttribute("Role", assoc.getEnd1().getRole()); writer.writeAttribute("Type", assoc.getEnd1().getType().getFullyQualifiedTypeName()); writer.writeAttribute("Multiplicity", assoc.getEnd1().getMultiplicity().getSymbolString()); writer.endElement("End"); writer.startElement(new QName2("End")); writer.writeAttribute("Role", assoc.getEnd2().getRole()); writer.writeAttribute("Type", assoc.getEnd2().getType().getFullyQualifiedTypeName()); writer.writeAttribute("Multiplicity", assoc.getEnd2().getMultiplicity().getSymbolString()); writer.endElement("End"); writeAnnotationElements(assoc, writer); writer.endElement("Association"); } // EntityContainer for (EdmEntityContainer container : schema.getEntityContainers()) { writer.startElement(new QName2("EntityContainer")); writer.writeAttribute("Name", container.getName()); writer.writeAttribute( new QName2(m, "IsDefaultEntityContainer", "m"), Boolean.toString(container.isDefault())); writeAnnotationAttributes(container, writer); writeDocumentation(container, writer); for (EdmEntitySet ees : container.getEntitySets()) { writer.startElement(new QName2("EntitySet")); writer.writeAttribute("Name", ees.getName()); writer.writeAttribute("EntityType", ees.getType().getFullyQualifiedTypeName()); writeAnnotationAttributes(ees, writer); writeDocumentation(ees, writer); writeAnnotationElements(ees, writer); writer.endElement("EntitySet"); } for (EdmFunctionImport fi : container.getFunctionImports()) { writer.startElement(new QName2("FunctionImport")); writer.writeAttribute("Name", fi.getName()); if (null != fi.getEntitySet()) { writer.writeAttribute("EntitySet", fi.getEntitySet().getName()); } if (fi.getReturnType() != null) { // TODO: how to differentiate inline ReturnType vs embedded ReturnType? writer.writeAttribute("ReturnType", fi.getReturnType().getFullyQualifiedTypeName()); } writer.writeAttribute(new QName2(m, "HttpMethod", "m"), fi.getHttpMethod()); writeAnnotationAttributes(fi, writer); writeDocumentation(fi, writer); for (EdmFunctionParameter param : fi.getParameters()) { writer.startElement(new QName2("Parameter")); writer.writeAttribute("Name", param.getName()); writer.writeAttribute("Type", param.getType().getFullyQualifiedTypeName()); if (param.getMode() != null) writer.writeAttribute("Mode", param.getMode().toString()); writeAnnotationAttributes(param, writer); writeDocumentation(param, writer); writeAnnotationElements(param, writer); writer.endElement("Parameter"); } writeAnnotationElements(fi, writer); writer.endElement("FunctionImport"); } for (EdmAssociationSet eas : container.getAssociationSets()) { writer.startElement(new QName2("AssociationSet")); writer.writeAttribute("Name", eas.getName()); writer.writeAttribute("Association", eas.getAssociation().getFQNamespaceName()); writeAnnotationAttributes(eas, writer); writeDocumentation(eas, writer); writer.startElement(new QName2("End")); writer.writeAttribute("Role", eas.getEnd1().getRole().getRole()); writer.writeAttribute("EntitySet", eas.getEnd1().getEntitySet().getName()); writer.endElement("End"); writer.startElement(new QName2("End")); writer.writeAttribute("Role", eas.getEnd2().getRole().getRole()); writer.writeAttribute("EntitySet", eas.getEnd2().getEntitySet().getName()); writer.endElement("End"); writeAnnotationElements(eas, writer); writer.endElement("AssociationSet"); } writeAnnotationElements(container, writer); writer.endElement("EntityContainer"); } writeAnnotationElements(schema, writer); writer.endElement("Schema"); } writer.endDocument(); }
private static void buildEntityTypes( MetadataStore metadataStore, List<EdmSchema.Builder> edmSchemas) { for (Schema schema : metadataStore.getSchemaList()) { List<EdmEntitySet.Builder> entitySets = new ArrayList<EdmEntitySet.Builder>(); List<EdmEntityType.Builder> entityTypes = new ArrayList<EdmEntityType.Builder>(); for (Table table : schema.getTables().values()) { KeyRecord primaryKey = table.getPrimaryKey(); List<KeyRecord> uniques = table.getUniqueKeys(); if (primaryKey == null && uniques.isEmpty()) { LogManager.logDetail( LogConstants.CTX_ODATA, ODataPlugin.Util.gs(ODataPlugin.Event.TEIID16002, table.getFullName())); continue; } EdmEntityType.Builder entityType = EdmEntityType.newBuilder().setName(table.getName()).setNamespace(schema.getName()); // adding key if (primaryKey != null) { for (Column c : primaryKey.getColumns()) { entityType.addKeys(c.getName()); } } else { for (Column c : uniques.get(0).getColumns()) { entityType.addKeys(c.getName()); } } // adding properties for (Column c : table.getColumns()) { EdmProperty.Builder property = EdmProperty.newBuilder(c.getName()) .setType(ODataTypeManager.odataType(c.getDatatype().getRuntimeTypeName())) .setNullable(c.getNullType() == NullType.Nullable); if (c.getDatatype() .getRuntimeTypeName() .equals(DataTypeManager.DefaultDataTypes.STRING)) { property.setFixedLength(c.isFixedLength()).setMaxLength(c.getLength()).setUnicode(true); } entityType.addProperties(property); } // entity set one for one entity type EdmEntitySet.Builder entitySet = EdmEntitySet.newBuilder().setName(table.getName()).setEntityType(entityType); entityType.setNamespace(schema.getName()); entitySets.add(entitySet); // add enitity types for entity schema entityTypes.add(entityType); } // entity container is holder entity sets, association sets, function imports EdmEntityContainer.Builder entityContainer = EdmEntityContainer.newBuilder() .setName(schema.getName()) .setIsDefault(false) .addEntitySets(entitySets); // build entity schema EdmSchema.Builder modelSchema = EdmSchema.newBuilder() .setNamespace(schema.getName()) .addEntityTypes(entityTypes) .addEntityContainers(entityContainer); edmSchemas.add(modelSchema); } }
private EdmSchema.Builder parseEdmSchema(XMLEventReader2 reader, StartElement2 schemaElement) { String schemaNamespace = schemaElement.getAttributeByName(new QName2("Namespace")).getValue(); String schemaAlias = getAttributeValueIfExists(schemaElement, new QName2("Alias")); final List<EdmEntityType.Builder> edmEntityTypes = new ArrayList<EdmEntityType.Builder>(); List<EdmComplexType.Builder> edmComplexTypes = new ArrayList<EdmComplexType.Builder>(); List<EdmAssociation.Builder> edmAssociations = new ArrayList<EdmAssociation.Builder>(); List<EdmEntityContainer.Builder> edmEntityContainers = new ArrayList<EdmEntityContainer.Builder>(); List<EdmAnnotation<?>> annotElements = new ArrayList<EdmAnnotation<?>>(); while (reader.hasNext()) { XMLEvent2 event = reader.nextEvent(); if (event.isStartElement()) { if (isElement( event, EDM2006_ENTITYTYPE, EDM2007_ENTITYTYPE, EDM2008_1_ENTITYTYPE, EDM2008_9_ENTITYTYPE, EDM2009_8_ENTITYTYPE, EDM2009_11_ENTITYTYPE)) { EdmEntityType.Builder edmEntityType = parseEdmEntityType(reader, schemaNamespace, schemaAlias, event.asStartElement()); edmEntityTypes.add(edmEntityType); } else if (isElement( event, EDM2006_ASSOCIATION, EDM2007_ASSOCIATION, EDM2008_1_ASSOCIATION, EDM2008_9_ASSOCIATION, EDM2009_8_ASSOCIATION, EDM2009_11_ASSOCIATION)) { EdmAssociation.Builder edmAssociation = parseEdmAssociation(reader, schemaNamespace, schemaAlias, event.asStartElement()); edmAssociations.add(edmAssociation); } else if (isElement( event, EDM2006_COMPLEXTYPE, EDM2007_COMPLEXTYPE, EDM2008_1_COMPLEXTYPE, EDM2008_9_COMPLEXTYPE, EDM2009_8_COMPLEXTYPE, EDM2009_11_COMPLEXTYPE)) { EdmComplexType.Builder edmComplexType = parseEdmComplexType(reader, schemaNamespace, event.asStartElement()); edmComplexTypes.add(edmComplexType); } else if (isElement( event, EDM2006_ENTITYCONTAINER, EDM2007_ENTITYCONTAINER, EDM2008_1_ENTITYCONTAINER, EDM2008_9_ENTITYCONTAINER, EDM2009_8_ENTITYCONTAINER, EDM2009_11_ENTITYCONTAINER)) { EdmEntityContainer.Builder edmEntityContainer = parseEdmEntityContainer(reader, schemaNamespace, event.asStartElement()); edmEntityContainers.add(edmEntityContainer); } else { EdmAnnotation<?> anElement = getAnnotationElements(event, reader); if (anElement != null) { annotElements.add(anElement); } } } if (isEndElement(event, schemaElement.getName())) { return EdmSchema.newBuilder() .setNamespace(schemaNamespace) .setAlias(schemaAlias) .addEntityTypes(edmEntityTypes) .addComplexTypes(edmComplexTypes) .addAssociations(edmAssociations) .addEntityContainers(edmEntityContainers) .setAnnotations(getAnnotations(schemaElement)) .setAnnotationElements(annotElements); } } throw new UnsupportedOperationException(); }