Now that the TypeHelper.define() function is implemented in SDO, we can remove the remaining EMF references from the DAS.
 
Brent
Index: 
src/main/java/org/apache/tuscany/das/rdb/graphbuilder/schema/EReferenceMaker.java
===================================================================
--- 
src/main/java/org/apache/tuscany/das/rdb/graphbuilder/schema/EReferenceMaker.java
   (revision 393599)
+++ 
src/main/java/org/apache/tuscany/das/rdb/graphbuilder/schema/EReferenceMaker.java
   (working copy)
@@ -1,131 +0,0 @@
-/**
-*
-*  Copyright 2005 The Apache Software Foundation or its licensors, as 
applicable.
-*
-*  Licensed under the Apache License, Version 2.0 (the "License");
-*  you may not use this file except in compliance with the License.
-*  You may obtain a copy of the License at
-*
-*     http://www.apache.org/licenses/LICENSE-2.0
-*
-*  Unless required by applicable law or agreed to in writing, software
-*  distributed under the License is distributed on an "AS IS" BASIS,
-*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-*  See the License for the specific language governing permissions and
-*  limitations under the License.
-*/
-package org.apache.tuscany.das.rdb.graphbuilder.schema;
-
-import org.apache.tuscany.das.rdb.config.Relationship;
-import org.apache.tuscany.sdo.SDOFactory;
-import org.apache.tuscany.sdo.impl.ClassImpl;
-import org.apache.tuscany.sdo.impl.ReferenceImpl;
-
-import commonj.sdo.Type;
-
-/**
- * A helper used to create EReferences
- */
-public class EReferenceMaker {
-       
-       public EReferenceMaker() {
-               // Empty Constructor
-       }
-       
-       /**
-        * Create an EReference with the specified name, type, lower bound, 
upper bound
-        * and containment.
-        * @param name
-        * @param type
-        * @param lowerBound
-        * @param upperBound
-        * @param containment
-        * @return EReference
-        */
-       public ReferenceImpl createReference(String name, Type type, int 
lowerBound, int upperBound, boolean containment, boolean changeable) {
-               ReferenceImpl ref = (ReferenceImpl) 
getFactory().createReference();             
-               ref.setName(name);
-               ref.setEType((ClassImpl)type);
-               ref.setLowerBound(lowerBound);
-               ref.setUpperBound(upperBound);
-               ref.setContainment(containment);
-               ref.setResolveProxies(false);
-               ref.setChangeable(changeable);
-               
-               return ref;
-       }
-       
-       public ReferenceImpl createReference(String name, Type type, int 
lowerBound, int upperBound, boolean containment) {
-               return createReference(name, type, lowerBound, upperBound, 
containment, true);
-       }
-       /**
-        * Create a one to many reference with the specified name, type, and 
containment
-        * @param name
-        * @param type
-        * @param containment
-        * @return
-        */
-       public ReferenceImpl createOneToManyReference(String name, Type type, 
boolean containment) {
-               return createReference(name, type, 0, -1, containment);
-       }
-       
-       /**
-        * Create a one to many reference with the specified name and type
-        * @param name
-        * @param type
-        * @return EReference
-        */
-       public ReferenceImpl createOneToManyReference(String name, Type type) {
-               return createOneToManyReference(name, type, false);     
-       }
-       
-       /**
-        * Create a one to one reference with the specified name and type
-        * @param name
-        * @param type
-        * @return EReference
-        */
-       public ReferenceImpl createOneToOneReference(String name, Type type) {
-               ReferenceImpl ref = createReference(name, type,0,1,false);      
          
-               return ref;
-       }
-       
-       /**
-        * Create a many to one reference with the specified name and type
-        * @param name
-        * @param type
-        * @return EReference
-        */
-       public ReferenceImpl createManyToOneReference(String name, Type type) {
-               return createReference(name,type,0,1,false);
-       }
-       
-       private SDOFactory getFactory() {
-               return SDOFactory.eINSTANCE;
-       }
-
-
-       /**
-        * @param r
-        * @param parent
-        * @param child
-        * @return
-        */
-       public ReferenceImpl createReference(Relationship r, Type parent, Type 
child) {
-               if ( !r.isMany() ) {
-                       ReferenceImpl ref = 
createOneToOneReference(r.getName(), child);
-                       ReferenceImpl opp = createOneToOneReference(r.getName() 
+ "_opposite", parent);
-                       ref.setEOpposite(opp);
-                       opp.setEOpposite(ref);
-                       return ref;
-               } else {
-                       ReferenceImpl ref = 
createOneToManyReference(r.getName(), child);
-                       ReferenceImpl opp = 
createManyToOneReference(r.getName() + "_opposite", parent);
-                       ref.setEOpposite(opp);
-                       opp.setEOpposite(ref);
-                       return ref;
-               }
-               
-       }
-
-}
Index: 
src/main/java/org/apache/tuscany/das/rdb/graphbuilder/schema/ESchemaMaker.java
===================================================================
--- 
src/main/java/org/apache/tuscany/das/rdb/graphbuilder/schema/ESchemaMaker.java  
    (revision 393597)
