Author: lresende
Date: Mon Mar  3 11:18:32 2008
New Revision: 633238

URL: http://svn.apache.org/viewvc?rev=633238&view=rev
Log:
TUSCANY-1917 - Applying patch from Douglas Leite. Thanks

Added:
    
incubator/tuscany/java/sca/modules/implementation-data-xml/src/test/resources/insert.xml
   (with props)
    
incubator/tuscany/java/sca/modules/implementation-data-xml/src/test/resources/update.xml
   (with props)
Modified:
    
incubator/tuscany/java/sca/modules/implementation-data-xml/src/main/java/org/apache/tuscany/sca/implementation/data/DATA.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/src/main/java/org/apache/tuscany/sca/implementation/data/DATA.java
URL: 
http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/implementation-data-xml/src/main/java/org/apache/tuscany/sca/implementation/data/DATA.java?rev=633238&r1=633237&r2=633238&view=diff
==============================================================================
--- 
incubator/tuscany/java/sca/modules/implementation-data-xml/src/main/java/org/apache/tuscany/sca/implementation/data/DATA.java
 (original)
+++ 
incubator/tuscany/java/sca/modules/implementation-data-xml/src/main/java/org/apache/tuscany/sca/implementation/data/DATA.java
 Mon Mar  3 11:18:32 2008
@@ -22,25 +22,43 @@
 
 /**
  * The service interface of a DAS service provided by DAS components.
- *
+ * 
  * @version $Rev$ $Date$
  */
 public interface DATA {
-    
-    /**
-     * Retrieve the Database table contents
-     * If a id is given, the results will be filtered to a matching row
-     * @param id The PK that identifies the row on the table
-     * @return The row content in XML format
-     */
-    XMLStreamReader get(String id);
-    
-    /**
-     * Delete the Database table contents
-     * If a id is given, only a specific row will be deleted
-     * @param id The PK that identifies the row on the table
-     * @return The number of rows affected
-     */
-    int delete(String id);
-    
+
+       /**
+        * Retrieve the Database table contents If a id is given, the results 
will
+        * be filtered to a matching row
+        * 
+        * @param id The PK that identifies the row on the table
+        * @return The row content in XML format
+        */
+       XMLStreamReader get(String id);
+
+       /**
+        * Insert new content in the Database
+        * 
+        * @param insertStream The insertion in XML format
+        * @return The number of rows affected
+        */
+       int insert(XMLStreamReader insertStream);
+
+       /**
+        * Update the Database table contents
+        * 
+        * @param updateStream The updates in XML format
+        * @return The number of rows affected
+        */
+       int update(XMLStreamReader updateStream);
+
+       /**
+        * Delete the Database table contents If a id is given, only a specific 
row
+        * will be deleted
+        * 
+        * @param id The PK that identifies the row on the table
+        * @return The number of rows affected
+        */
+       int delete(String id);
+
 }

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=633238&r1=633237&r2=633238&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
 Mon Mar  3 11:18:32 2008
@@ -31,14 +31,14 @@
  * @version $Rev$ $Date$
  */
 public class DATAImplementationProvider implements ImplementationProvider {
-    private RuntimeComponent component;
+    //private RuntimeComponent component;
     private DATAImplementation implementation;
 
     /**
      * Constructs a new DATA implementation.
      */
     public DATAImplementationProvider(RuntimeComponent component, 
DATAImplementation implementation) {
-        this.component = component;
+        //this.component = component;
         this.implementation = implementation;
     }
 
@@ -48,8 +48,11 @@
 
         if (operationName.equals("get")) {
             return new DATAInvoker.GetInvoker(operation, 
implementation.getConnectionInfo(), tableName);
-        }      
-        else if (operationName.equals("delete")) {
+        } 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);
         }
 

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=633238&r1=633237&r2=633238&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
 Mon Mar  3 11:18:32 2008
@@ -23,6 +23,11 @@
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamReader;
 
 import org.apache.tuscany.sca.data.engine.config.ConnectionInfo;
 import org.apache.tuscany.sca.implementation.data.jdbc.JDBCHelper;
@@ -115,11 +120,166 @@
             msg.setBody(new JDBCResultSetStreamReader(resultSet));
             return msg;
         }
-    }    
+    }   
+    
     
