Function NDW_Audit_Viewer_Events(CtrlEntId, Event, Param1, Param2, Param3, Param4, Param5, Param6, Param7, Param8, Param9, Param10, Param11, Param12, Param13, Param14, Param15)

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

    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        :   NDW_Audit_Viewer_Events

    Description :   This function acts as a commuter module for all events related to this window.

    Notes       :   Commuter Modules are automatically called from the Promoted_Events function which is called by the
                    application-specific promoted event handler. This makes it possible to add QuickEvents that need to
                    execute Basic+ logic without having use the Form Designer to make the association, although this is
                    limited to the events which are currently promoted.

                    If the form needs to call the commuter module directly then the QuickEvent parameters should be
                    formatted like this:

                        '@SELF','@EVENT',['@PARAM1','@PARAMx']

    Parameters  :
        CtrlEntId   [in] -- The fully qualified name of the control calling the promoted event
        Event       [in] -- The event being executed. See the Notes section regarding "PRE" events
        Param1-15   [in] -- Additional event parameter holders
        EventFlow  [out] -- Set to 1 or 0 so the calling event knows whether or not to chain forward. See comments in
                            EVENT_SETUP insert

    History     :   (Date, Initials, Notes)
        07/23/98    dmb     Original programmer.
        03/30/06    axf     Removed reference to Application.dll.
        04/06/13    dmb     Replace ErrorLines property with MismatchLines property for better comparison viewing.
                            - [SRPFW-9]

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

$insert APP_INSERTS
$insert EVENT_SETUP
$insert MSG_EQUATES

Equ WM_USER$ To 1024
Equ WM_SETREADONLY$ To WM_USER$ + 31

AuditManagerTable = "APP_INFO"

Declare Subroutine Msg, Send_Event, V119, SRP_Set_Prop_Array, Utility, Start_Window, Send_Message, Get_SysInfo, SRP_Show_Window, Set_Property, Post_Event
Declare Function GetPointer, GetSystemInfo, Send_Message

Begin Case
    Case Control EQ Window
        // This event is window specific.

        Begin Case
            Case Event EQ "CREATE"          ; GoSub CREATE
            Case Event EQ 'EXPAND_ROW'      ; GoSub EXPAND_ROW
            Case Event EQ 'COLLAPSE_ROW'    ; GoSub COLLAPSE_ROW
            Case Event EQ 'GOTO_MARKER'     ; GoSub TOGGLE_CURSOR
        End Case

    Case EventType EQ "CLICK"
        
        Begin Case
            Case Control EQ "PUB_RESTORE"   ; GoSub CLICK.PUB_RESTORE
            Case Control EQ "PUB_COMPARE"   ; GoSub CLICK.PUB_COMPARE
            Case Control EQ "PUB_RETURN"    ; GoSub CLICK.PUB_RETURN
        End Case
        
    Case Event EQ "PosChanged"              ; GoSub PosChanged
    Case Event EQ "OnGotFocus"              ; GoSub PosChanged
    Case Event EQ "OnVScroll"
        
        Begin Case
            Case Control EQ "OLE_ARCHIVE"   ; GoSub OnVScroll.OLE_ARCHIVE
            Case Control EQ "OLE_ORIGINAL"  ; GoSub OnVScroll.OLE_ORIGINAL
        End Case
        
    Case Event EQ 'MENU.EXPAND_ROW'         ; GoSub MENU.EXPAND_ROW
    Case Event EQ 'MENU.COLLAPSE_ROW'       ; GoSub MENU.COLLAPSE_ROW
    Case Event EQ 'MENU.NEXT_DIFFERENCE'    ; GoSub MENU.NEXT_DIFFERENCE

End Case

If Assigned(EventFlow) else EventFlow = EVENT_CONTINUE$

Return EventFlow

CREATE:

    // qualify editor events
    Events = "PosChanged,OnGotFocus,OnVScroll"
	LenEvents = Len(Events)
	Pos = 1
	Loop Until Pos GE LenEvents
		Event = "OLE.":Events[Pos, ","]
		Pos = Col2() + 1
		Send_Message(@Window:".OLE_ARCHIVE", "QUALIFY_EVENT", Event, Yes$)
		Send_Message(@Window:".OLE_ORIGINAL", "QUALIFY_EVENT", Event, Yes$)
	Repeat
	Send_Message(@Window:".OLE_VIEWER", "QUALIFY_EVENT", "PosChanged", Yes$)
    
    // show the viewer
	SRP_Show_Window(@Window, "", "L", "T", Yes$, "", No$, No$)
	
