exec sql include sqlca;

 

#include <stdio.h>

#include <stdlib.h>

#include <errno.h>

#include <string.h>

#include <sys/types.h>

#include <sys/stat.h>

#include "event.h"

#include "hl7_getset.h"

#include "str2mesg.h"

 

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

 

$Source$

 

Purpose:    This process builds a ksh script using supplied days (argv[1] )

            in order to use shell utilities find/gunzip to find event.dat

            archives to open. The ksh script emits file names when completed.

 

            It runs the ksh script and streams stdout from it using popen.

            Each file name received from the ksh script is opened and

            lines retrieved using fgets. Event table columns are parsed

            from each line.

 

            This process expects each line to contain the following format:

            event_seq|seg_no|continued|calling_pgm|hl7_message

 

            Lines in the file must be sorted by event_seq and seg_no in

            ascending order.

 

            Column hl7_message is allowed to grow until continued column = 'n'.

            The hl7_message and all its continued segments are then submitted

            in their entirety to HIT rtif routine str2mesg() in order to

            build hl7_union_t uMsg.

           

            Selected hl7 segments are extracted from the resulting uMsg and

            inserted into a session table. ADC, AKC and DEL corrections

            elasped times are selected from the session table and written

            to pipe '|' seperated text files (ADC.txt, AKC.txt, DEL.txt)

 

Author:     Jeff Martin

 

$Date$

 

$Revision$

 

$Log$

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

 

static char rcsid[] = "$Id$";

 

FILE *fpFILE,                            // a file with '|' delimited event table columns

     *fpTXT,                             // output file handle

     *fpSCRIPT,                          // shell script and pipe for popen

     *fpPIPE;

 

char cTxt[512];                          // output filename

char *buf = (char *)'\0';                // pointer for fgets

char *pBuf;                              // save buf pointer

int iPipe = 0;                           // '|' counter

char *cSegNo;                            //event table columns

char *cCont;

char *cCallingPgm;

char *cMsg;

 

int  now_d;                              // current date                   

int  now_tm;                             // current time as HHMMSS         

char SQLDate[26];                        // converted hl7 date 

char *c;                                 // general purpose char*

int  i;                                  // general purpose integer

int iMax = 512;                          // guard against buffer overruns

int iRecCounter = 0;                     // count session.stay_corrections inserts

 

hl7_union_t *uMsg;                       // parsed hl7 message string

 

// hl7 segment spec_t's

spec_t msh_msg_type          = {"MSH", 8, 0, 0};  // MSH.8.0

spec_t msh_msg_evn           = {"MSH", 8, 1, 0};  // MSH.8.1

spec_t evn_timestamp         = {"EVN", 2, 0, 0};  // EVN.2.0

spec_t evn_operator_id       = {"EVN", 5, 0, 0};  // EVN.5

spec_t pid_pat_id_int        = {"PID", 3, 0, 0};  // PID.3

spec_t pv1_admit_dt_tm       = {"PV1", 44, 0, 0}; // PV1.44

spec_t zv2_eff_dt_from       = {"ZV2", 25, 0, 0}; // ZV2.25

spec_t zv2_eff_from_dt       = {"ZV2", 66, 0, 0}; // ZV2.66

spec_t zv2_trans_from_dt     = {"ZV2", 57, 0, 0}; // ZV2.57

spec_t zv2_stay_visit_seq_no = {"ZV2", 6, 0, 0};  // ZV2.6

spec_t zv1_ifc_cor_tp        = {"ZV1", 1,  0, 0}; // ZV1.1

spec_t zv1_xfer_types        = {"ZV1", 3,  0, 0}; // ZV1.3

spec_t zvn_upd_by            = {"ZVN", 1, 0, 0};  // ZVN.1

spec_t zvn_screen_id         = {"ZVN", 3, 0, 0};  // ZVN.3

 

EXEC SQL BEGIN DECLARE SECTION;

  char dbname[26];

  char error_text[512];

 

  char sqlEvent_timestamp[32];

  char sqlAdmit_dt_tm[32];

  char sqlEff_dt_from[32];

  char sqlTrans_from_dt[32];

 

  char msg_type[9];                      

  char msg_evn[9];                     

  char event_timestamp[64];          

  char screen_id[64];              

  char admit_dt_tm[64];        

  char eff_dt_from[64];     

  char trans_from_dt[64];             

  char ifc_cor_tp[9];             

  char xfer_types[9];          

  char stay_visit_seq_no[16];        

  char pat_id[9];                

  char hosp[2];             

  char pat_no[8];                      

  char upd_by[16];                 

  char operator_id[16];         

  char comment_txt[101];

  char cFile[512];            

  char *cEvnSeq;        

  char elasped_time[32];

EXEC SQL END DECLARE SECTION;

 

 

