use of org.odata4j.edm.EdmSchema.Builder in project teiid by teiid.
the class ODataEntitySchemaBuilder method buildAssosiationSets.
public static void buildAssosiationSets(Schema schema, List<Builder> edmSchemas, boolean preserveEntityTypeName) {
EdmSchema.Builder odataSchema = findSchema(edmSchemas, schema.getName());
EdmEntityContainer.Builder entityContainer = findEntityContainer(edmSchemas, schema.getName());
List<EdmAssociationSet.Builder> assosiationSets = new ArrayList<EdmAssociationSet.Builder>();
List<EdmAssociation.Builder> assosiations = new ArrayList<EdmAssociation.Builder>();
for (Table table : schema.getTables().values()) {
// skip if the table does not have the PK or unique
KeyRecord primaryKey = table.getPrimaryKey();
List<KeyRecord> uniques = table.getUniqueKeys();
if (primaryKey == null && uniques.isEmpty()) {
continue;
}
// build Associations
for (ForeignKey fk : table.getForeignKeys()) {
EdmEntitySet.Builder entitySet = findEntitySet(edmSchemas, schema.getName(), table.getName());
EdmEntitySet.Builder refEntitySet = findEntitySet(edmSchemas, schema.getName(), fk.getReferenceTableName());
EdmEntityType.Builder entityType = findEntityType(edmSchemas, schema.getName(), getEntityTypeName(table, preserveEntityTypeName));
EdmEntityType.Builder refEntityType = findEntityType(edmSchemas, schema.getName(), getEntityTypeName(fk.getPrimaryKey().getParent(), preserveEntityTypeName));
// check to see if fk is part of this table's pk, then it is 1 to 1 relation
boolean onetoone = sameColumnSet(table.getPrimaryKey(), fk);
// Build Association Ends
EdmAssociationEnd.Builder endSelf = EdmAssociationEnd.newBuilder().setRole(table.getName()).setType(entityType).setMultiplicity(onetoone ? EdmMultiplicity.ZERO_TO_ONE : EdmMultiplicity.MANY);
EdmAssociationEnd.Builder endRef = EdmAssociationEnd.newBuilder().setRole(fk.getReferenceTableName()).setType(refEntityType).setMultiplicity(EdmMultiplicity.ZERO_TO_ONE);
// Build Association
EdmAssociation.Builder association = EdmAssociation.newBuilder();
// $NON-NLS-1$
association.setName(table.getName() + "_" + fk.getName());
association.setEnds(endSelf, endRef);
association.setNamespace(refEntityType.getFullyQualifiedTypeName().substring(0, refEntityType.getFullyQualifiedTypeName().indexOf('.')));
assosiations.add(association);
// Build ReferentialConstraint
if (fk.getReferenceColumns() != null) {
EdmReferentialConstraint.Builder erc = EdmReferentialConstraint.newBuilder();
erc.setPrincipalRole(fk.getReferenceTableName());
erc.addPrincipalReferences(fk.getReferenceColumns());
erc.setDependentRole(table.getName());
erc.addDependentReferences(getColumnNames(fk.getColumns()));
association.setRefConstraint(erc);
}
if (!fk.getReferenceTableName().equalsIgnoreCase(table.getName())) {
// Add EdmNavigationProperty to entity type
String navigationName = getNavigationName(entityType, fk.getReferenceTableName());
EdmNavigationProperty.Builder nav = EdmNavigationProperty.newBuilder(navigationName);
nav.setRelationshipName(fk.getName());
nav.setFromToName(table.getName(), fk.getReferenceTableName());
nav.setRelationship(association);
nav.setFromTo(endSelf, endRef);
entityType.addNavigationProperties(nav);
}
// Add EdmNavigationProperty to Reference entity type
String navigationName = getNavigationName(refEntityType, table.getName());
EdmNavigationProperty.Builder refNav = EdmNavigationProperty.newBuilder(navigationName);
refNav.setRelationshipName(fk.getName());
refNav.setFromToName(fk.getReferenceTableName(), table.getName());
refNav.setRelationship(association);
refNav.setFromTo(endRef, endSelf);
refEntityType.addNavigationProperties(refNav);
// build AssosiationSet
EdmAssociationSet.Builder assosiationSet = EdmAssociationSet.newBuilder().setName(// $NON-NLS-1$
table.getName() + "_" + fk.getName()).setAssociationName(fk.getName());
// Build AssosiationSet Ends
EdmAssociationSetEnd.Builder endOne = EdmAssociationSetEnd.newBuilder().setEntitySet(entitySet).setRoleName(table.getName()).setRole(EdmAssociationEnd.newBuilder().setType(entityType).setRole(entityType.getName()));
EdmAssociationSetEnd.Builder endTwo = EdmAssociationSetEnd.newBuilder().setEntitySet(refEntitySet).setRoleName(fk.getReferenceTableName()).setRole(EdmAssociationEnd.newBuilder().setType(refEntityType).setRole(refEntityType.getName()));
assosiationSet.setEnds(endOne, endTwo);
assosiationSet.setAssociation(association);
assosiationSets.add(assosiationSet);
}
}
entityContainer.addAssociationSets(assosiationSets);
odataSchema.addAssociations(assosiations);
}
use of org.odata4j.edm.EdmSchema.Builder in project teiid by teiid.
the class ODataEntitySchemaBuilder method buildFunctionImports.
public static void buildFunctionImports(Schema schema, List<Builder> edmSchemas, boolean preserveEntityTypeName) {
EdmSchema.Builder odataSchema = findSchema(edmSchemas, schema.getName());
EdmEntityContainer.Builder entityContainer = findEntityContainer(edmSchemas, schema.getName());
// procedures
for (Procedure proc : schema.getProcedures().values()) {
EdmFunctionImport.Builder edmProcedure = EdmFunctionImport.newBuilder();
edmProcedure.setName(proc.getName());
// $NON-NLS-1$
String httpMethod = "POST";
for (ProcedureParameter pp : proc.getParameters()) {
if (pp.getName().equals("return")) {
// $NON-NLS-1$
// $NON-NLS-1$
httpMethod = "GET";
edmProcedure.setReturnType(ODataTypeManager.odataType(pp.getRuntimeType()));
continue;
}
EdmFunctionParameter.Builder param = EdmFunctionParameter.newBuilder();
param.setName(pp.getName());
param.setType(ODataTypeManager.odataType(pp.getRuntimeType()));
if (pp.getType() == ProcedureParameter.Type.In) {
param.setMode(Mode.In);
} else if (pp.getType() == ProcedureParameter.Type.InOut) {
param.setMode(Mode.InOut);
} else if (pp.getType() == ProcedureParameter.Type.Out) {
param.setMode(Mode.Out);
}
param.setNullable(pp.getNullType() == NullType.Nullable);
edmProcedure.addParameters(param);
}
// add a complex type for return resultset.
ColumnSet<Procedure> returnColumns = proc.getResultSet();
if (returnColumns != null) {
// $NON-NLS-1$
httpMethod = "GET";
EdmComplexType.Builder complexType = EdmComplexType.newBuilder();
// $NON-NLS-1$
String entityTypeName = proc.getName() + "_" + returnColumns.getName();
if (preserveEntityTypeName && proc.getProperty(ODataMetadataProcessor.ENTITY_TYPE, false) != null) {
entityTypeName = proc.getProperty(ODataMetadataProcessor.ENTITY_TYPE, false);
}
complexType.setName(entityTypeName);
complexType.setNamespace(schema.getName());
for (Column c : returnColumns.getColumns()) {
EdmProperty.Builder property = EdmProperty.newBuilder(c.getName()).setType(ODataTypeManager.odataType(c.getRuntimeType())).setNullable(c.getNullType() == NullType.Nullable);
if (c.getRuntimeType().equals(DataTypeManager.DefaultDataTypes.STRING)) {
property.setFixedLength(c.isFixedLength()).setMaxLength(c.getLength()).setUnicode(true);
}
complexType.addProperties(property);
}
odataSchema.addComplexTypes(complexType);
edmProcedure.setIsCollection(true);
edmProcedure.setReturnType(EdmCollectionType.newBuilder().setCollectionType(complexType).setKind(CollectionKind.Collection));
}
edmProcedure.setHttpMethod(httpMethod);
entityContainer.addFunctionImports(edmProcedure);
}
}