Subroutine Audit_Manager(ID, Table, CurrentRecord, OrigRecord, Activity)

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

    This program is proprietary and is not to be used by or disclosed to others, nor is it to be copied without written
    permission from SRP Computer Solutions, Inc.

    Name        :   Audit_Manager

    Description :   Updates the Audit table to track modifications of a record.

    Notes       :   This process is normally called by the Audit_Manager_MFS. It handles all of the audit trail
                    management logic.

    Parameters  :
        AutoSet         [in] -- Allows automatic setting of the SYSTEM_MAINTENANCE record without user intervention.
        SysFlag        [out] -- Returns True if the flag was set to stop people from logging in. Returns False if people
                                are still allowed to log into the application.

        ID                  [in] -- ID of the record.
        Table               [in] -- Table name where the record being audited is stored.
        CurrentRecord   [in/out] -- Current record as it will be written to disk. If the Activity is "Write" then the
                                    audit fields will be updated.
        OrigRecord          [in] -- If called by Audit_Manager_MFS then this is the original record, otherwise this
                                    represents a marker for special audit tracking.
        Activity            [in] -- Identifies the audit activity being performed: Read, Write, Delete, or anything else
                                    if called directly for special audit tracking.



    History     :   (Date, Initials, Notes)
        05/19/05    axf     Initial Program (Original Program MFS_Update_History).
        11/26/07    dmb     Code clean-up and refactoring.
        04/05/13    dmb     Repalce SECURITY_EQUATES with Memory Services to get login security. - [SRPFW-9]

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

$insert APP_INSERTS

Declare Subroutine Msg, Lock_Record
Declare Function RetStack, Memory_Services

If Assigned(ID)             else ID             = ""
If Assigned(Table)          else Table          = ""
If Assigned(CurrentRecord)  else CurrentRecord  = ""
If Assigned(OrigRecord)     else OrigRecord     = ""

AuditManagerTable = "APP_INFO"

Convert @Lower_Case to @Upper_Case In Table
AuditTable  = 'AUDIT_' : Table
Username    = @USERNAME

* Declare function GetNetworkUsername
* If GetNetworkUsername() EQ "dbakke1" AND Activity _NEC "READ" then debug

Open AuditTable to HistoryCheck then
    RecordTracker = No$
    Locked_handle = ""
    Temp_activity = ""
    Success = ""
    Details = ""
    New = No$
    TrackActivity = No$

    CallProgName = RetStack()<2>

    Gosub App_Info

    If TrackActivity EQ Yes$ then Gosub Main_Process
End

Return

App_Info:
    Fields      = ""
    Num_fields  = 0
    Open AuditManagerTable to hAIT then
        ReadO App_record from hAIT, "AUDIT_MANAGER_SETTINGS" then
            Locate Table in App_record<1> using @VM setting vPos then
                Begin Case
                    Case ID EQ ""
                        // Do nothing.

                    Case CallProgName[1, 18] NE "AUDIT_MANAGER_MFS*"
                        // Program is being called directly from a stored procedure.
                        If App_record<6,vPos> EQ Yes$ Then
                            If CurrentRecord NE "" Then 
                                TrackActivity = Yes$                        
                                RecordTracker = Yes$    ; // Flag to indicate that special audit tracking is ocurring
                                Marker        = OrigRecord
                                OrigRecord    = CurrentRecord
                            End
                        End

                    Case Activity EQ "Write"
                        If App_record<3,vPos> EQ Yes$ Then
                            TrackActivity = Yes$
                            Locate Table in App_record<1> using @VM setting Position then
                                StartField = App_record<2, Position>
                                If StartField Then
                                    CurrentRecord<StartField> = Username
                                    CurrentRecord<StartField + 1> = Date()
                                    CurrentRecord<StartField + 2> = Time()
                                End
                            End
                        End

                    Case Activity EQ "Delete"
                        If App_record<4,vPos> EQ Yes$ Then TrackActivity = Yes$

                    Case Activity EQ "Read"
                        If App_record<5,vPos> EQ Yes$ Then TrackActivity = Yes$

                End Case            
            end else
*                Msg("|Unable to locate the ":Table:" information|in the Audit Manager Settings.|")
            end
        end else