///////function prototypes ////////////////

int CreateScript(char *);                // create script stay_corrections.sh

int CreateSessionTable();                // create session.stay_corrections

int ParseEventDat();                     // parse event.dat archive files

int CreateAKC();                         // create AKC.txt

int CreateADC();                         // create ADC.txt

int CreateDEL();                         // create DEL.txt

int CreateMIN();                         // create MIN.txt

int CreateMAX();                         // create MAX.txt

 

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

*****************     MAIN       ************************************

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

 

int

main(int argc, char *argv[]) {

 

  // check user interface

  if( argc != 3) {

     fprintf(stdout,"\nusage: %s <DBNAME> <DAYS>\n", argv[0] );

     fprintf(stdout,"Ex: %s zerlina::regprod 7\n", argv[0] );

     fprintf(stdout,"To extract 7 days worth of events.\n\n");

     exit(-1);

  }

  fprintf(stdout,"%s\n\n",rcsid);

 

  // connect to the database

  strncpy(dbname, argv[1], 25);

  //EXEC SQL whenever sqlerror call sqlprint;

  EXEC SQL connect :dbname;

 

  if(sqlca.sqlcode) {

    error_text[0] = '\0';

    EXEC SQL INQUIRE_SQL (:error_text = ERRORTEXT);

    fprintf(stderr,"Can't connect to %s\nError %d: %s\n",

            dbname,sqlca.sqlcode,error_text);

    exit(-1);

  }

 

  // create a session table of stay corrections elasped time

  fprintf(stdout,"%d %d:  Creating session table stay_corrections...\n",

          hl7_getdate(), gettime() );

 

  if( CreateSessionTable() != 0) {

     exit(-1);

  }

 

  // use ksh utilities find and gzip to stream a list of event archives 

  fprintf(stdout,"%d %d:  Creating script stay_corrections.sh...\n",

          hl7_getdate(), gettime() );

 

  if( CreateScript( argv[2] ) != 0 ) {

     fprintf(stderr,"\nCan't create script: stay_corrections.sh\n");

     exit(-1);

  }

 

  // set execute permissions to all.

  i = system("chmod a+x stay_corrections.sh");

 

  // stream shell stdout using popen

  fpPIPE = popen("stay_corrections.sh", "r");

 

  if( fpPIPE == NULL) {

     fprintf(stderr,"\nCan't execute script: stay_corrections.sh\n");

     perror("stay_corrections.sh");

     exit(-1);

  }

 

  ////////////////////////////////////////////////////////////

  // open each file name emitted by the script

  while( fscanf(fpPIPE,"%s",cFile) != EOF) {

 

     if(! (fpFILE = fopen(cFile,"r"))) {

        fprintf(stderr,"\nCan't open %s\n",cFile);

        perror(cFile);

        continue;

        //exit(-1);

     }

 

     // parse segments from event.dat archived messages and

     // insert them into session.stay_corrections table

     if( ParseEventDat() != 0) {

        exit(-1);

     }

 

  } // fpPIPE EOF

 

  //////////////////////////////////////////////////////////////

  // select AKC elasped times from session.stay_corrections

  // and write to AKC.txt

  if( CreateAKC() != 0) {

     exit(-1);

  }

 

  // select ADC elasped times from session.stay_corrections

  // and write to ADC.txt

  if( CreateADC() != 0) {

     exit(-1);

  }

 

  // select DEL elasped times from session.stay_corrections

  // and write to DEL.txt

  if( CreateDEL() != 0) {

     exit(-1);

  }

 

  // select MIN elasped times from session.stay_corrections

  // and write to MIN.txt

  if( CreateMIN() != 0) {

     exit(-1);

  }

 

  // select MAX elasped times from session.stay_corrections

  // and write to MAX.txt

  if( CreateMAX() != 0) {

     exit(-1);

  }

 

  ///////////////////////////////////////////////////////////////

  // free memory

  free(buf);

  free(cMsg);

  free(cCont);

  free(cEvnSeq);

  free(cSegNo);

  free(cCallingPgm);

  freemesg(uMsg);

 

  // close the shell pipe and remove the temp directory

  pclose(fpPIPE);

  i = rmdir("stay_corrections_tmp");

 

  fprintf(stdout,"%d %d:  %s finished.\n",

          hl7_getdate(), gettime(), argv[0] );

 

exit(0);

}

 

 

//////////////////////////////////////////////////////////////////////////////

//                   Functions

 

//////////////////////////////////////////////////////////////////////////////

// Create script stay_corrections.sh 

//

// stay_corrections.sh gets a list of event_dat archives newer then 

// Argv days from today. It unzips them into a subfolder and returns 

// a stdout stream containing their names. It then deletes the subfolder.

/////////////////////////////////////////////////////////////////////////////

int

