/**
 * File: contextxmlfactoryatomic.cpp
 * Created by: <Joern Reimerdes>
 * Created on: 2004/10/13 18:00 
 * @file
 * $Id$
 * $Revision$
 * $Author$
 * $Date$
 *
 * This file is a part of The iWear Framework.
 * In particular is this file a part of the Framework context Library
 *
 * The iWear Framework is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by the
 * Free Software Foundation as in version 2 of the License.

 * 
 * The iWear Framework is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * The iWear Framework; if not, write to the Free Software Foundation, Inc., 59
 * Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <iwear-context/contextxmlfactoryatomic.h>

namespace iwear {
namespace context {

    /** Creates a new ContextXMLFactoryAtomic
     *
     */
    ContextXMLFactoryAtomic::ContextXMLFactoryAtomic(ContextManager* context_manager,
					     ContextXMLHandler* context_xml_handler){
	//	this->context_xml_parser = context_xml_parser;
	this->context_manager = context_manager;
	this->context_xml_handler = context_xml_handler;

	/* Initalize AtomicFactories */
	this->atomic_factory_sensor = 
	    new AtomicFactorySensor(context_manager, context_xml_handler);

	/* Atomic Types */
	atomic_type_enum_map = new map<const string, AtomicType>;
	(*atomic_type_enum_map)["sensor"] = SENSOR;
	(*atomic_type_enum_map)["database"] = DATABASE;
	(*atomic_type_enum_map)["power"] = POWER;
	(*atomic_type_enum_map)["location"] = LOCATION;
    }

    /** Destroys the context atomic factory for xml context definitions.
     *
     */
    ContextXMLFactoryAtomic::~ContextXMLFactoryAtomic(){
	delete atomic_type_enum_map;
	delete atomic_factory_sensor;
    }

    ContextObject* 
    ContextXMLFactoryAtomic::create_atomic_context(string context_name,  
						   DOMElement* atomic_context_node){
	ContextObject* context_object;

	/* if atomic context type  */
	const string atomic_type_string = 
	    this->context_xml_handler->get_type_from_node(atomic_context_node);
	AtomicType atomic_type = 
	    atomic_type_enum_map->find(atomic_type_string)->second;
	// d_dbg << "AtomicType " << atomic_type << endl;
	switch(atomic_type) {
	case SENSOR: 
	    {		    
		context_object = 
		    this->atomic_factory_sensor->create_atomic_context(context_name, 
								   atomic_context_node);
		break;
	    }
	case LOCATION:
	    {
		// @todo: implement location creation
		break;
	    }
	case POWER:
	    {
		// @todo: implement power creation
		break;
	    }
	default:
	    {
		stringstream error_message;
		error_message << "There is no handling for the specified "
			      << "atomic type \"" << atomic_type_string 
			      <<  "\". " << endl
			      << __FFL__ << endl;
		throw consistency_error(error_message.str());
		return NULL;
	    }
	} // atomic_type switch
	return context_object;
    }


} // context
} // iwear

