Author: lresende
Date: Mon Nov 19 17:21:46 2007
New Revision: 596507

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

Modified:
    
incubator/tuscany/java/sca/modules/implementation-data/src/main/java/org/apache/tuscany/sca/implementation/data/DATA.java
    
incubator/tuscany/java/sca/modules/implementation-data/src/main/java/org/apache/tuscany/sca/implementation/data/jdbc/JDBCHelper.java
    
incubator/tuscany/java/sca/modules/implementation-data/src/main/java/org/apache/tuscany/sca/implementation/data/provider/DATAImplementationProvider.java
    
incubator/tuscany/java/sca/modules/implementation-data/src/main/java/org/apache/tuscany/sca/implementation/data/provider/DATAInvoker.java
    
incubator/tuscany/java/sca/modules/implementation-data/src/test/java/org/apache/tuscany/sca/implementation/data/DATATestCase.java
    
incubator/tuscany/java/sca/modules/implementation-data/src/test/resources/data.composite

Modified: 
incubator/tuscany/java/sca/modules/implementation-data/src/main/java/org/apache/tuscany/sca/implementation/data/DATA.java
URL: 
http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/implementation-data/src/main/java/org/apache/tuscany/sca/implementation/data/DATA.java?rev=596507&r1=596506&r2=596507&view=diff
==============================================================================
--- 
incubator/tuscany/java/sca/modules/implementation-data/src/main/java/org/apache/tuscany/sca/implementation/data/DATA.java
 (original)
+++ 
incubator/tuscany/java/sca/modules/implementation-data/src/main/java/org/apache/tuscany/sca/implementation/data/DATA.java
 Mon Nov 19 17:21:46 2007
@@ -6,15 +6,15 @@
  * 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.    
+ * under the License.
  */
 package org.apache.tuscany.sca.implementation.data;
 
@@ -22,7 +22,7 @@
 
 /**
  * The service interface of a DAS service provided by DAS components.
- * 
+ *
  * @version $Rev$ $Date$
  */
 public interface DATA {
@@ -35,5 +35,12 @@
      */
     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);
+    
 }

Modified: 
incubator/tuscany/java/sca/modules/implementation-data/src/main/java/org/apache/tuscany/sca/implementation/data/jdbc/JDBCHelper.java
URL: 
http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/implementation-data/src/main/java/org/apache/tuscany/sca/implementation/data/jdbc/JDBCHelper.java?rev=596507&r1=596506&r2=596507&view=diff
==============================================================================
--- 
incubator/tuscany/java/sca/modules/implementation-data/src/main/java/org/apache/tuscany/sca/implementation/data/jdbc/JDBCHelper.java
 (original)
+++ 
incubator/tuscany/java/sca/modules/implementation-data/src/main/java/org/apache/tuscany/sca/implementation/data/jdbc/JDBCHelper.java
 Mon Nov 19 17:21:46 2007