CreateScript (char * Argv) {

 

  if(! (fpSCRIPT = fopen("stay_corrections.sh", "w"))) {

        printf("Can't create stay_corrections.sh\n");

        perror("stay_corrections.sh");

        return(-1);

  }

 

  fprintf(fpSCRIPT,"#!/usr/bin/ksh\n#\n"

  "#*******************************************************************         \n"

  "# Created by: %s                                                             \n"

  "#*******************************************************************         \n"

  "# set enviornment                                                            \n"

  "if [[ -n $REGENV ]] ; then                                                   \n"

  "  # aliases aren't inhereted from the shell so they need to be sourced again \n"

  "  . $REGENV/reg.commands                                                     \n"

  "else                                                                         \n"

  "  #source reg.kshvar based upon file location.                               \n"

  "  . ${.sh.file%/reg//scripts*}/reg.kshvar                                    \n"

  "fi                                                                         \n\n"

  "#********************************************************************        \n"

  "# get hostname and event directory                                           \n"

  "#********************************************************************        \n"

  "EVNHOST=`hostname -s`                                                        \n"

  "EVNDIR=%c$DBADATA/event%c                                                  \n\n"

  "# dev/unit test                                                              \n"

  "if [[ $EVNHOST = %cjunior%c ]]; then                                         \n"

  "EVNDIR=%c/home/pms/jemartin/jeff_src/make/stay_corrections%c                 \n"

  "EVNHOST=%czerlina%c                                                          \n"

  "fi                                                                         \n\n"

  "#*******************************************************************         \n"

  "# unzip event archives in sub-folder                                         \n"

  "#*******************************************************************         \n"

  "find $EVNDIR -name %cevent.dat.$EVNHOST.*%c -mtime -%s -print > $0.find    \n\n"

  "#*******************************************************************         \n"

  "# create a sub-folder to unzip files in                                      \n"

  "#*******************************************************************         \n"

  "if [[ -d stay_corrections_tmp ]] ; then                                      \n"

  "  rm -f stay_corrections_tmp/* > /dev/null 2>&1                              \n"

  "fi                                                                           \n"

  "if [[ ! -d  stay_corrections_tmp ]] ; then                                   \n"

  "  mkdir stay_corrections_tmp                                                 \n"

  "fi                                                                         \n\n"

  "cat $0.find |                                                                \n"

  "while read ENVFILE                                                           \n"

  "do                                                                           \n"

  " cp -f $ENVFILE stay_corrections_tmp > /dev/null 2>&1                        \n"

  "done                                                                         \n"

  "gunzip -rf stay_corrections_tmp > /dev/null 2>&1                            \n\n"

  "#*******************************************************************         \n"

  "# emit file names.                                                           \n"

  "#*******************************************************************         \n"

  "find stay_corrections_tmp -print > $0.find                                   \n"

  "cat $0.find |                                                                \n"

  "while read ENVFILE                                                           \n"

  "do                                                                           \n"

  "   if [[ -f $ENVFILE ]] ; then                                               \n"

  "     echo $ENVFILE                                                           \n"

  "   fi                                                                        \n"

  "done                                                                       \n\n"

  "rm -f $0.find                                                                \n"

 

  ,rcsid,34,34,34,34,34,34,34,34,34,34,Argv); // 34 = double quotes

 

  // flush write buffers before popen is called later..

  fflush(fpSCRIPT);

  fclose(fpSCRIPT);

 

  return(0);

}

 

 

//////////////////////////////////////////////////////////////////////////////

// CreateSessionTable

//

// create session table stay_corrections. this is the target for selected

// events found in $DBADATA/event archive. it is the source of flat files

// written after all events in the target range have been collected.

//////////////////////////////////////////////////////////////////////////////

int

CreateSessionTable() {

 

  EXEC SQL

  declare global temporary table

  session.stay_corrections

  (    

    event               varchar(8),

    event_timestamp     date,

    hosp                varchar(1),

    pat_no              varchar(7),

    screen_id           varchar(16),

    admit_dt_tm         date,

    trans_from_dt       date,

    stay_visit_seq_no   varchar(16),

    ifc_cor_tp          varchar(8),

    xfer_types          varchar(8),

    operator_id         varchar(16),

    upd_by              varchar(16),

    event_seq_no        varchar(8),

    event_dat_file      varchar(128))

  on commit preserve rows with norecovery;

 

  if( sqlca.sqlcode) {

     error_text[0] = '\0';

     EXEC SQL INQUIRE_SQL (:error_text = ERRORTEXT);

     fprintf(stdout,"Can't create session.stay_corrections.\n"

                    "Error %d: %s\n",sqlca.sqlcode, error_text);

     return(-1);

  }

 

return(0);

}

 

//////////////////////////////////////////////////////////////////////////////

// ParseEventDat

//

