/* imp-address.p - Import addresses to carrier route */ /* 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 - Street # high REQUIRED */ /* # 2 - Street # low REQUIRED */ /* # 3 - Street side REQUIRED */ /* # 4 - Street Name REQUIRED */ /* # 5 - City REQUIRED */ /* # 6 - State REQUIRED */ /* # 7 - Zip Code REQUIRED */ /* # 8 - Carrier route */ /* # 9 - County code */ /* #10 - External code 1 */ /* #11 - External code 2 */ /* #12 - External code 3 */ /* #13 - External code 4 */ /* #14 - External code 5 */ /* #15 - Locality code */ /* #16 - Locality precinct */ /* #17 - School district code */ /* #18 - School princinct */ /* */ /* Include commons */ {common.i NEW} {p-init.i "'IMPORTER'"} {addrutil.i DEFINE} /* Constants */ &SCOPED WriteCmd PUT STREAM LogStream UNFORMATTED &SCOPED WriteHdr "#" LineCnt ", " ImportText[1] ", " ImportText[2] ": " &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 20 NO-UNDO. DEFINE VARIABLE LineCnt AS INTEGER 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. /* Define Buffers */ DEFINE BUFFER CarrierRoute FOR carrier-route. /* 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) " Carrier-Route 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) 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) 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 Addresses to Carrier-Routes ". /* See if the note exists */ IF SEARCH(ImportFile) = ? THEN DO: BELL. {safe-msg.i "'Cannot find the import source file'"} NEXT. END. /* Check if 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(0) " Do you want to overwrite it?" SKIP(0) VIEW-AS ALERT-BOX QUESTION BUTTONS YES-NO UPDATE OverwriteFile AS LOGICAL. END. /* Get next address if not overwriting */ IF NOT OverwriteFile THEN NEXT. END. /* Done */ LEAVE. END. /* Load the profile */ FIND FIRST spm-prof NO-LOCK. /* 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 files */ 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. /* Check required fields */ IF ImportText[1] = "" OR ImportText[1] = ? OR ImportText[2] = "" OR ImportText[2] = ? OR ImportText[3] = "" OR ImportText[3] = ? OR ImportText[4] = "" OR ImportText[4] = ? OR ImportText[5] = "" OR ImportText[5] = ? OR ImportText[6] = "" OR ImportText[6] = ? OR ImportText[7] = "" OR ImportText[7] = ? THEN DO: /* Increment error count */ ErrorCnt = ErrorCnt + 1. /* Write a log message */ {&WriteLog} "One or more REQUIRED fields missing, NOT imported" SKIP(0). NEXT ImportLoop. END. /* Check for an existing carrier-route entry */ FIND FIRST CarrierRoute WHERE CarrierRoute.street-high = ImportText[1] AND CarrierRoute.street-low = ImportText[2] AND CarrierRoute.street-side = ImportText[3] AND CarrierRoute.street-name = ImportText[4] AND CarrierRoute.city = ImportText[5] AND CarrierRoute.state = ImportText[6] AND CarrierRoute.zip-code = ImportText[7] AND CarrierRoute.carrier-route = ImportText[8] AND CarrierRoute.county-code = ImportText[9] AND CarrierRoute.external-code-1 = ImportText[10] AND CarrierRoute.external-code-2 = ImportText[11] AND CarrierRoute.external-code-3 = ImportText[12] AND CarrierRoute.external-code-4 = ImportText[13] AND CarrierRoute.external-code-5 = ImportText[14] AND CarrierRoute.locality-code = ImportText[15] AND CarrierRoute.locality-precinct = ImportText[16] AND CarrierRoute.school-district-code = ImportText[17] AND CarrierRoute.school-precinct = ImportText[18] NO-ERROR. IF AVAILABLE CarrierRoute THEN IF ReplaceMode THEN DO: /* Export existing data to a backup file */ EXPORT STREAM ExportStream CarrierRoute.street-high CarrierRoute.street-low CarrierRoute.street-side CarrierRoute.street-name CarrierRoute.city CarrierRoute.state CarrierRoute.zip-code CarrierRoute.carrier-route CarrierRoute.county-code CarrierRoute.external-code-1 CarrierRoute.external-code-2 CarrierRoute.external-code-3 CarrierRoute.external-code-4 CarrierRoute.external-code-5 CarrierRoute.locality-code CarrierRoute.locality-precinct CarrierRoute.school-district-code CarrierRoute.school-precinct. /* Replace the carrier-route data */ ASSIGN CarrierRoute.street-high = ImportText[1] CarrierRoute.street-low = ImportText[2] CarrierRoute.street-side = ImportText[3] CarrierRoute.street-name = ImportText[4] CarrierRoute.city = ImportText[5] CarrierRoute.state = ImportText[6] CarrierRoute.zip-code = ImportText[7] CarrierRoute.carrier-route = ImportText[8] CarrierRoute.county-code = ImportText[9] CarrierRoute.external-code-1 = ImportText[10] CarrierRoute.external-code-2 = ImportText[11] CarrierRoute.external-code-3 = ImportText[12] CarrierRoute.external-code-4 = ImportText[13] CarrierRoute.external-code-5 = ImportText[14] CarrierRoute.locality-code = ImportText[15] CarrierRoute.locality-precinct = ImportText[16] CarrierRoute.school-district-code = ImportText[17] CarrierRoute.school-precinct = ImportText[18]. END. ELSE DO: /* Not in replace mode so don't overwrite existing carrier-route */ {&WriteLog} "Existing carrier-route, NOT imported" SKIP(0). NEXT ImportLoop. END. ELSE DO: /* Create a new carrier-route record */ CREATE CarrierRoute. ASSIGN CarrierRoute.street-high = ImportText[1] CarrierRoute.street-low = ImportText[2] CarrierRoute.street-side = ImportText[3] CarrierRoute.street-name = ImportText[4] CarrierRoute.city = ImportText[5] CarrierRoute.state = ImportText[6] CarrierRoute.zip-code = ImportText[7] CarrierRoute.carrier-route = (IF ImportText[8] <> "" OR ImportText[8] <> ? THEN ImportText[8] ELSE "UNKN") CarrierRoute.county-code = ImportText[9] CarrierRoute.external-code-1 = ImportText[10] CarrierRoute.external-code-2 = ImportText[11] CarrierRoute.external-code-3 = ImportText[12] CarrierRoute.external-code-4 = ImportText[13] CarrierRoute.external-code-5 = ImportText[14] CarrierRoute.locality-code = ImportText[15] CarrierRoute.locality-precinct = ImportText[16] CarrierRoute.school-district-code = ImportText[17] CarrierRoute.school-precinct = ImportText[18]. 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.