/***************************************************************************

    supportware
    Copyright (C) 2001-2008 by c.holzheuer
    c.holzheuer@sourceworx.org

    This program 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; either version 2 of the License, or
    (at your option) any later version.

***************************************************************************/


#include <ddwebblock.h>
#include <ddwebsite.h>
#include <ddwebpage.h>


/**
	Default Constructor
*/

ddwebblock::ddwebblock( const ddstring& name, ddwebsite* site )
: dddataclient( name ), _table( 0 )
{

	// fragwürdig
	site->get_block_map().add_by_key( name, this );	
	
}


/**
	Destructor.
*/

ddwebblock::~ddwebblock()
{
	// passiert woanders, wird nicht eigentümer, nur besitzer :-)
	//delete _table;
}


/**
	Einsprung
*/

dd_result ddwebblock::handle_request( ddwebpage& pagelet, dditem& item )
{
	ddthreadautomutex tmp(_blocklock);
	try
	{
		// neu erzeugen=
		if( !is_valid() || _content.empty() )
			create_content( pagelet, item );
		pagelet.write( _content );		
	}
	catch( exception& error )
	{
		ddstring text = "Block Exception: " + get_name() + error.what();
		return pagelet.create_error( text );
	}
	return rs_ok;
}


/**
	Prototype only.	
*/
	
dd_result ddwebblock::create_content( ddwebpage& pagelet, dditem& item )
{
	_content =  DDBLOCKTXT1 + pagelet.get_name() + ": " + get_name() + DDBLOCKTXT2;
	validate();
	return rs_ok;
}