// Parses lines from pipe delimited event.dat archives and inserts

// selected hl7 segments into session.stay_corrections table.

/////////////////////////////////////////////////////////////////////////////

int

ParseEventDat() {

 

  // allocate string for fgets

  if(!buf ) {

    if( !(buf = (char *)calloc(8193,sizeof(char)) )) {

       fprintf(stderr,"\nCan`t allocate 8193 bytes\n");

       return(-1);

    }

    // save the first address because I will

    // increment it in order to parse it.

    pBuf = buf;

 

    // allocate strings to parse event.dat columns in.

    // these should be large (don't trust hl7 values).

    if( !(cMsg = (char *)calloc(8193,sizeof(char)) )) {

       fprintf(stderr,"\nCan`t allocate 8193 bytes\n");

       return(-1);

    }

    if( !(cCont = (char *)calloc(iMax+1,sizeof(char)) )) {

       fprintf(stderr,"\nCan`t allocate %d bytes\n",iMax+1);

       return(-1);

    }

    if( !(cEvnSeq = (char *)calloc(iMax+1,sizeof(char)) )) {

       fprintf(stderr,"\nCan`t allocate %d bytes\n",iMax+1);

       return(-1);

    }

    if( !(cSegNo = (char *)calloc(iMax+1,sizeof(char)) )) {

       fprintf(stderr,"\nCan`t allocate %d bytes\n",iMax+1);

       return(-1);

    }

    if( !(cCallingPgm = (char *)calloc(iMax+1,sizeof(char)) )) {

       fprintf(stderr,"\nCan`t allocate %d bytes\n",iMax+1);

       return(-1);

    }

  }

 

  // these are all global

  *buf = (char )'\0';

  *cCont = (char )'\0';

  *cEvnSeq = (char )'\0';

  *cSegNo = (char )'\0';

  *cCallingPgm = (char )'\0';

 

  sqlEvent_timestamp[0] = (char )'\0';

  sqlAdmit_dt_tm[0] = (char )'\0';

  sqlTrans_from_dt[0] = (char )'\0';

 

  event_timestamp[0] = (char )'\0';

  admit_dt_tm[0] = (char )'\0';

  eff_dt_from[0] = (char )'\0';

  trans_from_dt[0] = (char )'\0';

 

  msg_type[0]  = (char )'\0';

  msg_evn[0]  = (char )'\0';

  screen_id[0] = (char )'\0';

  ifc_cor_tp[0] = (char )'\0';

  xfer_types[0] = (char )'\0';

  stay_visit_seq_no[0] = (char )'\0';

  pat_id[0] = (char )'\0';

  hosp[0] = (char )'\0';

  pat_no[0] = (char )'\0';

  operator_id[0] = (char )'\0';

  upd_by[0] = (char )'\0';

 

 

///////////////////////////////////////////////////////////////////////////

  fprintf(stdout,"%d %d:  Processing %s...\n",

          hl7_getdate(), gettime(), cFile );

 

  // buf is null terminated by fgets up to 8192 bytes

  while( fgets(buf,8192,fpFILE) != NULL) {

     // parse event table columns

     while( *buf) {

            switch( *buf) {

               // uncomment this to print the message on stdout..

               // case '\r':

               //      break;

               case '\n':

                  break;

               case '|':

                   // count pipes

                   iPipe++;

 

                   // reset buffer counter

                   i=0;

    

                   // ignore pipes in the header         

                   if( iPipe > 4) {

                      strncat(cMsg,buf,1);

                   }

                   break;

               default:

                    switch (iPipe) {

                       // parse header

                       case 0:

                           if( i >= iMax) {

                             break;

                           }

                           i++;

 

                           strncat(cEvnSeq,buf,1);

                           break;

                       case 1:

                           if( i >= iMax) {

                              break;

                           }

                           i++;

 

                           strncat(cSegNo,buf,1);

                           break;

                       case 2:

                           if( i >= iMax) {

                              break;

                           }

                           i++;

 

                           strncat(cCont,buf,1);

                           break;

                       case 3:

                           if( i >= iMax) {

                              break;

                           }

                           i++;

 

                           strncat(cCallingPgm,buf,1);

                           break;

                       // parse message

                       default:

                           if( i >= 8192) {

                              break;

                           }

                           i++;

 

                           strncat(cMsg,buf,1);

                           break;

                       }

            }

            buf++;   // increment buf

     }               // buf terminated

 

     // the message is not continued

     if( strncmp(cCont,"y",1) != 0) {

       // fprintf(stdout,"%s %s %s %s\r",cEvnSeq,cSegNo,cCont,cCallingPgm);

         

        // build hl7_union_t

        if( cMsg) {

           uMsg = str2mesg(cMsg);

        }

        if( uMsg) {

          // printmesg(uMsg);

 

          if((c=(char *)hl7_get(uMsg, &zvn_screen_id, 1, 0, NULL, 0, 0)) != NULL) {

               strncpy(screen_id, c, 32);

          }

          if((c=(char *)hl7_get(uMsg, &msh_msg_evn, 1, 0, NULL, 0, 0)) != NULL) {

                  strncpy(msg_evn, c, 8);

          }

 

          // gather only screen_id BICOR and BUADC, ignore A08's

          if( (strncmp(screen_id,"BICOR",32) == 0  ||

               strncmp(screen_id,"BUADC",32) == 0) &&

               strncmp(msg_evn,"A08",3) != 0) {             

 

               // get hl7 segment values

               if((c=(char *)hl7_get(uMsg, &pid_pat_id_int, 1, 0, NULL, 0, 0)) != NULL) {

                  strncpy(pat_id, c, 8);

               }

               sscanf(pat_id,"%c%s",&hosp,&pat_no);

 

               if((c=(char *)hl7_get(uMsg, &zvn_upd_by, 1, 0, NULL, 0, 0)) != NULL) {

                  strncpy(upd_by, c, 16);

               }

               if((c=(char *)hl7_get(uMsg, &msh_msg_type, 1, 0, NULL, 0, 0)) != NULL) {

                  strncpy(msg_type, c, 8);

               }

               if((c=(char *)hl7_get(uMsg, &evn_timestamp, 1, 0, NULL, 0, 0)) != NULL) {

                  strncpy(event_timestamp, c, 32);

               }

               if((c=(char *)hl7_get(uMsg, &evn_operator_id, 1, 0, NULL, 0, 0)) != NULL) {

                  strncpy(operator_id, c, 16);

               }

               if((c=(char *)hl7_get(uMsg, &pv1_admit_dt_tm, 1, 0, NULL, 0, 0)) != NULL) {

                  strncpy(admit_dt_tm, c, 32);

               }

               if((c=(char *)hl7_get(uMsg, &zv2_trans_from_dt, 1, 0, NULL, 0, 0)) != NULL) {

                  strncpy(trans_from_dt, c, 32);

               }

               if((c=(char *)hl7_get(uMsg, &zv1_ifc_cor_tp, 1, 0, NULL, 0, 0)) != NULL) {

                  strncpy(ifc_cor_tp, c, 8);

               }

               if((c=(char *)hl7_get(uMsg, &zv1_xfer_types, 1, 0, NULL, 0, 0)) != NULL) {

                  strncpy(xfer_types, c, 8);

               }

               if((c=(char *)hl7_get(uMsg, &zv2_stay_visit_seq_no, 1, 0, NULL, 0, 0)) != NULL) {

                  strncpy(stay_visit_seq_no, c, 12);

               }

 

               // 20081122150516

               // select date('11-22-2008 15:05:16')

               if( strncmp(event_timestamp,"00000000000",11) != 0 &&

                   event_timestamp[0] != (char )'\0') {

                  sprintf(sqlEvent_timestamp,"%c%c-%c%c-%c%c%c%c %c%c:%c%c:%c%c",

                          event_timestamp[4],  event_timestamp[5],

                          event_timestamp[6],  event_timestamp[7],

                          event_timestamp[0],  event_timestamp[1],

                          event_timestamp[2],  event_timestamp[3],

                          event_timestamp[8],  event_timestamp[9],

                          event_timestamp[10], event_timestamp[11],

                          event_timestamp[12], event_timestamp[13]);

               }

               if( strncmp(admit_dt_tm,"00000000000",11) != 0 &&

                   admit_dt_tm[0] != (char )'\0') {

                  sprintf(sqlAdmit_dt_tm,"%c%c-%c%c-%c%c%c%c %c%c:%c%c:%c%c",

                          admit_dt_tm[4],  admit_dt_tm[5],

                          admit_dt_tm[6],  admit_dt_tm[7],

                          admit_dt_tm[0],  admit_dt_tm[1],

                          admit_dt_tm[2],  admit_dt_tm[3],

                          admit_dt_tm[8],  admit_dt_tm[9],

                          admit_dt_tm[10], admit_dt_tm[11],

                          admit_dt_tm[12], admit_dt_tm[13]);

               }

               if( strncmp(trans_from_dt,"00000000000",11) != 0 &&

                   trans_from_dt[0] != (char )'\0') {

                  sprintf(sqlTrans_from_dt,"%c%c-%c%c-%c%c%c%c %c%c:%c%c:%c%c",

                          trans_from_dt[4],  trans_from_dt[5],

                          trans_from_dt[6],  trans_from_dt[7],

                          trans_from_dt[0],  trans_from_dt[1],

                          trans_from_dt[2],  trans_from_dt[3],

                          trans_from_dt[8],  trans_from_dt[9],

                          trans_from_dt[10], trans_from_dt[11],

                          trans_from_dt[12], trans_from_dt[13]);

               }

 

               // insert a row into session.stay_corrections

               EXEC SQL

                  insert into session.stay_corrections values(

                        :msg_evn,

                        :sqlEvent_timestamp,

                        :hosp,

                        :pat_no,

                        :screen_id,

                        :sqlAdmit_dt_tm,

                        :sqlTrans_from_dt,

                        :stay_visit_seq_no,

                        :ifc_cor_tp,

                        :xfer_types,

                        :operator_id,

                        :upd_by,

                        :cEvnSeq,

                        :cFile);

 

                if( sqlca.sqlcode) {   

                     EXEC SQL INQUIRE_SQL (:error_text = ERRORTEXT);

                     fprintf(stdout,"Can't insert into session.stay_corrections.\n"

                             "Error %d: %s\n",sqlca.sqlcode, error_text);

                     return(-1);               

                }                    

                iRecCounter++;

                   

 

          } // if( (strncmp(screen_id,"BICOR",32)

        }  //  if( uMsg)

 

        *cMsg = (char )'\0';

     } // start a new message 

 

     // start a new line

     iPipe = 0;

     buf = pBuf;

     *buf = (char )'\0';

     *cCont = (char )'\0';

     *cEvnSeq = (char )'\0';

     *cSegNo = (char )'\0';

     *cCallingPgm = (char )'\0';

 

     sqlEvent_timestamp[0] = (char )'\0';

     sqlAdmit_dt_tm[0] = (char )'\0';

     sqlTrans_from_dt[0] = (char )'\0';

 

     event_timestamp[0] = (char )'\0';

     admit_dt_tm[0] = (char )'\0';

     eff_dt_from[0] = (char )'\0';

     trans_from_dt[0] = (char )'\0';

 

     msg_type[0]  = (char )'\0';                    

     msg_evn[0]  = (char )'\0';  

     screen_id[0] = (char )'\0';

     ifc_cor_tp[0] = (char )'\0';

     xfer_types[0] = (char )'\0';

     stay_visit_seq_no[0] = (char )'\0';

     pat_id[0] = (char )'\0';

     hosp[0] = (char )'\0';

     pat_no[0] = (char )'\0';

     operator_id[0] = (char )'\0';

     upd_by[0] = (char )'\0';

 

 

  }  // fpFILE EOF

  fprintf(stdout,"%d %d:  Loaded %d events.\n",

          hl7_getdate(), gettime(), iRecCounter );

  iRecCounter = 0;

 

  // close the event.dat copy and delete it

  fclose(fpFILE);

  i = unlink(cFile);

 

return(0);

}

 