+++ 
src/main/java/org/apache/tuscany/das/rdb/graphbuilder/schema/ESchemaMaker.java  
    (working copy)
@@ -16,6 +16,8 @@
  */
 package org.apache.tuscany.das.rdb.graphbuilder.schema;
 
+import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.Iterator;
 
 import org.apache.tuscany.das.rdb.config.Relationship;
@@ -23,19 +25,10 @@
 import org.apache.tuscany.das.rdb.graphbuilder.impl.GraphBuilderMetadata;
 import org.apache.tuscany.das.rdb.graphbuilder.impl.ResultMetadata;
 import org.apache.tuscany.das.rdb.util.DebugUtil;
-import org.apache.tuscany.sdo.SDOFactory;
-import org.apache.tuscany.sdo.impl.AttributeImpl;
-import org.apache.tuscany.sdo.impl.ClassImpl;
-import org.apache.tuscany.sdo.impl.DynamicDataObjectImpl;
-import org.apache.tuscany.sdo.impl.ReferenceImpl;
 import org.apache.tuscany.sdo.util.DataObjectUtil;
 import org.apache.tuscany.sdo.util.SDOUtil;
-import org.eclipse.emf.ecore.EClass;
-import org.eclipse.emf.ecore.EObject;
-import org.eclipse.emf.ecore.EPackage;
-import org.eclipse.emf.ecore.EcoreFactory;
-import org.eclipse.emf.ecore.impl.EFactoryImpl;
 
+import commonj.sdo.Property;
 import commonj.sdo.Type;
 import commonj.sdo.helper.TypeHelper;
 
@@ -49,51 +42,18 @@
 
        private final GraphBuilderMetadata metadata;
 
-       private EPackage dataGraphPackage;
-
-       private final String nsPrefix;
-
-       private final String pkgName;
-
        private boolean debug = false;
 
        /**
-        * Constructor for ESchemaMaker. Creates an EMF Schema based on the 
metadata
+        * Constructor for ESchemaMaker. Creates an SDO model based on the 
metadata
         * passed in.
         * 
         * @param metadata
         */
        public ESchemaMaker(GraphBuilderMetadata metadata) {
-               this(metadata, null, null);
-       }
-
-       /**
-        * Constructor for ESshemaMaker. Creates an EMF Schema based on the 
supplied
-        * metadata, namespace prefix, and package name
-        * 
-        * @param metadata
-        *            the metadata
-        * @param nsPrefix
-        *            the namespace prefix - this affects the generated
-        *            Factory/Package name - defaults to datagraph
-        * @param pkgName
-        *            the package name - determines package name for generated 
code -
-        *            defaults to datagraph
-        */
-       public ESchemaMaker(GraphBuilderMetadata metadata, String nsPrefix,
-                       String pkgName) {
-               if (nsPrefix == null)
-                       nsPrefix = "datagraph";
-               if (pkgName == null)
-                       pkgName = "datagraph";
-
                this.metadata = metadata;
-               this.nsPrefix = nsPrefix;
-               this.pkgName = pkgName;
+       }       
 
-               TypeHelper helper = TypeHelper.INSTANCE;
-       }
-
        /**
         * Creates an EMF Schema by using the
         * 
@@ -102,19 +62,15 @@
         * @link Metadata elements into EMF Schema elements.
         */
 
