| 1 | /* ************************************************** */ |
|---|
| 2 | /* Prototype code to rename reports to be picked up */ |
|---|
| 3 | /* by a 3rd party report scheduler */ |
|---|
| 4 | /* Copyright 2010 - Grey Sparling Solutions */ |
|---|
| 5 | /*-----------------------------------------------------/ |
|---|
| 6 | /* This program works under the following assumptions */ |
|---|
| 7 | /* - It runs on a windows process scheduler */ |
|---|
| 8 | /* - The machine running this program will have the */ |
|---|
| 9 | /* following */ |
|---|
| 10 | /* o Access to the files stored across servers */ |
|---|
| 11 | /* running the reports */ |
|---|
| 12 | /* o The paths of the shared reports are the same */ |
|---|
| 13 | /* regardless of the server that ran the report */ |
|---|
| 14 | /* ************************************************** */ |
|---|
| 15 | Function GetLastInstanceProcessed() Returns number; |
|---|
| 16 | |
|---|
| 17 | Local number &RetValue; |
|---|
| 18 | |
|---|
| 19 | SQLExec("SELECT MAX(PRCSINSTANCE) FROM PS_GS_LASTPRCS", &RetValue); |
|---|
| 20 | Warning ("Processing output with Process Instance Greater than " | &RetValue); |
|---|
| 21 | |
|---|
| 22 | Return &RetValue; |
|---|
| 23 | |
|---|
| 24 | End-Function; |
|---|
| 25 | |
|---|
| 26 | Function UpdateGetLastInstanceProcessed(&nInstanceToUpdate As number); |
|---|
| 27 | |
|---|
| 28 | Local Record &recLastPrcs = CreateRecord(Record.GS_LASTPRCS); |
|---|
| 29 | &recLastPrcs.GetField(Field.PRCSINSTANCE).Value = &nInstanceToUpdate; |
|---|
| 30 | If &recLastPrcs.Save() Then |
|---|
| 31 | Warning ("Updated max process instance to be " | &nInstanceToUpdate); |
|---|
| 32 | Else |
|---|
| 33 | Error ("Could not update max process instance to be " | &nInstanceToUpdate); |
|---|
| 34 | End-If; |
|---|
| 35 | |
|---|
| 36 | End-Function; |
|---|
| 37 | |
|---|
| 38 | Function RenameFile(&sOldFileName As string, &sNewFileName As string); |
|---|
| 39 | |
|---|
| 40 | rem This function calls the windows rename function to update the file name; |
|---|
| 41 | Local string &sDir = GetEnv("PS_SERVDIR"); |
|---|
| 42 | Local number &RetCode; |
|---|
| 43 | |
|---|
| 44 | If FileExists(&sOldFileName, %FilePath_Absolute) Then |
|---|
| 45 | Warning ("Executing --> rename " | Char(34) | &sOldFileName | Char(34) | " " | Char(34) | &sNewFileName | Char(34)); |
|---|
| 46 | &RetCode = Exec("cmd.exe /c rename " | Char(34) | &sOldFileName | Char(34) | " " | Char(34) | &sNewFileName | Char(34), %Exec_Synchronous + %FilePath_Absolute); |
|---|
| 47 | Warning ("Return Code = " | &RetCode); |
|---|
| 48 | Else |
|---|
| 49 | Warning ("Could not find file to rename: " | &sOldFileName); |
|---|
| 50 | End-If; |
|---|
| 51 | End-Function; |
|---|
| 52 | |
|---|
| 53 | Function GetOldFileName(&nPrcsInstance As number, &sParmList As string) Returns string; |
|---|
| 54 | |
|---|
| 55 | Local string &RetValue = ""; |
|---|
| 56 | Local number &nLocFilePath; |
|---|
| 57 | Local number &nLocEndOfPath; |
|---|
| 58 | |
|---|
| 59 | rem find the output file name by searching for command line parm; |
|---|
| 60 | &nLocFilePath = Find("-OP", &sParmList) + 4; |
|---|
| 61 | If &nLocFilePath > 4 Then |
|---|
| 62 | rem Find the end of the current parameter; |
|---|
| 63 | &nLocEndOfPath = Find(" -", &sParmList, &nLocFilePath); |
|---|
| 64 | If &nLocEndOfPath = 0 Then |
|---|
| 65 | &nLocEndOfPath = Len(&sParmList); |
|---|
| 66 | End-If; |
|---|
| 67 | rem Return the string from starting position to ending position; |
|---|
| 68 | &RetValue = RTrim(Substring(&sParmList, &nLocFilePath, &nLocEndOfPath - &nLocFilePath)); |
|---|
| 69 | End-If; |
|---|
| 70 | |
|---|
| 71 | Return &RetValue; |
|---|
| 72 | |
|---|
| 73 | End-Function; |
|---|
| 74 | |
|---|
| 75 | Function getNewFileName(&nInstance As number, &sOldFilePath As string) Returns string; |
|---|
| 76 | |
|---|
| 77 | Local string &sOldFile; |
|---|
| 78 | Local string &sRunCntlId; |
|---|
| 79 | Local string &RetValue = ""; |
|---|
| 80 | Local array of string &sPathSegments; |
|---|
| 81 | |
|---|
| 82 | &sPathSegments = Split(&sOldFilePath, "\"); |
|---|
| 83 | If &sPathSegments.Len = 0 Then |
|---|
| 84 | &sPathSegments = Split(&sOldFilePath, "/"); |
|---|
| 85 | End-If; |
|---|
| 86 | |
|---|
| 87 | &sOldFile = &sPathSegments [&sPathSegments.Len]; |
|---|
| 88 | |
|---|
| 89 | If All(&sOldFile) Then |
|---|
| 90 | |
|---|
| 91 | rem get run control ID for current process instance; |
|---|
| 92 | SQLExec("SELECT RUNCNTLID FROM PSPRCSRQST WHERE PRCSINSTANCE = :1", &nInstance, &sRunCntlId); |
|---|
| 93 | |
|---|
| 94 | rem Concatenate run control ID at the proper place; |
|---|
| 95 | If Substring(&sOldFile, Len(&sOldFile) - 3, 1) = "." Then |
|---|
| 96 | &RetValue = Left(&sOldFile, Len(&sOldFile) - 4) | "-" | RTrim(&sRunCntlId) | Right(&sOldFile, 4); |
|---|
| 97 | Else |
|---|
| 98 | &RetValue = RTrim(&sOldFile) | "-" | RTrim(&sRunCntlId); |
|---|
| 99 | End-If; |
|---|
| 100 | End-If; |
|---|
| 101 | |
|---|
| 102 | Return &RetValue; |
|---|
| 103 | |
|---|
| 104 | End-Function; |
|---|
| 105 | |
|---|
| 106 | Local number &nPrcsInstance; |
|---|
| 107 | Local string &sParameter; |
|---|
| 108 | Local string &sStartingFileName; |
|---|
| 109 | Local string &sNewFileName; |
|---|
| 110 | Local number &nMaxPrcsInstance; |
|---|
| 111 | Local number &nStartingInstance = GetLastInstanceProcessed(); |
|---|
| 112 | |
|---|
| 113 | REM Get List of Reports Recently Processed ; |
|---|
| 114 | Local SQL &sqlPrcsParms = CreateSQL("select PRCSINSTANCE, PARMLIST from PSPRCSPARMS WHERE PRCSINSTANCE > :1", &nStartingInstance); |
|---|
| 115 | While &sqlPrcsParms.Fetch(&nPrcsInstance, &sParameter) |
|---|
| 116 | |
|---|
| 117 | &sStartingFileName = GetOldFileName(&nPrcsInstance, &sParameter); |
|---|
| 118 | |
|---|
| 119 | If All(&sStartingFileName) Then |
|---|
| 120 | |
|---|
| 121 | &sNewFileName = getNewFileName(&nPrcsInstance, &sStartingFileName); |
|---|
| 122 | |
|---|
| 123 | RenameFile(&sStartingFileName, &sNewFileName); |
|---|
| 124 | Else |
|---|
| 125 | Warning ("No Output File found for Process Instance " | &nPrcsInstance); |
|---|
| 126 | End-If; |
|---|
| 127 | |
|---|
| 128 | &nMaxPrcsInstance = &nPrcsInstance; |
|---|
| 129 | |
|---|
| 130 | End-While; |
|---|
| 131 | |
|---|
| 132 | UpdateGetLastInstanceProcessed(&nMaxPrcsInstance); |
|---|
| 133 | |
|---|
| 134 | &sqlPrcsParms.Close(); |
|---|