#include "StdAfx.h"

#include "c_KgOraUtil.h"
#include "ut_KgOraSdeSchema.h"
// Provider specific headers
#include <KingOracle/FdoKgOraOverrides.h>
#include "KgOraProvider.h"
#include "../Message/inc/KgOraMessage.h"

#include "c_KgOraSridDesc.h"
#include "c_SdeGeom2AGF.h"


#include <c_KgOraSridDesc.h>
#include <c_KgOraConnectionCapabilities.h>
#include <c_KgOraCommandCapabilities.h>
#include <c_KgOraExpressionCapabilities.h>
#include <c_KgOraFilterCapabilities.h>
#include <c_KgOraSchemaCapabilities.h>
#include <c_KgOraRasterCapabilities.h>
#include <c_KgOraTopologyCapabilities.h>
#include <c_KgOraGeometryCapabilities.h>
#include <c_KgOraConnection.h>
#include <c_KgOraConnectionInfo.h>
#include <c_KgOraFdoFeatureCommand.h>
#include <c_KgOraSchemaDesc.h>
#include <c_KgOraDescribeSchemaCommand.h>



CPPUNIT_TEST_SUITE_REGISTRATION(ut_KgOraSdeSchema);



ut_KgOraSdeSchema::ut_KgOraSdeSchema(void)
{
}

ut_KgOraSdeSchema::~ut_KgOraSdeSchema(void)
{
}




void ut_KgOraSdeSchema::DescribeSdeSchema()
{
try
{
  FdoPtr<FdoIConnection> conn = c_KgOraUtil::OpenUnitTestConn_10_2_SDE();
  
  c_KgOraConnection* kingora_conn = (c_KgOraConnection*)conn.p;
  kingora_conn->ClearCachedSchemaDesc();
  
  FdoPtr<FdoIDescribeSchema> comm = (FdoIDescribeSchema*)conn->CreateCommand(FdoCommandType_DescribeSchema);

  
  FdoPtr<FdoFeatureSchemaCollection> schemas = comm->Execute();
  
  CPPUNIT_ASSERT_MESSAGE( "FdoIDescribeSchema:Execute returns NULL schema collection" , schemas );
    

  long count_schema =schemas->GetCount();
  
  FdoPtr<FdoFeatureSchema> schema = schemas->GetItem(0);
  
  FdoPtr<FdoClassCollection> coll_class = schema->GetClasses();
  long count_classes = coll_class->GetCount();
  
  conn->Close();
  
  
  //CPPUNIT_ASSERT_MESSAGE( "FdoIDescribeSchema:Execute returns 0 spatial contexts " , count );
}
catch(FdoException* ex)
{
  FdoStringP str = ex->GetExceptionMessage();
  ex->Release();
  CPPUNIT_FAIL( (const char*)str );
}
  
}//end of ut_KgOraSdeSchema::DescribeSchema


