Author: amita
Date: Thu Nov 29 03:50:28 2007
New Revision: 599394

URL: http://svn.apache.org/viewvc?rev=599394&view=rev
Log:
TUSCANY-1923 first set of changes - tool and test cases

Added:
    incubator/tuscany/java/das/tools/
    incubator/tuscany/java/das/tools/pom.xml
    incubator/tuscany/java/das/tools/readme.htm
    incubator/tuscany/java/das/tools/src/
    incubator/tuscany/java/das/tools/src/main/
    incubator/tuscany/java/das/tools/src/main/java/
    incubator/tuscany/java/das/tools/src/main/java/org/
    incubator/tuscany/java/das/tools/src/main/java/org/apache/
    incubator/tuscany/java/das/tools/src/main/java/org/apache/tuscany/
    incubator/tuscany/java/das/tools/src/main/java/org/apache/tuscany/das/
    incubator/tuscany/java/das/tools/src/main/java/org/apache/tuscany/das/rdb/
    
incubator/tuscany/java/das/tools/src/main/java/org/apache/tuscany/das/rdb/util/
    
incubator/tuscany/java/das/tools/src/main/java/org/apache/tuscany/das/rdb/util/Column.java
    
incubator/tuscany/java/das/tools/src/main/java/org/apache/tuscany/das/rdb/util/DBSchema.java
    
incubator/tuscany/java/das/tools/src/main/java/org/apache/tuscany/das/rdb/util/DBToSchemaFile.java
    
incubator/tuscany/java/das/tools/src/main/java/org/apache/tuscany/das/rdb/util/DBToXSDGenerator.java
    
incubator/tuscany/java/das/tools/src/main/java/org/apache/tuscany/das/rdb/util/ForeignKey.java
    
incubator/tuscany/java/das/tools/src/main/java/org/apache/tuscany/das/rdb/util/ForeignKeyRef.java
    
incubator/tuscany/java/das/tools/src/main/java/org/apache/tuscany/das/rdb/util/SQLTypeChecker.java
    
incubator/tuscany/java/das/tools/src/main/java/org/apache/tuscany/das/rdb/util/SchemaFileToXSD.java
    
incubator/tuscany/java/das/tools/src/main/java/org/apache/tuscany/das/rdb/util/Table.java
    incubator/tuscany/java/das/tools/src/main/resources/
    incubator/tuscany/java/das/tools/src/test/
    incubator/tuscany/java/das/tools/src/test/java/
    incubator/tuscany/java/das/tools/src/test/java/org/
    incubator/tuscany/java/das/tools/src/test/java/org/apache/
    incubator/tuscany/java/das/tools/src/test/java/org/apache/tuscany/
    incubator/tuscany/java/das/tools/src/test/java/org/apache/tuscany/das/
    incubator/tuscany/java/das/tools/src/test/java/org/apache/tuscany/das/rdb/
    
incubator/tuscany/java/das/tools/src/test/java/org/apache/tuscany/das/rdb/test/
    
incubator/tuscany/java/das/tools/src/test/java/org/apache/tuscany/das/rdb/test/DBToXSDTests.java
    incubator/tuscany/java/das/tools/src/test/resources/
    incubator/tuscany/java/das/tools/src/test/resources/DBConnectionConfig.xml
    incubator/tuscany/java/das/tools/src/test/resources/dbConfig.xml
Modified:
    incubator/tuscany/java/das/pom.xml

Modified: incubator/tuscany/java/das/pom.xml
URL: 
http://svn.apache.org/viewvc/incubator/tuscany/java/das/pom.xml?rev=599394&r1=599393&r2=599394&view=diff
==============================================================================
--- incubator/tuscany/java/das/pom.xml (original)
+++ incubator/tuscany/java/das/pom.xml Thu Nov 29 03:50:28 2007
@@ -140,6 +140,7 @@
             <modules>
                 <module>rdb</module>
                 <module>samples</module>
+                <module>tools</module>
             </modules>
         </profile>
         
@@ -148,6 +149,7 @@
             <modules>
                 <module>rdb</module>
                 <module>samples</module>
+                <module>tools</module>
                 <module>distribution/binary</module>
                 <module>distribution/source</module>
             </modules>

