/*
 * 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.    
 */
#include "apache/das/rdb/DASImpl.h"

namespace apache {
	namespace das {
		namespace rdb {

DASFactory* DASImpl::FACTORY = new DASFactoryImpl();

DASImpl::DASImpl(Connection& inConnection) {
	/*if (FACTORY == 0) {
		FACTORY = new DASFactoryImpl();
	}*/

	createdCommands = new std::list<CommandPtr*>();
	config = new ConfigImpl();
    setConnection(&inConnection);
	
}

DASImpl::DASImpl(const Config& config, Connection& inConnection) {
	/*if (FACTORY == 0) {
		FACTORY = new DASFactoryImpl();
	}*/

	createdCommands = new std::list<CommandPtr*>();
	this->config = new ConfigImpl(config);
	setConnection(&inConnection);

}

DASImpl::DASImpl(const Config& config) {
	/*if (FACTORY == 0) {
		FACTORY = new DASFactoryImpl();
	}*/

	createdCommands = new std::list<CommandPtr*>();
	this->config = new ConfigImpl((ConfigImpl&) config);

}

DASImpl::~DASImpl() {
	std::list<CommandPtr*>::iterator it;

	for (it = createdCommands->begin() ; it != createdCommands->end() ; it++) {
		delete **it;
		delete *it;

	}

	delete createdCommands;
	delete config;

}

DASFactory& DASImpl::getFACTORY(void) {
	return *FACTORY;
}

void DASImpl::setConnection(Connection* aConnection) {
	connection = aConnection;
}

Connection* DASImpl::getConnection(void) {
	return connection;
}

void DASImpl::releaseResources(void) {
	closeConnection();
}

CommandPtr DASImpl::getCommand(std::string commandName) {
	std::string commandSQL = config->getCommand(commandName);

	return createCommand(commandSQL);

}

CommandPtr DASImpl::createCommand(std::string sql) {
	CommandPtr command = 0;
    //trim(inSql);
    char firstChar = toupper(sql[0]);

    switch (firstChar) {
        case 'S':
            command = new ReadCommandImpl(*this, sql);
            break;

		default :
			command = new CommandImpl(*this, sql);
		
    }

	std::list<CommandPtr*>::iterator it;

	for (it = createdCommands->begin() ; it != createdCommands->end() ; ) {
		
		if (**it) {
			it++;

		} else {
			std::list<CommandPtr*>::iterator aux = it;
			it++;
			createdCommands->erase(aux);

		}

	}
	
	createdCommands->push_back(new CommandPtr(command, false));
    return command;

}

void DASImpl::closeConnection(void) {
	
	if (connection != 0) {

        //try {
            delete connection;
			connection = 0;
        //} catch (SQLException e) {
          //  throw new RuntimeException(e);
        //}

    }

}

const ::apache::das::Config& DASImpl::getConfig(void) const {
	return *((Config*) config);
}

void DASImpl::applyChanges(commonj::sdo::DataObjectPtr root) {
	ApplyChanges(*this, root);
	commonj::sdo::ChangeSummaryPtr csummary = root->getChangeSummary();
	csummary->endLogging();
	csummary->beginLogging();
	
}

		};
	};
};
