Author: lresende
Date: Thu May  8 08:32:34 2008
New Revision: 654554

URL: http://svn.apache.org/viewvc?rev=654554&view=rev
Log:
TUSCANY-2297 - Applying patch from Douglas to add support for data collection 
interface

Modified:
    incubator/tuscany/java/sca/modules/implementation-data-xml/pom.xml
    
incubator/tuscany/java/sca/modules/implementation-data-xml/src/main/java/org/apache/tuscany/sca/implementation/data/DATAImplementation.java
    
incubator/tuscany/java/sca/modules/implementation-data-xml/src/main/java/org/apache/tuscany/sca/implementation/data/provider/DATAImplementationProvider.java
    
incubator/tuscany/java/sca/modules/implementation-data-xml/src/main/java/org/apache/tuscany/sca/implementation/data/provider/DATAInvoker.java
    
incubator/tuscany/java/sca/modules/implementation-data-xml/src/test/java/org/apache/tuscany/sca/implementation/data/DATATestCase.java

Modified: incubator/tuscany/java/sca/modules/implementation-data-xml/pom.xml
URL: 
http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/implementation-data-xml/pom.xml?rev=654554&r1=654553&r2=654554&view=diff
==============================================================================
--- incubator/tuscany/java/sca/modules/implementation-data-xml/pom.xml 
(original)
+++ incubator/tuscany/java/sca/modules/implementation-data-xml/pom.xml Thu May  
8 08:32:34 2008
@@ -1,23 +1,22 @@
 <?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>
+<!--
+    * 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.sca</groupId>
@@ -120,8 +119,8 @@
                <dependency>
                        <groupId>junit</groupId>
                        <artifactId>junit</artifactId>
-                       <version>3.8.1</version>
-                       <scope>test</scope>
+                       <version>4.2</version>
+                       <scope>compile</scope>
                </dependency>
                <dependency>
                        <groupId>org.easymock</groupId>
@@ -129,6 +128,12 @@
                        <version>2.2</version>
                        <scope>test</scope>
                </dependency>
+               <dependency>
+            <groupId>org.apache.tuscany.sca</groupId>
+            <artifactId>tuscany-implementation-data-api</artifactId>
+            <version>2.0-incubating-SNAPSHOT</version>
+            <scope>compile</scope>
+        </dependency>
        </dependencies>
 
     <build>

Modified: 
incubator/tuscany/java/sca/modules/implementation-data-xml/src/main/java/org/apache/tuscany/sca/implementation/data/DATAImplementation.java
URL: 
http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/implementation-data-xml/src/main/java/org/apache/tuscany/sca/implementation/data/DATAImplementation.java?rev=654554&r1=654553&r2=654554&view=diff
==============================================================================
--- 
incubator/tuscany/java/sca/modules/implementation-data-xml/src/main/java/org/apache/tuscany/sca/implementation/data/DATAImplementation.java
 (original)
+++ 
incubator/tuscany/java/sca/modules/implementation-data-xml/src/main/java/org/apache/tuscany/sca/implementation/data/DATAImplementation.java
 Thu May  8 08:32:34 2008
