// Change History

// DRS_CIF.CPP
// DLL C interface for access to DART functionality
#include <process.h>

#include "ddp/ddp_cif.h"
#include "drs/drs_cif.h"

#include "ddp/DDP_Session.h"

//////////////////////////////////////////////////////////////////////////////////////////////
// Session
//////////////////////////////////////////////////////////////////////////////////////////////
DLL_Erc _ddp_call_c DDP_SessionOpen(DLL_Handle app)
{         //_export    _stdcall

    DLL_Erc retErr = ERC_none;
    DDP_Session * session = NULL;
    
try
    {
    if(!app)
        app = DRS_ApplicationGet();
        
    DRS_Application * Application = (DRS_Application *)app;
    AT_Assert( Application, DRS_Application, "Application");
    DDP_Session * session = DDP_Session::PushSession();
    if(session)
        {
        if(!session->Load(Application))
            retErr = ERC_any;
        else
            Application->SetSessionClose(DDP_SessionClose);
        }
    }
    
catch (AT_Error &err)
    {
    if( session)
        session->PushError(err);
    retErr = ERC_any;
    }

return retErr;
}
//------------------------------------------------------------------------

void _ddp_call_c DDP_SessionClose()
{
    DRS_Application * app = (DRS_Application *)DRS_ApplicationGet();
    //Close session
    DDP_Session::PopSession(app);
}
//------------------------------------------------------------------------

DLL_Handle _ddp_call_c DDP_SessionGet()
{
    DRS_Application * app = (DRS_Application *)DRS_ApplicationGet();
    DDP_Session * session = DDP_Session::GetSession(app);
    if(!session)
        DDP_SessionOpen(NULL);
    else
        {
        if(!app)
            return 0;

        // test session and application consistency
        if(app == session->GetApplication())
            return (DLL_Handle)session;

        DDP_SessionOpen((DLL_Handle)app);
        }

return DDP_SessionGet();
}
//------------------------------------------------------------------------

DLL_Erc _ddp_call_c DDP_ProcessCount(uint16 * iCount)
{
    DLL_Erc retErr = ERC_none;
    DDP_Session * session = (DDP_Session *)DDP_SessionGet();
try
        {
        AT_Assert( session, session, "Session");
        *iCount = (uint16) session->GetJobCount();
        }
catch (AT_Error &err)
        {
        if( session)
            session->PushError( err);
        retErr = ERC_any;
        }

return(retErr);
}
//------------------------------------------------------------------------

DLL_Erc _ddp_call_c DDP_ProcessClearCache()
{
    DLL_Erc retErr = ERC_none;
    DDP_Session * session = (DDP_Session *)DDP_SessionGet();
try
    {
    AT_Assert( session, session, "Session");
    session->Clear();
    }
catch (AT_Error &err)
    {
    if( session)
        session->PushError( err);
    retErr = ERC_any;
    }

return(retErr);
}
//------------------------------------------------------------------------

DLL_Erc _ddp_call_c DDP_ProcessCachedNames(byte * buf, uint16 sz, uint16 * iCount)
{
    DLL_Erc retErr = ERC_none;
    DDP_Session * session = (DDP_Session *)DDP_SessionGet();
try
    {
    AT_Assert( session, session, "Session");
    *iCount = session->GetCachedProcessNames(buf, sz);
    }
catch (AT_Error &err)
    {
    if( session)
        session->PushError( err);
    retErr = ERC_any;
    }

return(retErr);
}
//------------------------------------------------------------------------


//////////////////////////////////////////////////////////////////////////////////////////////
// Data Fields
//////////////////////////////////////////////////////////////////////////////////////////////
DLL_Erc _ddp_call_c DDP_FieldCount( uint16 *pCount)
{
    DLL_Erc retErr = ERC_none;
    DDP_Session * session = (DDP_Session *)DDP_SessionGet();
try
    {
    const DDP_Layout * layout = session->GetFieldLayout();
    *pCount = (uint16) layout->GetCount();
    }
catch (AT_Error &err)
    {
    if( session)
        session->PushError( err);
    retErr = ERC_any;
    }
return(retErr);
}
//------------------------------------------------------------------------

