// Change History
// 12/03/03 AV Development: Code cleanup. const/none const
// 8/19/03 AV Development: Call DeleteRecord

#include "drs/drs_type.h"
#include "drs/drs_appl.h"
#include "drs/drs_rec.h"
#include "drs/drs_formula.h"

// ****************************************************************Nick Allen
// DRS_RecordSet
// **************************************************************************
DRS_RecordSet::DRS_RecordSet( DRS_Application const *pAppl, uint32 lRecNo) :
    RecNum(lRecNo), rAppl(*pAppl), AT_Records(pAppl->DatabaseCount(), NULL),
    IsNewRecord(false)
{
}

void DRS_RecordSet::Clear()
{
  for(std::vector<AT_Record const *>::iterator it = AT_Records.begin();
      it != AT_Records.end(); it++)
    {
      rAppl.DeleteRecord(*it);
      *it = NULL;
    }
  IsNewRecord = false;
}

DRS_RecordSet::~DRS_RecordSet()
{
  Clear();
}

AT_Record const * DRS_RecordSet::Record( uint db)
{
  AT_Record const * rec = AT_Records[db];
  if(!rec)
    {
    rec = rAppl.RecordGet(RecNum, db);
    AT_Records[db] = rec;
    }
  else
    {
    if(IsNewRecord)
      {
      if(!rAppl.RecordRead(RecNum, db, (AT_Record*)rec))
        return NULL;

      IsNewRecord = false;
      }
    }
    
  return rec;
}

inline uint32 DRS_RecordSet::VectorCount( uint iVectorIdx)
{
  uint db = rAppl.VectorDatabase(iVectorIdx);
  return Record(db)->getVectorCount();
}

void DRS_RecordSet::VectorGet( uint iVectorIdx, uint32 pVectorAddress[], uint iFormula, DRS_VectorDataElem* pResults[], bool *pIsAvail)
   { DRS_Formula * f = (DRS_Formula *)&rAppl.Vector(iVectorIdx).Formula(iFormula);
     f->GetVector( *this, pVectorAddress, pResults, pIsAvail); }


//Field retrievaL MOVED OUT. vECTOR NEXT!

//TO_DO
//Retrieval restructure:
//Previously, the record was used for vector retrieval, it went to the application
//for vector data retrieval.
//Now, users retrieve the DRS_Formula for the desired vector and the Formula retrieves
//its data from the parent vector.

//Tell the formula to get the data for this DRS_RecordSet. The formula accesses its parent for data.
//formula->vector->database to get AT_Record from DRS_RecordSet.

/*
Note that formulas load record stats on new record. Now, each formula access reloads
record stats. Record stats are valid for duration of record. BUT, since
each formula is static, it cannot be guaranteed that the record has not changed.
Simultaneous access by different processes (view/export/etc.) having different
record lists must be considered.
Solution: User retrieves a new formula (copy) for a record and retrieves vector data for the
formula. A new formula must be retrieved for each record.
*/

#if(0)
inline void DRS_RecordSet::VectorGetN( (uint)iVectorIdx, iVectorOcc, pVector, pVectorIDs, (uint)iVectorIdx))
{
  if( ! pRecord->pAT_Record->getVector( pVector, pVectorIDs, (uint)iVectorIdx))
    throw AT_Unexpected_Err;
}
#endif