Added: incubator/tuscany/java/das/tools/pom.xml
URL: 
http://svn.apache.org/viewvc/incubator/tuscany/java/das/tools/pom.xml?rev=599394&view=auto
==============================================================================
--- incubator/tuscany/java/das/tools/pom.xml (added)
+++ incubator/tuscany/java/das/tools/pom.xml Thu Nov 29 03:50:28 2007
@@ -0,0 +1,147 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+-->
+<project>
+    <modelVersion>4.0.0</modelVersion>
+    <parent>
+        <groupId>org.apache.tuscany.das</groupId>
+        <artifactId>tuscany-das</artifactId>
+        <version>1.0-incubating-SNAPSHOT</version>
+    </parent>
+
+    <artifactId>tuscany-das-tools</artifactId>
+    <name>Tuscany DAS Tools for Relational Databases</name>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.apache.tuscany.das</groupId>
+            <artifactId>tuscany-das-rdb</artifactId>
+            <version>${das.version}</version>
+            <scope>compile</scope>
+        </dependency>
+    
+        <dependency>
+            <groupId>org.apache.tuscany.das.samples</groupId>
+            <artifactId>tuscany-das-sample-dbconfig</artifactId>
+            <version>${das.version}</version>
+            <scope>compile</scope>
+            <exclusions>
+                <exclusion>
+                    <groupId>org.apache.derby</groupId>
+                   <artifactId>derby</artifactId>
+                </exclusion>
+            </exclusions>
+        </dependency>
+    
+        <dependency>
+            <groupId>org.apache.tuscany.sdo</groupId>
+            <artifactId>tuscany-sdo-api-r2.1</artifactId>
+            <version>${sdo.version}</version>
+        </dependency>
+
+        <dependency>
+            <groupId>org.apache.tuscany.sdo</groupId>
+            <artifactId>tuscany-sdo-impl</artifactId>
+            <version>${sdo.version}</version>
+        </dependency>
+
+        <dependency>
+            <groupId>org.apache.tuscany.sdo</groupId>
+            <artifactId>tuscany-sdo-lib</artifactId>
+            <version>${sdo.version}</version>
+        </dependency>
+        
+        <dependency>
+            <groupId>stax</groupId>
+            <artifactId>stax-api</artifactId>
+            <version>1.0.1</version>
+        </dependency>
+
+        <dependency>
+            <groupId>org.apache.derby</groupId>
+            <artifactId>derby</artifactId>
+            <version>10.2.2.0</version>
+            <scope>test</scope>
+        </dependency>
+
+        <dependency>
+            <groupId>log4j</groupId>
+            <artifactId>log4j</artifactId>
+            <version>1.2.14</version>
+            <scope>compile</scope>
+        </dependency>
+
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+            <version>3.8.1</version>
+            <scope>test</scope>
+        </dependency>
+        
+        <dependency>
+           <groupId>torque</groupId>
+           <artifactId>torque-gen</artifactId>
+           <version>3.3-RC2</version>
+       </dependency>
+    </dependencies>
+
+    <!-- this will place the java source files inside the jar -->
+    <build>
+        <finalName>tuscany-das-tools</finalName>
+        <sourceDirectory>src/main/java</sourceDirectory>
+        <resources>
+            <resource>
+                <directory>src/main/java</directory>
+            </resource>
+            <resource>
+              <directory>src/main/resources</directory>
+            </resource>
+        </resources>
+          <plugins>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-jar-plugin</artifactId>
+                <configuration>
+                    <archive>
+                        <manifest> 
+                           <addClasspath>true</addClasspath> 
+                           <!-- classpathPrefix>../../lib/</classpathPrefix 
--> 
+                        </manifest> 
+
+                    </archive>
+                </configuration>
+            </plugin>
+                   <plugin>
+                       <groupId>org.apache.maven.plugins</groupId>
+                       <artifactId>maven-surefire-plugin</artifactId>
+                       <configuration>
+                           <includes>
+                               <include>**/*Tests.java</include>
+                           </includes>
+                       </configuration>
+                   </plugin>            
+        </plugins>
+    </build>
+</project>
+
+
+
+
+
+

Added: incubator/tuscany/java/das/tools/readme.htm
URL: 
http://svn.apache.org/viewvc/incubator/tuscany/java/das/tools/readme.htm?rev=599394&view=auto
==============================================================================
--- incubator/tuscany/java/das/tools/readme.htm (added)
+++ incubator/tuscany/java/das/tools/readme.htm Thu Nov 29 03:50:28 2007
@@ -0,0 +1,75 @@
+                                                               <!DOCTYPE HTML 
PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
+<html>
+<head>
+  <!--
+    Licensed to the Apache Software Foundation (ASF) under one
+    or more contributor license agreements.  See the NOTICE file
+    distributed with this work for additional information
+    regarding copyright ownership.  The ASF licenses this file
+    to you 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.    
+  -->
+
+<meta content="text/html; charset=iso-8859-1" http-equiv="Content-Type">
+<meta content="text/css" http-equiv="Content-Style-Type"><title>Tuscany DAS 
Tools</title>
+
+<style type="text/css" media="all">
[EMAIL PROTECTED] url("../../css/maven-base.css");
[EMAIL PROTECTED] url("../../css/maven-theme.css");
[EMAIL PROTECTED] url("../../css/site.css");
+</style>
+<link rel="stylesheet" href="./css/print.css" type="text/css" 
media="print"></head>
+
+<body>
+<h2>Tuscany DAS Tools</h2>
+<h3>Building DAS Tools</h3>
+
+DBToXSDGenerator provides utility methods for use when DB Schema is available 
and SDO Model xsds are not. This <br>
+utility will help build SDO Model xsds based on DB Schema. These xsds can be 
supplied to Data Access Service <br>
+like (RDB-DAS) to have SDO <-> DB data exchange. <br>
+
+ <br>
+ <ul>
+  <li>getModelFileFromDB(dbInfoFile); - connect to database and convert DB 
Schema to XSD Model XSDs </li>
+  <li>getModelFileFromSchemaFile(schemaFileName, SDOModelFileName); - no DB 
Connection needed, but need Apache Torque output of DB schema file </li>
+  <li>getSchemaFileFromDB(dbInfoFile); - connect to database and use Apache 
Torque to get DB Schema xml file </li>
+</ul>
+  
+To build the Tuscany DAS DBToXSDGenerator tool, perform the following steps 
<br>
+<ul>
+<li> Ensure that the environment variable JAVA_HOME is set to point to an 
installation of Java 1.4 JDK      </li>
+<li> --->To run as a maven plugin, run the command "mvn" </li>
+<li> --->To run as a standalone tool, call the above listed methods from 
DBToXSDGenerator </li>
+</ul>
+
+<b>Assumption</b> Database exists and contains required tables, 
constraints.<br>
+
+<ul  style="border-right-style: solid; border-left-style: solid; 
border-top-style: solid; border-bottom-style: solid">
+ <b>Example dbInfoFile</b><br>
+ &lt;Config xmlns="http:///org.apache.tuscany.das.rdb/config.xsd"&gt; <br>
+<br>
+    &lt;!--Uncomment below for derby test--&gt;<br>
+       &lt;ConnectionInfo&gt;<br>
+               &nbsp;&nbsp;&lt;ConnectionProperties<br>
+                       
&nbsp;&nbsp;&nbsp;&nbsp;driverClass="org.apache.derby.jdbc.EmbeddedDriver"<br>
+                       
&nbsp;&nbsp;&nbsp;&nbsp;databaseURL="jdbc:derby:target/dastest"<br>
+                       &nbsp;&nbsp;&nbsp;&nbsp;schemaName = "APP"<br>
+                       &nbsp;&nbsp;&nbsp;&nbsp;loginTimeout="600000"/&gt;<br>
+       &lt;/ConnectionInfo&gt;<br><br>
+       &lt;OutFiles<br>
+               &nbsp;&nbsp;schemaFile="target/dbSchema.txt"<br>
+               &nbsp;&nbsp;modelFile="target/schemaModel.xsd"<br>
+       /&gt;<br>
+&lt;/Config&gt;
+</ul>
+</body></html>

Added: 
incubator/tuscany/java/das/tools/src/main/java/org/apache/tuscany/das/rdb/util/Column.java
URL: 
http://svn.apache.org/viewvc/incubator/tuscany/java/das/tools/src/main/java/org/apache/tuscany/das/rdb/util/Column.java?rev=599394&view=auto
==============================================================================
--- 
incubator/tuscany/java/das/tools/src/main/java/org/apache/tuscany/das/rdb/util/Column.java
 (added)
+++ 
incubator/tuscany/java/das/tools/src/main/java/org/apache/tuscany/das/rdb/util/Column.java
 Thu Nov 29 03:50:28 2007
@@ -0,0 +1,57 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.util;
+
+public class Column {
+       private String name;
+       private String type;
+       private boolean isPK;
+       private boolean isRequired;
+       
+       protected String getName() {
+               return name;
+       }
+       protected void setName(String name) {
+               this.name = name;
+       }
+       protected String getType() {
+               return type;
+       }
+       protected void setType(String type) {
+               this.type = type;
+       }
+       protected boolean isPK() {
+               return isPK;
+       }
+       protected void setPK(boolean isPK) {
+               this.isPK = isPK;
+       }
+       protected boolean isRequired() {
+               return isRequired;
+       }
+       protected void setRequired(boolean isRequired) {
+               this.isRequired = isRequired;
+       }
+       
+       public String toString() {
+               StringBuffer dbSchemaStr = new StringBuffer();
+               dbSchemaStr.append("Name:"+this.getName()+", 
Type:"+this.getType()+", isPK:"+this.isPK()+", 
isRequired:"+this.isRequired()+"\n");
+               return dbSchemaStr.toString();
+       }       
+}

Added: 
incubator/tuscany/java/das/tools/src/main/java/org/apache/tuscany/das/rdb/util/DBSchema.java
URL: 
http://svn.apache.org/viewvc/incubator/tuscany/java/das/tools/src/main/java/org/apache/tuscany/das/rdb/util/DBSchema.java?rev=599394&view=auto
==============================================================================
--- 
incubator/tuscany/java/das/tools/src/main/java/org/apache/tuscany/das/rdb/util/DBSchema.java
 (added)
+++ 
incubator/tuscany/java/das/tools/src/main/java/org/apache/tuscany/das/rdb/util/DBSchema.java
 Thu Nov 29 03:50:28 2007
@@ -0,0 +1,45 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.util;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class DBSchema {
+       private List tables = new ArrayList();
+
+       protected List getTables() {
+               return tables;
+       }
+
+       protected void setTables(List tables) {
+               this.tables = tables;
+       }
+       
+       public String toString() {
+               StringBuffer dbSchemaStr = new StringBuffer();
+               dbSchemaStr.append("_____DB SChema_______\n");
+               for(int i=0; i<this.tables.size(); i++) {                       
+                       Table curTable = (Table)this.tables.get(i);
+                       dbSchemaStr.append(curTable);
+               }
+               
+               return dbSchemaStr.toString();
+       }
+}

Added: 
incubator/tuscany/java/das/tools/src/main/java/org/apache/tuscany/das/rdb/util/DBToSchemaFile.java
URL: 
http://svn.apache.org/viewvc/incubator/tuscany/java/das/tools/src/main/java/org/apache/tuscany/das/rdb/util/DBToSchemaFile.java?rev=599394&view=auto
==============================================================================
--- 
incubator/tuscany/java/das/tools/src/main/java/org/apache/tuscany/das/rdb/util/DBToSchemaFile.java
 (added)
+++ 
incubator/tuscany/java/das/tools/src/main/java/org/apache/tuscany/das/rdb/util/DBToSchemaFile.java
 Thu Nov 29 03:50:28 2007
@@ -0,0 +1,133 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.util;
+
+import java.io.File;
+import java.io.InputStreamReader;
+
+import javax.xml.stream.XMLInputFactory;
+import javax.xml.stream.XMLStreamReader;
+
+import org.apache.log4j.Logger;
+import org.apache.torque.task.TorqueJDBCTransformTask;
+import org.apache.tools.ant.Project;
+
+public class DBToSchemaFile {
+       private static final Logger logger = 
Logger.getLogger(DBToSchemaFile.class);
+       
+       private static String driverClass = null;
+       private static String url = null;
+       private static String schemaName = null;
+       private static String userName = null;
+       private static String password = null;
+       private static String schemaFileName = null;//should not be null
+       private static String modelFileName = null;//if null STDOUT
+       
+       protected static void schemaFileFromDB() throws Exception {
+               Project p = new Project();
+           p.setBaseDir(new File("."));
+               TorqueJDBCTransformTask tsk = new TorqueJDBCTransformTask();
+               tsk.setProject(p);
+               tsk.setDbDriver(driverClass);
+               tsk.setDbUrl(url);
+               tsk.setSameJavaName(true);
+               tsk.setDbSchema(schemaName);
+               tsk.setTaskName("jdbc");
+               tsk.setDbUser(userName);
+               tsk.setDbPassword(password);
+               if(!schemaFileName.equals("")) {
+                       File schemaFile = new File( schemaFileName);
+                       schemaFile.createNewFile();
+               }
+               tsk.setOutputFile(schemaFileName);
+               tsk.execute();
+       }
+       
+       protected static void read(String dbInfoFileName) throws Exception {
+               XMLInputFactory xmlFactory = XMLInputFactory.newInstance();
+               
+               XMLStreamReader reader = xmlFactory.createXMLStreamReader(new 
InputStreamReader(DBToXSDGenerator.getStream(dbInfoFileName)));
+
+        while (true) {
+            int event = reader.next();
+            if(javax.xml.stream.XMLStreamConstants.END_DOCUMENT == event) {
+               break;
+            }
+            
+            switch (event) {            
+                   case javax.xml.stream.XMLStreamConstants.START_ELEMENT: {
+                      if 
(reader.getName().getLocalPart().equals("ConnectionProperties")) {
+                               driverClass = reader.getAttributeValue(null, 
"driverClass");
+                               url = reader.getAttributeValue(null, 
"databaseURL");
+                               schemaName = reader.getAttributeValue(null, 
"schemaName");
+                               userName = reader.getAttributeValue(null, 
"userName");
+                               if(userName == null)
+                                       userName = "";
+                               password = reader.getAttributeValue(null, 
"password");
+                               if(password == null)
+                                       password = "";
+                       } else if 
(reader.getName().getLocalPart().equals("ConnectionInfo")) {                  
+                               //ignore
+                       } else if 
(reader.getName().getLocalPart().equals("Config")) {
+                               //ignore
+                       } else if 
(reader.getName().getLocalPart().equals("OutFiles")) {                        
+                               schemaFileName = reader.getAttributeValue(null, 
"schemaFile");
+                               modelFileName = reader.getAttributeValue(null, 
"modelFile");
+                       } else {
+                               throw new RuntimeException("not got dbInfo  - 
tableNames List or connectionInfo:"+reader.getName()+":");
+                       }
+                       break;
+                   }
+            }            
+        }
+        
+        if (logger.isDebugEnabled()) {
+                       logger.debug("driverClass:"+driverClass);
+                       logger.debug("url:"+url);
+                       logger.debug("schemaName:"+schemaName);
+                       logger.debug("schemaFileName:"+schemaFileName);
+                       logger.debug("modelFileName:"+modelFileName);
+                       logger.debug("userName:"+userName);
+                       logger.debug("password:"+password);
+               }            
+
+        if(driverClass == null || url == null || schemaName == null || 
schemaFileName == null || modelFileName == null) {
+          throw new RuntimeException("Required inputs missing - check 
driverClass, url, schemaName, schemaFile, modelFile!");
+        }
+
+        return;
+    }
+
+       protected static String getSchemaFileName() {
+               return schemaFileName;
+       }
+
+       protected static void setSchemaFileName(String schemaFileName) {
+               DBToSchemaFile.schemaFileName = schemaFileName;
+       }
+
+       protected static String getModelFileName() {
+               return modelFileName;
+       }
+
+       protected static void setModelFileName(String modelFileName) {
+               DBToSchemaFile.modelFileName = modelFileName;
+       }
+       
+}

Added: 
incubator/tuscany/java/das/tools/src/main/java/org/apache/tuscany/das/rdb/util/DBToXSDGenerator.java
URL: 
http://svn.apache.org/viewvc/incubator/tuscany/java/das/tools/src/main/java/org/apache/tuscany/das/rdb/util/DBToXSDGenerator.java?rev=599394&view=auto
==============================================================================
--- 
incubator/tuscany/java/das/tools/src/main/java/org/apache/tuscany/das/rdb/util/DBToXSDGenerator.java
 (added)
+++ 
incubator/tuscany/java/das/tools/src/main/java/org/apache/tuscany/das/rdb/util/DBToXSDGenerator.java
 Thu Nov 29 03:50:28 2007
@@ -0,0 +1,58 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.util;
+
+import java.io.InputStream;
+
+public class DBToXSDGenerator {
+       /**
+        * Output is dbSchemaFile as specified in dbInfoFileName.schemaFile
+        * @param dbInfoFileName  name of file which contains db connection 
info and output files names for dbschema and model 
+        * @throws Exception
+        */     
+       public static void getSchemaFileFromDB(String dbInfoFileName) throws 
Exception {
+               DBToSchemaFile.read(dbInfoFileName);
+               DBToSchemaFile.schemaFileFromDB();
+       }
+       
+       /**
+        * If user supplies schemaFileName and modelFileName will use it 
instead of the one from DBToSchemaFile.
+        * schemaFile should exist. If modelFileName is null, will use STDOUT.
+        * @param schemaFileName then one with Torque output
+        * @throws Exception
+        */
+       public static void getModelFileFromSchemaFile(String schemaFileName, 
String modelFileName) throws Exception {
+               SchemaFileToXSD.convert(schemaFileName, modelFileName);
+       }
+       
+       /**
+        * All in one
+        * @param dbInfoFileName
+        * @throws Exception
+        */
+       public static void getModelFileFromDB(String dbInfoFileName) throws 
Exception {
+               DBToSchemaFile.read(dbInfoFileName);
+               DBToSchemaFile.schemaFileFromDB();
+               getModelFileFromSchemaFile(DBToSchemaFile.getSchemaFileName(), 
DBToSchemaFile.getModelFileName());
+       }
+       
+    protected static InputStream getStream(String fileName) {
+        return 
Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);
+    }    
+}