DLL_Erc _ddp_call_c DDP_FieldGet( uint16 iField, DLL_HandlePtr pField)
{
    DLL_Erc retErr = ERC_none;
    DDP_Session * session = (DDP_Session *)DDP_SessionGet();
try
    {
    const DDP_Layout * layout = session->GetFieldLayout();
    *pField = (DLL_Handle)layout->Get(iField);
    }
catch (AT_Error &err)
    {
    if( session)
        session->PushError( err);
    retErr = ERC_any;
    }
return(retErr);
}
//------------------------------------------------------------------------

DLL_Erc _ddp_call_c DDP_FieldOpen( uint16 iField, DLL_HandlePtr pField)
{
    DLL_Erc retErr = ERC_none;
    DDP_Session * session = (DDP_Session *)DDP_SessionGet();
try
    {
    const DDP_Layout * layout = session->GetFieldLayout();
    const DDP_Field * defField = layout->Get(iField);
    if(!defField)
        {
        session->PushError("Failed to open field for id: %u Field can not be found in the application", iField);
        return ERC_any;
        }

    DDP_Field * p = new DDP_Field(*defField);
    *pField = (DLL_Handle)p;
    }
catch (AT_Error &err)
    {
    if( session)
        session->PushError( err);
    retErr = ERC_any;
    }
return(retErr);
}
//------------------------------------------------------------------------

DLL_Erc _ddp_call_c DDP_FieldClose( DLL_HandlePtr pField)
{
    DLL_Erc retErr = ERC_none;
    DDP_Session * session = (DDP_Session *)DDP_SessionGet();
try
    {
    DDP_Field * field = (DDP_Field *)pField;
    delete field;
    }
catch (AT_Error &err)
    {
    if( session)
        session->PushError( err);
    retErr = ERC_any;
    }
return( retErr);
}
//------------------------------------------------------------------------

DLL_Erc _ddp_call_c DDP_FieldUserNameSet( DLL_Handle hField, const byte * name)
{
    DLL_Erc retErr = ERC_none;
    DDP_Session * session = (DDP_Session *)DDP_SessionGet();    
try
    {
    if(!hField)
        return ERC_any;
    DDP_Field * pField = (DDP_Field *)hField;
    pField->SetUserDefinedName(name);
    }
catch (AT_Error &err)
    {
    if( session)
        session->PushError( err);
    retErr = ERC_any;
    }
return(retErr);
}

//------------------------------------------------------------------------
// Vector Def
DLL_Erc _ddp_call_c DDP_VectorDefOpen( uint16 iVectorID, VectorAddress VectorAddress[], uint Formula, uint Period, DLL_HandlePtr VectorDef)
{
    DLL_Erc retErr = ERC_none;
    DDP_Session * session = (DDP_Session *)DDP_SessionGet();
try
    {
    const DDP_VectorTree::RootNode * vroot = session->GetVectorRoot(iVectorID);
    if(!vroot)
        {
        if(session)
            session->PushError("Failed to open vector definition for vector id: %u", iVectorID);
        return ERC_any;
        }
    *VectorDef = (DLL_Handle) new DDP_VectorDef(vroot, VectorAddress, Formula, Period);
    }
catch (AT_Error &err)
    {
    if( session)
        session->PushError( err);
    *VectorDef = NULL;
    retErr = ERC_any;
    }
return( retErr);
}
//------------------------------------------------------------------------

DLL_Erc _ddp_call_c DDP_VectorDefIndexState( DLL_Handle VectorDef, uint IndexState)
{
    DLL_Erc retErr = ERC_none;
    DDP_Session * session = (DDP_Session *)DDP_SessionGet();
try
    {
    DDP_VectorDef * def = (DDP_VectorDef *)VectorDef;
    if(!def)
        return ERC_any;
    if(!def->SetIndexState(IndexState))
        return ERC_any;
    }
catch (AT_Error &err)
    {
    if( session)
        session->PushError( err);
    retErr = ERC_any;
    }
return( retErr);
}
//------------------------------------------------------------------------