void ut_KgOraSdeSchema::ReadDataProperty(FdoIReader* Reader,FdoDataType DataType,FdoString* Name)
{
  if( Reader->IsNull(Name) ) return;

  switch(DataType)
  {
    /// Represents a Boolean value of true or false.
  case  FdoDataType_Boolean:
    Reader->GetBoolean(Name);
    break;

    /// Represents unsigned 8-bit integers with values between 0 and 255.
  case FdoDataType_Byte:
    Reader->GetByte(Name);
    break;

    /// Represents a date and time value.
  case FdoDataType_DateTime:
    Reader->GetDateTime(Name);
    break;

    /// Represents values ranging from 1.0 x 10^-28 to approximately 7.9 x 10^28
    /// with 28-29 significant digits.
  case FdoDataType_Decimal:
    Reader->GetDouble(Name);
    break;

    /// Represents a floating point value ranging from approximately 5.0 
    /// x 10^-324 to 1.7 x 10^308 with a precision of 15-16 digits.
  case FdoDataType_Double:
    Reader->GetDouble(Name);
    break;

    /// Represents signed 16-bit integers with values between -32768 and 32767.
  case FdoDataType_Int16:
    Reader->GetInt16(Name);
    break;

    /// Represents signed 32-bit integers with values between -2147483648 
    /// and 2147483647.
  case FdoDataType_Int32:
    Reader->GetInt32(Name);
    break;

    /// Represents signed 64-bit integers with values 
    /// between -9223372036854775808 and 9223372036854775807.
  case FdoDataType_Int64:
    Reader->GetInt64(Name);
    break;

    /// Represents floating point values ranging from approximately 1.5 x 10^-45
    /// to 3.4 x 10^38, with a precision of 7 digits. 
  case FdoDataType_Single:
    Reader->GetSingle(Name);
    break;

    /// Represents a Unicode character strings.
  case FdoDataType_String:
    Reader->GetString(Name);
    break;

    /// Represents a binary large object stored as a collection of bytes.
    case FdoDataType_BLOB:
    Reader->GetLOB(Name);
    break;

    /// Represents a character large object stored as a collection of
    /// characters.
    case FdoDataType_CLOB:
    Reader->GetLOB(Name);
    break;
  }
      

}
void ut_KgOraSdeSchema::ReadAllProperties(FdoIDataReader* Reader)
{
  
  for(int ind=0;ind<Reader->GetPropertyCount();ind++)
  {
    switch(Reader->GetPropertyType(Reader->GetPropertyName(ind)))
    {
      case FdoPropertyType_DataProperty:
      {
        ReadDataProperty(Reader,Reader->GetDataType(Reader->GetPropertyName(ind)),Reader->GetPropertyName(ind));
      }
      break;
      case FdoPropertyType_GeometricProperty:
      {
        if( !Reader->IsNull(Reader->GetPropertyName(ind) ) )
        {
          FdoPtr<FdoByteArray> bytes = Reader->GetGeometry(Reader->GetPropertyName(ind));
          FdoPtr<FdoGeometryValue> geomval = FdoGeometryValue::Create( bytes );
          FdoStringP strval=geomval->ToString();
        }
      }
      break;
    
    }
  }
}
void ut_KgOraSdeSchema::ReadAllProperties(FdoIFeatureReader* Reader)
{
  FdoPtr<FdoClassDefinition> fdoclass = Reader->GetClassDefinition();
  FdoPtr<FdoPropertyDefinitionCollection> props = fdoclass->GetProperties();
  
  for(int ind=0;ind<props->GetCount();ind++)
  {
    FdoPtr<FdoPropertyDefinition> prop = props->GetItem(ind);
    switch(prop->GetPropertyType())
    {
      case FdoPropertyType_DataProperty:
      {
        FdoDataPropertyDefinition * dataprop = (FdoDataPropertyDefinition *)prop.p;
        ReadDataProperty(Reader,dataprop->GetDataType(),dataprop->GetName());
      }
      break;
      case FdoPropertyType_GeometricProperty:
      {
        if( !Reader->IsNull(prop->GetName() ) )
        {
          FdoPtr<FdoByteArray> bytes = Reader->GetGeometry(prop->GetName());
          FdoPtr<FdoGeometryValue> geomval = FdoGeometryValue::Create( bytes );
          FdoStringP strval=geomval->ToString();
        }
      }
      break;
    }                  
  }
  
}

