/* * 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.data.engine; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.tuscany.das.rdb.Command; import org.apache.tuscany.das.rdb.DAS; import commonj.sdo.DataObject; import commonj.sdo.Property; /** * Facade to hide DAS implementation details of handling commands * * @version $Rev$ $Date$ */ public class DataAccessEngine { private final DAS das; public DataAccessEngine(DAS das) { this.das = das; } public DataObject executeGet(ArrayList keyVals, String table, String key) {//TODO need to consider compound keys try { String sqlQuery = "select * from " + table.toUpperCase(); List keys = null; if(key == null) { if(keyVals != null && keyVals.size() == 1) { sqlQuery += " where ID = " + keyVals.get(0); } } else {//can be other than ID , can be compound keys keys = getKeys(key); if(keyVals.size() != keys.size()) { throw new RuntimeException("One or more PK values missing"); } sqlQuery += " where "; for(int i=0; i executeGetAll(String table, String key) { try { String sqlQuery = "select * from " + table.toUpperCase(); Command command = this.das.createCommand(sqlQuery); DataObject result = command.executeQuery(); List keys = getKeys(key); List resultDataObjects = result.getList(table); return getMappedDataObjects(resultDataObjects, keys); } catch (Exception e) { //e.printStackTrace(); throw new RuntimeException(e); } } public Map executeQuery(String queryString, String table, String key) { try { Command command = this.das.createCommand(queryString); DataObject result = command.executeQuery(); List keys = getKeys(key); List resultDataObjects = result.getList(table); return getMappedDataObjects(resultDataObjects, keys); } catch (Exception e) { //e.printStackTrace(); throw new RuntimeException(e); } } //origDataObject should be with change summary. table, pk is already known to DAS, so no need to have these here public void executePut(DataObject origDataObject) { this.das.applyChanges(origDataObject); return; } //return PK/s public ArrayList executePost(DataObject origDataObject, String table, String key){ //TODO check that PKs are present before insert - this is not correct for auto incr PKs, so let it be upto user whether to send PK or not /*List keys = getKeys(key); for(int i=0; i keys = getKeys(key); for(int i=0; i keys = null; if(key == null) { if(keyVals != null && keyVals.size() == 1) { sqlQuery += " where ID = " + keyVals.get(0); } } else {//can be other than ID , can be compound keys keys = getKeys(key); if(keyVals.size() != keys.size()) { throw new RuntimeException("One or more PK values missing"); } sqlQuery += " where "; for(int i=0; i resultDOs = result.getList(table); if(resultDOs != null) { for(int i=0; i getMappedDataObjects(List resultDataObjects, List keys) { Map resultMap = new HashMap(); ArrayList keyCols = null; for(int j=0; j(); for(int i=0; i getKeys(String key) { String[] keys = key.split(","); return Arrays.asList(keys); } public DataObject executeCommand(String commandName) { try { Command command = this.das.getCommand(commandName); return command.executeQuery(); } catch (Exception e) { e.printStackTrace(); return null; } } public DataObject executeCommand(String commandName, String xPath) { DataObject root = executeCommand(commandName); return root.getDataObject(xPath); } }