//////////////////////////////////////////////////////////////////////////////

// CreateDEL

//

// Selects DEL elasped times from session.stay_corrections and writes them

// to DEL.txt

/////////////////////////////////////////////////////////////////////////////

int

CreateDEL() {

 

 

  sprintf(cTxt,"DEL.txt");

  if(! (fpTXT = fopen(cTxt, "w"))) {

      fprintf(stderr,"\nCan't create %s\n",cTxt);

      perror(cTxt);

      return(-1);

  }

 

  fprintf(stdout,"%d %d:  Writing %s...",

          hl7_getdate(), gettime(), cTxt);

 

  // select data from session.stay_corrections

  iRecCounter = 0;

 

 

  EXEC SQL SELECT SC.hosp,

                  SC.pat_no,

                  SC.admit_dt_tm,

                   C.comment_txt,

                  SC.operator_id,

                  SC.upd_by,

                  SC.screen_id,

                  SC.event_timestamp,

                  (SC.event_timestamp - SC.admit_dt_tm) elasped_time

           INTO   :hosp,

                  :pat_no,

                  :admit_dt_tm,

                  :comment_txt,

                  :operator_id,

                  :upd_by,

                  :screen_id,

                  :event_timestamp,

                  :elasped_time

           FROM   session.stay_corrections    SC,

                  del_stay            DS,

                  comment             c

           WHERE  SC.ifc_cor_tp         = 'DEL'

           AND    SC.event              = 'A11'

           AND    SC.stay_visit_seq_no  = DS.stay_visit_seq_no

           AND    DS.del_reason         = C.comment_key

           ORDER BY SC.hosp, SC.pat_no, (SC.event_timestamp - SC.admit_dt_tm) DESC;

 

EXEC SQL BEGIN;

 

 fprintf(fpTXT,"%s|%s|%s|%s|%s|%s|%s|%s|%s\n",

         hosp,

         pat_no,

         admit_dt_tm,

         comment_txt,

         operator_id,

         upd_by,

         screen_id,

         event_timestamp,

         elasped_time);

 

         iRecCounter++;

 

EXEC SQL END;

 

switch(sqlca.sqlcode) {

    // no error, got data

    case 0:

      break;

 

    // no error, no data

    case 100:

       break;

 

    /* an error occurred              */

    default:

      error_text[0] = '\0';

      EXEC SQL INQUIRE_SQL (:error_text = ERRORTEXT);

      fprintf(stdout,"\nCan't select DEL from session.stay_corrections.\n"

                     "Error %d: %s\n",sqlca.sqlcode, error_text);

     return(-1);

}

 

fprintf(stdout,"  Wrote %d DEL records.\n",

        iRecCounter );

 

fclose(fpTXT);

return(0);

}

 