void ut_KgOraSdeSchema::SelectCommand()
{
try
{
  FdoPtr<FdoIConnection> conn = c_KgOraUtil::OpenUnitTestConn_10_2_SDE();

  c_KgOraConnection* kingora_conn = (c_KgOraConnection*)conn.p;
  
  // create test table with different data types
  FdoPtr<FdoISelect> comm = (FdoISelect*)kingora_conn->CreateCommand(FdoCommandType_Select);
  //comm->SetFeatureClassName(L"KingOra:UNISDETRAIN~UFRM_LICASE_POLY~FdoGeom");
  //comm->SetFeatureClassName(L"KingOra:UNISDETRAIN~UFRM_CGCASE_POLY~FdoGeom");
  
  //comm->SetFeatureClassName(L"KingOra:UNISDETRAIN~UFRM_CCCASE_POINT~FdoGeom");
  //comm->SetFeatureClassName(L"KingOra:UNISDETRAIN~UFRM_DCAPPL_POLY~FdoGeom");
  comm->SetFeatureClassName(L"KingOra:UNISDETRAIN~NEATH_OS_POINT~FdoGeom");
  
  //comm->SetFeatureClassName(L"KingOra:UNISDETRAIN~UFRM_DCAPPL_POLY~FdoGeom");

  // -		testsql	0x089e7778 "GEOMETRY ENVELOPEINTERSECTS GeomFromText('POLYGON ((406319.018404908 120000, 453680.981595092 120000, 453680.981595092 150000, 406319.018404908 150000, 406319.018404908 120000))')"	const wchar_t *
  // -		testsql	0x088331e8 "GEOMETRY INTERSECTS GeomFromText('POLYGON ((424110.429447853 144969.325153374, 424355.828220859 144969.325153374, 424355.828220859 144723.926380368, 424110.429447853 144723.926380368, 424110.429447853 144969.325153374))')"	const wchar_t *

  // -		testsql	0x03de5d48 "GEOMETRY ENVELOPEINTERSECTS GeomFromText('POLYGON ((424846.625766871 135378.111192346, 429754.601226994 135378.111192346, 429754.601226994 138486.919482501, 424846.625766871 138486.919482501, 424846.625766871 135378.111192346))')"	const wchar_t *
  //-		testsql	0x0867a888 "GEOMETRY INTERSECTS GeomFromText('POLYGON ((425889.252678089 135994.786865444, 425914.682602753 135994.786865444, 425914.682602753 135969.35694078, 425889.252678089 135969.35694078, 425889.252678089 135994.786865444))')"	const wchar_t *

  //comm->SetFilter(L"SHAPE=50 AND FdoGeom ENVELOPEINTERSECTS GeomFromText('POLYGON ((274278.6 194322.0, 274278.8 194322.0, 274278.8 194322.2, 274278.6 194322.2, 274278.6 194322.0))')");
  //comm->SetFilter(L"DESCGROUP='Terrain And Height' AND FdoGeom ENVELOPEINTERSECTS GeomFromText('POLYGON ((274278.6 194322.0, 274278.8 194322.0, 274278.8 194322.2, 274278.6 194322.2, 274278.6 194322.0))')");
  comm->SetFilter(L"SHAPE=50 AND FdoGeom ENVELOPEINTERSECTS GeomFromText('POLYGON ((274278.6 194322.0, 274278.8 194322.0, 274278.8 194322.2, 274278.6 194322.2, 274278.6 194322.0))')");
  //comm->SetFilter(L"DESCGROUP='Terrain And Height' AND SHAPE=50");
  
  
  

  FdoString *strval;
  int count=0;
  FdoPtr<FdoIFeatureReader> freader = comm->Execute();
  while( freader->ReadNext() )
  {
    ReadAllProperties(freader);
    int shape_id = freader->GetInt32(L"SHAPE");
    FdoString* desc = freader->GetString(L"DESCGROUP");
    //FdoStringP keyval = freader->GetString(L"KEYVAL");
    FdoPtr<FdoByteArray> fgf = freader->GetGeometry(L"FdoGeom");
    FdoPtr<FdoGeometryValue> geomval = FdoGeometryValue::Create( fgf );
    strval=geomval->ToString();
    count++;
  }
  freader->Close();
  
  if( count != 1 ) CPPUNIT_FAIL("only 1 should be in recordset");
}
catch(FdoException* ex)
{
  FdoStringP str = ex->GetExceptionMessage();
  ex->Release();
  CPPUNIT_FAIL( (const char*)str );
}  
}