@@ -141,8 +141,10 @@
             if(connection == null){
                 throw new DataSourceInitializationException("Error 
initializing connection : null");
             }
-
-            connection.setAutoCommit(false);
+            
+            //FIXME we should make this flexible, we can't autocommit when 
participating in transactions
+            connection.setAutoCommit(true);
+            
         }catch(ClassNotFoundException cnf){
             throw new DataSourceInitializationException("JDBC Driver '" + 
connectionInfo.getConnectionProperties().getDriverClass() + "' not found", cnf);
         }catch(SQLException sqle){

Modified: 
incubator/tuscany/java/sca/modules/implementation-data/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/src/main/java/org/apache/tuscany/sca/implementation/data/provider/DATAImplementationProvider.java?rev=596507&r1=596506&r2=596507&view=diff
==============================================================================
--- 
incubator/tuscany/java/sca/modules/implementation-data/src/main/java/org/apache/tuscany/sca/implementation/data/provider/DATAImplementationProvider.java
 (original)
+++ 
incubator/tuscany/java/sca/modules/implementation-data/src/main/java/org/apache/tuscany/sca/implementation/data/provider/DATAImplementationProvider.java
 Mon Nov 19 17:21:46 2007
@@ -48,6 +48,9 @@
 
         if (operationName.equals("get")) {
             return new DATAInvoker.GetInvoker(operation, 
implementation.getConnectionInfo(), tableName);
+        }      
+        else if (operationName.equals("delete")) {
+            return new DATAInvoker.DeleteInvoker(operation, 
implementation.getConnectionInfo(), tableName);
         }
 
         return new DATAInvoker(operation, implementation.getConnectionInfo(), 
tableName);

Modified: 
incubator/tuscany/java/sca/modules/implementation-data/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/src/main/java/org/apache/tuscany/sca/implementation/data/provider/DATAInvoker.java?rev=596507&r1=596506&r2=596507&view=diff
==============================================================================
--- 
incubator/tuscany/java/sca/modules/implementation-data/src/main/java/org/apache/tuscany/sca/implementation/data/provider/DATAInvoker.java
 (original)
+++ 
incubator/tuscany/java/sca/modules/implementation-data/src/main/java/org/apache/tuscany/sca/implementation/data/provider/DATAInvoker.java
 Mon Nov 19 17:21:46 2007
@@ -6,15 +6,15 @@
  * 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.    
+ * under the License.
  */
 
 package org.apache.tuscany.sca.implementation.data.provider;
@@ -35,11 +35,11 @@
 
 /**
  * Implements a target invoker for DAS component implementations.
- * 
+ *
  * The target invoker is responsible for dispatching invocations to the 
particular
  * component implementation logic. The current component implementation will
  * dispatch calls to the DAS apis to retrieve the requested data from the 
backend store
- * 
+ *
  * @version $Rev$ $Date$
  */
 public class DATAInvoker implements Invoker {
@@ -54,7 +54,7 @@
     }
     
     public Message invoke(Message msg) {
-        // Shouldn't get here, as the only supported operations 
+        // Shouldn't get here, as the only supported operations
         // are the ones defined DATA interface and implemented
         // by specific invoker subclasses
         
@@ -63,23 +63,23 @@
     
     
     /****************************************************************
-     * 
+     *
      * Internal invoker implementations for each supported operation
-     * 
+     *
      *****************************************************************/
     
     
     /**
      * 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) {
             
@@ -88,11 +88,11 @@
             String id = (String)((Object[])msg.getBody())[0];
             
             if (id == null) {
-                sqlQuery = "SELECT * FROM " + this.table.toUpperCase();
+                sqlQuery = "SELECT * FROM " + this.table;
             } else {
-                sqlQuery = "SELECT * FROM " + this.table.toUpperCase() + " 
WHERE ID = " + id;
+                sqlQuery = "SELECT * FROM " + this.table + " WHERE ID = " + id;
             }
-
+            
             Connection connection = null;
             PreparedStatement queryStatement = null;
             ResultSet resultSet = null;
@@ -101,7 +101,7 @@
                 queryStatement = connection.prepareStatement(sqlQuery);
                 resultSet = queryStatement.executeQuery();
                 
-
+                
             } catch(SQLException sqle) {
                 msg.setFaultBody(new ServiceRuntimeException(sqle.getCause()));
                 JDBCHelper.cleanupResources(connection, queryStatement, 
resultSet);
@@ -111,9 +111,53 @@
             } finally {
                 //default we leave the connection open to pass to the 
JDBCStreamReader
             }
-
+            
             msg.setBody(new JDBCResultSetStreamReader(resultSet));
             return msg;
+        }
+    }    
+    
+    /**
+     * 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 sqlQuery = null;
+            String id = (String)((Object[])msg.getBody())[0];
+            
+            if (id == null) {
+                sqlQuery = "DELETE FROM " + this.table;
+            } else {
+                sqlQuery = "DELETE FROM " + this.table + " WHERE ID = " + id;
+            }
+            
+            Connection connection = null;
+            PreparedStatement queryStatement = null;
+            int result = -1;
+            
+            try {
+                connection = JDBCHelper.getConnection(connectionInfo);
+                queryStatement = connection.prepareStatement(sqlQuery);
+                result = queryStatement.executeUpdate();
+                
+            } catch(SQLException sqle) {
+                msg.setFaultBody(new ServiceRuntimeException(sqle.getCause()));
+            } catch (Exception e) {
+                msg.setFaultBody(new ServiceRuntimeException(e));
+            } finally {
+                JDBCHelper.cleanupResources(connection, queryStatement, null);
+            }            
+            
+            msg.setBody(result);
+            return msg;            
         }
     }
     

Modified: 
incubator/tuscany/java/sca/modules/implementation-data/src/test/java/org/apache/tuscany/sca/implementation/data/DATATestCase.java
URL: 
http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/implementation-data/src/test/java/org/apache/tuscany/sca/implementation/data/DATATestCase.java?rev=596507&r1=596506&r2=596507&view=diff
==============================================================================
--- 
incubator/tuscany/java/sca/modules/implementation-data/src/test/java/org/apache/tuscany/sca/implementation/data/DATATestCase.java
 (original)
+++ 
incubator/tuscany/java/sca/modules/implementation-data/src/test/java/org/apache/tuscany/sca/implementation/data/DATATestCase.java
 Mon Nov 19 17:21:46 2007
@@ -6,15 +6,15 @@
  * 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.    
+ * under the License.
  */
 
 package org.apache.tuscany.sca.implementation.data;
@@ -28,7 +28,7 @@
 
 /**
  * Tests the DAS service
- * 
+ *
  * @version $Rev$ $Date$
  */
 public class DATATestCase extends TestCase {
@@ -43,7 +43,7 @@
         scaDomain = SCADomain.newInstance("data.composite");
         dataService = scaDomain.getService(DATA.class, 
"DataComponent/COMPANY");
     }
-
+    
     /**
      * @throws java.lang.Exception
      */
@@ -51,9 +51,17 @@
     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");
+        System.out.println(">testGet");
         
         XMLStreamReader reader = dataService.get(null);
         String xml = new XMLStreamReader2String().transform(reader, null);
@@ -62,13 +70,22 @@
     }
     
     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();        
+        reader.close();
     }
-
+    
+    public void testDelete() throws Exception {
+        System.out.println(">testDelete");
+        
+        int dRows = dataService.delete(null);
+        System.out.println("Deleted rows: "+dRows);
+    }
+    
 }

Modified: 
incubator/tuscany/java/sca/modules/implementation-data/src/test/resources/data.composite
URL: 
http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/implementation-data/src/test/resources/data.composite?rev=596507&r1=596506&r2=596507&view=diff
==============================================================================
--- 
incubator/tuscany/java/sca/modules/implementation-data/src/test/resources/data.composite
 (original)
+++ 
incubator/tuscany/java/sca/modules/implementation-data/src/test/resources/data.composite
 Mon Nov 19 17:21:46 2007
@@ -18,8 +18,8 @@
     * under the License.    
 -->
 <composite xmlns="http://www.osoa.org/xmlns/sca/1.0";
-    xmlns:tuscany="http://tuscany.apache.org/xmlns/sca/1.0";
-       targetNamespace="http://data";
+    xmlns:tuscany="http://tuscany.apache.org/xmlns/sca/1.0";
+       targetNamespace="http://data";
        name="data">
 
     <component name="DataComponent">



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

Reply via email to