-       public Type createESchema() {
-               TypeHelper.INSTANCE.getType("commonj.sdo", "Integer");
+       public Type createTypes() {
+               TypeHelper types = SDOUtil.createTypeHelper();  
+               
                DataObjectUtil.initRuntime();
                SDOUtil.createDataGraph();
 
-               ClassImpl rootClass = (ClassImpl) 
SDOFactory.eINSTANCE.createClass();
-               Type rootType = (Type) rootClass;
+       
+               Type rootType = SDOUtil.createType(types, getURI(), 
"DataGraphRoot", false);    
 
-               rootClass.setName("DataGraphRoot");
-               getEPackage().getEClassifiers().add(rootClass);
-
-               EReferenceMaker refMaker = new EReferenceMaker();
-
                Iterator iter = metadata.getResultMetadata().iterator();
                while (iter.hasNext()) {
 
@@ -124,40 +80,47 @@
                                        .iterator();
                        while (names.hasNext()) {
                                String tableName = (String) names.next();
-                               if (rootClass.getEStructuralFeature(tableName) 
== null) {
-                                       Type clazz = createType(tableName);
-                                       
getEPackage().getEClassifiers().add(clazz);
-                                       ReferenceImpl ref = 
refMaker.createOneToManyReference(
-                                                       tableName, clazz, true);
-                                       
rootClass.getEStructuralFeatures().add(ref);
-                               }
+       
+                               Type tableType = SDOUtil.createType(types, 
getURI(), tableName, false);                                                    
                     
+                               Property property = 
SDOUtil.createProperty(rootType, tableName, tableType);
+                               SDOUtil.setMany(property,true);         
+                               SDOUtil.setContainment(property, true);
                        }
-
+               
+                       // TODO tablePropertyMap is temporary until Tuscany-203 
is fixed
+                       HashMap tablePropertyMap = new HashMap();
+                       
                        for (int i = 1; i <= 
resultMetadata.getColumnNames().size(); i++) {
 
-                               ReferenceImpl ref = (ReferenceImpl) rootType
-                                               
.getProperty(resultMetadata.getTablePropertyName(i));
-
+                               Property ref = 
rootType.getProperty(resultMetadata.getTablePropertyName(i));
+                               
+                               // TODO Temporary code to check to see if a 
property has already been added.
+                               // Replace when Tuscany-203 is fixed
+                               ArrayList addedProperties = (ArrayList) 
tablePropertyMap.get(ref.getName());
+                               if ( addedProperties == null ) {
+                                       addedProperties = new ArrayList();
+                                       tablePropertyMap.put(ref.getName(), 
addedProperties);
+                               }
+                               
                                if (ref == null)
                                        throw new RuntimeException("Could not 
find table "
                                                        + 
resultMetadata.getTablePropertyName(i)
                                                        + " in the SDO model");
-                               EClass clazz = ref.getEReferenceType();
+                       
                                String columnName = 
resultMetadata.getColumnPropertyName(i);
 
-                               if (clazz.getEStructuralFeature(columnName) == 
null) {
-                                       Type atype = (Type) 
resultMetadata.getDataType(i);
+                               // TODO temporary check until Tuscany-203 is 
fixed
+                               if ( !addedProperties.contains(columnName)) {
+                                       addedProperties.add(columnName);
+                                       Type atype = (Type) 
resultMetadata.getDataType(i);                                      
+       
+                                       SDOUtil.createProperty(ref.getType(), 
columnName, atype);                               
 
-                                       // EDataType type = (EDataType) 
atype.getEClassifier();
-
-                                       AttributeImpl attr = 
getAttributeMaker().createEAttribute(
-                                                       columnName, atype);
-
                                        DebugUtil.debugln(getClass(), debug, 
"Adding column "
                                                        + columnName + " to "
-                                                       + 
resultMetadata.getTablePropertyName(i));
-                                       
clazz.getEStructuralFeatures().add(attr);
+                                                       + 
resultMetadata.getTablePropertyName(i));      
                                }
+
                        }
                }
 
@@ -167,10 +130,10 @@
                        while (i.hasNext()) {
                                Relationship r = (Relationship) i.next();
 
-                               EClass parent = (EClass) 
getEPackage().getEClassifier(
-                                               
wrapper.getTablePropertyName(r.getPrimaryKeyTable()));
-                               EClass child = (EClass) 
getEPackage().getEClassifier(
-                                               
wrapper.getTablePropertyName(r.getForeignKeyTable()));
+                               Type parent = rootType.getProperty(
+                                               
wrapper.getTablePropertyName(r.getPrimaryKeyTable())).getType();
+                               Type child = rootType.getProperty(
+                                               
wrapper.getTablePropertyName(r.getForeignKeyTable())).getType();
                                if (parent == null) {
                                        throw new RuntimeException("The parent 
table ("
                                                        + 
r.getPrimaryKeyTable() + ") in relationship "
@@ -183,79 +146,26 @@
                                                        + " was not found in 
the mapping information.");
                                }
 
-                               ReferenceImpl ref = refMaker.createReference(r, 
(Type)parent, (Type)child);
+                       //      ReferenceImpl ref = refMaker.createReference(r, 
parent, child); 
+                               
+                               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());
+                               
+                                                               
 
-                               DebugUtil.debugln(getClass(), debug, "Adding 
reference: "
-                                               + ref.getName() + " to " + 
parent.getName());
-                               if (parent.getEStructuralFeature(ref.getName()) 
== null)
-                                       
parent.getEStructuralFeatures().add(ref);
-
-                               if 
(child.getEStructuralFeature(ref.getEOpposite().getName()) == null)
-                                       
child.getEStructuralFeatures().add(ref.getEOpposite());
-
                        }
 
                }
-
-               // EcoreUtil.freeze(rootObject.getEPackage());
-
-               return (Type) rootClass;
-       }
-
-       /**
-        * Create an EClass with the specified name
-        * 
-        * @param name
-        * @return EClass
-        */
-       protected Type createType(String name) {
-               ClassImpl ecl = (ClassImpl) SDOFactory.eINSTANCE.createClass();
-               ecl.setName(name);
-
-               return ecl;
-       }
-
-       /**
-        * Get an EAttributeMaker singleton
-        * 
-        * @return EAttributeMaker
-        */
-       private EAttributeMaker getAttributeMaker() {
-               return EAttributeMaker.singleton();
-       }
-
-
-       /**
-        * @return the EPackage for this schema
-        */
-       public EPackage getEPackage() {
-               if (this.dataGraphPackage == null)
-                       this.dataGraphPackage = createEPackage();
-               return this.dataGraphPackage;
-       }
-
-       /**
-        * Create the EPackage for this schema Uses the packageName and nsPrefix
-        * values set in the constructors The EPackage overrides the default
-        * EFactory so that DataObjects will be created using MapDataObjectImpl.
-        * 
-        * @return the new EPackage
-        */
-       protected EPackage createEPackage() {
                
-               EPackage pkg = EcoreFactory.eINSTANCE.createEPackage();
-               pkg.setName(pkgName);
-               pkg.setNsPrefix(nsPrefix);
-               pkg.setNsURI("datagraph.ecore");
-
-               pkg.setEFactoryInstance(new EFactoryImpl() {
-                       public EObject basicCreate(EClass cls) {
-                               EObject result = new DynamicDataObjectImpl(cls);
-                               return result;
-                       }
-               });
-
-               return pkg;
+               return rootType;
        }
 