unsigned char * HexStringToByteArray(char* Str,int& SdeLen)
{
  int lenstr = strlen(Str);

  if( (lenstr % 2) != 0 )
    CPPUNIT_FAIL( "HexStringToByteArray:: Invalid string. Odd number of characters." );
  

  SdeLen = lenstr/2;
  unsigned char byteval;
  unsigned char *sdebinary = new unsigned char[SdeLen];
  unsigned char *dest = sdebinary;
  char *cp = Str;
  while(*cp)
  {
    if( (*cp >= '0') && (*cp <= '9') )
      byteval = *cp - '0';
    else
      byteval = 10 + *cp - 'A';

    byteval = byteval * 16;  
    cp++;
    if( (*cp >= '0') && (*cp <= '9') )
      byteval += *cp - '0';
    else
      byteval += 10 + *cp - 'A';
    cp++;

    *dest++ = byteval;
  }
  
  return sdebinary;
}

char g_SdeString[] = " ";
char g_SdeString2[] = " ";
void ut_KgOraSdeSchema::SdeBinaryToFGF()
{


  try
  {
    c_SdeGeom2AGF sde2fgf;
    c_KgOraSridDesc srid;

    srid.m_SDE_FalseX = -5220400;
    srid.m_SDE_FalseY = -15524400;
    srid.m_SDE_XYUnit = 10000;
    int sde_len;
    unsigned char* sdebinary = HexStringToByteArray(g_SdeString2,sde_len);
    sde2fgf.SetSdeGeometry(&srid,11,1066,8,sde_len,sdebinary);
    int fgf_len = sde2fgf.ToAGF();
    FdoPtr<FdoByteArray> fgf = FdoByteArray::Create((const unsigned char*)sde2fgf.GetBuff(), fgf_len);
    FdoPtr<FdoGeometryValue> geomval = FdoGeometryValue::Create( fgf );
    FdoString *strval=geomval->ToString();
    delete []sdebinary;
  }
  catch(FdoException* ex)
  {
    FdoStringP str = ex->GetExceptionMessage();
    ex->Release();
    CPPUNIT_FAIL( (const char*)str );
  }  
  
  try
  {
    c_SdeGeom2AGF sde2fgf;
    c_KgOraSridDesc srid;

    srid.m_SDE_FalseX = -5220400;
    srid.m_SDE_FalseY = -15524400;
    srid.m_SDE_XYUnit = 10000;
    int sde_len;
    unsigned char* sdebinary = HexStringToByteArray(g_SdeString,sde_len);
    sde2fgf.SetSdeGeometry(&srid,11,1066,8,sde_len,sdebinary);
    int fgf_len = sde2fgf.ToAGF();
    FdoPtr<FdoByteArray> fgf = FdoByteArray::Create((const unsigned char*)sde2fgf.GetBuff(), fgf_len);
    FdoPtr<FdoGeometryValue> geomval = FdoGeometryValue::Create( fgf );
    FdoString *strval=geomval->ToString();
    delete []sdebinary;
  }
  catch(FdoException* ex)
  {
    FdoStringP str = ex->GetExceptionMessage();
    ex->Release();
    CPPUNIT_FAIL( (const char*)str );
  }  
  
  

  try
  {
    c_SdeGeom2AGF sde2fgf;
    c_KgOraSridDesc srid;

    srid.m_SDE_FalseX = 377166.1095 ;
    srid.m_SDE_FalseY = 795020.8085;
    srid.m_SDE_XYUnit = 10000;
    
    int sde_len;
    //unsigned char* sdebinary = HexStringToByteArray("08000000010000008BE8AC3C83AAFC33",sde_len);
    unsigned char* sdebinary = HexStringToByteArray("0900000001000000A38AE69C01AFC79365",sde_len);
    sde2fgf.SetSdeGeometry(&srid,7,2,8,sde_len,sdebinary);
    int fgf_len = sde2fgf.ToAGF();
    FdoPtr<FdoByteArray> fgf = FdoByteArray::Create((const unsigned char*)sde2fgf.GetBuff(), fgf_len);
    FdoPtr<FdoGeometryValue> geomval = FdoGeometryValue::Create( fgf );
    FdoString *strval=geomval->ToString();
    delete []sdebinary;
  }
  catch(FdoException* ex)
  {
    FdoStringP str = ex->GetExceptionMessage();
    ex->Release();
    CPPUNIT_FAIL( (const char*)str );
  }  
  
  try
  {
    c_SdeGeom2AGF sde2fgf;
    c_KgOraSridDesc srid;

    srid.m_SDE_FalseX = 377166.1095 ;
    srid.m_SDE_FalseY = 795020.8085;
    srid.m_SDE_XYUnit = 10000;

    int sde_len;
    //unsigned char* sdebinary = HexStringToByteArray("0A0000000100000081BAA491018DECF3A001",sde_len);
    unsigned char* sdebinary = HexStringToByteArray("0A00000001000000A1E691A00191B0C38E01",sde_len);
    sde2fgf.SetSdeGeometry(&srid,7,2,8,sde_len,sdebinary);
    int fgf_len = sde2fgf.ToAGF();
    FdoPtr<FdoByteArray> fgf = FdoByteArray::Create((const unsigned char*)sde2fgf.GetBuff(), fgf_len);
    FdoPtr<FdoGeometryValue> geomval = FdoGeometryValue::Create( fgf );
    FdoString *strval=geomval->ToString();
    delete []sdebinary;
  }
  catch(FdoException* ex)
  {
    FdoStringP str = ex->GetExceptionMessage();
    ex->Release();
    CPPUNIT_FAIL( (const char*)str );
  } 
   
}

