'ObjectEvents_AccountArchive_Archive
Option Explicit On
Option Strict On

' #################################################################
' Account Archive Script

' Version: 1.00 (8/07/2013)

' Usage: When archiving an Account.
' #################################################################

Public Function Main(ByVal eventId As String, ByRef eventHandled As Boolean, ByVal source As Object, ByVal sourceIndex As Integer, ByVal contextData1 As Object, ByVal contextData2 As Object, ByVal parameters As Object) As Boolean

  ' Parameters:
  '   eventId - The event to process
  '   eventHandled - Used to indicate that an event was handled by this Script so normal processing should be ignored (only applies to certain events)
  '   source - The Source object
  '   sourceIndex - The Source object's index, if applicable
  '   contextData1 - Context specific data
  '   contextData2 - Context specific data
  '   parameters - Other values passed to the Script

  Dim AccountArchive As finAccountArchive
  Dim AccountLog As finLogRO
  Dim AccountWorkflow As finAccountWorkflow
  Dim kvlDocuments As ISKeyValueList
  Dim kvlLogs As ISKeyValueList
  Dim kvlWorkflows As ISKeyValueList
  Dim key As String

  ' Assume Success
  Main = True

  ' Initialise
  AccountArchive = DirectCast(source, finAccountArchive)

  ' Handle Events
  Select Case eventId
    Case "AccountArchivePre"
      ' Populate UserData with additional information you want to save with the Archive. Return False to cancel.

      ' Initialise
      kvlDocuments = finBL.CreateKeyValueList
      kvlLogs = finBL.CreateKeyValueList
      kvlWorkflows = finBL.CreateKeyValueList

      ' Document/ Log Summary
      For Each AccountLog In AccountArchive.Account.Logs
        If AccountLog.DocumentPk <> 0 Then
          key = AccountLog.DocumentId & ": " & finbl.Documents.Item(AccountLog.DocumentId).Description
          kvlDocuments.SetInteger(key, kvlDocuments.GetInteger(Key) + 1)
        ElseIf Len(AccountLog.Subject) <> 0 Then
          Select Case AccountLog.LogType
            Case isefinLogType.Audit, isefinLogType.AuditChange, isefinLogType.AuditSecurity, isefinLogType.Service
              ' Ignore
            Case Else
              key = AccountLog.Subject
              kvlLogs.SetInteger(key, kvlLogs.GetInteger(Key) + 1)
          End Select
        End If
      Next

      ' Workflow Summary
      For Each AccountWorkflow In AccountArchive.Account.Workflows
        key = AccountWorkflow.WorkflowTypeId & ": " & finbl.WorkflowTypes.Item(AccountWorkflow.WorkflowTypeId).Description
        kvlWorkflows.SetInteger(key, kvlWorkflows.GetInteger(Key) + 1)
      Next

      ' Add Summaries
      If kvlDocuments.Count <> 0 Then AccountArchive.UserData.SetKeyValueList("Documents", kvlDocuments)
      If kvlLogs.Count <> 0 Then AccountArchive.UserData.SetKeyValueList("Logs", kvlLogs)
      If kvlWorkflows.Count <> 0 Then AccountArchive.UserData.SetKeyValueList("Workflows", kvlWorkflows)

    Case "AccountArchivePreInTransaction"
      ' Database Transaction begun, so you can update database information if required. Return False to cancel.

  End Select

End Function