*            Msg("|Unable to read the Audit Manager Settings Record.|")
        end
    end else
*        Msg("|Unable to open the App_Info Table.|")
    end
return


Main_Process:
    Lock_Record(AuditTable, Locked_handle, ID, Lock$, No$, Yes$, Success)
    If Success then
        Read AuditTrail from Locked_handle, ID then
            Num_items = Count(AuditTrail<1>, @VM) + (AuditTrail<1> NE "")
*            If Len(AuditTrail) GT 250000 then
*                For DelNumItems = 1 to 100
*                    For Loop = 1 to 8
*                        AuditTrail = Delete(AuditTrail, Loop, Num_items, 0)
*                    Next Loop
*                    Num_items -= 1
*                Next DelNumItems
*            end
        end else
            AuditTrail = ""
            New = Yes$
        end
        Current_date = Date()
        Current_time = Time()
        Seq = "DR"
        StationName = @Station
        MacAddress = ""
        
        Locate Current_date in AuditTrail<1> by Seq Using @VM setting Pos else Null
        AuditTrail<1> = Insert(AuditTrail<1>, 1, Pos, 0, Current_Date)
        AuditTrail<2> = Insert(AuditTrail<2>, 1, Pos, 0, Current_Time)
        AuditTrail<3> = Insert(AuditTrail<3>, 1, Pos, 0, Username)
        AuditTrail<4> = Insert(AuditTrail<4>, 1, Pos, 0, StationName)
        AuditTrail<5> = Insert(AuditTrail<5>, 1, Pos, 0, MacAddress)
        AuditTrail<8> = Insert(AuditTrail<8>, 1, Pos, 0, Activity)
        AuditTrail<9> = "AUDIT TRACKING RECORD"
        
        If RecordTracker EQ Yes$ Then
            // Special audit tracking is occuring. Store the current program
            // and the special marker in the audit trail entry.
            AuditTrail<6>  = Insert(AuditTrail<6>, 1, Pos, 0, RetStack()<2>)
            AuditTrail<7>  = Insert(AuditTrail<7>, 1, Pos, 0, Marker)
            AuditTrail<10> = "PROGRAM TRACKER"
        End Else
            AuditTrail<6>  = Insert(AuditTrail<6>, 1, Pos, 0, "")
            AuditTrail<7>  = Insert(AuditTrail<7>, 1, Pos, 0, "")
            AuditTrail<10> = AuditTrail<10>
        End
        
        // Update the Audit Trail record.
        Write AuditTrail to Locked_handle, ID Then
            If Activity NE "Read" Then
                // Only create an audit record if the Activity isn't Read.
                AuditRecord = OrigRecord
                If MacAddress EQ "" Then MacAddress = @Station                
                StoredRecId = ID:"*":MacAddress:"*":Current_Date:"*":Current_Time
                Write AuditRecord To Locked_handle, StoredRecId Then
                    Read RecentlyUpdatedList from Locked_handle, "%RECENTLY_UPDATED%" Else
                        RecentlyUpdatedList = ""
                    End
                    Locate ID in RecentlyUpdatedList using @FM setting UpdatePos Then
                        RecentlyUpdatedList = Delete(RecentlyUpdatedList, UpdatePos, 0, 0)
                    End
                    If RecentlyUpdatedList EQ "" then
                        RecentlyUpdatedList = ID
                    End Else
                        RecentlyUpdatedList = ID:@FM:RecentlyUpdatedList
                        If Count(RecentlyUpdatedList, @FM) GT 499 then
                            RecentlyUpdatedList = Field(RecentlyUpdatedList, @FM, 1, 500)
                        end
                    End
                    Write RecentlyUpdatedList to Locked_handle, "%RECENTLY_UPDATED%" Else
*                        Msg("|Unable to Write the record %RECENTLY_UPDATED%|to the ":AuditTable:".|")
                    End
                End Else
*                    Msg("|Unable to Write the record ":StoredRecId:"|to the ":AuditTable:".|")
                End
            End
        End Else
*            Msg("|Unable to Write the record ":ID:"|to the ":AuditTable:".|")
        End
        Lock_Record(AuditTable, Locked_handle, ID, Unlock$)
    End Else
*        Msg("|Unable to update ":AuditTable:" log.|")
    end
return