void ut_KgOraSdeSchema::TestPolygonWithHoles()
{
  try
  {
    FdoPtr<FdoIConnection> conn = c_KgOraUtil::OpenUnitTestConn_10_2_SDE();

    c_KgOraConnection* kingora_conn = (c_KgOraConnection*)conn.p;

    // create test table with different data types
    FdoPtr<FdoISelect> comm = (FdoISelect*)kingora_conn->CreateCommand(FdoCommandType_Select);
    comm->SetFeatureClassName(L"KingOra:UNISDETRAIN~NEATH_OS_AREA~FdoGeom");
    comm->SetFilter(L"SHAPE=139");
    //comm->SetFilter(L"GEOMETRY ENVELOPEINTERSECTS GeomFromText('POLYGON ((406319.018404908 120000, 453680.981595092 120000, 453680.981595092 150000, 406319.018404908 150000, 406319.018404908 120000))')");
    

    FdoString *strval;
    int count=0;
    FdoPtr<FdoIFeatureReader> freader = comm->Execute();
    while( freader->ReadNext() )
    {
      ReadAllProperties(freader);
      int shape_id = freader->GetInt32(L"SHAPE");
      //FdoStringP keyval = freader->GetString(L"KEYVAL");
      FdoPtr<FdoByteArray> fgf = freader->GetGeometry(L"FdoGeom");
      FdoPtr<FdoGeometryValue> geomval = FdoGeometryValue::Create( fgf );
      strval=geomval->ToString();
      count++;
    }
    freader->Close();
  }
  catch(FdoException* ex)
  {
    FdoStringP str = ex->GetExceptionMessage();
    ex->Release();
    CPPUNIT_FAIL( (const char*)str );
  }  
}