+       private String getURI() {
+               return "http:///org.apache.tuscany.das.rdb/das";;
+       }       
+       
+
 }
Index: 
src/main/java/org/apache/tuscany/das/rdb/graphbuilder/schema/EAttributeMaker.java
===================================================================
--- 
src/main/java/org/apache/tuscany/das/rdb/graphbuilder/schema/EAttributeMaker.java
   (revision 393599)
+++ 
src/main/java/org/apache/tuscany/das/rdb/graphbuilder/schema/EAttributeMaker.java
   (working copy)
@@ -1,67 +0,0 @@
-/**
- *
- *  Copyright 2005 The Apache Software Foundation or its licensors, as 
applicable.
- *
- *  Licensed under the Apache License, Version 2.0 (the "License");
- *  you may not use this file except in compliance with the License.
- *  You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- *  Unless required by applicable law or agreed to in writing, software
- *  distributed under the License is distributed on an "AS IS" BASIS,
- *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *  See the License for the specific language governing permissions and
- *  limitations under the License.
- */
-package org.apache.tuscany.das.rdb.graphbuilder.schema;
-
-import org.apache.tuscany.sdo.SDOFactory;
-import org.apache.tuscany.sdo.impl.AttributeImpl;
-import org.apache.tuscany.sdo.impl.DataTypeImpl;
-
-import commonj.sdo.Type;
-
-
-/**
- *
- * A helper used to create new EAttributes
- */
-public class EAttributeMaker {
-
-       private static EAttributeMaker maker;
-
-       protected EAttributeMaker() {
-               // Empty Constructor
-       }
-
-       /**
-        * Return an EAttributeMaker singleton
-        * @return EAttributeMaker
-        */
-       public static EAttributeMaker singleton() {
-               if (maker == null)
-                       maker = new EAttributeMaker();
-               return maker;
-       }
-
-       /**
-        * Create a new EAttribute with the specified name and type
-        * @param name The name of the new EAttribute
-        * @param type The type of the new EAttribute
-        * @return EAttribute
-        */
-       public AttributeImpl createEAttribute(String name, Type type) {
-               
-               AttributeImpl attr = (AttributeImpl) 
getFactory().createAttribute();
-               attr.setName(name);
-               attr.setEType((DataTypeImpl)type);
-               attr.setUnique(false);
-                       
-               return attr;
-       }
-
-       private SDOFactory getFactory() {
-               return SDOFactory.eINSTANCE;
-       }
-}
Index: 
src/main/java/org/apache/tuscany/das/rdb/graphbuilder/impl/GraphBuilderMetadata.java
===================================================================
--- 
src/main/java/org/apache/tuscany/das/rdb/graphbuilder/impl/GraphBuilderMetadata.java
        (revision 393597)
+++ 
src/main/java/org/apache/tuscany/das/rdb/graphbuilder/impl/GraphBuilderMetadata.java
        (working copy)
@@ -86,7 +86,7 @@
        public Type getSchema() {
                if ( this.schema == null ) {
                        ESchemaMaker schemaMaker = new ESchemaMaker(this);
-                       return schemaMaker.createESchema();
+                       return schemaMaker.createTypes();
                } else {
                        return this.schema;
                }

Reply via email to