@@ -73,18 +73,32 @@
             while(tables.next()) {
                 //create the SCA service for the table
                 Service dataService = assemblyFactory.createService();
-                dataService.setName( tables.getString(3));
-                JavaInterface javaInterface;
+                Service dataCollectionService = 
assemblyFactory.createService();
+                
+                dataService.setName(tables.getString(3)+"_DATA");
+                dataCollectionService.setName(tables.getString(3));
+                
+                JavaInterface dataInterface;
+                JavaInterface dataCollectionInterface;
+                
                 try {
-                    javaInterface = 
javaFactory.createJavaInterface(DATA.class);
+                    dataInterface = 
javaFactory.createJavaInterface(DATA.class);
+                    dataCollectionInterface = 
javaFactory.createJavaInterface(DATACollection.class);
                 } catch (InvalidInterfaceException e) {
                     throw new IllegalArgumentException(e);
                 }
-                JavaInterfaceContract interfaceContract = 
javaFactory.createJavaInterfaceContract();
-                interfaceContract.setInterface(javaInterface);
-                dataService.setInterfaceContract(interfaceContract);
+                JavaInterfaceContract dataInterfaceContract = 
javaFactory.createJavaInterfaceContract();
+                JavaInterfaceContract dataCollectionInterfaceContract = 
javaFactory.createJavaInterfaceContract();
                 
+                dataInterfaceContract.setInterface(dataInterface);
+                
dataCollectionInterfaceContract.setInterface(dataCollectionInterface);
+                
+                dataService.setInterfaceContract(dataInterfaceContract);
+                
dataCollectionService.setInterfaceContract(dataCollectionInterfaceContract);  
+               
                 services.add(dataService);
+                services.add(dataCollectionService);           
+                
             }
         } catch(SQLException e) {
             

Modified: 
incubator/tuscany/java/sca/modules/implementation-data-xml/src/main/java/org/apache/tuscany/sca/implementation/data/provider/DATAImplementationProvider.java
URL: 
http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/implementation-data-xml/src/main/java/org/apache/tuscany/sca/implementation/data/provider/DATAImplementationProvider.java?rev=654554&r1=654553&r2=654554&view=diff
==============================================================================
--- 
incubator/tuscany/java/sca/modules/implementation-data-xml/src/main/java/org/apache/tuscany/sca/implementation/data/provider/DATAImplementationProvider.java
 (original)
+++ 
incubator/tuscany/java/sca/modules/implementation-data-xml/src/main/java/org/apache/tuscany/sca/implementation/data/provider/DATAImplementationProvider.java
 Thu May  8 08:32:34 2008
@@ -46,16 +46,41 @@
         String operationName = operation.getName();
         String tableName = service.getName();
 
-        if (operationName.equals("get")) {
-            return new DATAInvoker.GetInvoker(operation, 
implementation.getConnectionInfo(), tableName);
-        } else if (operationName.equals("insert")) {
-               return new DATAInvoker.InsertInvoker(operation, 
implementation.getConnectionInfo(), tableName);
-        } else if (operationName.equals("update")) {
-               return new DATAInvoker.UpdateInvoker(operation, 
implementation.getConnectionInfo(), tableName);
-        } else if (operationName.equals("delete")) {
-            return new DATAInvoker.DeleteInvoker(operation, 
implementation.getConnectionInfo(), tableName);
+        String interfaceFullName = operation.getInterface().toString();
+        int index = interfaceFullName.lastIndexOf(".") + 1;
+        String interfaceName = interfaceFullName.substring(index, 
interfaceFullName.length());
+
+        if (interfaceName.equals("DATACollection")) {
+
+            if (operationName.equals("getAll")) {
+                return new DATAInvoker.GetAllInvoker(operation, 
implementation.getConnectionInfo(), tableName);
+            } else if (operationName.equals("query")) {
+                return new DATAInvoker.QueryInvoker(operation, 
implementation.getConnectionInfo(), tableName);
+            } else if (operationName.equals("post")) {
+                return new DATAInvoker.PostInvoker(operation, 
implementation.getConnectionInfo(), tableName);
+            } else if (operationName.equals("get")) {
+                return new DATAInvoker.GetInvoker(operation, 
implementation.getConnectionInfo(), tableName);
+            } else if (operationName.equals("put")) {
+                return new DATAInvoker.PutInvoker(operation, 
implementation.getConnectionInfo(), tableName);
+            } else if (operationName.equals("delete")) {
+                return new DATAInvoker.DeleteInvoker(operation, 
implementation.getConnectionInfo(), tableName);
+            }
+
+        } else if (interfaceName.equals("DATA")) {
+            
+            tableName = tableName.split("_")[0];
+            
+            if (operationName.equals("get")) {
+                return new DATAInvoker.GetDATAInvoker(operation, 
implementation.getConnectionInfo(), tableName);
+            } else if (operationName.equals("insert")) {
+                return new DATAInvoker.InsertDATAInvoker(operation, 
implementation.getConnectionInfo(), tableName);
+            } else if (operationName.equals("update")) {
+                return new DATAInvoker.UpdateDATAInvoker(operation, 
implementation.getConnectionInfo(), tableName);
+            } else if (operationName.equals("delete")) {
+                return new DATAInvoker.DeleteDATAInvoker(operation, 
implementation.getConnectionInfo(), tableName);
+            }
         }
-
+        
         return new DATAInvoker(operation, implementation.getConnectionInfo(), 
tableName);
     }
 
@@ -64,11 +89,10 @@
     }
 
     public void start() {
-        // System.out.println("Starting " + component.getName());
+    // System.out.println("Starting " + component.getName());
     }
 
     public void stop() {
-        // System.out.println("Stopping " + component.getName());
+    // System.out.println("Stopping " + component.getName());
     }
-
 }