void ut_KgOraSdeSchema::TestBigGeometry()
{
  try
  {
    FdoPtr<FdoIConnection> conn = c_KgOraUtil::OpenUnitTestConn_10_2_SDE();

    c_KgOraConnection* kingora_conn = (c_KgOraConnection*)conn.p;

    // create test table with different data types
    FdoPtr<FdoISelect> comm = (FdoISelect*)kingora_conn->CreateCommand(FdoCommandType_Select);
    comm->SetFeatureClassName(L"KingOra:UNISDETRAIN~NEATH_OS_AREA~FdoGeom");
    comm->SetFilter(L"SHAPE=109");
    //comm->SetFilter(L"GEOMETRY ENVELOPEINTERSECTS GeomFromText('POLYGON ((406319.018404908 120000, 453680.981595092 120000, 453680.981595092 150000, 406319.018404908 150000, 406319.018404908 120000))')");


    FdoString *strval;
    int count=0;
    FdoPtr<FdoIFeatureReader> freader = comm->Execute();
    while( freader->ReadNext() )
    {
      ReadAllProperties(freader);
      int shape_id = freader->GetInt32(L"SHAPE");
      //FdoStringP keyval = freader->GetString(L"KEYVAL");
      FdoPtr<FdoByteArray> fgf = freader->GetGeometry(L"FdoGeom");
      FdoPtr<FdoGeometryValue> geomval = FdoGeometryValue::Create( fgf );
      strval=geomval->ToString();
      #ifdef DEBUG
      std::cout << strval;
      #endif

      count++;
    }
    freader->Close();
  }
  catch(FdoException* ex)
  {
    FdoStringP str = ex->GetExceptionMessage();
    ex->Release();
    CPPUNIT_FAIL( (const char*)str );
  }  
}

void ut_KgOraSdeSchema::SelectAggregate()
{
  try
  {
    FdoPtr<FdoIConnection> conn = c_KgOraUtil::OpenUnitTestConn_10_2_SDE();

    c_KgOraConnection* kingora_conn = (c_KgOraConnection*)conn.p;

    // create test table with different data types
    FdoPtr<FdoISelectAggregates> comm = (FdoISelectAggregates*)kingora_conn->CreateCommand(FdoCommandType_SelectAggregates);
    //comm->SetFeatureClassName(L"KingOra:UNISDETRAIN~UFRM_LICASE_POLY~FdoGeom");
    //comm->SetFeatureClassName(L"KingOra:UNISDETRAIN~UFRM_CGCASE_POLY~FdoGeom");
    comm->SetFeatureClassName(L"KingOra:UNISDETRAIN~UFRM_CCCASE_POINT~FdoGeom");
    
    FdoPtr<FdoIdentifierCollection> ids = comm->GetPropertyNames();
    FdoPtr<FdoExpression> expr = FdoExpression::Parse(L"SpatialExtents(FdoGeom)");
    FdoPtr<FdoComputedIdentifier> cid = FdoComputedIdentifier::Create(L"MBR", expr);
    ids->Add(cid);

    // -		testsql	0x089e7778 "GEOMETRY ENVELOPEINTERSECTS GeomFromText('POLYGON ((406319.018404908 120000, 453680.981595092 120000, 453680.981595092 150000, 406319.018404908 150000, 406319.018404908 120000))')"	const wchar_t *
    // -		testsql	0x088331e8 "GEOMETRY INTERSECTS GeomFromText('POLYGON ((424110.429447853 144969.325153374, 424355.828220859 144969.325153374, 424355.828220859 144723.926380368, 424110.429447853 144723.926380368, 424110.429447853 144969.325153374))')"	const wchar_t *

    // -		testsql	0x03de5d48 "GEOMETRY ENVELOPEINTERSECTS GeomFromText('POLYGON ((424846.625766871 135378.111192346, 429754.601226994 135378.111192346, 429754.601226994 138486.919482501, 424846.625766871 138486.919482501, 424846.625766871 135378.111192346))')"	const wchar_t *
    //-		testsql	0x0867a888 "GEOMETRY INTERSECTS GeomFromText('POLYGON ((425889.252678089 135994.786865444, 425914.682602753 135994.786865444, 425914.682602753 135969.35694078, 425889.252678089 135969.35694078, 425889.252678089 135994.786865444))')"	const wchar_t *

    //comm->SetFilter(L"GEOMETRY ENVELOPEINTERSECTS GeomFromText('POLYGON ((406319.018404908 120000, 453680.981595092 120000, 453680.981595092 150000, 406319.018404908 150000, 406319.018404908 120000))')");


    FdoString *strval;
    FdoPtr<FdoIDataReader> freader = comm->Execute();
    while( freader->ReadNext() )
    {
      ReadAllProperties(freader);
      if( !freader->IsNull(L"MBR") )
      {
        FdoPtr<FdoByteArray> fgf = freader->GetGeometry(L"MBR");
        FdoPtr<FdoGeometryValue> geomval = FdoGeometryValue::Create( fgf );
        strval=geomval->ToString();
      }
    }
    freader->Close();
    conn->Close();
  }
  catch(FdoException* ex)
  {
    FdoStringP str = ex->GetExceptionMessage();
    ex->Release();
    CPPUNIT_FAIL( (const char*)str );
  }  
}