return

EXPAND_ROW:
    *----------------------------------------------------------------------------------------------------
    * Expands row(s) in the Audit Viewer Window
    *----------------------------------------------------------------------------------------------------
    WindowControl = Get_Property(@Window, "FOCUS")
    Set_Property(WindowControl, "READONLY", No$)
    rv = Send_Message(WindowControl, "OLE.ExpandCurrLine")
    Set_Property(WindowControl, "READONLY", Yes$)
    
return

COLLAPSE_ROW:
    *----------------------------------------------------------------------------------------------------
    * Collapses Row(s) in the Audit Viewer Window
    *----------------------------------------------------------------------------------------------------
    WindowControl = Get_Property(@Window, "FOCUS")
    Set_Property(WindowControl, "READONLY", No$)
    rv = Send_Message(WindowControl, "OLE.CollapseCurrLine")
    Set_Property(WindowControl, "READONLY", Yes$)
return

TOGGLE_CURSOR:
    *----------------------------------------------------------------------------------------------------
    * Toggles from marker to marker in the Audit View Window
    *----------------------------------------------------------------------------------------------------
    Set_Property(WindowControl, "READONLY", No$)
    WindowControl = Get_Property(@Window, "FOCUS")
    rv = Send_Message(WindowControl, "OLE.GoToMarker")
    Set_Property(WindowControl, "READONLY", Yes$)
return

CLICK.PUB_RESTORE:
    *----------------------------------------------------------------------------------------------------
    * 
    *----------------------------------------------------------------------------------------------------

    Begin Case
        Case Window = "NDW_AUDIT_TRAIL"
            Win$ = @Window
        Case Otherwise$
            Win$ = Get_Property(@Window:".EDL_WINDOW", "INVALUE")
    End Case
    TableName = Get_Property(Win$:".COB_TABLE", "INVALUE")
    Convert @Lower_Case to @Upper_Case in TableName
    rVal = Get_Property(Win$:".EDT_INFO_BAK", "ARRAY")
    Convert @VM to " " in rVal
    rVal = Trim(rVal)
    TestVal = Count(rVal, " ")
    If TestVal GT 0 Then
        ErrorMsg = "TOOMANY"
        Gosub MsgAlert
    End Else
        ErrorMsg = "RESTORE"
        Gosub MsgAlert
        If rv = Yes$ Then
            AuditId = rVal
            OrigId = Get_Property(Win$:".EDL_ID", "INVALUE")
            Begin Case
                Case AuditId = ""
                    ErrorMsg = "ID"
                    IdName = "Audit Primary Key"
                    Gosub MsgAlert
                Case OrigId  = ""
                    ErorMsg = "ID"
                    IdName = "Primary Key"
                    Gosub MsgAlert
                Case Otherwise$
                    Open TableName To hORT Then
                        Open "AUDIT_":TableName To hADT Then
                            ReadO Record From hADT, AuditId Then
                                Write Record To hORT, OrigId Then
                                    ErrorMsg = "RESTORED"
                                    Gosub MsgAlert
                                End Else
                                    RecId = OrigId
                                    ErrorMsg = "WRITE"
                                    Gosub MsgAlert
                                End
                            End Else
                                ErrorMsg = "READ"
                                RecId = AuditId
                                Gosub MsgAlert
                            End
                        End Else
                            ErrorMsg = "OPEN"                
                            Gosub MsgAlert
                        End
                    End Else
                        ErrorMsg = "OPEN"
                        Gosub MsgAlert
                    End                    
            End Case
        End
    End    
    
return

CLICK.PUB_RETURN:
    *----------------------------------------------------------------------------------------------------
    * 
    *----------------------------------------------------------------------------------------------------
    Send_Event(@Window, "PAGE", 1)
    cWin$ = @Window
    Page  = 1
    Gosub ResetTabOrder
    
return