Modified: 
incubator/tuscany/java/sca/modules/implementation-data-xml/src/main/java/org/apache/tuscany/sca/implementation/data/provider/DATAInvoker.java
URL: 
http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/implementation-data-xml/src/main/java/org/apache/tuscany/sca/implementation/data/provider/DATAInvoker.java?rev=654554&r1=654553&r2=654554&view=diff
==============================================================================
--- 
incubator/tuscany/java/sca/modules/implementation-data-xml/src/main/java/org/apache/tuscany/sca/implementation/data/provider/DATAInvoker.java
 (original)
+++ 
incubator/tuscany/java/sca/modules/implementation-data-xml/src/main/java/org/apache/tuscany/sca/implementation/data/provider/DATAInvoker.java
 Thu May  8 08:32:34 2008
@@ -16,7 +16,6 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-
 package org.apache.tuscany.sca.implementation.data.provider;
 
 import java.sql.Connection;
@@ -37,7 +36,6 @@
 import org.apache.tuscany.sca.invocation.Message;
 import org.osoa.sca.ServiceRuntimeException;
 
-
 /**
  * Implements a target invoker for DAS component implementations.
  *
@@ -48,56 +46,168 @@
  * @version $Rev$ $Date$
  */
 public class DATAInvoker implements Invoker {
+
     protected final Operation operation;
     protected final ConnectionInfo connectionInfo;
     protected final String table;
-    
+
     public DATAInvoker(Operation operation, ConnectionInfo connectionInfo, 
String table) {
         this.operation = operation;
         this.connectionInfo = connectionInfo;
         this.table = table;
     }
-    
+
     public Message invoke(Message msg) {
         // Shouldn't get here, as the only supported operations
         // are the ones defined DATA interface and implemented
         // by specific invoker subclasses
-        
+
         throw new UnsupportedOperationException(operation.getName());
     }
-    
-    
+
     /****************************************************************
      *
      * Internal invoker implementations for each supported operation
      *
      *****************************************************************/
-    
-    
+    /**
+     * GetAll operation invoker
+     */
+    public static class GetAllInvoker extends DATAInvoker {
+
+        public GetAllInvoker(Operation operation, ConnectionInfo 
connectionInfo, String table) {
+            super(operation, connectionInfo, table);
+        }
+
+        @Override
+        public Message invoke(Message msg) {
+            return msg;
+        }
+    }
+
+    /**
+     * Query operation invoker
+     */
+    public static class QueryInvoker extends DATAInvoker {
+
+        public QueryInvoker(Operation operation, ConnectionInfo 
connectionInfo, String table) {
+            super(operation, connectionInfo, table);
+        }
+
+        @Override
+        public Message invoke(Message msg) {
+            return msg;
+        }
+    }
+
+    /**
+     * Post operation invoker
+     */
+    public static class PostInvoker extends DATAInvoker {
+
+        public PostInvoker(Operation operation, ConnectionInfo connectionInfo, 
String table) {
+            super(operation, connectionInfo, table);
+        }
+
+        @Override
+        public Message invoke(Message msg) {
+            
+            StringBuilder sqlInsert = new StringBuilder();
+            XMLStreamReader insertStream = (XMLStreamReader) ((Object[]) 
msg.getBody())[1];
+
+            if (insertStream == null) {
+                throw new IllegalArgumentException("The XMLStreamReader 
\"insertStream\" must not be null");
+            }
+
+
+            Connection connection = null;
+            PreparedStatement inStmt = null;
+
+            List<String> colNames = new ArrayList<String>();
+            List<String> values = new ArrayList<String>();
+
+            int result = 0;
+            try {
+
+                connection = JDBCHelper.getConnection(connectionInfo);
+
+                while (insertStream.hasNext()) {
+
+                    insertStream.next();
+                    if (insertStream.isStartElement()) {
+                        if (insertStream.getLocalName().equals("record")) {
+                            sqlInsert.append("INSERT INTO " + this.table + " 
(");
+                        } else if 
(insertStream.getLocalName().equals("column")) {
+                            colNames.add(insertStream.getAttributeValue(0));
+                            insertStream.next();
+                            if (insertStream.isCharacters()) {
+                                values.add(insertStream.getText());
+                            }
+                        }
+                    } else if (insertStream.isEndElement() && 
insertStream.getLocalName().equals("record")) {
+                        for (String c : colNames) {
+                            sqlInsert.append(" " + c + ",");
+                        }
+
+                        sqlInsert.deleteCharAt(sqlInsert.length() - 1);
+                        sqlInsert.append(" ) VALUES (");
+
+                        for (String v : values) {
+                            sqlInsert.append(" '" + v + "',");
+                        }
+
+                        sqlInsert.deleteCharAt(sqlInsert.length() - 1);
+                        sqlInsert.append(" )");
+
+                        inStmt = 
connection.prepareStatement(sqlInsert.toString());
+
+                        result += inStmt.executeUpdate();
+
+                        // Clean up resources
+                        inStmt.close();
+                        sqlInsert.delete(0, sqlInsert.length());
+                        values.clear();
+                        colNames.clear();
+                    }
+                }
+            } catch (XMLStreamException e) {
+                msg.setFaultBody(new ServiceRuntimeException(e));
+            } catch (SQLException sqle) {
+                sqle.printStackTrace();
+                msg.setFaultBody(new ServiceRuntimeException(sqle.getCause()));
+            } catch (Exception e) {
+                msg.setFaultBody(new ServiceRuntimeException(e));
+            } finally {
+                JDBCHelper.cleanupResources(connection, inStmt, null);
+            }
+
+            msg.setBody(Integer.toString(result));
+            return msg;
+        }
+    }
+
     /**
      * Get operation invoker
-     *
-     * @version $Rev$ $Date$
      */
     public static class GetInvoker extends DATAInvoker {
-        
+
         public GetInvoker(Operation operation, ConnectionInfo connectionInfo, 
String table) {
             super(operation, connectionInfo, table);
         }
-        
+
         @Override
         public Message invoke(Message msg) {
             
             // Get an entry
             String sqlQuery = null;
-            String id = (String)((Object[])msg.getBody())[0];
-            
+            String id = (String) ((Object[]) msg.getBody())[0];
+
             if (id == null) {
                 sqlQuery = "SELECT * FROM " + this.table;
             } else {
                 sqlQuery = "SELECT * FROM " + this.table + " WHERE ID = " + id;
             }
-            
+
             Connection connection = null;
             PreparedStatement queryStatement = null;
             ResultSet resultSet = null;
@@ -105,220 +215,369 @@
                 connection = JDBCHelper.getConnection(connectionInfo);
                 queryStatement = connection.prepareStatement(sqlQuery);
                 resultSet = queryStatement.executeQuery();
-                
-                
-            } catch(SQLException sqle) {
+
+
+            } catch (SQLException sqle) {
                 msg.setFaultBody(new ServiceRuntimeException(sqle.getCause()));
                 JDBCHelper.cleanupResources(connection, queryStatement, 
resultSet);
             } catch (Exception e) {
                 msg.setFaultBody(new ServiceRuntimeException(e));
                 JDBCHelper.cleanupResources(connection, queryStatement, 
resultSet);
             } finally {
-                //default we leave the connection open to pass to the 
JDBCStreamReader
+            //default we leave the connection open to pass to the 
JDBCStreamReader
             }
-            
+
             msg.setBody(new JDBCResultSetStreamReader(resultSet));
             return msg;
         }
-    }   
-    
-    
+    }
 
     /**
-        * Insert operation invoker
-        */
-       public static class InsertInvoker extends DATAInvoker {
-
-               public InsertInvoker(Operation operation,
-                               ConnectionInfo connectionInfo, String table) {
-                       super(operation, connectionInfo, table);
-               }
-
-               @Override
-               public Message invoke(Message msg) throws 
IllegalArgumentException {
-                       StringBuilder sqlInsert = new StringBuilder();
-                       XMLStreamReader insertStream = (XMLStreamReader) 
((Object[]) msg.getBody())[0];
-
-                       if (insertStream == null) {
-                               throw new IllegalArgumentException("The 
XMLStreamReader \"insertStream\" must not be null");
-                       }
-
-                       
-                       Connection connection = null;
-                       PreparedStatement inStmt = null;
-
-                       List<String> colNames = new ArrayList<String>();
-                       List<String> values = new ArrayList<String>();
-
-                       int result = 0;
-                       try {
-
-                               connection = 
JDBCHelper.getConnection(connectionInfo);
-                               while (insertStream.hasNext()) {
-
-                                       insertStream.next();
-                                       if (insertStream.isStartElement()) {
-                                               if 
(insertStream.getLocalName().equals("record")) {
-                                                       
sqlInsert.append("INSERT INTO " + this.table + " (");
-                                               }else if 
(insertStream.getLocalName().equals("column")) {
-                                                       
colNames.add(insertStream.getAttributeValue(0));
-                                                       insertStream.next();
-                                                       if 
(insertStream.isCharacters()) {
-                                                               
values.add(insertStream.getText());
-                                                       }
-                                               }
-                                       } else if (insertStream.isEndElement() 
&& insertStream.getLocalName().equals("record")) {
-                                               for (String c : colNames) {
-                                                       sqlInsert.append(" " + 
c + ",");
-                                               }
-
-                                               
sqlInsert.deleteCharAt(sqlInsert.length() - 1);
-                                               sqlInsert.append(" ) VALUES (");
-
-                                               for (String v : values) {
-                                                       sqlInsert.append(" '" + 
v + "',");
-                                               }
-
-                                               
sqlInsert.deleteCharAt(sqlInsert.length() - 1);
-                                               sqlInsert.append(" )");
-
-                                               inStmt = 
connection.prepareStatement(sqlInsert.toString());
-                                               result += 
inStmt.executeUpdate();
-
-                                               // Clean up resources
-                                               inStmt.close();
-                                               sqlInsert.delete(0, 
sqlInsert.length());
-                                               values.clear();
-                                               colNames.clear();
-                                       }
-                               }
-                       } catch (XMLStreamException e) {
-                               msg.setFaultBody(new 
ServiceRuntimeException(e));
-                       } catch (SQLException sqle) {
-                               sqle.printStackTrace();
-                               msg.setFaultBody(new 
ServiceRuntimeException(sqle.getCause()));
-                       } catch (Exception e) {
-                               msg.setFaultBody(new 
ServiceRuntimeException(e));
-                       } finally {
-                               JDBCHelper.cleanupResources(connection, inStmt, 
null);
-                       }
-
-                       msg.setBody(result);
-                       return msg;
-               }
-       }    
-       
-    /**
-        * Update operation invoker
-        */
-       public static class UpdateInvoker extends DATAInvoker {
-
-               public UpdateInvoker(Operation operation,
-                               ConnectionInfo connectionInfo, String table) {
-                       super(operation, connectionInfo, table);
-               }
-
-               @Override
-               public Message invoke(Message msg) throws 
IllegalArgumentException {
-
-                       XMLStreamReader updateStream = (XMLStreamReader) 
((Object[]) msg.getBody())[0];
-
-                       if (updateStream == null) {
-                               throw new IllegalArgumentException("The 
XMLStreamReader \"updateStream\" must not be null");
-                       }
-
-                       Connection connection = null;
-                       PreparedStatement upStmt = null;
-                       
-                       String id = null;
-                       String columnName = null;
-                       String newValue = null;
-                       int result = 0;
-
-                       try {
-                               connection = 
JDBCHelper.getConnection(connectionInfo);
-                               while (updateStream.hasNext()) {
-                                       updateStream.next();
-
-                                       if (updateStream.isStartElement() && 
updateStream.getLocalName().equals("column")) {
-                                               columnName = 
updateStream.getAttributeValue(0);
-                                               updateStream.next();
-                                               if 
(updateStream.isCharacters()) {
-                                                       if 
(columnName.equals("ID")) {
-                                                               id = 
updateStream.getText();
-                                                       } else {
-                                                               newValue = 
updateStream.getText();
-
-                                                               upStmt = 
connection.prepareStatement("UPDATE "
-                                                                               
+ this.table + " SET " + columnName
-                                                                               
+ " = '" + newValue + "' WHERE ID = "
-                                                                               
+ id);
-
-                                                               result += 
upStmt.executeUpdate();
-                                                               upStmt.close();
-                                                       }
-                                               }
-                                       }
-                               }
-                       } catch (XMLStreamException e) {
-                               msg.setFaultBody(new 
ServiceRuntimeException(e));
-                       } catch (SQLException sqle) {
-                               sqle.printStackTrace();
-                               msg.setFaultBody(new 
ServiceRuntimeException(sqle.getCause()));
-                       } catch (Exception e) {
-                               msg.setFaultBody(new 
ServiceRuntimeException(e));
-                       } finally {
-                               JDBCHelper.cleanupResources(connection, upStmt, 
null);
-                       }
-
-                       msg.setBody(result);
-                       return msg;
-               }
-       }
-       
-    
+     * Put operation invoker
+     */
+    public static class PutInvoker extends DATAInvoker {
+
+        public PutInvoker(Operation operation, ConnectionInfo connectionInfo, 
String table) {
+            super(operation, connectionInfo, table);
+        }
+
+        @Override
+        public Message invoke(Message msg) {
+            
+            XMLStreamReader updateStream = (XMLStreamReader) ((Object[]) 
msg.getBody())[1];
+
+            if (updateStream == null) {
+                throw new IllegalArgumentException("The XMLStreamReader 
\"updateStream\" must not be null");
+            }
+
+            Connection connection = null;
+            PreparedStatement upStmt = null;
+
+            String id = null;
+            String columnName = null;
+            String newValue = null;
+            int result = 0;
+
+            try {
+                connection = JDBCHelper.getConnection(connectionInfo);
+                while (updateStream.hasNext()) {
+                    updateStream.next();
+
+                    if (updateStream.isStartElement() && 
updateStream.getLocalName().equals("column")) {
+                        columnName = updateStream.getAttributeValue(0);
+                        updateStream.next();
+                        if (updateStream.isCharacters()) {
+                            if (columnName.equals("ID")) {
+                                id = updateStream.getText();
+                            } else {
+                                newValue = updateStream.getText();
+
+                                upStmt = connection.prepareStatement("UPDATE " 
+ this.table + " SET " + columnName + " = '" + newValue + "' WHERE ID = " + id);
+
+                                result += upStmt.executeUpdate();
+                                upStmt.close();
+                            }
+                        }
+                    }
+                }
+            } catch (XMLStreamException e) {
+                msg.setFaultBody(new ServiceRuntimeException(e));
+            } catch (SQLException sqle) {
+                sqle.printStackTrace();
+                msg.setFaultBody(new ServiceRuntimeException(sqle.getCause()));
+            } catch (Exception e) {
+                msg.setFaultBody(new ServiceRuntimeException(e));
+            } finally {
+                JDBCHelper.cleanupResources(connection, upStmt, null);
+            }
+
+            msg.setBody(result);
+            return msg;
+        }
+    }
+
     /**
-        * Delete operation invoker
-        */
+     * Delete operation invoker
+     */
     public static class DeleteInvoker extends DATAInvoker {
-        
+
         public DeleteInvoker(Operation operation, ConnectionInfo 
connectionInfo, String table) {
             super(operation, connectionInfo, table);
         }
-        
+
         @Override
         public Message invoke(Message msg) {
             
             // Get an entry
             String sqlDelete = null;
-            String id = (String)((Object[])msg.getBody())[0];
-            
+            String id = (String) ((Object[]) msg.getBody())[0];
+
             if (id == null) {
                 sqlDelete = "DELETE FROM " + this.table;
             } else {
                 sqlDelete = "DELETE FROM " + this.table + " WHERE ID = " + id;
             }
-            
+
             Connection connection = null;
             PreparedStatement deleteStatement = null;
             int result = -1;
-            
+
             try {
                 connection = JDBCHelper.getConnection(connectionInfo);
                 deleteStatement = connection.prepareStatement(sqlDelete);
                 result = deleteStatement.executeUpdate();
-                
-            } catch(SQLException sqle) {
+
+            } catch (SQLException sqle) {
                 msg.setFaultBody(new ServiceRuntimeException(sqle.getCause()));
             } catch (Exception e) {
                 msg.setFaultBody(new ServiceRuntimeException(e));
             } finally {
                 JDBCHelper.cleanupResources(connection, deleteStatement, null);
-            }            
-            
+            }
+
             msg.setBody(result);
-            return msg;            
+            return msg;
+        }
+    }
+    /**
+     * Get operation invoker
+     *
+     * @version $Rev$ $Date$
+     */
+    public static class GetDATAInvoker extends DATAInvoker {
+
+        public GetDATAInvoker(Operation operation, ConnectionInfo 
connectionInfo, String table) {
+            super(operation, connectionInfo, table);
+        }
+
+        @Override
+        public Message invoke(Message msg) {
+
+            // Get an entry
+            String sqlQuery = null;
+            String id = (String) ((Object[]) msg.getBody())[0];
+
+            if (id == null) {
+                sqlQuery = "SELECT * FROM " + this.table;
+            } else {
+                sqlQuery = "SELECT * FROM " + this.table + " WHERE ID = " + id;
+            }
+
+            Connection connection = null;
+            PreparedStatement queryStatement = null;
+            ResultSet resultSet = null;
+            try {
+                connection = JDBCHelper.getConnection(connectionInfo);
+                queryStatement = connection.prepareStatement(sqlQuery);
+                resultSet = queryStatement.executeQuery();
+
+
+            } catch (SQLException sqle) {
+                msg.setFaultBody(new ServiceRuntimeException(sqle.getCause()));
+                JDBCHelper.cleanupResources(connection, queryStatement, 
resultSet);
+            } catch (Exception e) {
+                msg.setFaultBody(new ServiceRuntimeException(e));
+                JDBCHelper.cleanupResources(connection, queryStatement, 
resultSet);
+            } finally {
+            //default we leave the connection open to pass to the 
JDBCStreamReader
+            }
+
+            msg.setBody(new JDBCResultSetStreamReader(resultSet));
+            return msg;
+        }
+    }
+
+    /**
+     * Insert operation invoker
+     */
+    public static class InsertDATAInvoker extends DATAInvoker {
+
+        public InsertDATAInvoker(Operation operation,
+                ConnectionInfo connectionInfo, String table) {
+            super(operation, connectionInfo, table);
+        }
+
+        @Override
+        public Message invoke(Message msg) throws IllegalArgumentException {
+            StringBuilder sqlInsert = new StringBuilder();
+            XMLStreamReader insertStream = (XMLStreamReader) ((Object[]) 
msg.getBody())[0];
+
+            if (insertStream == null) {
+                throw new IllegalArgumentException("The XMLStreamReader 
\"insertStream\" must not be null");
+            }
+
+
+            Connection connection = null;
+            PreparedStatement inStmt = null;
+
+            List<String> colNames = new ArrayList<String>();
+            List<String> values = new ArrayList<String>();
+
+            int result = 0;
+            try {
+
+                connection = JDBCHelper.getConnection(connectionInfo);
+
+                while (insertStream.hasNext()) {
+
+                    insertStream.next();
+                    if (insertStream.isStartElement()) {
+                        if (insertStream.getLocalName().equals("record")) {
+                            sqlInsert.append("INSERT INTO " + this.table + " 
(");
+                        } else if 
(insertStream.getLocalName().equals("column")) {
+                            colNames.add(insertStream.getAttributeValue(0));
+                            insertStream.next();
+                            if (insertStream.isCharacters()) {
+                                values.add(insertStream.getText());
+                            }
+                        }
+                    } else if (insertStream.isEndElement() && 
insertStream.getLocalName().equals("record")) {
+                        for (String c : colNames) {
+                            sqlInsert.append(" " + c + ",");
+                        }
+
+                        sqlInsert.deleteCharAt(sqlInsert.length() - 1);
+                        sqlInsert.append(" ) VALUES (");
+
+                        for (String v : values) {
+                            sqlInsert.append(" '" + v + "',");
+                        }
+
+                        sqlInsert.deleteCharAt(sqlInsert.length() - 1);
+                        sqlInsert.append(" )");
+
+                        inStmt = 
connection.prepareStatement(sqlInsert.toString());
+                        result += inStmt.executeUpdate();
+
+                        // Clean up resources
+                        inStmt.close();
+                        sqlInsert.delete(0, sqlInsert.length());
+                        values.clear();
+                        colNames.clear();
+                    }
+                }
+            } catch (XMLStreamException e) {
+                msg.setFaultBody(new ServiceRuntimeException(e));
+            } catch (SQLException sqle) {
+                sqle.printStackTrace();
+                msg.setFaultBody(new ServiceRuntimeException(sqle.getCause()));
+            } catch (Exception e) {
+                msg.setFaultBody(new ServiceRuntimeException(e));
+            } finally {
+                JDBCHelper.cleanupResources(connection, inStmt, null);
+            }
+
+            msg.setBody(result);
+            return msg;
+        }
+    }
+
+    /**
+     * Update operation invoker
+     */
+    public static class UpdateDATAInvoker extends DATAInvoker {
+
+        public UpdateDATAInvoker(Operation operation,
+                ConnectionInfo connectionInfo, String table) {
+            super(operation, connectionInfo, table);
+        }
+
+        @Override
+        public Message invoke(Message msg) throws IllegalArgumentException {
+
+            XMLStreamReader updateStream = (XMLStreamReader) ((Object[]) 
msg.getBody())[0];
+
+            if (updateStream == null) {
+                throw new IllegalArgumentException("The XMLStreamReader 
\"updateStream\" must not be null");
+            }
+
+            Connection connection = null;
+            PreparedStatement upStmt = null;
+
+            String id = null;
+            String columnName = null;
+            String newValue = null;
+            int result = 0;
+
+            try {
+                connection = JDBCHelper.getConnection(connectionInfo);
+                while (updateStream.hasNext()) {
+                    updateStream.next();
+
+                    if (updateStream.isStartElement() && 
updateStream.getLocalName().equals("column")) {
+                        columnName = updateStream.getAttributeValue(0);
+                        updateStream.next();
+                        if (updateStream.isCharacters()) {
+                            if (columnName.equals("ID")) {
+                                id = updateStream.getText();
+                            } else {
+                                newValue = updateStream.getText();
+
+                                upStmt = connection.prepareStatement("UPDATE " 
+ this.table + " SET " + columnName + " = '" + newValue + "' WHERE ID = " + id);
+
+                                result += upStmt.executeUpdate();
+                                upStmt.close();
+                            }
+                        }
+                    }
+                }
+            } catch (XMLStreamException e) {
+                msg.setFaultBody(new ServiceRuntimeException(e));
+            } catch (SQLException sqle) {
+                sqle.printStackTrace();
+                msg.setFaultBody(new ServiceRuntimeException(sqle.getCause()));
+            } catch (Exception e) {
+                msg.setFaultBody(new ServiceRuntimeException(e));
+            } finally {
+                JDBCHelper.cleanupResources(connection, upStmt, null);
+            }
+
+            msg.setBody(result);
+            return msg;
+        }
+    }
+
+    /**
+     * Delete operation invoker
+     */
+    public static class DeleteDATAInvoker extends DATAInvoker {
+
+        public DeleteDATAInvoker(Operation operation, ConnectionInfo 
connectionInfo, String table) {
+            super(operation, connectionInfo, table);
+        }
+
+        @Override
+        public Message invoke(Message msg) {
+
+            // Get an entry
+            String sqlDelete = null;
+            String id = (String) ((Object[]) msg.getBody())[0];
+
+            if (id == null) {
+                sqlDelete = "DELETE FROM " + this.table;
+            } else {
+                sqlDelete = "DELETE FROM " + this.table + " WHERE ID = " + id;
+            }
+
+            Connection connection = null;
+            PreparedStatement deleteStatement = null;
+            int result = -1;
+
+            try {
+                connection = JDBCHelper.getConnection(connectionInfo);
+                deleteStatement = connection.prepareStatement(sqlDelete);
+                result = deleteStatement.executeUpdate();
+
+            } catch (SQLException sqle) {
+                msg.setFaultBody(new ServiceRuntimeException(sqle.getCause()));
+            } catch (Exception e) {
+                msg.setFaultBody(new ServiceRuntimeException(e));
+            } finally {
+                JDBCHelper.cleanupResources(connection, deleteStatement, null);
+            }
+
+            msg.setBody(result);
+            return msg;
         }
     }
-    
 }

Modified: 
incubator/tuscany/java/sca/modules/implementation-data-xml/src/test/java/org/apache/tuscany/sca/implementation/data/DATATestCase.java
URL: 
http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/implementation-data-xml/src/test/java/org/apache/tuscany/sca/implementation/data/DATATestCase.java?rev=654554&r1=654553&r2=654554&view=diff
==============================================================================
--- 
incubator/tuscany/java/sca/modules/implementation-data-xml/src/test/java/org/apache/tuscany/sca/implementation/data/DATATestCase.java
 (original)
+++ 
incubator/tuscany/java/sca/modules/implementation-data-xml/src/test/java/org/apache/tuscany/sca/implementation/data/DATATestCase.java
 Thu May  8 08:32:34 2008
@@ -17,7 +17,7 @@
  * under the License.
  */
 
-package org.apache.tuscany.sca.implementation.data;
+package src.test.java.org.apache.tuscany.sca.implementation.data;
 
 import java.io.FileInputStream;
 
@@ -28,6 +28,7 @@
 
 import org.apache.tuscany.sca.databinding.xml.XMLStreamReader2String;
 import org.apache.tuscany.sca.host.embedded.SCADomain;
+import org.apache.tuscany.sca.implementation.data.DATA;
 
 /**
  * Tests the DAS service
@@ -44,7 +45,7 @@
     @Override
     protected void setUp() throws Exception {
         scaDomain = SCADomain.newInstance("data.composite");
-        dataService = scaDomain.getService(DATA.class, 
"DataComponent/COMPANY");
+        dataService = scaDomain.getService(DATA.class, 
"DataComponent/COMPANY_DATA");
     }
     
     /**


Reply via email to