void ut_KgOraSdeSchema::ReadAllClassesGeomtries()
{
  try
  {
    FdoPtr<FdoIConnection> conn = c_KgOraUtil::OpenUnitTestConn_10_2_SDE();

    c_KgOraConnection* kingora_conn = (c_KgOraConnection*)conn.p;


    FdoPtr<FdoIDescribeSchema> comm_describe = (FdoIDescribeSchema*)kingora_conn->CreateCommand(FdoCommandType_DescribeSchema);
    FdoPtr<FdoFeatureSchemaCollection> schemas = comm_describe->Execute();
    
    FdoPtr<FdoFeatureSchema> sde_schema = (FdoFeatureSchema*)schemas->FindItem(L"KingOra");
    FdoPtr<FdoClassCollection> classes = sde_schema->GetClasses();
    
    int count = classes->GetCount();
    for(int ind=0;ind<count;ind++)
    {       
      FdoPtr<FdoClassDefinition> fdoclass = classes->GetItem(ind);
      
      // create test table with different data types
      FdoPtr<FdoISelect> comm = (FdoISelect*)kingora_conn->CreateCommand(FdoCommandType_Select);
      comm->SetFeatureClassName(fdoclass->GetName());
      //comm->SetFeatureClassName(L"KingOra:UNISDETRAIN~UFRM_CCCASE_POINT~FdoGeom");

      // -		testsql	0x089e7778 "GEOMETRY ENVELOPEINTERSECTS GeomFromText('POLYGON ((406319.018404908 120000, 453680.981595092 120000, 453680.981595092 150000, 406319.018404908 150000, 406319.018404908 120000))')"	const wchar_t *
      // -		testsql	0x088331e8 "GEOMETRY INTERSECTS GeomFromText('POLYGON ((424110.429447853 144969.325153374, 424355.828220859 144969.325153374, 424355.828220859 144723.926380368, 424110.429447853 144723.926380368, 424110.429447853 144969.325153374))')"	const wchar_t *

      // -		testsql	0x03de5d48 "GEOMETRY ENVELOPEINTERSECTS GeomFromText('POLYGON ((424846.625766871 135378.111192346, 429754.601226994 135378.111192346, 429754.601226994 138486.919482501, 424846.625766871 138486.919482501, 424846.625766871 135378.111192346))')"	const wchar_t *
      //-		testsql	0x0867a888 "GEOMETRY INTERSECTS GeomFromText('POLYGON ((425889.252678089 135994.786865444, 425914.682602753 135994.786865444, 425914.682602753 135969.35694078, 425889.252678089 135969.35694078, 425889.252678089 135994.786865444))')"	const wchar_t *

      //comm->SetFilter(L"GEOMETRY ENVELOPEINTERSECTS GeomFromText('POLYGON ((406319.018404908 120000, 453680.981595092 120000, 453680.981595092 150000, 406319.018404908 150000, 406319.018404908 120000))')");


      FdoPtr<FdoIFeatureReader> freader;
      
      // In this test I wrap up select command into try/catch to ignore exceptions
      // ignore exception is needed because of some classes generated on test database (from 1Spatial) where some sde layers are 
      // coming from oracle views which gives error when trying to execute them (missing tables/schemas in test database)
      try
      {         
        
        freader = comm->Execute();
      }
      catch (FdoException*)
      {
        freader = NULL;
      }
      if( freader.p )
      {
      
        while( freader->ReadNext() )
        {
          ReadAllProperties(freader);
          //FdoPtr<FdoByteArray> fgf = freader->GetGeometry(L"FdoGeom");
          //FdoPtr<FdoGeometryValue> geomval = FdoGeometryValue::Create( fgf );
          //strval=geomval->ToString();
        }
      }
    }
  }
  catch(FdoException* ex)
  {
    FdoStringP str = ex->GetExceptionMessage();
    ex->Release();
    CPPUNIT_FAIL( (const char*)str );
  }  
}