DLL_Erc _ddp_call_c DDP_VectorDefUserNameSet( DLL_Handle hVectorDef, const byte * name)
{
    DLL_Erc retErr = ERC_none;
    DDP_Session * session = (DDP_Session *)DDP_SessionGet();
try
    {
    DDP_VectorDef * def = (DDP_VectorDef *)hVectorDef;
    if(!def)
        return ERC_any;
    def->SetUserDefinedName(name);
    }
catch (AT_Error &err)
    {
    if( session)
        session->PushError( err);
    retErr = ERC_any;
    }
return( retErr);
}
//------------------------------------------------------------------------

DLL_Erc _ddp_call_c DDP_VectorDecimalsSet( DLL_Handle hVectorDef, uint iDecimals)
{
    DLL_Erc retErr = ERC_none;
    DDP_Session * session = (DDP_Session *)DDP_SessionGet();
try
    {
    DDP_VectorDef * def = (DDP_VectorDef *)hVectorDef;
    if(!def)
        return ERC_any;
    def->SetDecimals(iDecimals);
    }
catch (AT_Error &err)
    {
    if( session)
        session->PushError( err);
    retErr = ERC_any;
    }
return( retErr);
}
//------------------------------------------------------------------------

DLL_Erc _ddp_call_c DDP_VectorDefClose( DLL_Handle VectorDef)
{
    DLL_Erc retErr = ERC_none;
    DDP_Session * session = (DDP_Session *)DDP_SessionGet();
try
    {
    DDP_VectorDef * def = (DDP_VectorDef *)VectorDef;
    delete def;
    }
catch (AT_Error &err)
    {
    if( session)
        session->PushError( err);
    retErr = ERC_any;
    }
return( retErr);
}
//------------------------------------------------------------------------

// Export Field
DLL_Erc _ddp_call_c DDP_ExportFieldUserNameSet( DLL_Handle hExpField, const byte * name)
{
    DLL_Erc retErr = ERC_none;
    DDP_Session * session = (DDP_Session *)DDP_SessionGet();
try
    {
    DDP_ExportDef * expDef = (DDP_ExportDef *)hExpField;
    //DDP_Field * expDef = (DDP_Field *)hExpField;
    if(!expDef)
        return ERC_any;
    expDef->SetUserDefinedName(name);
    }
catch (AT_Error &err)
    {
    if( session)
        session->PushError( err);
    retErr = ERC_any;
    }
return( retErr);
}
//------------------------------------------------------------------------

DLL_Erc _ddp_call_c DDP_ExportFieldActionSet( DLL_Handle hExpField, uint Action)
{
    DLL_Erc retErr = ERC_none;
    DDP_Session * session = (DDP_Session *)DDP_SessionGet();
try
    {
    DDP_ExportDef * expDef = (DDP_ExportDef *)hExpField;
    if(!expDef)
        return ERC_any;
    expDef->SetActionType((DART_AggregationType)Action);
    }
catch (AT_Error &err)
    {
    if( session)
        session->PushError( err);
    retErr = ERC_any;
    }
return( retErr);
}
//------------------------------------------------------------------------

DLL_Erc _ddp_call_c DDP_ExportFieldSortSet( DLL_Handle hExpField, uint Sort)
{
    DLL_Erc retErr = ERC_none;
    DDP_Session * session = (DDP_Session *)DDP_SessionGet();
try
    {
    DDP_ExportDef * expDef = (DDP_ExportDef *)hExpField;
    if(!expDef)
        return ERC_any;
    expDef->SetSortVal((DART_SortValue)Sort);
    }
catch (AT_Error &err)
    {
    if( session)
        session->PushError( err);
    retErr = ERC_any;
    }
return( retErr);
}
//------------------------------------------------------------------------

// Macro Def
DLL_Erc _ddp_call_c DDP_MacroRecord( const byte * fn)
{
    DLL_Erc retErr = ERC_none;
    DDP_Session * session = (DDP_Session *)DDP_SessionGet();
try
    {
    AT_Assert( session, session, "Session");
    AT_Assert( fn, fn, "Macro File Name is NULL");
    session->RecordMacro(fn);
    }
catch (AT_Error &err)
    {
    if( session)
        session->PushError( err);
    retErr = ERC_any;
    }
return(retErr);
}
//------------------------------------------------------------------------