//////////////////////////////////////////////////////////////////////////////

// CreateAKC

//

// Selects AKC elasped times from session.stay_corrections and writes them

// to AKC.txt                              

/////////////////////////////////////////////////////////////////////////////

int

CreateAKC() {

 

  sprintf(cTxt,"AKC.txt");

  if(! (fpTXT = fopen(cTxt, "w"))) {

      fprintf(stderr,"\nCan't create %s\n",cTxt);

      perror(cTxt);

      return(-1);

  }

 

  fprintf(stdout,"%d %d:  Writing %s...",

          hl7_getdate(), gettime(), cTxt );

 

  // select data from session.stay_corrections

  iRecCounter = 0;

  EXEC SQL SELECT hosp,

                  pat_no,

                  admit_dt_tm,

                  operator_id,

                  upd_by,

                  screen_id,

                  event,

                  event_timestamp,

                 (event_timestamp - admit_dt_tm) elasped_time

           INTO  :hosp,

                 :pat_no,

                 :admit_dt_tm,

                 :operator_id,

                 :upd_by,

                 :screen_id,

                 :msg_evn,

                 :event_timestamp,

                 :elasped_time

           FROM   session.stay_corrections

           WHERE  ifc_cor_tp = 'AKC'

           ORDER BY hosp, pat_no, (event_timestamp - admit_dt_tm) DESC;

 

EXEC SQL BEGIN;

 

 fprintf(fpTXT,"%s|%s|%s|%s|%s|%s|%s|%s|%s\n",

         hosp,

         pat_no,

         admit_dt_tm,

         operator_id,

         upd_by,

         screen_id,

         msg_evn,

         event_timestamp,

         elasped_time);

 

         iRecCounter++;

 

EXEC SQL END;

 

switch(sqlca.sqlcode) {

    // no error, got data

    case 0:

      break;

 

    // no error, no data

    case 100:

       break;

 

    /* an error occurred              */

    default:

      error_text[0] = '\0';

      EXEC SQL INQUIRE_SQL (:error_text = ERRORTEXT);

      fprintf(stdout,"\nCan't select AKC from session.stay_corrections.\n"

                     "Error %d: %s\n",sqlca.sqlcode, error_text);

     return(-1);

}

 

fprintf(stdout,"  Wrote %d AKC records.\n",

        iRecCounter );

 

fclose(fpTXT);

return(0);

}

 

 