void ut_KgOraSdeSchema::SelectCommand_RemoteSDE()
{
  try
  {
    FdoPtr<FdoIConnection> conn = c_KgOraUtil::OpentConnection(L"Username=unisdetrain;Password=unisde;Service=//193.195.73.43/idoxdb;SDE Schema=SDE;OracleSchema=UNISDETRAIN");    
    

    c_KgOraConnection* kingora_conn = (c_KgOraConnection*)conn.p;

    // create test table with different data types
    FdoPtr<FdoISelect> comm = (FdoISelect*)kingora_conn->CreateCommand(FdoCommandType_Select);
    //comm->SetFeatureClassName(L"KingOra:UNISDETRAIN~UFRM_LICASE_POLY~FdoGeom");
    //comm->SetFeatureClassName(L"KingOra:UNISDETRAIN~UFRM_CGCASE_POLY~FdoGeom");
    comm->SetFeatureClassName(L"KingOra:UNISDETRAIN~UFRM_CCCASE_POINT~FdoGeom");

    // -		testsql	0x089e7778 "GEOMETRY ENVELOPEINTERSECTS GeomFromText('POLYGON ((406319.018404908 120000, 453680.981595092 120000, 453680.981595092 150000, 406319.018404908 150000, 406319.018404908 120000))')"	const wchar_t *
    // -		testsql	0x088331e8 "GEOMETRY INTERSECTS GeomFromText('POLYGON ((424110.429447853 144969.325153374, 424355.828220859 144969.325153374, 424355.828220859 144723.926380368, 424110.429447853 144723.926380368, 424110.429447853 144969.325153374))')"	const wchar_t *

    // -		testsql	0x03de5d48 "GEOMETRY ENVELOPEINTERSECTS GeomFromText('POLYGON ((424846.625766871 135378.111192346, 429754.601226994 135378.111192346, 429754.601226994 138486.919482501, 424846.625766871 138486.919482501, 424846.625766871 135378.111192346))')"	const wchar_t *
    //-		testsql	0x0867a888 "GEOMETRY INTERSECTS GeomFromText('POLYGON ((425889.252678089 135994.786865444, 425914.682602753 135994.786865444, 425914.682602753 135969.35694078, 425889.252678089 135969.35694078, 425889.252678089 135994.786865444))')"	const wchar_t *

    //comm->SetFilter(L"GEOMETRY ENVELOPEINTERSECTS GeomFromText('POLYGON ((406319.018404908 120000, 453680.981595092 120000, 453680.981595092 150000, 406319.018404908 150000, 406319.018404908 120000))')");


    FdoString *strval;
    FdoPtr<FdoIFeatureReader> freader = comm->Execute();
    while( freader->ReadNext() )
    {
      ReadAllProperties(freader);
      FdoPtr<FdoByteArray> fgf = freader->GetGeometry(L"FdoGeom");
      FdoPtr<FdoGeometryValue> geomval = FdoGeometryValue::Create( fgf );
      strval=geomval->ToString();
    }
    freader->Close();
  }
  catch(FdoException* ex)
  {
    FdoStringP str = ex->GetExceptionMessage();
    ex->Release();
    CPPUNIT_FAIL( (const char*)str );
  }  
}