/* imp-history.p - Import History Notes */ /* Format of an import line: */ /* NOTE: If you do not have data for an item, replace the field with */ /* a '-'. DO NOT put bogus data in there as it is used when */ /* looking at a record to determine it's intent! */ /* */ /* # 1 - Student ID REQUIRED SPM ID of the student to record note for */ /* # 2 - History Code REQUIRED History code to import */ /* # 3 - History Date REQUIRED Effective date of history code */ /* # 4 - Comments OPTIONAL Comments for the history entry */ /* Include commons */ {common.i NEW} {spm.i} {p-init.i "'IMPORTER'"} {histutil.i DEFINE NEW} /* Constants */ &SCOPED WriteCmd PUT STREAM LogStream UNFORMATTED &SCOPED WriteHdr "#" LineCnt ", " ImportText[1] ", " ImportText[2] ", " ImportText[3] ": " &SCOPED WriteLog {&WriteCmd} {&WriteHdr} /* Define Variables */ DEFINE VARIABLE ImportFile AS CHARACTER NO-UNDO. DEFINE VARIABLE ExportFile AS CHARACTER NO-UNDO. DEFINE VARIABLE LogFile AS CHARACTER NO-UNDO. DEFINE VARIABLE ReplaceMode AS LOGICAL NO-UNDO. DEFINE VARIABLE ImportText AS CHARACTER EXTENT 50 NO-UNDO. DEFINE VARIABLE LineCnt AS INTEGER NO-UNDO. DEFINE VARIABLE AutoCreate AS LOGICAL NO-UNDO. /* Variables for statistics */ DEFINE VARIABLE TotalLineCnt AS INTEGER NO-UNDO. DEFINE VARIABLE StartTime AS INTEGER NO-UNDO. DEFINE VARIABLE LastUpdate AS INTEGER NO-UNDO. DEFINE VARIABLE PercentDone AS DECIMAL NO-UNDO. DEFINE VARIABLE LinesPerSec AS DECIMAL NO-UNDO. DEFINE VARIABLE ErrorCnt AS INTEGER NO-UNDO. DEFINE VARIABLE Elapsed AS CHARACTER NO-UNDO. DEFINE VARIABLE TimeRemain AS CHARACTER NO-UNDO. /* Misc variables */ DEFINE VARIABLE TheYear AS CHARACTER NO-UNDO. DEFINE VARIABLE HistDate AS DATE NO-UNDO. DEFINE VARIABLE CreateOK AS LOGICAL NO-UNDO. /* Define Buffers */ DEFINE BUFFER NewHist FOR stu-history. DEFINE BUFFER StuYear FOR stu-year. DEFINE BUFFER YearBuff FOR school-year. /* Define Streams */ DEFINE STREAM ImportStream. DEFINE STREAM ExportStream. DEFINE STREAM LogStream. /* Status frame */ FORM TotalLineCnt COLUMN-LABEL "Total!Lines" FORMAT "zzzzz9" LineCnt COLUMN-LABEL "Curnt!Line" FORMAT "zzzzz9" PercentDone COLUMN-LABEL "%age!Done" FORMAT "zz9.99%" LinesPerSec COLUMN-LABEL "Lines!/sec" FORMAT "zz9.99" ErrorCnt COLUMN-LABEL "Errors" FORMAT "zzzz9" Elapsed COLUMN-LABEL "Elapsed!Time" TimeRemain COLUMN-LABEL "Time!Remain" WITH FRAME StatusFrame 1 DOWN CENTERED ROW 9 OVERLAY COLOR VALUE(the-color.c-proc) TITLE COLOR VALUE(the-color.c-title) " History Data Importation Statistics ". /* First, get the import file */ QueryBlock: DO WHILE TRUE ON ERROR UNDO, RETURN ON ENDKEY UNDO, RETURN: /* Get a default year */ {schlyear.i YearBuff} IF NOT AVAILABLE YearBuff THEN FIND LAST YearBuff NO-LOCK. TheYear = YearBuff.school-year-name. /* Define the form */ FORM TheYear LABEL " Year to import into" FORMAT "x(9)" SKIP ImportFile LABEL " Import from" FORMAT "x(255)" VIEW-AS FILL-IN SIZE 32 BY 1 SPACE(1) /* ReplaceMode LABEL " Existing records should be" FORMAT "Replaced/Kept" */ LogFile LABEL " Log import results to" FORMAT "x(255)" VIEW-AS FILL-IN SIZE 32 BY 1 SPACE(1) AutoCreate LABEL " Auto-create codes" FORMAT "Yes/No" /* ExportFile LABEL " Backup replaced data to" FORMAT "x(255)" VIEW-AS FILL-IN SIZE 32 BY 1 */ SKIP WITH FRAME QueryFrame OVERLAY 1 DOWN CENTERED ROW 10 SIDE-LABELS COLOR VALUE(the-color.c-proc) PROMPT VALUE(the-color.c-input). /* Default things for the user */ ON LEAVE OF ImportFile IN FRAME QueryFrame DO: /* Define Variables */ DEFINE VARIABLE DotPtr AS INTEGER NO-UNDO. /* Get pointer to decimal */ DotPtr = INDEX(ImportFile:SCREEN-VALUE, "."). IF DotPtr = 0 THEN RETURN. /* Default the log file */ IF LogFile:SCREEN-VALUE = "" THEN LogFile:SCREEN-VALUE = SUBSTR(SELF:SCREEN-VALUE, 1, DotPtr - 1) + ".log". /* Default the backup file */ /*IF ExportFile:SCREEN-VALUE = "" THEN ExportFile:SCREEN-VALUE = SUBSTR(SELF:SCREEN-VALUE, 1, DotPtr - 1) + ".org".*/ END. /* Query the user */ UPDATE ImportFile LABEL " Import from" FORMAT "x(255)" VIEW-AS FILL-IN SIZE 32 BY 1 SPACE(1) /* ReplaceMode LABEL " Existing records should be" FORMAT "Replaced/Kept" */ LogFile LABEL " Log import results to" FORMAT "x(255)" VIEW-AS FILL-IN SIZE 32 BY 1 SPACE(1) AutoCreate LABEL " Auto-create codes" FORMAT "Yes/No" /* ExportFile LABEL " Backup replaced data to" FORMAT "x(255)" VIEW-AS FILL-IN SIZE 32 BY 1 */ TheYear LABEL " Year to import into" FORMAT "x(8)" WITH FRAME QueryFrame OVERLAY 1 DOWN CENTERED ROW 10 SIDE-LABELS COLOR VALUE(the-color.c-proc) PROMPT VALUE(the-color.c-input) TITLE COLOR VALUE(the-color.c-title) " Import Student History Data ". /* See if the note exists */ IF SEARCH(ImportFile) = ? THEN DO: BELL. {safe-msg.i "'Cannot find the import source file'"} NEXT. END. /* Make sure the year exists */ FIND FIRST YearBuff WHERE YearBuff.school-year-name = TheYear NO-LOCK NO-ERROR. IF NOT AVAILABLE YearBuff THEN DO: BELL. {safe-msg.i "'School year is not on file'"} NEXT. END. /* Make sure the backup file should be overwritten */ /* IF SEARCH(ExportFile) <> ? THEN DO: DO ON ERROR UNDO, NEXT QueryBlock ON ENDKEY UNDO, NEXT QueryBlock: MESSAGE COLOR VALUE(the-color.c-alert) " " ExportFile "already exists " SKIP " Do you want to overwrite it?" SKIP VIEW-AS ALERT-BOX QUESTION BUTTONS YES-NO UPDATE OverwriteFile AS LOGICAL. END. /* Iterate if not okay */ IF NOT OverwriteFile THEN NEXT. END. */ /* Done */ LEAVE. END. /* Get line count */ INPUT STREAM ImportStream THROUGH VALUE("wc -l " + ImportFile). IMPORT STREAM ImportStream ImportText. INPUT STREAM ImportStream CLOSE. TotalLineCnt = INTEGER(ImportText[1]). DISPLAY TotalLineCnt WITH FRAME StatusFrame. /* Open the file */ INPUT STREAM ImportStream FROM VALUE(ImportFile). OUTPUT STREAM LogStream TO VALUE(LogFile) PAGE-SIZE 0. /* OUTPUT STREAM ExportStream TO VALUE(ExportFile) PAGE-SIZE 0. */ /* Hide the query frame */ HIDE FRAME QueryFrame NO-PAUSE. /* Reset counters */ ASSIGN StartTime = TIME LastUpdate = StartTime. /* Begin Processing */ ImportLoop: REPEAT ON ERROR UNDO, LEAVE ON ENDKEY UNDO, LEAVE: /* Get a line */ ImportText = "". IMPORT STREAM ImportStream ImportText. LineCnt = LineCnt + 1. /* Update status display */ IF (TIME - LastUpdate) > 2 THEN DO: DISPLAY LineCnt (LineCnt / TotalLineCnt) * 100 @ PercentDone LineCnt / (TIME - StartTime) @ LinesPerSec ErrorCnt STRING(TIME - StartTime, "HH:MM:SS") @ Elapsed STRING(INTEGER((TotalLineCnt - LineCnt) / (LineCnt / (TIME - StartTime))), "HH:MM:SS") @ TimeRemain WITH FRAME StatusFrame. LastUpdate = TIME. END. /* Make sure we have a required fields */ IF (ImportText[1] = "") OR (ImportText[1] = ?) OR (ImportText[2] = "") OR (ImportText[2] = ?) OR (ImportText[3] = "") OR (ImportText[3] = ?) THEN DO: ErrorCnt = ErrorCnt + 1. {&WriteLog} "One or more REQUIRED fields missing, NOT imported" SKIP. NEXT ImportLoop. END. /* Lookup the student */ FIND stu-base WHERE stu-base.stu-id = ImportText[1] NO-LOCK NO-ERROR. IF NOT AVAILABLE stu-base THEN DO: {&WriteLog} "Student ID *NOT* on file, NOT imported" SKIP. ErrorCnt = ErrorCnt + 1. NEXT ImportLoop. END. /* Locate a stu year record */ FIND StuYear WHERE StuYear.school-year-id = YearBuff.school-year-id AND StuYear.stu-seq = stu-base.stu-seq NO-LOCK NO-ERROR. IF NOT AVAILABLE StuYear THEN DO: ErrorCnt = ErrorCnt + 1. {&WriteLog} "No record for student in selected school year, NOT imported" SKIP. NEXT ImportLoop. END. /* Lookup the history code */ FIND history-code WHERE history-code.history-code = ImportText[2] NO-LOCK NO-ERROR. IF NOT AVAILABLE history-code THEN IF AutoCreate THEN DO: CREATE history-code. ASSIGN history-code.history-code = CAPS(ImportText[2]) history-code.description = "<< CREATED BY IMPORTER >>". {&WriteLog} "Created history code, please check it!" SKIP. END. ELSE DO: ErrorCnt = ErrorCnt + 1. {&WriteLog} "History code not on file, NOT imported" SKIP. NEXT ImportLoop. END. /* Get the history date */ HistDate = DATE(ImportText[3]) NO-ERROR. IF ERROR-STATUS:ERROR THEN DO: ErrorCnt = ErrorCnt + 1. {&WriteLog} "Bad Date, NOT imported" SKIP. NEXT ImportLoop. END. /* Create a new history event */ CREATE NewHist. ASSIGN NewHist.school-year-id = StuYear.school-year-id NewHist.stu-seq = StuYear.stu-seq NewHist.history-date = HistDate NewHist.history-code-id = history-code.history-code-id NewHist.comments = ImportText[4]. /* Insert it into the history */ {histutil.i CREATE EVENT NewHist CreateOK} IF NOT CreateOK THEN DO: ErrorCnt = ErrorCnt + 1. {&WriteLog} "HISTUTIL failed to record the history event, NOT imported" SKIP. UNDO ImportLoop, NEXT ImportLoop. END. END. /* Final statistics update */ DISPLAY LineCnt (LineCnt / TotalLineCnt) * 100 @ PercentDone LineCnt / (TIME - StartTime) @ LinesPerSec ErrorCnt STRING(TIME - StartTime, "HH:MM:SS") @ Elapsed STRING(INTEGER((TotalLineCnt - LineCnt) / (LineCnt / (TIME - StartTime))), "HH:MM:SS") @ TimeRemain WITH FRAME StatusFrame. /* Close the input & log streams */ INPUT STREAM ImportStream CLOSE. OUTPUT STREAM LogStream CLOSE. /* OUTPUT STREAM ExportStream CLOSE. */ /* Shut down histutil */ {histutil.i CLEAN_UP} /* We are done */ {safe-msg.i "'Import Complete'"} RETURN.