private void createDynamicTypes()

in rdb/src/main/java/org/apache/tuscany/das/rdb/graphbuilder/impl/GraphBuilderMetadata.java [115:218]


    private void createDynamicTypes() {

        Type root = SDOUtil.createType(helperContext, getDefaultURI(), "DataGraphRoot", false);
		if (this.logger.isDebugEnabled()) {
			this.logger.debug("GBMD.createDynamicTypes():created Type for "+getDefaultURI());
		}

		Iterator iter = getResultMetadata().iterator();
        while (iter.hasNext()) {

            ResultMetadata resultMetadata = (ResultMetadata) iter.next();

			// Create a Type for each Table represented in the ResultSet
			Iterator names = resultMetadata.getAllTablePropertyNames().iterator();
			while (names.hasNext()) {
				String tableName = (String) names.next();
				if (root.getProperty(tableName) == null) {
					Type tableType = SDOUtil.createType(helperContext, getDefaultURI(), tableName, false);
					Property property = SDOUtil.createProperty(root, tableName, tableType);
					SDOUtil.setMany(property, true);
					SDOUtil.setContainment(property, true);
					if (this.logger.isDebugEnabled()) {
						this.logger.debug("GBMD.createDynamicTypes():CREATING NEW TABLE TYPE & PROPERTY :"+tableName);
					}                    
				}
			}

			// TODO tablePropertyMap is temporary until Tuscany-203 is fixed
			Map tablePropertyMap = new HashMap();

			for (int i = 1; i <= resultMetadata.getResultSetSize(); i++) {

				Property ref = root.getProperty(resultMetadata.getTablePropertyName(i));

				if (ref == null) {
					throw new RuntimeException("Could not find table " + resultMetadata.getTablePropertyName(i) 
							+ " in the SDO model");
				}

				// TODO Temporary code to check to see if a property has already been added.
				// Replace when Tuscany-203 is fixed
				List addedProperties = (List) tablePropertyMap.get(ref.getName());
				if (addedProperties == null) {
					addedProperties = new ArrayList();
					tablePropertyMap.put(ref.getName(), addedProperties);
				}



				String columnName = resultMetadata.getColumnPropertyName(i);

				// TODO temporary check until Tuscany-203 is fixed
				if (!addedProperties.contains(columnName)) {
					addedProperties.add(columnName);
					Type atype = resultMetadata.getDataType(i);

					SDOUtil.createProperty(ref.getType(), columnName, atype);

				}

			}
        }

        MappingWrapper wrapper = getConfigWrapper();
        Iterator i = getRelationships().iterator();
        while (i.hasNext()) {
            Relationship r = (Relationship) i.next();

            String parentName = wrapper.getTableTypeName(r.getPrimaryKeyTable());
            String childName = wrapper.getTableTypeName(r.getForeignKeyTable());

            if (parentName == null) {
                throw new RuntimeException("The parent table (" + r.getPrimaryKeyTable() 
                        + ") in relationship " + r.getName()
                        + " was not found in the mapping information.");
            } else if (childName == null) {
                throw new RuntimeException("The child table (" + r.getForeignKeyTable() 
                        + ") in relationship " + r.getName()
                        + " was not found in the mapping information.");
            }

            Property parentProperty = root.getProperty(parentName);
            Property childProperty = root.getProperty(childName);

            if (parentProperty == null) {
                throw new RuntimeException("The parent table (" + parentName + ") in relationship " 
                        + r.getName() + " was not found.");
            } else if (childProperty == null) {
                throw new RuntimeException("The child table (" + childName + ") in relationship " 
                        + r.getName() + " was not found.");
            }

            Type parent = parentProperty.getType();
            Type child = childProperty.getType();

            Property parentProp = SDOUtil.createProperty(parent, r.getName(), child);
            Property childProp = SDOUtil.createProperty(child, r.getName() + "_opposite", parent);
            SDOUtil.setOpposite(parentProp, childProp);
            SDOUtil.setOpposite(childProp, parentProp);
            SDOUtil.setMany(parentProp, r.isMany());
        }

        this.rootType = root;
    }