Function NDW_Session_Manager_Events(CtrlEntId, Event, @PARAMS)
/***********************************************************************************************************************

    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 Infineon.

    Name        :   NDW_Session_Manager_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)
        06/17/18    dmb     Created initial commuter module.

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

#pragma precomp SRP_PreCompiler
#Window NDW_SESSION_MANAGER

$insert APP_INSERTS
$insert EVENT_SETUP
$insert MSG_EQUATES

Declare subroutine  SRP_Show_Window, Messaging_Services, Placedialog
Declare function    Database_Services, Messaging_Services, GetCurrentProcessId, SRP_Array

SubclassInfo    = Form_Services('FindSubclassControl')
Subclass        = SubclassInfo<1>

// Update the arguments so that the OpenInsight OLE event will treate the ActiveX event as a native event handler.
If Event EQ 'OLE' then
    Transfer Event to OIEvent
    Transfer Param1 to Event
    Transfer Param2 to Param1
    Transfer Param3 to Param2
    Transfer Param4 to Param3
    Transfer Param5 to Param4
*    Transfer Param6 to Param5
*    Transfer Param7 to Param6
*    Transfer Param8 to Param7
end

GoToEvent Event for CtrlEntID

Return EventFlow else EVENT_CONTINUE$


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Events
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Event WINDOW.CREATE(CreateParam)

    GoSub Setup_OLE_Controls

    Set_Property(@Window : '.PUB_CLOSE', 'NEXT', @Window : '.PUB_CLOSE')
    Set_Property(@Window : '.PUB_RELEASE_LOCK', 'NEXT', @Window : '.PUB_REFRESH')

    SRP_Show_Window(@Window, 'SYSTEM', 'C', 'C', 1, '', False$, False$)
    Placedialog(-2, -2)

end event


Event WINDOW.OMNIEVENT(Message, Param1, Param2, Param3, Param4)

    Begin Case
        Case Message _EQC 'GetLoggedInUsers'
            Transfer Param1 to SessionInfo
            Station        = SessionInfo<1, 2>
            OIUserName     = SessionInfo<2, 2>
            SSOUserName    = SessionInfo<3, 2>
            ProcessID      = SessionInfo<4, 2>
            LSL2Version    = SessionInfo<5, 2>
            OCXVersion     = SessionInfo<6, 2>
            Arguments      = Station:@VM:OIUserName:@VM:SSOUserName:@VM:ProcessID:@VM:LSL2Version:@VM:OCXVersion
            Send_Message(@Window : '.OLE_RPT_USER_SESSIONS', 'OLE.Insert', -1, Arguments)
            NumOfSessions  = Get_Property(@Window : '.EDL_NUMBER_OF_SESSIONS', 'TEXT')
            NumOfSessions += 1
            Set_Property(@Window : '.EDL_NUMBER_OF_SESSIONS', 'TEXT', NumOfSessions)

        Case Message _EQC 'GetLockOwner'
            Transfer Param1 to LockInfo
            AtSession   = LockInfo<1>[-1, 'B ']
            UserList    = Get_Property(@Window : '.OLE_RPT_USER_SESSIONS', 'OLE.List')
            UserArray   = SRP_Array('Rotate', UserList, @FM, @VM)
            Locate AtSession in UserArray<1> using @VM setting vPos then
                Set_Property(@Window : '.OLE_RPT_USER_SESSIONS', 'OLE.SelPos', vPos)
            end
            Set_Property(@Window : '.OLE_EDT_LOCK_INFORMATION', 'OLE.CellText[1;1]', AtSession)
            Set_Property(@Window : '.OLE_EDT_LOCK_INFORMATION', 'OLE.CellText[1;2]', LockInfo<2>[-1, 'B '])
            Set_Property(@Window : '.OLE_EDT_LOCK_INFORMATION', 'OLE.CellText[1;3]', LockInfo<3>[-1, 'B '])
            Set_Property(@Window : '.OLE_EDT_LOCK_INFORMATION', 'OLE.CellText[1;4]', LockInfo<4>[-1, 'B '])
            Set_Property(@Window : '.OLE_EDT_LOCK_INFORMATION', 'OLE.CellText[1;5]', 'Lock owner found.')

        Case Message _EQC 'ReleaseLock'
            Transfer Param1 to LockInfo
            AtSession   = LockInfo<1>[-1, 'B ']
            UserList    = Get_Property(@Window : '.OLE_RPT_USER_SESSIONS', 'OLE.List')
            UserArray   = SRP_Array('Rotate', UserList, @FM, @VM)
            Locate AtSession in UserArray<1> using @VM setting vPos then
                Set_Property(@Window : '.OLE_RPT_USER_SESSIONS', 'OLE.SelPos', vPos)
            end
            Set_Property(@Window : '.OLE_EDT_LOCK_INFORMATION', 'OLE.CellText[1;1]', AtSession)
            Set_Property(@Window : '.OLE_EDT_LOCK_INFORMATION', 'OLE.CellText[1;2]', LockInfo<2>[-1, 'B '])
            Set_Property(@Window : '.OLE_EDT_LOCK_INFORMATION', 'OLE.CellText[1;3]', LockInfo<3>[-1, 'B '])
            Set_Property(@Window : '.OLE_EDT_LOCK_INFORMATION', 'OLE.CellText[1;4]', LockInfo<4>[-1, 'B '])
            Set_Property(@Window : '.OLE_EDT_LOCK_INFORMATION', 'OLE.CellText[1;5]', 'Lock was released.')
            
    End Case

end event


Event PUB_REFRESH.CLICK()

    Set_Property(@Window : '.OLE_RPT_USER_SESSIONS', 'OLE.List', '')
    Set_Property(@Window : '.EDL_NUMBER_OF_SESSIONS', 'TEXT', 0)
    Messaging_Services('SendMessage', 'GetLoggedInUsers', 'Request', '', 'All', '', 'EventHandler', @Window : ',OMNIEVENT,@MESSAGE,@ARGUMENTS')

end event


Event PUB_FIND_LOCK_OWNER.CLICK()

    TableName   = Get_Property(@Window : '.EDL_TABLE_NAME', 'TEXT')
    KeyID       = Get_Property(@Window : '.EDL_KEY_ID', 'TEXT')
    If (TableName NE '') AND (KeyID NE '') then
        Set_Property(@Window : '.OLE_EDT_LOCK_INFORMATION', 'OLE.CellText[1;All]', '')
        Messaging_Services('SendMessage', 'GetLockOwner', 'Request', '', 'All', TableName : ',' : KeyID, 'EventHandler', @Window : ',OMNIEVENT,@MESSAGE,@ARGUMENTS')
    end

end event


Event PUB_RELEASE_LOCK.CLICK()

    TableName   = Get_Property(@Window : '.EDL_TABLE_NAME', 'TEXT')
    KeyID       = Get_Property(@Window : '.EDL_KEY_ID', 'TEXT')
    If (TableName NE '') AND (KeyID NE '') then
        Set_Property(@Window : '.OLE_EDT_LOCK_INFORMATION', 'OLE.CellText[1;All]', '')
        Messaging_Services('SendMessage', 'ReleaseLock', 'Request', '', 'All', TableName : ',' : KeyID, 'EventHandler', @Window : ',OMNIEVENT,@MESSAGE,@ARGUMENTS')
    end

end event


Event OLE_RPT_USER_SESSIONS.OnItemClick(Row, Button, Col, Point)

    Begin Case
        Case Button _EQC 'Right'
            ContextMenu     = 'SEND_MESSAGE' : @VM : 'Send Message...' : @FM
            ContextMenu    := 'CLOSE_SESSION' : @VM : 'Close Session...'
            SelPos          = Get_Property(CtrlEntId, 'OLE.SelPos')
            Convert @FM to ';' in SelPos
            Send_Message(CtrlEntId, 'OLE.ShowContextMenu', Point, ContextMenu, SelPos)

    End Case

end event


Event OLE_RPT_USER_SESSIONS.OnContextMenuClick(Item, UserData)

    Begin Case
        Case Item _EQC 'SEND_MESSAGE'
            Message     = Dialog_Box('NDW_SESSION_MESSAGE', @Window)
            If Message NE '' then
                For Each Row in UserData using ';'
                    SessionID   = Get_Property(CtrlEntID, 'OLE.RowData[' : Row : ']')<1>
                    Messaging_Services('SendMessage', 'SetPopupMessage', 'Request', '', SessionID, Message)
                Next Row
            end

        Case Item _EQC 'CLOSE_SESSION'
            CloseSession    = Msg(@Window, '', 'YESNO', '', 'Session Manager' : @FM : 'Warning! This will attempt to close active OpenInsight sessions. Please confirm that this is what you want to do.')
            If CloseSession EQ True$ then
                For Each Row in UserData using ';'
                    SessionID   = Get_Property(CtrlEntID, 'OLE.RowData[' : Row : ']')<1>
                    Messaging_Services('SendMessage', 'CloseSession', 'Request', '', SessionID, SessionID)
                Next Row
            end

    End Case

end event


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Internal GoSubs
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Setup_OLE_Controls:

    Qualify     = ''
    Qualify<1>  = 1
    Qualify<4>  = 0

    Ctrl        = @Window : '.OLE_PIC_HORIZONTAL_1'
    Set_Property(Ctrl, 'ControlPart', 'Divider')
    Set_Property(Ctrl, 'Caption', 'Active Sessions (Right-Click for Session Options)')
    Set_Property(Ctrl, 'OLE.Alignment', 'Top' : @FM : 'Left')
    Set_Property(Ctrl, 'OLE.CaptionOffset', 0 : @FM : -9)
    Set_Property(Ctrl, 'OLE.Font', 'Segoe UI' : @SVM : 9 : @SVM : 700)

    Ctrl        = @Window : '.OLE_PIC_HORIZONTAL_2'
    Set_Property(Ctrl, 'ControlPart', 'Divider')
    Set_Property(Ctrl, 'Caption', 'Lock Management')
    Set_Property(Ctrl, 'OLE.Alignment', 'Top' : @FM : 'Left')
    Set_Property(Ctrl, 'OLE.CaptionOffset', 0 : @FM : -9)
    Set_Property(Ctrl, 'OLE.Font', 'Segoe UI' : @SVM : 9 : @SVM : 700)

    Ctrl        = @Window : '.OLE_RPT_USER_SESSIONS'
    ColumnList  = 'Station' : @VM : 'Text' : @VM : '80' : @VM : True$ : @FM
    ColumnList := 'OI UserName' : @VM : 'Text' : @VM : '80' : @VM : True$ : @FM
    ColumnList := 'SSO UserName' : @VM : 'Text' : @VM : '80' : @VM : True$ : @FM
    ColumnList := 'Process ID' : @VM : 'Number' : @VM : '60' : @VM : @VM : @VM : @VM : @VM : @VM : @VM : 'Right' : @VM : @VM : '#######' : @FM
    ColumnList := 'LSL2 Version' : @VM : 'Text' : @VM : '50' : @VM : True$ : @VM : @VM : @VM : @VM : @VM : @VM : 'Right' : @VM : @VM : @FM
    ColumnList := 'OCX Version' : @VM : 'Text' : @VM : '50' : @VM : True$ : @VM : @VM : @VM : @VM : @VM : @VM : 'Right' : @VM : @VM : @FM
    Set_Property(Ctrl, 'OLE.ColumnList', ColumnList)
    Set_Property(Ctrl, 'OLE.ColumnFont[All]', 'Segoe UI' : @SVM : 9)
    Set_Property(Ctrl, 'OLE.ColumnHeaderFont[All]', 'Segoe UI' : @SVM : 9 : @SVM : 700)
    Set_Property(Ctrl, 'OLE.AlwaysShowSelection', True$)
    Set_Property(Ctrl, 'OLE.MultiSelect', 1)
    Send_Message(Ctrl, 'QUALIFY_EVENT', 'OLE.OnItemClick', 1)
    Send_Message(Ctrl, 'QUALIFY_EVENT', 'OLE.OnContextMenuClick', 1)

    Ctrl        = @Window : '.OLE_EDT_LOCK_INFORMATION'
    Set_Property(Ctrl, 'OLE.Dimension', 1 : @FM : 5)
    Set_Property(Ctrl, 'OLE.HeaderColumn[1]', 80 : @FM : True$ : @FM : False$ : @FM : False$)
    Set_Property(Ctrl, 'OLE.DataRow[All]', 23 : @FM : True$ : @FM : False$)
    Set_Property(Ctrl, 'OLE.HeaderRow[1]', @FM : False$)
    Set_Property(Ctrl, 'OLE.HeaderText[1;2]', '@Station')
    Set_Property(Ctrl, 'OLE.HeaderText[1;3]', '@User4')
    Set_Property(Ctrl, 'OLE.HeaderText[1;4]', '@UserName')
    Set_Property(Ctrl, 'OLE.HeaderText[1;5]', 'Process ID')
    Set_Property(Ctrl, 'OLE.HeaderText[1;6]', 'Message')
    Set_Property(Ctrl, 'OLE.DataColumn[1]', 240 : @FM : True$ : @FM : False$ : @FM : True$)
    Set_Property(Ctrl, 'OLE.HeaderFont[1;All]', 'Segoe UI' : @SVM : 9)
    Set_Property(Ctrl, 'OLE.HeaderFont[1;6]', 'Segoe UI' : @SVM : 9 : @SVM : 700)
    Set_Property(Ctrl, 'OLE.CellFont[1;All]', 'Segoe UI' : @SVM : 9)
    Set_Property(Ctrl, 'OLE.CellFont[1;5]', 'Segoe UI' : @SVM : 9 : @SVM : 700)
    Set_Property(Ctrl, 'OLE.ScrollBarsVisible', 'Never' : @FM : 'Never')
    Set_Property(Ctrl, 'ENABLED', False$)

return