//////////////////////////////////////////////////////////////////////////////

// CreateADC

//

// Selects ADC elasped times from session.stay_corrections and writes them

// to ADC.txt

/////////////////////////////////////////////////////////////////////////////

int

CreateADC() {

 

  sprintf(cTxt,"ADC.txt");

  if(! (fpTXT = fopen(cTxt, "w"))) {

      fprintf(stderr,"\nCan't create %s\n",cTxt);

      perror(cTxt);

      return(-1);

  }

 

  fprintf(stdout,"%d %d:  Writing %s...",

          hl7_getdate(), gettime(), cTxt);

 

  // select data from session.stay_corrections

  iRecCounter = 0;

  EXEC SQL SELECT hosp,

                  pat_no,

                  admit_dt_tm,

                  operator_id,

                  upd_by,

                  screen_id,

                  event,

                  event_timestamp,

                 (event_timestamp - admit_dt_tm) elasped_time

           INTO  :hosp,

                 :pat_no,

                 :admit_dt_tm,

                 :operator_id,

                 :upd_by,

                 :screen_id,

                 :msg_evn,

                 :event_timestamp,

                 :elasped_time

           FROM   session.stay_corrections

           WHERE  ifc_cor_tp = 'ADC'

           ORDER BY hosp, pat_no, (event_timestamp - admit_dt_tm) DESC;

 

EXEC SQL BEGIN;

 

 fprintf(fpTXT,"%s|%s|%s|%s|%s|%s|%s|%s|%s\n",

         hosp,

         pat_no,

         admit_dt_tm,

         operator_id,

         upd_by,

         screen_id,

         msg_evn,

         event_timestamp,

         elasped_time);

 

         iRecCounter++;

 

EXEC SQL END;

 

switch(sqlca.sqlcode) {

    // no error, got data

    case 0:

      break;

 

    // no error, no data

    case 100:

       break;

 

    /* an error occurred              */

    default:

      error_text[0] = '\0';

      EXEC SQL INQUIRE_SQL (:error_text = ERRORTEXT);

      fprintf(stdout,"\nCan't select ADC from session.stay_corrections.\n"

                     "Error %d: %s\n",sqlca.sqlcode, error_text);

     return(-1);

}

 

fprintf(stdout,"  Wrote %d ADC records.\n",

        iRecCounter );

 

fclose(fpTXT);

 

return(0);

}

 

