///////////////////////////////////////////////////////////////////////////////
//                                                         
// CegoXMLHelper.cc
// ----------------
// Cego XML helper implementation
//     
// Design and Implementation by Bjoern Lemke
//     
// (C)opyright 2000-2010 Bjoern Lemke
//
// 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, or (at your option)
// any later version.
// 
// This program 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 this program; see the file COPYING.  If not, write to
// the Free Software Foundation, 59 Temple Place - Suite 330,
// Boston, MA 02111-1307, USA.
//
// IMPLEMENTATION MODULE
//
// Class: CegoXMLHelper
// 
// Description: 
//
// Status: QG-2.6
//
///////////////////////////////////////////////////////////////////////////////

// xml includes
#include <lfc/Element.h>

// cego includes
#include "CegoXMLHelper.h"
#include "CegoTypeConverter.h"
#include "CegoXMLdef.h"

CegoXMLHelper::CegoXMLHelper()
{
}

CegoXMLHelper::~CegoXMLHelper()
{
}

void CegoXMLHelper::setColInfo(Element* pColElement, CegoField *pF )
{

    pColElement->setAttribute(XML_COLNAME_ATTR, pF->getAttrName());
    if ( pF->isNullable() )
	pColElement->setAttribute(XML_COLNULLABLE_ATTR, XML_TRUE_VALUE);
    else
	pColElement->setAttribute(XML_COLNULLABLE_ATTR, XML_FALSE_VALUE);

    if ( pF->getValue().getValue() )
	pColElement->setAttribute(XML_COLDEFVALUE_ATTR, pF->getValue().valAsChain() );
    
    if ( pF->getType() == VARCHAR_TYPE )
    {
	pColElement->setAttribute(XML_COLTYPE_ATTR, XML_STRING_VALUE);
	pColElement->setAttribute(XML_JAVATYPE_ATTR, Chain(CEGO_JAVATYPE_MAP[VARCHAR_TYPE]));
    }
    else if ( pF->getType() == INT_TYPE )
    {
	pColElement->setAttribute(XML_COLTYPE_ATTR, XML_INT_VALUE);
	pColElement->setAttribute(XML_JAVATYPE_ATTR, Chain(CEGO_JAVATYPE_MAP[INT_TYPE]));
    }
    else if ( pF->getType() == LONG_TYPE )
    {
	pColElement->setAttribute(XML_COLTYPE_ATTR, XML_LONG_VALUE);
	pColElement->setAttribute(XML_JAVATYPE_ATTR, Chain(CEGO_JAVATYPE_MAP[LONG_TYPE]));
    }
    else if ( pF->getType() == BOOL_TYPE )
    {
	pColElement->setAttribute(XML_COLTYPE_ATTR, XML_BOOL_VALUE);
	pColElement->setAttribute(XML_JAVATYPE_ATTR, Chain(CEGO_JAVATYPE_MAP[BOOL_TYPE]));
    }
    else if ( pF->getType() == DATETIME_TYPE )
    {
	pColElement->setAttribute(XML_COLTYPE_ATTR, XML_DATETIME_VALUE);
	pColElement->setAttribute(XML_JAVATYPE_ATTR, Chain(CEGO_JAVATYPE_MAP[DATETIME_TYPE]));
    }
    else if ( pF->getType() == BIGINT_TYPE )
    {
	pColElement->setAttribute(XML_COLTYPE_ATTR, XML_BIGINT_VALUE);
	pColElement->setAttribute(XML_JAVATYPE_ATTR, Chain(CEGO_JAVATYPE_MAP[BIGINT_TYPE]));
    }
    else if ( pF->getType() == DECIMAL_TYPE )
    {
	pColElement->setAttribute(XML_COLTYPE_ATTR, XML_DECIMAL_VALUE);
	pColElement->setAttribute(XML_JAVATYPE_ATTR, Chain(CEGO_JAVATYPE_MAP[DECIMAL_TYPE]));
    }
    else if ( pF->getType() == FIXED_TYPE )
    {
	pColElement->setAttribute(XML_COLTYPE_ATTR, XML_FIXED_VALUE);
	pColElement->setAttribute(XML_JAVATYPE_ATTR, Chain(CEGO_JAVATYPE_MAP[FIXED_TYPE]));
    }
    else if ( pF->getType() == FLOAT_TYPE )
    {
	pColElement->setAttribute(XML_COLTYPE_ATTR, XML_FLOAT_VALUE);
	pColElement->setAttribute(XML_JAVATYPE_ATTR, Chain(CEGO_JAVATYPE_MAP[FLOAT_TYPE]));
    }
    else if ( pF->getType() == DOUBLE_TYPE )
    {
	pColElement->setAttribute(XML_COLTYPE_ATTR, XML_DOUBLE_VALUE);
	pColElement->setAttribute(XML_JAVATYPE_ATTR, Chain(CEGO_JAVATYPE_MAP[DOUBLE_TYPE]));
    }
    else if ( pF->getType() == SMALLINT_TYPE )
    {
	pColElement->setAttribute(XML_COLTYPE_ATTR, XML_SMALLINT_VALUE);
	pColElement->setAttribute(XML_JAVATYPE_ATTR, Chain(CEGO_JAVATYPE_MAP[SMALLINT_TYPE]));
    }
    else if ( pF->getType() == TINYINT_TYPE )
    {
	pColElement->setAttribute(XML_COLTYPE_ATTR, XML_TINYINT_VALUE);
	pColElement->setAttribute(XML_JAVATYPE_ATTR, Chain(CEGO_JAVATYPE_MAP[TINYINT_TYPE]));
    }
    else if ( pF->getType() == BLOB_TYPE )
    {
	pColElement->setAttribute(XML_COLTYPE_ATTR, XML_BLOB_VALUE);
	pColElement->setAttribute(XML_JAVATYPE_ATTR, Chain(CEGO_JAVATYPE_MAP[BLOB_TYPE]));
    }

    
    pColElement->setAttribute(XML_COLSIZE_ATTR, Chain(pF->getLength()));
}

void CegoXMLHelper::getColInfo(const Chain& objName, Element* pColElement, CegoField &f)
{
    Chain colName = pColElement->getAttributeValue(XML_COLNAME_ATTR);
    Chain colType = pColElement->getAttributeValue(XML_COLTYPE_ATTR);
    Chain colSize = pColElement->getAttributeValue(XML_COLSIZE_ATTR);
    Chain colNullable = pColElement->getAttributeValue(XML_COLNULLABLE_ATTR);
    Chain colDefValue = pColElement->getAttributeValue(XML_COLDEFVALUE_ATTR);

    bool isNullable;
    if ( colNullable == Chain(XML_TRUE_VALUE) ) 
	isNullable = true;
    else
	isNullable = false;

    CegoTypeConverter tc;
    CegoDataType dataType = tc.getTypeId(colType);
    
    CegoFieldValue defValue;
    if ( colDefValue != Chain("") )
    {
	defValue = CegoFieldValue(dataType, colDefValue);
    }
    
    if ( dataType != NULL_TYPE )
	f = CegoField(CegoField(objName, objName, colName, dataType, colSize.asInteger(), defValue, isNullable));		
    else
	f = CegoField(objName, colName);

}
