/* imp-notes.p - Import Notes Codes */ /* 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 - Note Group Name REQUIRED *EXISTING* note group to import into */ /* #3 - Note Code Name REQUIRED Note code for the actual note */ /* #4 - Note Date OPTIONAL Date for note (if any), use - if no date*/ /* #5 - Note Flag OPTIONAL Yes/No flag for note, use - if none */ /* #6 - Table Name OPTIONAL Name of table for table code or - */ /* #7 - Table Code OPTIONAL Name of table code or - if none */ /* #8 - Comments OPTIONAL Comments for note */ /* #9 - Note End Date OPTIONAL Ending date for note, use - if no end */ /* Include commons */ {common.i NEW} {p-init.i "'IMPORTER'"} /* Define 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 Vars */ DEFINE VARIABLE CreateNote AS LOGICAL NO-UNDO. DEFINE VARIABLE TableName AS CHARACTER NO-UNDO. DEFINE VARIABLE TableCode AS CHARACTER NO-UNDO. /* Define Streams */ DEFINE STREAM ImportStream. DEFINE STREAM LogStream. DEFINE STREAM ExportStream. /* 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) " Note Importation Statistics ". /* First, get the import file */ QueryBlock: DO WHILE TRUE ON ERROR UNDO, RETURN ON ENDKEY UNDO, RETURN: /* Define the form */ FORM 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 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 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 Note 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 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} "or 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. /* Lookup the note group */ FIND note-group WHERE note-group.note-group-code = ImportText[2] NO-LOCK NO-ERROR. IF NOT AVAILABLE note-group THEN DO: {&WriteLog} "Note group not on file, NOT imported" SKIP. ErrorCnt = ErrorCnt + 1. NEXT ImportLoop. END. /* Lookup note code */ FIND FIRST note-code OF note-group NO-LOCK WHERE note-code.note-code = ImportText[3] NO-ERROR. IF NOT AVAILABLE note-code THEN IF AutoCreate THEN DO: CREATE note-code. ASSIGN note-code.note-group-id = note-group.note-group-id note-code.note-code = CAPS(ImportText[3]) note-code.description = "<< CREATED BY IMPORTER >>". {&WriteLog} "Created note code - Please check it!" SKIP. END. ELSE DO: {&WriteLog} "Note Code not on file, NOT imported" SKIP. ErrorCnt = ErrorCnt + 1. NEXT ImportLoop. END. /* See if there is an existing note code */ FIND LAST stu-note OF stu-base WHERE stu-note.note-group-id = note-code.note-group-id AND stu-note.note-code-id = note-code.note-code-id NO-ERROR. /* Handle an existing record */ IF AVAILABLE stu-note THEN DO: IF note-code.allow-multi THEN CreateNote = Yes. ELSE IF ReplaceMode THEN CreateNote = No. ELSE DO: {&WriteLog} "Note already exists for student, *NOT* imported" SKIP. NEXT ImportLoop. END. END. ELSE CreateNote = Yes. /* Export existing note */ IF NOT CreateNote THEN DO: /* Init the table name/code */ ASSIGN TableName = "-" TableCode = "-". /* Load up the table, if we have one */ IF note-code.table-verified THEN DO: FIND site-table-name OF stu-note NO-LOCK NO-ERROR. FIND site-table-code OF stu-note NO-LOCK NO-ERROR. IF AVAILABLE site-table-name AND AVAILABLE site-table-code THEN ASSIGN TableName = site-table-name.table-name TableCode = site-table-code.table-code. END. /* Export the note */ EXPORT STREAM ExportStream stu-base.stu-id note-group.note-group-code note-code.note-code stu-note.note-date stu-note.note-flag TableName TableCode stu-note.comments. END. /* Create a note, if needed */ IF CreateNote THEN DO: CREATE stu-note. ASSIGN stu-note.stu-seq = stu-base.stu-seq stu-note.note-group-id = note-code.note-group-id stu-note.note-code-id = note-code.note-code-id. END. /* See if there is a date */ IF (ImportText[4] <> "") AND (ImportText[4] <> ?) THEN IF note-code.use-date OR NEW note-code THEN DO: /* Convert the note date */ stu-note.note-date = DATE(ImportText[4]) NO-ERROR. IF ERROR-STATUS:ERROR THEN DO: {&WriteLog} "Bad date for note, NOT imported" SKIP. ErrorCnt = ErrorCnt + 1. UNDO ImportLoop, NEXT ImportLoop. END. /* If this is a new note code, mark it accepting dates */ IF NEW note-code THEN note-code.use-date = Yes. END. ELSE DO: {&WriteLog} "Date specified but not record/supported for note code - date ignored" SKIP. END. /* See if there is a flag */ IF (ImportText[5] <> "") AND (ImportText[5] <> ?) THEN IF note-code.use-flag OR NEW note-code THEN DO: stu-note.note-flag = SUBSTR(ImportText[5], 1, 1) = "Y". IF NEW note-code THEN note-code.use-flag = Yes. END. ELSE DO: {&WriteLog} "Flag specified but not record/supported for note code - flag ignored" SKIP. END. IF NOT NEW note-code AND note-code.table-verified AND ((ImportText[6] = "") OR (ImportText[6] = ?) OR (ImportText[7] = "") OR (ImportText[7] = ?)) THEN DO: ErrorCnt = ErrorCnt + 1. {&WriteLog} "Missing table name or code for note, NOT imported" SKIP. UNDO ImportLoop, NEXT ImportLoop. END. /* Only do next section if we have a table code */ IF (ImportText[6] <> "") AND (ImportText[6] <> ?) AND (ImportText[7] <> "") AND (ImportText[7] <> ?) THEN TableBlock: DO: /* See if the note code uses tables */ IF note-code.table-verified OR NEW note-code THEN DO: /* See if the note code already has a table and load it */ IF note-code.table-name-id <> ? THEN DO: FIND site-table-name OF note-code NO-LOCK NO-ERROR. IF NOT AVAILABLE site-table-name THEN DO: {&WriteLog} "Cannot locate predefined table name - Table code ignored" SKIP. ErrorCnt = ErrorCnt + 1. LEAVE TableBlock. END. END. ELSE DO: /* Try to lookup the table from the import file */ FIND site-table-name NO-LOCK WHERE site-table-name.table-name = ImportText[6] NO-ERROR. IF NOT AVAILABLE site-table-name THEN IF AutoCreate THEN DO: CREATE site-table-name. ASSIGN site-table-name.table-name = CAPS(ImportText[6]) site-table-name.description = "<< CREATED BY IMPORTER >>". {&WriteLog} "Created Site Table '" CAPS(ImportText[6]) "', please check this over!" SKIP. END. ELSE DO: {&WriteLog} "Cannot locate import specified table name '" CAPS(ImportText[6]) "' - Table code ignored" SKIP. ErrorCnt = ErrorCnt + 1. LEAVE TableBlock. END. END. /* Set the table in place */ IF NEW note-code AND note-code.table-name-id = ? THEN ASSIGN note-code.table-verified = Yes note-code.table-name-id = site-table-name.table-name-id. END. ELSE DO: {&WriteLog} "Found table code, but note doesn't record tables - table ignored" SKIP. LEAVE TableBlock. END. /* Lookup the table code */ FIND site-table-code OF site-table-name NO-LOCK WHERE site-table-code.table-code = ImportText[7] NO-ERROR. IF NOT AVAILABLE site-table-code THEN IF AutoCreate THEN DO: CREATE site-table-code. ASSIGN site-table-code.table-name-id = site-table-name.table-name-id site-table-code.table-code = CAPS(ImportText[7]) site-table-code.description = "<< CREATED BY IMPORTER >>". {&WriteLog} "Created Site Table Code '" CAPS(ImportText[7]) "' for table '" site-table-name.table-name "', please check this over!" SKIP. END. ELSE DO: {&WriteLog} "Cannot locate table code '" CAPS(ImportText[7]) "' for table '" site-table-name.table-name "' - Table code ignored" SKIP. ErrorCnt = ErrorCnt + 1. LEAVE TableBlock. END. /* Set the table code into the note */ ASSIGN stu-note.table-name-id = site-table-code.table-name-id stu-note.table-code-id = site-table-code.table-code-id. /* Set veification style */ IF NEW note-code THEN note-code.table-verified = Yes. END. /* Set the comments in place */ stu-note.comments = ImportText[8]. /* check and set the note-end date, if applicable */ IF ImportText[9] <> "" AND ImportText[9] <> ? THEN EndDateBlock: DO: IF NOT note-code.use-end-date THEN DO: /* log that they tried to sneak one past us */ {&WriteLog} "This note code does not use end dates - date ignored" SKIP. LEAVE EndDateBlock. END. /* set it in place */ stu-note.note-end-date = DATE(ImportText[9]) NO-ERROR. IF ERROR-STATUS:ERROR THEN DO: {&WriteLog} "Bad end date for note, NOT imported" SKIP. ErrorCnt = ErrorCnt + 1. UNDO ImportLoop, NEXT ImportLoop. END. 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. /* We are done */ {safe-msg.i "'Import Complete'"} RETURN.