Added: 
incubator/tuscany/java/das/tools/src/main/java/org/apache/tuscany/das/rdb/util/ForeignKey.java
URL: 
http://svn.apache.org/viewvc/incubator/tuscany/java/das/tools/src/main/java/org/apache/tuscany/das/rdb/util/ForeignKey.java?rev=599394&view=auto
==============================================================================
--- 
incubator/tuscany/java/das/tools/src/main/java/org/apache/tuscany/das/rdb/util/ForeignKey.java
 (added)
+++ 
incubator/tuscany/java/das/tools/src/main/java/org/apache/tuscany/das/rdb/util/ForeignKey.java
 Thu Nov 29 03:50:28 2007
@@ -0,0 +1,51 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.util;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class ForeignKey {
+       private String foreignTableName;
+       private List fkRefs = new ArrayList();
+       
+       protected String getForeignTableName() {
+               return foreignTableName;
+       }
+       protected void setForeignTableName(String foreignTableName) {
+               this.foreignTableName = foreignTableName;
+       }
+       protected List getFkRefs() {
+               return fkRefs;
+       }
+       protected void setFkRefs(List fkRefs) {
+               this.fkRefs = fkRefs;
+       }
+       
+       public String toString() {
+               StringBuffer dbSchemaStr = new StringBuffer();
+               dbSchemaStr.append("_____Foreign 
Key_______"+this.foreignTableName+"\n");
+               for(int i=0; i<fkRefs.size(); i++) {
+                       ForeignKeyRef curFkRef = (ForeignKeyRef)fkRefs.get(i); 
+                       dbSchemaStr.append(curFkRef);
+               }
+
+               return dbSchemaStr.toString();
+       }       
+}

Added: 
incubator/tuscany/java/das/tools/src/main/java/org/apache/tuscany/das/rdb/util/ForeignKeyRef.java
URL: 
http://svn.apache.org/viewvc/incubator/tuscany/java/das/tools/src/main/java/org/apache/tuscany/das/rdb/util/ForeignKeyRef.java?rev=599394&view=auto
==============================================================================
--- 
incubator/tuscany/java/das/tools/src/main/java/org/apache/tuscany/das/rdb/util/ForeignKeyRef.java
 (added)
+++ 
incubator/tuscany/java/das/tools/src/main/java/org/apache/tuscany/das/rdb/util/ForeignKeyRef.java
 Thu Nov 29 03:50:28 2007
@@ -0,0 +1,44 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.util;
+
+public class ForeignKeyRef {
+       private String foreign;
+       private String local;
+       
+       protected String getForeign() {
+               return foreign;
+       }
+       protected void setForeign(String foreign) {
+               this.foreign = foreign;
+       }
+       protected String getLocal() {
+               return local;
+       }
+       protected void setLocal(String local) {
+               this.local = local;
+       }
+       
+       public String toString() {
+               StringBuffer dbSchemaStr = new StringBuffer();
+               dbSchemaStr.append("Foreign:"+this.foreign+", 
Local:"+this.local+"\n");
+
+               return dbSchemaStr.toString();
+       }       
+}

Added: 
incubator/tuscany/java/das/tools/src/main/java/org/apache/tuscany/das/rdb/util/SQLTypeChecker.java
URL: 
http://svn.apache.org/viewvc/incubator/tuscany/java/das/tools/src/main/java/org/apache/tuscany/das/rdb/util/SQLTypeChecker.java?rev=599394&view=auto
==============================================================================
--- 
incubator/tuscany/java/das/tools/src/main/java/org/apache/tuscany/das/rdb/util/SQLTypeChecker.java
 (added)
+++ 
incubator/tuscany/java/das/tools/src/main/java/org/apache/tuscany/das/rdb/util/SQLTypeChecker.java
 Thu Nov 29 03:50:28 2007
@@ -0,0 +1,186 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.util;
+
+import java.sql.Types;
+
+public class SQLTypeChecker {
+       
+       public static int getSQLTypeFromString(String sqlTypeString) {
+               if(sqlTypeString.equals("CHAR")) return Types.CHAR;
+               if(sqlTypeString.equals("VARCHAR")) return Types.VARCHAR;
+               if(sqlTypeString.equals("LONGVARCHAR")) return 
Types.LONGVARCHAR;
+               if(sqlTypeString.equals("NUMERIC")) return Types.NUMERIC;
+               if(sqlTypeString.equals("DECIMAL")) return Types.DECIMAL;
+               if(sqlTypeString.equals("BIT")) return Types.BIT;
+               if(sqlTypeString.equals("BOOLEAN")) return Types.BOOLEAN;
+               if(sqlTypeString.equals("TINYINT")) return Types.TINYINT;
+               if(sqlTypeString.equals("SMALLINT")) return Types.SMALLINT;
+               if(sqlTypeString.equals("INTEGER")) return Types.INTEGER;
+               if(sqlTypeString.equals("BIGINT")) return Types.BIGINT;
+               if(sqlTypeString.equals("REAL")) return Types.REAL;
+               if(sqlTypeString.equals("FLOAT")) return Types.FLOAT;
+               if(sqlTypeString.equals("DOUBLE")) return Types.DOUBLE;
+               if(sqlTypeString.equals("BINARY")) return Types.BINARY;
+               if(sqlTypeString.equals("VARBINARY")) return Types.VARBINARY;
+               if(sqlTypeString.equals("LONGVARBINARY")) return 
Types.LONGVARBINARY;
+               if(sqlTypeString.equals("DATE")) return Types.DATE;
+               if(sqlTypeString.equals("TIME")) return Types.TIME;
+               if(sqlTypeString.equals("TIMESTAMP")) return Types.TIMESTAMP;
+               if(sqlTypeString.equals("CLOB")) return Types.CLOB;
+               if(sqlTypeString.equals("BLOB")) return Types.BLOB;
+               if(sqlTypeString.equals("ARRAY")) return Types.ARRAY;
+               if(sqlTypeString.equals("DISTINCT")) return Types.DISTINCT;
+               if(sqlTypeString.equals("STRUCT")) return Types.STRUCT;
+               if(sqlTypeString.equals("REF")) return Types.REF;
+               if(sqlTypeString.equals("DATALINK")) return Types.DATALINK;
+               if(sqlTypeString.equals("JAVA_OBJECT")) return 
Types.JAVA_OBJECT;
+               return Types.OTHER;
+       }
+
+       /*xsd : SDO 
+       anySimpleType Object
+       anyType DataObject
+       anyURI URI
+       base64Binary Bytes
+       boolean Boolean
+       byte Byte
+       date YearMonthDay
+       dateTime DateTime
+       decimal Decimal
+       double Double
+       duration Duration
+       ENTITIES Strings
+       ENTITY String
+       float Float
+       gDay Day
+       gMonth Month
+       gMonthDay MonthDay
+       gYear Year
+       gYearMonth YearMonth
+       hexBinary Bytes
+       ID String
+       IDREF String
+       IDREFS Strings
+       int Int
+       integer Integer
+       language String
+       long Long
+       Name String
+       NCName String
+       negativeInteger Integer
+       NMTOKEN String
+       NMTOKENS Strings
+       nonNegativeInteger Integer
+       nonPositiveInteger Integer
+       normalizedString String
+       NOTATION String
+       positiveInteger Integer
+       QName URI
+       short Short
+       string String
+       time Time
+       token String
+       unsignedByte Short
+       unsignedInt long
+       unsignedLong Integer
+       unsignedShort Int*/
+       static final String anySimpleTypeXSD = "anySimpleType";
+       static final String anyTypeXSD = "anyType";
+       static final String anyURIXSD = "anyURI";
+       static final String base64BinaryXSD = "base64Binary";
+       static final String booleanXSD = "boolean";
+       static final String byteXSD = "byte";
+       static final String dateXSD = "date";
+       static final String dateTimeXSD = "dateTime";
+       static final String decimalXSD = "decimal";
+       static final String doubleXSD = "double";
+       static final String durationXSD = "duration";
+       static final String ENTITIESXSD = "ENTITIES";
+       static final String ENTITYXSD = "ENTITY";
+       static final String floatXSD = "float"; 
+       static final String gDayXSD = "gDay"; 
+       static final String gMonthXSD = "gMonth"; 
+       static final String gMonthDayXSD = "gMonthDay"; 
+       static final String gYearXSD = "gYear"; 
+       static final String gYearMonthXSD = "gYearMonth"; 
+       static final String hexBinaryXSD = "hexBinary"; 
+       static final String IDXSD = "ID"; 
+       static final String IDREFXSD = "IDREF"; 
+       static final String IDREFSXSD = "IDREF"; 
+       static final String intXSD = "int"; 
+       static final String integerXSD = "integer"; 
+       static final String languageXSD = "language"; 
+       static final String longXSD = "long"; 
+       static final String NameXSD = "Name"; 
+       static final String NCNameXSD = "NCName"; 
+       static final String negativeIntegerXSD = "negativeInteger"; 
+       static final String NMTOKENXSD = "NMTOKEN"; 
+       static final String NMTOKENSXSD = "NMTOKENS";
+       static final String nonNegativeIntegerXSD = "nonNegativeInteger"; 
+       static final String nonPositiveIntegerXSD = "nonPositiveInteger"; 
+       static final String normalizedStringXSD = "normalizedString"; 
+       static final String NOTATIONXSD = "NOTATION"; 
+       static final String positiveIntegerXSD = "positiveInteger"; 
+       static final String QNameXSD = "QName"; 
+       static final String shortXSD = "short"; 
+       static final String stringXSD = "string"; 
+       static final String timeXSD = "time"; 
+       static final String tokenXSD = "token"; 
+       static final String unsignedByteXSD = "unsignedByte"; 
+       static final String unsignedIntXSD = "unsignedInt"; 
+       static final String unsignedLongXSD = "unsignedLong"; 
+       static final String unsignedShortXSD = "unsignedShort"; 
+       
+       public static String xsdTypeForSDOType(String sdoTypeName) {
+               if(sdoTypeName.equals("Object")) return anySimpleTypeXSD;
+               if(sdoTypeName.equals("DataObject")) return anyTypeXSD;
+               if(sdoTypeName.equals("URI")) return anyURIXSD;
+               if(sdoTypeName.equals("Bytes")) return base64BinaryXSD;
+               if(sdoTypeName.equals("Boolean")) return booleanXSD;
+               if(sdoTypeName.equals("boolean")) return booleanXSD;
+               if(sdoTypeName.equals("Byte")) return byteXSD;
+               if(sdoTypeName.equals("YearMonthDay")) return dateXSD;
+               if(sdoTypeName.equals("DateTime")) return dateTimeXSD;
+               if(sdoTypeName.equals("Date")) return dateTimeXSD;
+               if(sdoTypeName.equals("Decimal")) return decimalXSD;
+               if(sdoTypeName.equals("Double")) return doubleXSD;
+               if(sdoTypeName.equals("double")) return doubleXSD;
+               if(sdoTypeName.equals("Duration")) return durationXSD;
+               if(sdoTypeName.equals("Strings")) return ENTITIESXSD;
+               if(sdoTypeName.equals("String")) return stringXSD;
+               if(sdoTypeName.equals("Float")) return floatXSD;
+               if(sdoTypeName.equals("float")) return floatXSD;
+               if(sdoTypeName.equals("Day")) return gDayXSD;
+               if(sdoTypeName.equals("Month")) return gMonthXSD;
+               if(sdoTypeName.equals("MonthDay")) return gMonthDayXSD;
+               if(sdoTypeName.equals("Year")) return gYearXSD;
+               if(sdoTypeName.equals("YearMonth")) return gYearMonthXSD;
+               if(sdoTypeName.equals("Bytes")) return hexBinaryXSD;
+               if(sdoTypeName.equals("Int")) return intXSD;
+               if(sdoTypeName.equals("IntObject")) return intXSD;
+               if(sdoTypeName.equals("Integer")) return integerXSD;
+               if(sdoTypeName.equals("Long")) return longXSD;          
+               if(sdoTypeName.equals("long")) return longXSD;
+               if(sdoTypeName.equals("Short")) return shortXSD;
+               if(sdoTypeName.equals("Time")) return timeXSD;
+               if(sdoTypeName.equals("long")) return unsignedIntXSD;
+               return "";
+       }
+}

Added: 
incubator/tuscany/java/das/tools/src/main/java/org/apache/tuscany/das/rdb/util/SchemaFileToXSD.java
URL: 
http://svn.apache.org/viewvc/incubator/tuscany/java/das/tools/src/main/java/org/apache/tuscany/das/rdb/util/SchemaFileToXSD.java?rev=599394&view=auto
==============================================================================
--- 
incubator/tuscany/java/das/tools/src/main/java/org/apache/tuscany/das/rdb/util/SchemaFileToXSD.java
 (added)
+++ 
incubator/tuscany/java/das/tools/src/main/java/org/apache/tuscany/das/rdb/util/SchemaFileToXSD.java
 Thu Nov 29 03:50:28 2007
@@ -0,0 +1,200 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.util;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.InputStreamReader;
+import java.io.PrintStream;
+import java.util.ArrayList;
+import java.util.Hashtable;
+
+import javax.xml.stream.XMLInputFactory;
+import javax.xml.stream.XMLStreamReader;
+
+import org.apache.log4j.Logger;
+import org.apache.tuscany.das.rdb.graphbuilder.schema.ResultSetTypeMap;
+
+import commonj.sdo.Type;
+
+public class SchemaFileToXSD {
+       private static final Logger logger = 
Logger.getLogger(SchemaFileToXSD.class);
+       private static XMLInputFactory xmlFactory;
+       
+       protected static DBSchema read(XMLStreamReader reader) throws Exception 
{
+               DBSchema dbSchema = new DBSchema();
+               Table table = null;
+               Column column = null;
+               ForeignKey fk = null;
+               ForeignKeyRef fkRef = null;
+               
+        while (true) {
+            int event = reader.next();
+            if(javax.xml.stream.XMLStreamConstants.END_DOCUMENT == event) {
+               break;
+            }
+            
+            switch (event) {
+                   case javax.xml.stream.XMLStreamConstants.START_ELEMENT:
+                       if (reader.getName().getLocalPart().equals("table")) {
+                               table = new Table();
+                               table.setName(reader.getAttributeValue(null, 
"name"));
+                       } else if 
(reader.getName().getLocalPart().equals("column")) {
+                               column = new Column();
+                               column.setName(reader.getAttributeValue(null, 
"name"));
+                               
column.setPK(Boolean.getBoolean(reader.getAttributeValue(null, "primaryKey")));
+                               
column.setRequired(Boolean.getBoolean(reader.getAttributeValue(null, 
"required")));
+                               column.setType(reader.getAttributeValue(null, 
"type"));
+                       } 
+                       else if 
(reader.getName().getLocalPart().equals("foreign-key")) {
+                               fk = new ForeignKey();
+                               
fk.setForeignTableName(reader.getAttributeValue(null,"foreignTable"));
+                       }
+                       else if 
(reader.getName().getLocalPart().equals("reference")) {
+                               fkRef = new ForeignKeyRef();
+                               
fkRef.setForeign(reader.getAttributeValue(null,"foreign"));
+                               
fkRef.setLocal(reader.getAttributeValue(null,"local"));
+                       }
+                       else if 
(reader.getName().getLocalPart().equals("database")) {
+                               //ignore
+                       } else {
+                               throw new RuntimeException("not valid 
element:"+reader.getName()+":");
+                       }
+                       break;
+                       
+                   case javax.xml.stream.XMLStreamConstants.END_ELEMENT:
+                       if (reader.getName().getLocalPart().equals("table")) {
+                               dbSchema.getTables().add(table);
+                       }
+                       
+                       if (reader.getName().getLocalPart().equals("column")) {
+                               if(table != null) {
+                                       table.getColumns().add(column);
+                               }
+                       }
+                       
+                       if 
(reader.getName().getLocalPart().equals("reference")) {
+                               if(fk != null) {
+                                       fk.getFkRefs().add(fkRef);
+                               }
+                       }
+                       
+                       if 
(reader.getName().getLocalPart().equals("foreign-key")) {
+                               if(table != null) {
+                                       table.getFks().add(fk);
+                               }
+                       }
+                       
+                       break;
+            }
+        }
+        
+        if (logger.isDebugEnabled()) {
+               logger.debug(dbSchema);
+        }
+        
+        return dbSchema;
+    }
+
+       protected static void convert(String schemaFileName, String 
xsdModelFileName) throws Exception{
+               xmlFactory = XMLInputFactory.newInstance();
+               
+               File schemaFile = new File(schemaFileName);
+               FileInputStream flStrm = new FileInputStream(schemaFile);
+               
+               XMLStreamReader reader = xmlFactory.createXMLStreamReader(new 
InputStreamReader(flStrm));
+               
+               DBSchema dbSchema = read(reader);
+               flStrm.close();
+               generateXSD(dbSchema, xsdModelFileName);
+       }
+       
+       protected static void generateXSD(DBSchema dbSchema, String 
xsdModelFileName) throws Exception {
+               boolean writeFilesToDir = false;
+               String startLine = "<xsd:schema 
xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"; 
xmlns:this=\"http:///org.apache.tuscany.das.rdb.test/schemaModel.xsd\"; 
targetNamespace=\"http:///org.apache.tuscany.das.rdb.test/schemaModel.xsd\";>\n";
+               String endLine = "</xsd:schema>"; 
+                       
+               if(xsdModelFileName != null) {
+                       writeFilesToDir = true;
+               }
+               
+               //get all FKs to form a Map - PKtable -> FKTable , e.g. if 
CITIES has FK such that CITIES.STATE_ID is STATES.ID, then
+               //the map shall have STATES -> CITIES, TABLE2,...
+               Hashtable pkTofkTable = new Hashtable();
+               
+               for(int i=0; i<dbSchema.getTables().size(); i++) {
+                       Table curTable = (Table)dbSchema.getTables().get(i);
+                       for(int k=0; k<curTable.getFks().size(); k++) {
+                               ForeignKey curFK = 
(ForeignKey)curTable.getFks().get(k);
+                               String pkTableName = 
curFK.getForeignTableName();
+                               
+                               if(pkTofkTable.get(pkTableName) == null) {
+                                       pkTofkTable.put(pkTableName, new 
ArrayList());
+                               }
+                               
((ArrayList)pkTofkTable.get(pkTableName)).add(curTable.getName());
+                       }
+               }
+               
+               String srcCode = startLine;
+               
+               for(int i=0; i<dbSchema.getTables().size(); i++) {
+                       Table curTable = (Table)dbSchema.getTables().get(i);
+                       srcCode = srcCode + "<xsd:complexType 
name=\""+curTable.getName()+"\">\n";
+                       srcCode = srcCode + "   <xsd:sequence>\n";
+                       
+                       for(int j=0; j<curTable.getColumns().size(); j++) {
+                               Column curColumn = 
(Column)curTable.getColumns().get(j);
+                               srcCode = srcCode + "      <xsd:element 
name=\""+       curColumn.getName();
+                               
+                               if(curColumn.isRequired()) {
+                                       srcCode = srcCode + " 
nillable=\"false\"";
+                               }
+                               
+                               Type sdoType = 
ResultSetTypeMap.INSTANCE.getType(SQLTypeChecker.getSQLTypeFromString(curColumn.getType()),
 true);
+                               
+                               srcCode = srcCode + " 
type=\"xsd:"+SQLTypeChecker.xsdTypeForSDOType(sdoType.getName())+"\"/>\n";
+                       }
+                       
+                       if(pkTofkTable.get(curTable.getName()) != null) {
+                               ArrayList fkTables = 
(ArrayList)pkTofkTable.get(curTable.getName());
+                               
+                               for(int k=0; k<fkTables.size(); k++) {
+                                       srcCode = srcCode + "      <xsd:element 
maxOccurs=\"unbounded\" name=\""+ fkTables.get(k)+"\" 
type=\"this:"+fkTables.get(k)+"\"/>\n";
+                               }
+                       }
+                       
+                       srcCode = srcCode + "   </xsd:sequence>\n";
+                       srcCode = srcCode + "</xsd:complexType>\n\n";
+               }
+               
+               srcCode = srcCode + endLine;
+
+               File javaFile = new File(xsdModelFileName);
+               javaFile.createNewFile();
+               PrintStream flStrm = new PrintStream(javaFile);
+               
+               if(writeFilesToDir) {
+                       flStrm.print(srcCode);
+                       flStrm.flush();
+                       flStrm.close();
+               } else {
+                       System.out.print(srcCode);
+               }
+       }       
+}
\ No newline at end of file

Added: 
incubator/tuscany/java/das/tools/src/main/java/org/apache/tuscany/das/rdb/util/Table.java
URL: 
http://svn.apache.org/viewvc/incubator/tuscany/java/das/tools/src/main/java/org/apache/tuscany/das/rdb/util/Table.java?rev=599394&view=auto
==============================================================================
--- 
incubator/tuscany/java/das/tools/src/main/java/org/apache/tuscany/das/rdb/util/Table.java
 (added)
+++ 
incubator/tuscany/java/das/tools/src/main/java/org/apache/tuscany/das/rdb/util/Table.java
 Thu Nov 29 03:50:28 2007
@@ -0,0 +1,66 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.util;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class Table {
+       private String name;
+       private List columns = new ArrayList();
+       private List fks = new ArrayList();
+       
+       protected String getName() {
+               return name;
+       }
+       protected void setName(String name) {
+               this.name = name;
+       }
+       protected List getColumns() {
+               return columns;
+       }
+       protected void setColumns(List columns) {
+               this.columns = columns;
+       }
+       protected List getFks() {
+               return fks;
+       }
+       protected void setFks(List fks) {
+               this.fks = fks;
+       }
+       
+       public String toString() {
+               StringBuffer dbSchemaStr = new StringBuffer();
+               dbSchemaStr.append("_____Table_______"+this.name+"\n");
+               dbSchemaStr.append("_____Columns_______\n");
+               for(int i=0; i<this.columns.size(); i++) {
+                       Column curColumn = (Column)this.columns.get(i);
+                       dbSchemaStr.append(curColumn);
+               }
+               
+               if(this.fks.size() > 0)
+                       dbSchemaStr.append("_____FKs_______\n");
+               for(int i=0; i<this.fks.size(); i++) {
+                       ForeignKey curFk = (ForeignKey)this.fks.get(i);
+                       dbSchemaStr.append(curFk);
+               }
+               
+               return dbSchemaStr.toString();
+       }       
+}

Added: 
incubator/tuscany/java/das/tools/src/test/java/org/apache/tuscany/das/rdb/test/DBToXSDTests.java
URL: 
http://svn.apache.org/viewvc/incubator/tuscany/java/das/tools/src/test/java/org/apache/tuscany/das/rdb/test/DBToXSDTests.java?rev=599394&view=auto
==============================================================================
--- 
incubator/tuscany/java/das/tools/src/test/java/org/apache/tuscany/das/rdb/test/DBToXSDTests.java
 (added)
+++ 
incubator/tuscany/java/das/tools/src/test/java/org/apache/tuscany/das/rdb/test/DBToXSDTests.java
 Thu Nov 29 03:50:28 2007
@@ -0,0 +1,142 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.test;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileReader;
+import java.io.IOException;
+
+import junit.framework.TestCase;
+
+import org.apache.tuscany.das.rdb.dbconfig.DBInitializer;
+import org.apache.tuscany.das.rdb.util.DBToXSDGenerator;
+/**
+ * Tests the DB Schema to Model XSD converter tool
+ */
+public class DBToXSDTests  extends TestCase {
+       protected void setUp() throws Exception {           
+        super.setUp();
+        DBInitializer dbInitializer = new DBInitializer("dbConfig.xml");
+        dbInitializer.initializeDatabase(true);
+       }
+       
+       protected void tearDown() throws Exception {
+               super.tearDown();
+       }
+       
+       static public String getContents(File aFile) {
+           //...checks on aFile are elided
+           StringBuffer contents = new StringBuffer();
+
+           //declared here only to make visible to finally clause
+           BufferedReader input = null;
+           try {
+             //use buffering, reading one line at a time
+             //FileReader always assumes default encoding is OK!
+             input = new BufferedReader( new FileReader(aFile) );
+             String line = null; //not declared within while loop
+             /*
+             * readLine is a bit quirky :
+             * it returns the content of a line MINUS the newline.
+             * it returns null only for the END of the stream.
+             * it returns an empty String if two newlines appear in a row.
+             */
+             while (( line = input.readLine()) != null){
+               contents.append(line);
+               contents.append(System.getProperty("line.separator"));
+             }
+           }
+           catch (FileNotFoundException ex) {
+             ex.printStackTrace();
+           }
+           catch (IOException ex){
+             ex.printStackTrace();
+           }
+           finally {
+             try {
+               if (input!= null) {
+                 //flush and close both "input" and its underlying FileReader
+                 input.close();
+               }
+             }
+             catch (IOException ex) {
+               ex.printStackTrace();
+             }
+           }
+           return contents.toString();
+         }
+       
+    String compare1 = "<table javaName=\"STATES\" name=\"STATES\">";
+    String compare2 = "<table javaName=\"CITIES\" name=\"CITIES\">";
+    String compare3 = "<xsd:complexType name=\"CITIES\">";
+    String compare4 = "<xsd:complexType name=\"STATES\">";
+
+       public void testDBToSchemaFile() throws Exception {
+               DBToXSDGenerator.getSchemaFileFromDB("DBConnectionConfig.xml");
+               File file = new File("target/dbSchema.txt");
+               if(file.isFile()) {
+                       String schemaContent = getContents(file);
+                       if(schemaContent.indexOf(compare1) != -1 && 
schemaContent.indexOf(compare2) != -1) {
+                               assertTrue(true);
+                       }
+               } else {
+                       fail("SchemaFile does not exists or has improper 
contents!");
+               }
+       }
+       
+       public void testSchemaFileToXSD() throws Exception {
+               
DBToXSDGenerator.getModelFileFromSchemaFile("target/dbSchema.txt", 
"target/schemaModel.xsd");
+               
+               File modelfile = new File("target/schemaModel.xsd");
+               if(modelfile.isFile()) {
+                       String modelContent = getContents(modelfile);
+                       if(modelContent.indexOf(compare3) != -1 && 
modelContent.indexOf(compare4) != -1) {
+                               assertTrue(true);
+                       }
+               } else {
+                       fail("ModelFile does not exists or has improper 
contents!");
+               }               
+       }
+       
+       public void testDBToXSD() throws Exception {
+               DBToXSDGenerator.getModelFileFromDB("DBConnectionConfig.xml");
+               
+               File file = new File("target/dbSchema.txt");
+               if(file.isFile()) {
+                       String schemaContent = getContents(file);
+                       if(schemaContent.indexOf(compare1) != -1 && 
schemaContent.indexOf(compare2) != -1) {
+                               assertTrue(true);
+                       }
+               } else {
+                       fail("SchemaFile does not exists or has improper 
contents!");
+               }
+               
+               File modelfile = new File("target/schemaModel.xsd");
+               if(modelfile.isFile()) {
+                       String modelContent = getContents(modelfile);
+                       if(modelContent.indexOf(compare3) != -1 && 
modelContent.indexOf(compare4) != -1) {
+                               assertTrue(true);
+                       }
+               } else {
+                       fail("ModelFile does not exists or has improper 
contents!");
+               }
+       }
+}

Added: 
incubator/tuscany/java/das/tools/src/test/resources/DBConnectionConfig.xml
URL: 
http://svn.apache.org/viewvc/incubator/tuscany/java/das/tools/src/test/resources/DBConnectionConfig.xml?rev=599394&view=auto
==============================================================================
--- incubator/tuscany/java/das/tools/src/test/resources/DBConnectionConfig.xml 
(added)
+++ incubator/tuscany/java/das/tools/src/test/resources/DBConnectionConfig.xml 
Thu Nov 29 03:50:28 2007
@@ -0,0 +1,34 @@
+<?xml version="1.0" encoding="ASCII"?>
+<!--
+  Licensed to the Apache Software Foundation (ASF) under one
+  or more contributor license agreements.  See the NOTICE file
+  distributed with this work for additional information
+  regarding copyright ownership.  The ASF licenses this file
+  to you 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.
+ -->
+ <Config xmlns="http:///org.apache.tuscany.das.rdb/config.xsd";> 
+    
+    <!--Uncomment below for derby test-->
+       <ConnectionInfo>
+               <ConnectionProperties
+                       driverClass="org.apache.derby.jdbc.EmbeddedDriver"
+                       databaseURL="jdbc:derby:target/dastest;create=true"
+                       schemaName = "APP"
+                       loginTimeout="600000"/>
+       </ConnectionInfo>
+       <OutFiles
+               schemaFile="target/dbSchema.txt"
+               modelFile="target/schemaModel.xsd"
+       />
+</Config>

Added: incubator/tuscany/java/das/tools/src/test/resources/dbConfig.xml
URL: 
http://svn.apache.org/viewvc/incubator/tuscany/java/das/tools/src/test/resources/dbConfig.xml?rev=599394&view=auto
==============================================================================
--- incubator/tuscany/java/das/tools/src/test/resources/dbConfig.xml (added)
+++ incubator/tuscany/java/das/tools/src/test/resources/dbConfig.xml Thu Nov 29 
03:50:28 2007
@@ -0,0 +1,32 @@
+<?xml version="1.0" encoding="ASCII"?>
+<!--
+  Licensed to the Apache Software Foundation (ASF) under one
+  or more contributor license agreements.  See the NOTICE file
+  distributed with this work for additional information
+  regarding copyright ownership.  The ASF licenses this file
+  to you 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.
+ -->
+<DBConfig xmlns="http:///org.apache.tuscany.das.rdb/dbconfig.xsd";>
+       <ConnectionInfo>
+               <ConnectionProperties 
+                       driverClass="org.apache.derby.jdbc.EmbeddedDriver" 
+                       databaseURL="jdbc:derby:target/dastest; create = true" 
+                       loginTimeout="600000"/>
+       </ConnectionInfo>
+       <Table name="STATES" SQLCreate="CREATE TABLE STATES (ID INT PRIMARY KEY 
NOT NULL, NAME VARCHAR(2))">
+       </Table>
+       <Table name="CITIES" SQLCreate="CREATE TABLE CITIES (ID INT PRIMARY KEY 
NOT NULL, NAME VARCHAR(50), STATES_ID INTEGER,  
+       CONSTRAINT FK1 FOREIGN KEY (STATES_ID) REFERENCES  STATES (ID) ON 
DELETE NO ACTION ON UPDATE NO ACTION)"        >
+       </Table>
+</DBConfig>



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to