DLL_Erc _ddp_call_c DDP_MacroStop()
{
    DLL_Erc retErr = ERC_none;
    DDP_Session * session = (DDP_Session *)DDP_SessionGet();
try
    {
    AT_Assert( session, session, "Session");
    session->StopMacro();
    }
catch (AT_Error &err)
    {
    if( session)
        session->PushError( err);
    retErr = ERC_any;
    }
return(retErr);
}
//------------------------------------------------------------------------

DLL_Erc _ddp_call_c DDP_MacroRun( const byte * fn)
{
    DLL_Erc retErr = ERC_none;
    DDP_Session * session = (DDP_Session *)DDP_SessionGet();
try
    {
    AT_Assert( session, session, "Session");
    AT_Assert( fn, fn, "Macro File Name is NULL");
    session->RunMacro(fn);
    }
catch (AT_Error &err)
    {
    if( session)
        session->PushError( err);
    retErr = ERC_any;
    }
return(retErr);
}
//------------------------------------------------------------------------
// Report
DLL_Erc _ddp_call_c DDP_ReportDumpParams(DLL_Handle pRecordList,
    DLL_Handle Fields, DLL_Handle Vectors)
{
    DLL_Erc retErr = ERC_none;
    DDP_Session * session = (DDP_Session *)DDP_SessionGet();
try
    {
    AT_Record_List * list = (AT_Record_List *)pRecordList;
    const DDP_ExportDef** gs = (const DDP_ExportDef**)Fields;
    const DDP_ExportDef** vs = (const DDP_ExportDef**)Vectors;

    if(!session->DumpReportParams(list, gs, vs))
        return ERC_any;
    }
catch (AT_Error &err)
    {
    if( session)
        session->PushError( err);
    retErr = ERC_any;
    }
return( retErr);
}
//------------------------------------------------------------------------
// Browse Export
DLL_Erc _ddp_call_c DDP_BrowseDumpParams(DLL_Handle pIndex, DLL_Handle Fields,
    const byte * FieldTypes, const byte * FileName, const byte * ExpType)
{
    DLL_Erc retErr = ERC_none;
    DDP_Session * session = (DDP_Session *)DDP_SessionGet();
try
    {
    const AT_Index * index = (const AT_Index *)pIndex;
    const void** fs = (const void**)Fields;

    if(!session->DumpBrowseParams(index, fs, FieldTypes, FileName, ExpType))
        return ERC_any;
    }
catch (AT_Error &err)
    {
    if( session)
        session->PushError( err);
    retErr = ERC_any;
    }
return( retErr);
}

//------------------------------------------------------------------------
//////////////////////////////////////////////////////////////////////////
// Export
//////////////////////////////////////////////////////////////////////////
DLL_Erc _ddp_call_c DDP_ExportDumpParams(DLL_Handle pRecordList,
    DLL_Handle Fields, const byte * FieldTypes, DLL_Handle SplitFields,
    const byte * FileName, const byte * ExpType, byte AutoSave)
{
    DLL_Erc retErr = ERC_none;
    DDP_Session * session = (DDP_Session *)DDP_SessionGet();
try
    {
    AT_Record_List * list = (AT_Record_List *)pRecordList;
    const void** fs = (const void**)Fields;
    const DDP_Field** ss = (const DDP_Field**)SplitFields;

    if(!session->DumpExportParams(list, fs, FieldTypes, ss,
            FileName, ExpType, AutoSave))
        return ERC_any;
    }
catch (AT_Error &err)
    {
    if( session)
        session->PushError( err);
    retErr = ERC_any;
    }
return( retErr);
}
//------------------------------------------------------------------------