CLICK.PUB_COMPARE:
    *----------------------------------------------------------------------------------------------------
    * 
    *----------------------------------------------------------------------------------------------------
    Post_Event(@Window, "PAGE", 2)
    OrigWin$ = Get_Property(@Window:".EDL_WINDOW", "INVALUE")    
    TableName = Get_Property(OrigWin$:".COB_TABLE", "INVALUE")
    Convert @Lower_Case to @Upper_Case In TableName 
    RecId = Get_Property(OrigWin$:".EDL_ID", "INVALUE")
    Open TableName to hORT Then
        ReadO RecInfo From hORT, RecId Then            
            Open "AUDIT_":TableName to hART Then
                ARecId = Get_Property(@Window:".EDL_AREC_ID", "INVALUE")
                ReadO ARecInfo From hART, ARecId Then
                    cWin$ = @Window
                    Page = 2
                    Gosub ResetTabOrder
                    Gosub CompareValues
                End
            End
        End
    End
    
return

MENU.EXPAND_ROW:
    FocusCtrl = Get_Property("SYSTEM", "FOCUS")
*    Set_Property(FocusCtrl, "OLE.ReadOnly", No$)
    Send_Message(FocusCtrl, "OLE.ExpandCurrLine")
*    Set_Property(FocusCtrl, "OLE.ReadOnly", Yes$)
return

MENU.COLLAPSE_ROW:
    FocusCtrl = Get_Property("SYSTEM", "FOCUS")
*    Set_Property(FocusCtrl, "OLE.ReadOnly", No$)
    Send_Message(FocusCtrl, "OLE.CollapseCurrLine")
*    Set_Property(FocusCtrl, "OLE.ReadOnly", Yes$)
return
        
MENU.NEXT_DIFFERENCE:
    FocusCtrl = Get_Property("SYSTEM", "FOCUS")
	Send_Message(FocusCtrl, "OLE.GotoMarker")
	Begin Case
	    Case FocusCtrl EQ @Window:".OLE_ARCHIVE"
	        Set_Property(@Window:".OLE_ORIGINAL", "OLE.TopLine", Get_Property(@Window:".OLE_ARCHIVE", "OLE.TopLine"))
	    Case FocusCtrl EQ @Window:".OLE_ORIGINAL"
	        Set_Property(@Window:".OLE_ARCHIVE", "OLE.TopLine", Get_Property(@Window:".OLE_ORIGINAL", "OLE.TopLine"))
	End Case
return

PosChanged:

    TrailWindow = Get_Property(@Window:".EDL_WINDOW", "TEXT")
    Table = Get_Property(TrailWindow:".COB_TABLE", "TEXT")
    FieldsRec = Get_Property(@Window, "@RECORD_FIELDS")
    CurrLine = Get_Property(CtrlEntId, "OLE.CurrPos")<2>
    Line = Send_Message(CtrlEntId, "OLE.GetLineField", CurrLine)
    
    Field = ""
    If FieldsRec EQ "" then
	    DictTable = If Table[1, 5] EQ "DICT." then "DICT.SYSCOLUMNS" else "DICT.":Table
	    Open DictTable to hTable then
	        Read FieldsRec from hTable, "%FIELDS%" then
	        	Set_Property(@Window, "@RECORD_FIELDS", FieldsRec)
	        end
	    end
	end
	
    Fields = FieldsRec<3>
    Positions = FieldsRec<5>
    Locate Line in Positions setting Pos then
	    Field = Fields<1, Pos>
    end
    
    Set_Property(@Window:".STA_COLUMN", "TEXT", Field)
    
return

OnVScroll.OLE_ARCHIVE:
    Set_Property(@Window:".OLE_ORIGINAL", "OLE.TopLine", Param1)
return

OnVScroll.OLE_ORIGINAL:
    Set_Property(@Window:".OLE_ARCHIVE", "OLE.TopLine", Param1)
return

*------------------------
* Internal Processes
*------------------------

CompareValues:
    *----------------------------------------------------------------------------------------------------
    * 
    *----------------------------------------------------------------------------------------------------
    dRow = ""
    MxORows = Count(RecInfo, @FM) + (RecInfo NE "")
    MxARows = Count(ARecINfo, @FM) + (ARecInfo NE "")
    
    If MxORows GE MxARows Then
        MxRows = MxORows
    End Else
        MxRows = MxARows
    End
    
    For X = 1 to MxRows
        If RecInfo<X> NE ARecInfo<X> Then
            dRow<-1> = X
        End
    Next X
    
    If dRow NE "" Then
        Ctrls = @Window:".OLE_ARCHIVE":@RM:@Window:".OLE_ORIGINAL":@RM:@Window:".OLE_VIEWER"