//////////////////////////////////////////////////////////////////////////////

// CreateMIN

//

// Selects MIN elasped times from session.stay_corrections and writes them

// to MIN.txt

/////////////////////////////////////////////////////////////////////////////

int

CreateMIN() {

 

 

  sprintf(cTxt,"MIN.txt");

  if(! (fpTXT = fopen(cTxt, "w"))) {

      fprintf(stderr,"\nCan't create %s\n",cTxt);

      perror(cTxt);

      return(-1);

  }

 

  fprintf(stdout,"%d %d:  Writing %s...",

          hl7_getdate(), gettime(), cTxt);

 

  // select data from session.stay_corrections

  iRecCounter = 0;

 

  EXEC SQL SELECT hosp,

                  pat_no,

                  operator_id,

                  upd_by,

                  screen_id,

                  ifc_cor_tp,

                  event,

                  event_timestamp,

                  admit_dt_tm,

                 (event_timestamp - admit_dt_tm) max_elasped_time

            INTO  :hosp,

                  :pat_no,

                  :operator_id,

                  :upd_by,

                  :screen_id,

                  :ifc_cor_tp,

                  :msg_evn,

                  :event_timestamp,

                  :admit_dt_tm,

                  :elasped_time

            FROM   session.stay_corrections

           WHERE  (event_timestamp - admit_dt_tm) =

                  (SELECT MIN(event_timestamp - admit_dt_tm)

                   FROM   session.stay_corrections

                   WHERE  event != 'A08'

                   AND    ifc_cor_tp = 'AKC')

            AND   ifc_cor_tp = 'AKC'

            ORDER BY hosp, pat_no, (event_timestamp - admit_dt_tm) DESC;

 

EXEC SQL BEGIN;

 

 fprintf(fpTXT,"%s|%s|%s|%s|%s|%s|%s|%s|%s|%s\n",

         hosp,

         pat_no,

         operator_id,

         upd_by,

         screen_id,

         ifc_cor_tp,

         msg_evn,

         event_timestamp,

         admit_dt_tm,

         elasped_time);

 

         iRecCounter++;

 

EXEC SQL END;

 

switch(sqlca.sqlcode) {

    // no error, got data

    case 0:

      break;

 

    // no error, no data

    case 100:

       break;

 

    /* an error occurred              */

    default:

      error_text[0] = '\0';

      EXEC SQL INQUIRE_SQL (:error_text = ERRORTEXT);

      fprintf(stdout,"\nCan't select MIN from session.stay_corrections.\n"

                     "Error %d: %s\n",sqlca.sqlcode, error_text);

     return(-1);

}

 

fprintf(stdout,"  Wrote %d MIN records.\n",

        iRecCounter );

 

fclose(fpTXT);

return(0);

}

 

//////////////////////////////////////////////////////////////////////////////

// CreateMAX

//

// Selects MAX elasped times from session.stay_corrections and writes them

// to MAX.txt

/////////////////////////////////////////////////////////////////////////////

int

CreateMAX() {

 

 

  sprintf(cTxt,"MAX.txt");

  if(! (fpTXT = fopen(cTxt, "w"))) {

      fprintf(stderr,"\nCan't create %s\n",cTxt);

      perror(cTxt);

      return(-1);

  }

 

  fprintf(stdout,"%d %d:  Writing %s...",

          hl7_getdate(), gettime(), cTxt);

 

  // select data from session.stay_corrections

  iRecCounter = 0;

 

  EXEC SQL SELECT hosp,

                  pat_no,

                  operator_id,

                  upd_by,

                  screen_id,

                  ifc_cor_tp,

                  event,

                  event_timestamp,

                  admit_dt_tm,

                 (event_timestamp - admit_dt_tm) max_elasped_time

            INTO  :hosp,

                  :pat_no,

                  :operator_id,

                  :upd_by,

                  :screen_id,

                  :ifc_cor_tp,

                  :msg_evn,

                  :event_timestamp,

                  :admit_dt_tm,

                  :elasped_time

           FROM   session.stay_corrections

           WHERE  (event_timestamp - admit_dt_tm) =

                  (SELECT MAX(event_timestamp - admit_dt_tm)

                   FROM   session.stay_corrections