summaryrefslogtreecommitdiffstats
path: root/sandbox/lresende/backup/container.das/src/main/java/org/apache/tuscany/container/dataaccessscript
diff options
context:
space:
mode:
authordims <dims@13f79535-47bb-0310-9956-ffa450edef68>2008-06-17 00:23:01 +0000
committerdims <dims@13f79535-47bb-0310-9956-ffa450edef68>2008-06-17 00:23:01 +0000
commitbdd0a41aed7edf21ec2a65cfa17a86af2ef8c48a (patch)
tree38a92061c0793434c4be189f1d70c3458b6bc41d /sandbox/lresende/backup/container.das/src/main/java/org/apache/tuscany/container/dataaccessscript
Move Tuscany from Incubator to top level.
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@668359 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'sandbox/lresende/backup/container.das/src/main/java/org/apache/tuscany/container/dataaccessscript')
-rw-r--r--sandbox/lresende/backup/container.das/src/main/java/org/apache/tuscany/container/dataaccessscript/CommandMapper.java44
-rw-r--r--sandbox/lresende/backup/container.das/src/main/java/org/apache/tuscany/container/dataaccessscript/DataAccessEngine.java65
-rw-r--r--sandbox/lresende/backup/container.das/src/main/java/org/apache/tuscany/container/dataaccessscript/DataAccessInstanceImpl.java328
-rw-r--r--sandbox/lresende/backup/container.das/src/main/java/org/apache/tuscany/container/dataaccessscript/DataAccessUtils.java90
4 files changed, 527 insertions, 0 deletions
diff --git a/sandbox/lresende/backup/container.das/src/main/java/org/apache/tuscany/container/dataaccessscript/CommandMapper.java b/sandbox/lresende/backup/container.das/src/main/java/org/apache/tuscany/container/dataaccessscript/CommandMapper.java
new file mode 100644
index 0000000000..9e8565b507
--- /dev/null
+++ b/sandbox/lresende/backup/container.das/src/main/java/org/apache/tuscany/container/dataaccessscript/CommandMapper.java
@@ -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.container.dataaccessscript;
+
+/**
+ * This class maps service APIs with the DAS config commands
+ * the logic can be more of some code generation tool type
+ * At present a simple mapping is introduced
+ */
+public class CommandMapper {
+
+ public static String getCommandName(String serviceAPIName) throws Exception{
+ int idx = serviceAPIName.indexOf("get");
+ if(idx != -1){
+ return serviceAPIName.substring(idx+3);
+ }
+ else{
+ //TODO better way to handle
+ throw new Exception("invalid service API name");
+ }
+ }
+
+ public static String getServiceAPIName(String commandName){
+ //TODO to be implemented
+ return null;
+ }
+}
+
diff --git a/sandbox/lresende/backup/container.das/src/main/java/org/apache/tuscany/container/dataaccessscript/DataAccessEngine.java b/sandbox/lresende/backup/container.das/src/main/java/org/apache/tuscany/container/dataaccessscript/DataAccessEngine.java
new file mode 100644
index 0000000000..1302315eeb
--- /dev/null
+++ b/sandbox/lresende/backup/container.das/src/main/java/org/apache/tuscany/container/dataaccessscript/DataAccessEngine.java
@@ -0,0 +1,65 @@
+/*
+ * 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.container.dataaccessscript;
+
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+
+import org.apache.tuscany.das.rdb.DAS;
+
+public class DataAccessEngine {
+ public final static String SEPARATOR = ":";
+ //For RDB
+ public final static String RDB = "rdb";
+ public final static String SELECT = "select";
+ public final static String DELETE = "delete";
+ public final static String UPDATE = "update";
+ public final static String INSERT = "insert";
+ public final static String PROCEDURE = "procedure";
+
+ //For XQUERY
+ public final static String XQUERY = "xquery";
+ //...
+ //TODO...this is the place to accomodate different data access mechanisms
+
+ public final static String DERBY = "derby";
+ public final static String MySQL = "mysql";
+ //...
+ //TODO ...this is the place to accomodate different databases
+
+ public DAS getDAS(String dataAccessType, String dasConfig){
+ if(dataAccessType.equals(RDB)){
+ return DAS.FACTORY.createDAS(new ByteArrayInputStream(dasConfig.getBytes()));//i18n? //do you need cl information here
+ }
+ else{
+ //TODO ..for different mechs - need to implement
+ return null;
+ }
+ }
+
+ public DAS getDAS(String dataAccessType, InputStream dasConfigStream){
+ if(dataAccessType.equals(RDB)){
+ return DAS.FACTORY.createDAS(dasConfigStream);//i18n?
+ }
+ else{
+ //TODO ..for different mechs - need to implement
+ return null;
+ }
+ }
+}
diff --git a/sandbox/lresende/backup/container.das/src/main/java/org/apache/tuscany/container/dataaccessscript/DataAccessInstanceImpl.java b/sandbox/lresende/backup/container.das/src/main/java/org/apache/tuscany/container/dataaccessscript/DataAccessInstanceImpl.java
new file mode 100644
index 0000000000..22300b4e56
--- /dev/null
+++ b/sandbox/lresende/backup/container.das/src/main/java/org/apache/tuscany/container/dataaccessscript/DataAccessInstanceImpl.java
@@ -0,0 +1,328 @@
+/*
+ * 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.container.dataaccessscript;
+
+import java.util.Hashtable;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Vector;
+
+import org.apache.tuscany.container.dataaccess.DataAccessInstance;
+import org.apache.tuscany.das.rdb.Command;
+import org.apache.tuscany.das.rdb.DAS;
+
+import commonj.sdo.DataObject;
+/**
+ * An invokeable instance of a DataAccess config script.
+ */
+public class DataAccessInstanceImpl implements DataAccessInstance {
+
+ private DAS dasInstance;
+ private String dataAccessType;
+ private String tableName = null;
+ private String genericSelectForRoot = "select * from "+ tableName;
+
+ public DataAccessInstanceImpl(DAS das, String dataAccessType){
+ this.dasInstance = das;
+ this.dataAccessType = dataAccessType;
+ }
+
+ public Object invokeFunction(String functionName, Object[] args, Class returnType) {
+ Command cmd = null;
+ //System.out.println("functionName:"+functionName);
+
+ Vector paramsList = null;
+ if(args != null){
+ try{
+ paramsList = (Vector)args[0];
+ }catch(ClassCastException e){
+ //TODO
+ e.printStackTrace();
+ }
+ }
+
+ //if any of the 3 below - its dynamic way , else , its static way
+ if(! "execute".equals(functionName) &&
+ ! "executeQuery".equals(functionName) &&
+ ! "applyChanges".equals(functionName)){
+ //try for static way
+ try{
+ String commandName = CommandMapper.getCommandName(functionName);
+ //System.out.println("commandName:"+commandName);
+ if(paramsList != null && paramsList.size()>1 &&
+ paramsList.get(1).toString().indexOf(DataAccessEngine.SEPARATOR) == -1){
+ cmd = dasInstance.getCommand(commandName);//got in static way
+ }
+ }catch(Exception e){
+ //TODO
+ e.printStackTrace();//if command not found in config or other exception
+ }
+ }
+ else{//dynamic way
+ //upto below statement is generic DAS..but select,etc..are particular
+ //to RDBDAS
+ //TODO if no matching command is found
+ if(!((String)paramsList.get(0)).equals("")){
+ cmd = dasInstance.getCommand((String)paramsList.get(0));//got in dynamic way
+ }else{
+ //this can be insert or delete as applyChanges-dynamic way
+ }
+ }
+
+ //TODO handle better
+ //this is not error case - as can be case for dynamic/static approach with applyChanges(), where command is
+ //obtained for generic select at later point. So, don't throw exception
+ /*if(cmd == null){
+ Exception e = new Exception("No command could be found!");
+ e.printStackTrace();
+ }*/
+
+ //Particular to RDBDAS
+ if(this.dataAccessType.equals(DataAccessEngine.RDB)){
+ DataObject dataObj = null;
+ dataObj = invokeRDBDASFunction(cmd, paramsList);
+ return dataObj;
+ }
+
+ //TODO ...implement for other than RDB
+ return null;
+ }
+
+ private DataObject invokeRDBDASSelect(Command cmd, Vector paramsList){
+ DataObject dObject = null;
+
+ if(paramsList != null && paramsList.size()>=3){
+ Map<Integer, Object> inParams = new Hashtable<Integer, Object>();
+ inParams = (Map<Integer, Object>)paramsList.get(2);
+ DataAccessUtils.fromJavaToRDBDASPositionBased(inParams, cmd);
+ }
+ dObject = cmd.executeQuery();
+ if(dObject == null){
+ System.out.println("null result returned in select");
+ }
+ //dasInstance.releaseResources();
+ return dObject;
+ }
+
+ private void invokeRDBDASDeleteExecute(Command cmd, Vector paramsList){
+ if(paramsList != null && paramsList.size()>=3){
+ Map<Integer, Object> inParams = new Hashtable<Integer, Object>();
+ inParams = (Map<Integer, Object>)paramsList.get(2);
+ DataAccessUtils.fromJavaToRDBDASPositionBased(inParams, cmd);
+ }
+ cmd.execute();
+ //dasInstance.releaseResources();
+ }
+
+ private void invokeRDBDASDeleteApplyChanges(Command cmd, Vector paramsList){
+ if(paramsList != null && paramsList.size()>=3){
+ String paramsTableName = paramsList.get(1).toString();
+ int idx = paramsTableName.indexOf(DataAccessEngine.SEPARATOR);
+ tableName = paramsTableName.substring(idx+1);
+ genericSelectForRoot = "select * from "+ tableName;
+ //System.out.println("genericSelectForRoot:"+genericSelectForRoot);
+ cmd = dasInstance.createCommand(genericSelectForRoot);
+
+ //set WHERE params
+ if(paramsList.get(2) instanceof Map){
+ Map<String, Object> whereParams = (Map<String, Object>)paramsList.get(2);
+
+ String whereStr = null;
+ //TODO needs better way
+ if(whereParams != null && whereParams.size()> 0){
+ for (Iterator i = whereParams.keySet().iterator(); i.hasNext();) {
+ String idxDel = (String)i.next();
+
+ Object val = whereParams.get(idxDel);
+ whereStr = tableName+"["+val+"]";
+ //System.out.println("whereStr:"+whereStr);
+ }
+ DataObject root = cmd.executeQuery();
+ DataObject dObj = root.getDataObject(whereStr);
+ dObj.delete();
+ dasInstance.applyChanges(root);
+ }
+ }
+ }
+
+ //dasInstance.releaseResources();
+ }
+
+ private void invokeRDBDASInsertExecute(Command cmd, Vector paramsList){
+ if(paramsList != null && paramsList.size()>=3){
+ Map<Integer, Object> inParams = new Hashtable<Integer, Object>();
+ inParams = (Map<Integer, Object>)paramsList.get(2);
+ DataAccessUtils.fromJavaToRDBDASPositionBased(inParams, cmd);
+ }
+ //System.out.println("before execute insert");
+
+ cmd.execute();
+ //dasInstance.releaseResources();
+ }
+
+ private void invokeRDBDASInsertApplyChanges(Command cmd, Vector paramsList){
+ if(paramsList != null && paramsList.size()>=3){
+ String paramsTableName = paramsList.get(1).toString();
+ int idx = paramsTableName.indexOf(DataAccessEngine.SEPARATOR);
+ tableName = paramsTableName.substring(idx+1);
+ genericSelectForRoot = "select * from "+ tableName;
+ //System.out.println("genericSelectForRoot:"+genericSelectForRoot);
+ cmd = dasInstance.createCommand(genericSelectForRoot);
+
+ //set INSERT params
+ if(paramsList.get(2) instanceof Map){
+ Map<String, Object> insertParams = (Map<String, Object>)paramsList.get(2);
+ DataObject root = cmd.executeQuery();
+ DataObject insertObj = root.createDataObject(tableName);
+
+ if(insertParams != null && insertParams.size()> 0){
+ for (Iterator i = insertParams.keySet().iterator(); i.hasNext();) {
+ String idxIns = (String)i.next();
+
+ Object val = insertParams.get(idxIns);
+ //System.out.println(idxIns+","+val+val.getClass().getName());
+ insertObj.set(idxIns, val);
+ }
+
+ }
+
+ dasInstance.applyChanges(root);
+ }
+ }
+ //dasInstance.releaseResources();
+ }
+
+ private void invokeRDBDASUpdate(Command cmd, Vector paramsList){
+ if(paramsList != null && paramsList.size()>=4){
+ String paramsTableName = paramsList.get(1).toString();
+ int idx = paramsTableName.indexOf(DataAccessEngine.SEPARATOR);
+ tableName = paramsTableName.substring(idx+1);
+ genericSelectForRoot = "select * from "+ tableName;
+ //System.out.println("genericSelectForRoot:"+genericSelectForRoot);
+ cmd = dasInstance.createCommand(genericSelectForRoot);
+
+ //set UPD and WHERE params
+ if(paramsList.get(2) instanceof Map){
+ Map<String, Object> updParams = (Map<String, Object>)paramsList.get(2);
+
+ if(paramsList.get(3) instanceof Map){
+ Map<String, Object> whereParams = (Map<String, Object>)paramsList.get(3);
+ DataObject root = DataAccessUtils.fromJavaToRDBDASNameBased(updParams, whereParams, cmd, tableName);
+ dasInstance.applyChanges(root);
+ }
+ }
+ }
+ //dasInstance.releaseResources();
+ }
+
+ private DataObject invokeRDBDASProcedure(Command cmd, Vector paramsList){
+ DataObject dataObj = null;
+ if(paramsList != null && paramsList.size()>=3){
+ //set IN params
+ if(paramsList.get(2) instanceof Map){
+ Map<Integer, Object> inParams = (Map<Integer, Object>)paramsList.get(2);
+ DataAccessUtils.fromJavaToRDBDASPositionBased(inParams, cmd);
+ }
+
+ dataObj = cmd.executeQuery();
+
+ if(paramsList.size()>=4 && paramsList.get(3) instanceof Map){
+ try{
+ Map<Integer, Object> outParams = (Map<Integer, Object>)paramsList.get(3);
+ DataAccessUtils.fromRDBDASToJavaPositionBased(outParams, cmd);
+ paramsList.add(3, outParams);
+ }catch(Exception e){
+ //TODO what if map is not of proper type
+ e.printStackTrace();
+ }
+ }
+
+ //System.out.println("executed SP..");
+ }
+ //dasInstance.releaseResources();
+ return dataObj; // NOPMD
+ }
+
+ //this method detects further what exact DAS function needs to be called e.g. applyChanges(), ...
+ private DataObject invokeRDBDASFunction(Command cmd, Vector paramsList){
+ String commandKind = null;
+ commandKind = (String)paramsList.get(1);//e.g. select, update, - sim. to one in DAS config for current command
+
+
+
+ //System.out.println("commandKind:"+commandKind);
+ //static or dynamic both - executeQuery()
+ if(commandKind.equals(DataAccessEngine.SELECT)){
+ DataObject dataObj = invokeRDBDASSelect(cmd, paramsList);
+ return dataObj; //NOPMD
+ }
+
+ //Delete can happen through execute() or applyChanges()
+ if(commandKind.startsWith(DataAccessEngine.DELETE)){
+ if(paramsList.get(0).toString().equals("")){
+ if(commandKind.indexOf(DataAccessEngine.SEPARATOR) != -1){//this will be applyChanges()-static/dynamic delete:tableName - cmd has no meaning here
+ invokeRDBDASDeleteApplyChanges(cmd, paramsList);
+ }else{//this will be execute() - static
+ invokeRDBDASDeleteExecute(cmd, paramsList);
+ return null;
+ }
+ }else{//this will be execute() - dynamic
+ invokeRDBDASDeleteExecute(cmd, paramsList);
+ return null;
+ }
+
+ return null;
+ }
+
+ //Insert can happen through execute() or applyChanges()
+ if(commandKind.startsWith(DataAccessEngine.INSERT)){
+ if(paramsList.get(0).toString().equals("")){
+ if(commandKind.indexOf(DataAccessEngine.SEPARATOR) != -1){//this will be applyChanges()-static/dynamic
+ invokeRDBDASInsertApplyChanges(cmd, paramsList);
+ }
+ else{//this will be execute() - static
+ //System.out.println("calling insert execute");
+ invokeRDBDASInsertExecute(cmd, paramsList);
+ return null;
+ }
+ }
+ else{//this will be execute() - dynamic
+ //System.out.println("calling insert execute");
+ invokeRDBDASInsertExecute(cmd, paramsList);
+ return null;
+ }
+
+ return null;
+ }
+
+ if(commandKind.startsWith(DataAccessEngine.UPDATE)){//update:tableName
+ invokeRDBDASUpdate(cmd, paramsList); //cmd has no meaning here
+ return null;
+ }
+
+ if(commandKind.equals(DataAccessEngine.PROCEDURE)){//executeQuery()
+ DataObject dataObj = invokeRDBDASProcedure(cmd, paramsList);
+ return dataObj;
+ }
+
+
+ return null;
+ }
+}
+
diff --git a/sandbox/lresende/backup/container.das/src/main/java/org/apache/tuscany/container/dataaccessscript/DataAccessUtils.java b/sandbox/lresende/backup/container.das/src/main/java/org/apache/tuscany/container/dataaccessscript/DataAccessUtils.java
new file mode 100644
index 0000000000..8d28a753f1
--- /dev/null
+++ b/sandbox/lresende/backup/container.das/src/main/java/org/apache/tuscany/container/dataaccessscript/DataAccessUtils.java
@@ -0,0 +1,90 @@
+/*
+ * 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.container.dataaccessscript;
+
+import java.util.Iterator;
+import java.util.Map;
+
+import org.apache.tuscany.das.rdb.Command;
+
+import commonj.sdo.DataObject;
+
+public class DataAccessUtils {
+ //in SP - when returning result - OUT params are populated here
+ public static void fromRDBDASToJavaPositionBased(Map outParams, Command cmd){
+ if(outParams != null && outParams.size()> 0){
+ for (Iterator i = outParams.keySet().iterator(); i.hasNext();) {
+ Integer odx = (Integer)i.next();
+
+ //result can be of any Object type
+ Object outRes = null;
+ outRes = cmd.getParameter(odx);
+
+ outParams.put(odx, outRes);
+ }
+ }
+
+ }
+
+ //in SP when calling executeQuery on SP, IN params are populated here
+ public static void fromJavaToRDBDASPositionBased(Map inParams, Command cmd){
+ if(inParams != null && inParams.size()> 0){
+ for (Iterator i = inParams.keySet().iterator(); i.hasNext();) {
+ Integer idx = (Integer)i.next();
+
+ Object val = inParams.get(idx);
+ cmd.setParameter(idx, val);
+ }
+ }
+ }
+
+ //Used in UPDATE
+ public static DataObject fromJavaToRDBDASNameBased(Map updParams, Map whereParams, Command cmd, String tableName){
+ //currently where clause is only for PKs - by convention
+ String whereStr = null;
+ //TODO needs better way
+ if(whereParams != null && whereParams.size()> 0){
+ for (Iterator i = whereParams.keySet().iterator(); i.hasNext();) {
+ String idx = (String)i.next();
+
+ Object val = whereParams.get(idx);
+ whereStr = tableName+"["+val+"]";
+ //System.out.println("whereStr:"+whereStr);
+ }
+ DataObject root = cmd.executeQuery();
+ DataObject dObj = root.getDataObject(whereStr);
+
+ if(updParams != null && updParams.size()> 0){
+ for (Iterator i = updParams.keySet().iterator(); i.hasNext();) {
+ String idx = (String)i.next();
+
+ Object val = updParams.get(idx);
+ dObj.set(idx, val);
+ }
+
+ return root;
+ }
+ }
+
+ return null;
+ }
+
+ //TODO ..similarly there will be fromJavaToXQueryDAS()..and revese etc.
+}
+