DLL_Erc _ddp_call_c DDP_RecList_Dump( DLL_Handle pRecList, char *file)
{
    DLL_Erc retErr = ERC_none;
    DDP_Session * session = (DDP_Session *)DDP_SessionGet();
try
    {
    AT_Record_List * list = (AT_Record_List *)pRecList;
    if(!session->DumpRecList(list, PORT_cbyte(file) ))
        return ERC_any;
    }
catch (AT_Error &err)
    {
    if( session)
        session->PushError( err);
    retErr = ERC_any;
    }
return(retErr);
}
//------------------------------------------------------------------------

//////////////////////////////////////////////////////////////////////////
// Process
//////////////////////////////////////////////////////////////////////////
DLL_Erc _ddp_call_c DDP_InitProcess()
{
    DLL_Erc retErr = ERC_none;
    DDP_Session * session = (DDP_Session *)DDP_SessionGet();
try
    {
    AT_Assert( session, DDP_Session, "Session");
    session->OpenProcessParams();
    }
catch (AT_Error &err)
    {
    if( session)
        session->PushError( err);
    retErr = ERC_any;
    }
return( retErr);
}
//------------------------------------------------------------------------

DLL_Erc _ddp_call_c DDP_ProcessParamSet( const byte * key, const byte * value)
{
    DLL_Erc retErr = ERC_none;
    DDP_Session * session = (DDP_Session *)DDP_SessionGet();
try
    {
    AT_Assert( session, DDP_Session, "Session");
    session->SetParam(key, value);
    }
catch (AT_Error &err)
    {
    if( session)
        session->PushError( err);
    retErr = ERC_any;
    }
return( retErr);
}
//------------------------------------------------------------------------

DLL_Erc _ddp_call_c DDP_ProcessParamObjectSet( const byte * key, DLL_Handle object, uint size)
{
    DLL_Erc retErr = ERC_none;
    DDP_Session * session = (DDP_Session *)DDP_SessionGet();
try
    {
    AT_Assert( session, DDP_Session, "Session");
    session->SetParam(key, (const void *)object, size);
    }
catch (AT_Error &err)
    {
    if( session)
        session->PushError( err);
    retErr = ERC_any;
    }
return( retErr);
}
//------------------------------------------------------------------------

DLL_Erc _ddp_call_c DDP_StartProcess()
{
    DLL_Erc retErr = ERC_none;
    DDP_Session * session = (DDP_Session *)DDP_SessionGet();
try
    {
    AT_Assert( session, DDP_Session, "Session");
    if(!session->Execute())
        return ERC_any;
    }
catch (AT_Error &err)
    {
    if( session)
        session->PushError( err);
    retErr = ERC_any;
    }
return( retErr);
}
//------------------------------------------------------------------------
//////////////////////////////////////////////////////////////////////////
// Exceptions
//////////////////////////////////////////////////////////////////////////

uint16 _ddp_call_c DDP_ErrorPop( byte *sDest, uint16 iDestLen)
{
    DDP_Session * session = (DDP_Session *)DDP_SessionGet();
try
    {
    if( session && sDest && iDestLen)
        return session->PopError(sDest, iDestLen);
    else
        return 0;
    }
catch (AT_Error &err)
    {
    if( session)
        session->PushError( err);
    }
return 0;
}
//------------------------------------------------------------------------

DLL_Erc _ddp_call_c DDP_ErrorGet( uint16 num, byte *sDest, uint16 iDestLen)
{
    DLL_Erc retErr = ERC_none;
    DDP_Session * session = (DDP_Session *)DDP_SessionGet();
try
    {
    if( session && sDest && iDestLen)
        {
        const byte * err = session->GetError(num);
        if(err)
            PORT_strncpy(sDest, err, iDestLen);
        }
    else
        return ERC_any;
    }
catch (AT_Error &err)
    {
    if( session)
        session->PushError( err);
    retErr = ERC_any;
    }
    
return retErr;
}
//------------------------------------------------------------------------

uint16 _ddp_call_c DDP_ErrorCount()
{
    DDP_Session * session = (DDP_Session *)DDP_SessionGet();
try
    {
    if( session)
        return session->GetErrorCount();
    else
        return 0;
    }
catch (AT_Error &err)
    {
    if( session)
        session->PushError( err);
    }
return 0;
}

//------------------------------------------------------------------------
// eof