+
     /**
-     * Delete operation invoker
-     */
+        * 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;
+               }
+       }
+       
+    
+    /**
+        * Delete operation invoker
+        */
     public static class DeleteInvoker extends DATAInvoker {
         
         public DeleteInvoker(Operation operation, ConnectionInfo 
connectionInfo, String table) {
@@ -130,30 +290,30 @@
         public Message invoke(Message msg) {
             
             // Get an entry
-            String sqlQuery = null;
+            String sqlDelete = null;
             String id = (String)((Object[])msg.getBody())[0];
             
             if (id == null) {
-                sqlQuery = "DELETE FROM " + this.table;
+                sqlDelete = "DELETE FROM " + this.table;
             } else {
-                sqlQuery = "DELETE FROM " + this.table + " WHERE ID = " + id;
+                sqlDelete = "DELETE FROM " + this.table + " WHERE ID = " + id;
             }
             
             Connection connection = null;
-            PreparedStatement queryStatement = null;
+            PreparedStatement deleteStatement = null;
             int result = -1;
             
             try {
                 connection = JDBCHelper.getConnection(connectionInfo);
-                queryStatement = connection.prepareStatement(sqlQuery);
-                result = queryStatement.executeUpdate();
+                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, queryStatement, null);
+                JDBCHelper.cleanupResources(connection, deleteStatement, null);
             }            
             
             msg.setBody(result);

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=633238&r1=633237&r2=633238&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
 Mon Mar  3 11:18:32 2008
@@ -1,91 +1,132 @@
-/*
- * 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.sca.implementation.data;
-
-import javax.xml.stream.XMLStreamReader;
-
-import junit.framework.TestCase;
-
-import org.apache.tuscany.sca.databinding.xml.XMLStreamReader2String;
-import org.apache.tuscany.sca.host.embedded.SCADomain;
-
-/**
- * Tests the DAS service
- *
- * @version $Rev$ $Date$
- */
-public class DATATestCase extends TestCase {
-    private SCADomain scaDomain;
-    private DATA dataService;
-    
-    /**
-     * @throws java.lang.Exception
-     */
-    @Override
-    protected void setUp() throws Exception {
-        scaDomain = SCADomain.newInstance("data.composite");
-        dataService = scaDomain.getService(DATA.class, 
"DataComponent/COMPANY");
-    }
-    
-    /**
-     * @throws java.lang.Exception
-     */
-    @Override
-    protected void tearDown() throws Exception {
-        scaDomain.close();
-    }
-    
-    public void testDeleteByID() throws Exception {
-        System.out.println(">testDeleteByID");
-        
-        Integer companyID = new Integer(52);
-        int dRows = dataService.delete(companyID.toString());
-        System.out.println("Deleted rows: "+dRows);
-    }
-    
-    public void testGet() throws Exception {
-        System.out.println(">testGet");
-        
-        XMLStreamReader reader = dataService.get(null);
-        String xml = new XMLStreamReader2String().transform(reader, null);
-        System.out.println(xml);
-        reader.close();
-    }
-    
-    public void testGetByID() throws Exception {
-        System.out.println(">testGetByID");
-        
-        Integer companyID = new Integer(51);
-        
-        XMLStreamReader reader = dataService.get(companyID.toString());
-        assertNotNull(reader);
-        String xml = new XMLStreamReader2String().transform(reader, null);
-        System.out.println(xml);
-        reader.close();
-    }
-    
-    public void testDelete() throws Exception {
-        System.out.println(">testDelete");
-        
-        int dRows = dataService.delete(null);
-        System.out.println("Deleted rows: "+dRows);
-    }
-    
-}
+/*
+ * 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.sca.implementation.data;
+
+import java.io.FileInputStream;
+
+import javax.xml.stream.XMLInputFactory;
+import javax.xml.stream.XMLStreamReader;
+
+import junit.framework.TestCase;
+
+import org.apache.tuscany.sca.databinding.xml.XMLStreamReader2String;
+import org.apache.tuscany.sca.host.embedded.SCADomain;
+
+/**
+ * Tests the DAS service
+ *
+ * @version $Rev$ $Date$
+ */
+public class DATATestCase extends TestCase {
+    private SCADomain scaDomain;
+    private DATA dataService;
+    
+    /**
+     * @throws java.lang.Exception
+     */
+    @Override
+    protected void setUp() throws Exception {
+        scaDomain = SCADomain.newInstance("data.composite");
+        dataService = scaDomain.getService(DATA.class, 
"DataComponent/COMPANY");
+    }
+    
+    /**
+     * @throws java.lang.Exception
+     */
+    @Override
+    protected void tearDown() throws Exception {
+        scaDomain.close();
+    }
+    
+    public void testInsert() throws Exception {
+        System.out.println(">testInsert");
+
+        //Read and process the XML file
+        FileInputStream fileInputStream = new 
FileInputStream("src/test/resources/insert.xml");
+        XMLStreamReader reader = 
XMLInputFactory.newInstance().createXMLStreamReader(fileInputStream);
+
+        int result = dataService.insert(reader);
+        assertEquals(result,2);
+
+        System.out.println("Number of rows inserted: "+result);
+
+        reader.close();
+    }
+
+    public void testGet() throws Exception {
+        
+        System.out.println(">testGet");
+        
+        XMLStreamReader reader = dataService.get(null);
+        assertNotNull(reader);
+        String xml = new XMLStreamReader2String().transform(reader, null);
+        System.out.println(xml);
+        reader.close();
+    }
+
+    public void testUpdate() throws Exception {
+
+        System.out.println(">testUpdate");
+
+        //Read and process the XML file
+        FileInputStream fileInputStream = new 
FileInputStream("src/test/resources/update.xml");
+        XMLStreamReader reader = 
XMLInputFactory.newInstance().createXMLStreamReader(fileInputStream);
+
+        int result = dataService.update(reader);
+        assertEquals(result,1);
+        System.out.println("Number of rows affected: "+result);
+
+        reader.close();
+    }
+
+    public void testGetByID() throws Exception {
+        System.out.println(">testGetByID");
+
+        Integer companyID = new Integer(4);
+
+        XMLStreamReader reader = dataService.get(companyID.toString());
+        assertNotNull(reader);
+        String xml = new XMLStreamReader2String().transform(reader, null);
+        System.out.println(xml);
+        reader.close();
+    }
+
+    public void testDeleteByID() throws Exception {
+        System.out.println(">testDeleteByID");
+
+        Integer companyID = new Integer(4);
+        int result = dataService.delete(companyID.toString());
+        assertEquals(result,1);
+        System.out.println("Number of rows deleted: "+result);
+    }
+
+    public void testDelete() throws Exception {
+        System.out.println(">testDelete");
+
+        int result = dataService.delete(null);
+        assertEquals(result,4);
+        System.out.println("Number of rows deleted: "+result);
+
+        System.out.println("recreating database...");
+        //Helper.createDB();
+        System.out.println("done!");
+    }
+    
+}

Added: 
incubator/tuscany/java/sca/modules/implementation-data-xml/src/test/resources/insert.xml
URL: 
http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/implementation-data-xml/src/test/resources/insert.xml?rev=633238&view=auto
==============================================================================
--- 
incubator/tuscany/java/sca/modules/implementation-data-xml/src/test/resources/insert.xml
 (added)
+++ 
incubator/tuscany/java/sca/modules/implementation-data-xml/src/test/resources/insert.xml
 Mon Mar  3 11:18:32 2008
@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<resultSet>
+    <record>
+        <column name="NAME">New Coorporation I</column>
+    </record>
+    <record>
+        <column name="NAME">New Coorporation II</column>
+    </record>
+</resultSet>

Propchange: 
incubator/tuscany/java/sca/modules/implementation-data-xml/src/test/resources/insert.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
incubator/tuscany/java/sca/modules/implementation-data-xml/src/test/resources/insert.xml
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Propchange: 
incubator/tuscany/java/sca/modules/implementation-data-xml/src/test/resources/insert.xml
------------------------------------------------------------------------------
    svn:mime-type = text/xml

Added: 
incubator/tuscany/java/sca/modules/implementation-data-xml/src/test/resources/update.xml
URL: 
http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/implementation-data-xml/src/test/resources/update.xml?rev=633238&view=auto
==============================================================================
--- 
incubator/tuscany/java/sca/modules/implementation-data-xml/src/test/resources/update.xml
 (added)
+++ 
incubator/tuscany/java/sca/modules/implementation-data-xml/src/test/resources/update.xml
 Mon Mar  3 11:18:32 2008
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<result_set>
+    <record>
+        <column name="ID">4</column>
+        <column name="NAME">Update Coorporation</column>
+    </record>
+</result_set>

Propchange: 
incubator/tuscany/java/sca/modules/implementation-data-xml/src/test/resources/update.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
incubator/tuscany/java/sca/modules/implementation-data-xml/src/test/resources/update.xml
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Propchange: 
incubator/tuscany/java/sca/modules/implementation-data-xml/src/test/resources/update.xml
------------------------------------------------------------------------------
    svn:mime-type = text/xml



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

Reply via email to