*        Props = "OLE.ErrorLines":@RM:"OLE.ErrorLines":@RM:"OLE.ErrorLines"
        Props = "OLE.MismatchLines":@RM:"OLE.MismatchLines":@RM:"OLE.MismatchLines"
        Vals  = dRow:@RM:dRow:@RM:dRow
        Set_Property(Ctrls, Props, Vals)
    End
return

MsgAlert:
    *-------------------------------------------------------------------------------------------------------------------------------------
    *
    *-------------------------------------------------------------------------------------------------------------------------------------

    Mess = ""
    Mess<MTYPE$>  = "BO"
    Mess<MICON$>  = "!"
    Mess<MCAPTION$> = "Audit Trail"

    Error = "An undefined error has occurred with this process"
    Begin Case
        Case ErrorMsg = "OPEN"
            Error = "Unable to Open the ": TableName: " table."

        Case ErrorMsg = "READ"
            Error = "Unable to Read ": RecId:" record."
            
        Case ErrorMsg = "WRITE"
            Error = "Unable to Write ": RecId:" record."
        
        Case ErrorMsg = "NOIDS"
            Error = "There are no audit records for the ":TableName:" table."
                
        Case ErrorMsg = "ID"
            Error = IdName: " is Null."
                        
        Case ErrorMsg = "RESTORED"
            Error = "Record ":OrigId:" has been restored to the ":TableName:" table."
        
        Case ErrorMsg = "TOOMANY"
            Error = "You only restore one record at a time."
            
        Case ErrorMsg = "RESTORE"
            Mess<MICON$>  = "?"
            Mess<MTYPE$>  = "BNY"
            Error = "Would you like to restore this entry?"
            
        Case ErrorMsg = "NOTABLE"
            Error = "No Table Selected"        
    End Case

    Mess<MTEXT$> = Error
    rv = Msg("",Mess)

return

ResetTabOrder:
    *-------------------------------------------------------------------------------------------------------------------------
    * Resets Tab Order
    *-------------------------------------------------------------------------------------------------------------------------

    LastPageCtrl = ""
    LastCtrl = ""
    FirstCtrl = ""
    rVal = ""
    PCtrls = ""
    NCtrls = ""
    NProps = ""

    Begin Case
        Case cWin$ EQ "NDW_AUDIT_TRAIL"
            rVal<1> = @Window:".COB_TABLE"
            rVal<2> = @Window:".EDL_ID"
            rVal<3> = @Window:".EDT_AUDIT_INFO"
            rVal<4> = @Window:".PUB_RESTORE"
            rVal<5> = @Window:".PUB_VIEW"
                    
        Case Otherwise$
            Begin Case
                Case Page EQ 1
                    Set_Property("SYSTEM", "FOCUS", cWin$:".OLE_VIEWER")
                    rVal<1> = cWin$:".OLE_VIEWER"
                    rVal<2> = cWin$:".PUB_RESTORE"
                    rVal<3> = cWin$:".PUB_COMPARE"                    
                    rVal<4> = cWin$:".PUB_CLOSE"
                Case Page EQ 2
                    Set_Property("SYSTEM", "FOCUS", cWin$:".OLE_ARCHIVE")
                    rVal<1> = cWin$:".OLE_ARCHIVE"
                    rVal<2> = cWin$:".OLE_ORIGINAL"
                    rVal<3> = cWin$:".PUB_RESTORE"                    
                    rVal<4> = cWin$:".PUB_RETURN"
                    rVal<5> = cWin$:".PUB_CLOSE"
            End Case    
    End Case
    MxC = Count(rVal,@FM) + (rVal NE "")

    For i = 1 to MxC
        If i = MxC Then
            nVal = 1
        End Else
            nVal = i + 1
        End
        PCtrls<-1> = rVal<i>        
        NCtrls<-1> = rVal<nVal>
        NProps<-1> = "NEXT"
    Next i
    
    Convert @FM to @RM in PCtrls
    Convert @FM to @RM in NCtrls
    Convert @FM to @RM in NProps

    Set_Property(PCtrls, NProps, NCtrls)
return
