'**********************************************************
' addPerformerToUserTable
' @description adds the author to the user's table if not present
' @param sActivity the current activity's name
' @return Nothing
'**********************************************************
Sub addPerformerToUserTable(sActivity)
	debug2File "Start - addPerformerToUserTable (" & sActivity & ")"
	Dim sCurrentUserGuid : sCurrentUserGuid = getCurrentActivityPerformerId
	If sCurrentUserGuid = "" Then
		debug2File "addPerformerToUserTable (" & sActivity & ") => No User Guid"	
		Exit Sub
	End If
	
	'CC-000138 start
	Dim sAuthorRoleName : sAuthorRoleName = ""
	If sActivity = "Create" Or sActivity = "Revise" Then 
		sAuthorRoleName = globalConfig("AuthorFunction")
	End If 'CC-000138 end
	
	
	'get the complete (firstname + lastname) username

	Dim sCurrentUserName : sCurrentUserName = wforganisation.GetOrgObjectById( sCurrentUserGuid ).GetAttributeValueByName("Vorname") & " " & wforganisation.GetOrgObjectById(sCurrentUserGuid).GetAttributeValueByName("Nachname")
	If Trim(sCurrentUserName) = "" Then 'use the LogIn Name if no complete name is defined
		sCurrentUserName = wforganisation.GetOrgObjectById( sCurrentUserGuid ).Name
	End If
	debug2File "addPerformerToUserTable (" & sActivity & ") => sCurrentUserGuid = " & sCurrentUserGuid & ", sCurrentUserName = " & sCurrentUserName 		
	
	'this could occur if the create activity was performed and forwarded to collaboration. if the the collaboration item is finshed the create wf-item appears again. 
	'in this case we don't want to have 2 Create entries in the user table
	For a = 0 To wfvariables.recUsersSorted.Count - 1	
		debug2File "Activity: " & wfvariables.recUsersSorted.Item(a).Item("sActivity").Value & " = " & sActivity & "   -   PerformedBy " &  wfvariables.recUsersSorted.Item(a).Item("sPerformedBy").Value & " = " & sCurrentUserName & " - PerformedAt: " & wfvariables.recUsersSorted.Item(a).Item("dPerformedAt").Value
		If wfvariables.recUsersSorted.Item(a).Item("sActivity").Value = sActivity And wfvariables.recUsersSorted.Item(a).Item("sPerformedBy").Value = sCurrentUserName And wfvariables.recUsersSorted.Item(a).Item("dPerformedAt").Value <> "" Then
			debug2File vbTab &  " -> Exit and don't add the user again to the usertable"
			'the activity was already performed, so don't add another activity
			
			'START CC-000201
			'now check if the user is still in the recUsers record, if not add him again
			'This could happen, if we got following workflow run
			'Revise -> Collobaration -> Revise (now the author deletes himself from the usertable) -> any workitem -> back to Revise
			Dim bFoundInUserTable : bFoundInUserTable = False
			For b = 0 To wfvariables.recUsers.Count - 1
				If wfvariables.recUsers.Item(b).Item("sActivity").Value = sActivity Then
					bFoundInUserTable = True
					Exit For
				End If
			Next
			If bFoundInUserTable = False Then
				debug2File "Activity " & sActivity  & " was already performed but is missing in the recUsers Variable, so add the activity again."
				Set oEntry = wfvariables.recUsers.CreateListItem 
				oEntry.Item("sActivity").Value = sActivity
				oEntry.Item("sUserGuid").Value = wfvariables.recUsersSorted.Item(a).Item("sUserGuid").Value
				oEntry.Item("sUserName").Value = wfvariables.recUsersSorted.Item(a).Item("sUserName").Value
				oEntry.Item("sPerformedBy").Value = wfvariables.recUsersSorted.Item(a).Item("sPerformedBy").Value
				oEntry.Item("sDecision").Value = wfvariables.recUsersSorted.Item(a).Item("sDecision").Value
				oEntry.Item("dPerformedAt").Value = wfvariables.recUsersSorted.Item(a).Item("dPerformedAt").Value
				oEntry.Item("sDate").Value = wfvariables.recUsersSorted.Item(a).Item("sDate").Value
				
				If sActivity = "Create" Or sActivity = "Revise" Then 'CC-000138 start
					oEntry.Item("sRoleName").Value = sAuthorRoleName 
					wfvariables.recUsersSorted.Item(a).Item("sRoleName").Value = sAuthorRoleName 
				End If 'CC-000138 end
	
				wfvariables.recUsers.AddListItem oEntry	
			End If
			'END CC-000201
			Exit sub							
		End If
	Next

	Dim oEntry
	Dim a
	Dim b
	
	debug2File "addPerformerToUserTable (" & sActivity & ") => check " & wfvariables.recUsers.Count & " present users"
	For a = 0 To wfvariables.recUsers.Count - 1	
		Dim sPlannedPerformerId : sPlannedPerformerId = wfvariables.sPlannedPerformerID.Value
		debug2File "addPerformerToUserTable (" & sActivity & ") => check activity " & wfvariables.recUsers.Item(a).Item("sActivity").Value & " = " & sActivity
		debug2File "addPerformerToUserTable (" & sActivity & ") => check sUserGuid " & wfvariables.recUsers.Item(a).Item("sUserGuid").Value & " = " & sPlannedPerformerId
		If wfvariables.recUsers.Item(a).Item("sActivity").Value = sActivity And wfvariables.recUsers.Item(a).Item("sUserGuid").Value = sPlannedPerformerId Then 'user are assigned in the table
			debug2File "addPerformerToUserTable (" & sActivity & ") => record " & sActivity & " found!"
			
			wfvariables.recUsers.Item(a).Item("sPerformedBy").Value = sCurrentUserName
			'wfvariables.recUsers.Item(a).Item("dPerformedAt").Value = Date()
			wfvariables.recUsers.Item(a).Item("dPerformedAt").Value = getCurrentCustomerLocalDate 'CR1.7
			wfvariables.recUsers.Item(a).Item("sDate").Value = getCurrentCustomerLocalDate 'CR 1.7
			wfvariables.recUsers.Item(a).Item("sDecision").Value = wfvariables.sOptions.Value						
			
			debug2File "addPerformerToUserTable (" & sActivity & ") => record " & sActivity & " updated with username " & sCurrentUserName

			'Set oEntry = wfvariables.recUsersSorted.CreateListItem	'CC-000159
			Set oEntry = wfvariables.recUsersSortedLocal.CreateListItem	'CC-000159
			
			oEntry.Item("sActivity").Value = sActivity
			oEntry.Item("sUserGuid").Value = wfvariables.recUsers.Item(a).Item("sUserGuid").Value
			oEntry.Item("sUserName").Value = wfvariables.recUsers.Item(a).Item("sUserName").Value
			oEntry.Item("sRoleGuid").Value = wfvariables.recUsers.Item(a).Item("sRoleGuid").Value
			oEntry.Item("sRoleName").Value = wfvariables.recUsers.Item(a).Item("sRoleName").Value		
			
			If sActivity = "Create" Or sActivity = "Revise" Then 'CC-000138 start
				oEntry.Item("sRoleName").Value = sAuthorRoleName 
				wfvariables.recUsers.Item(a).Item("sRoleName").Value = sAuthorRoleName
			End If 'CC-000138 end
			
			oEntry.Item("sPerformedBy").Value = sCurrentUserName
			oEntry.Item("sDecision").Value = wfvariables.sOptions.Value
			'oEntry.Item("dPerformedAt").Value = Date()
			oEntry.Item("dPerformedAt").Value = getCurrentCustomerLocalDate 'CR1.7
			oEntry.Item("sDate").Value = getCurrentCustomerLocalDate 'CR 1.7
			'wfvariables.recUsersSorted.AddListItem oEntry 'CC-000159
			wfvariables.recUsersSortedLocal.AddListItem oEntry 'CC-000159
			debug2File "addPerformerToUserTable (" & sActivity & ") => Updated recUsersSorted"			
			'exit the sub if found an according activity			
			Exit Sub
		End If
	Next

	'Das kann durchaus sein, wenn nämlich für eine Prüfaktivität nicht ein Performer konfiguriert ist, sondern eine feste Rolle genommen wird
	debug2File "addPerformerToUserTable (" & sActivity & ") => no record found!"
	Dim oRecord : Set oRecord = wfvariables.recUsers.CreateListItem		
	oRecord.Item("sActivity").Value = sActivity
	oRecord.Item("sUserGuid").Value = sCurrentUserGuid
	oRecord.Item("sUserName").Value = sCurrentUserName
	oRecord.Item("sPerformedBy").Value = sCurrentUserName
	oRecord.Item("sDecision").Value = wfvariables.sOptions.Value
	'oRecord.Item("dPerformedAt").Value = Date()
	oRecord.Item("dPerformedAt").Value = getCurrentCustomerLocalDate 'CR1.7
	oRecord.Item("sDate").Value = getCurrentCustomerLocalDate 'CR 1.7
	oRecord.Item("sRoleName").Value = sAuthorRoleName 'CC-000138
	wfvariables.recUsers.AddListItem oRecord 

	
	debug2File "addPerformerToUserTable (" & sActivity & ") => record created!"	

	'Insert into sorted record
	'Set oEntry = wfvariables.recUsersSorted.CreateListItem 'CC-000159
	Set oEntry = wfvariables.recUsersSortedLocal.CreateListItem 'CC-000159
	oEntry.Item("sActivity").Value = sActivity
	oEntry.Item("sUserGuid").Value = sCurrentUserGuid
	oEntry.Item("sUserName").Value = sCurrentUserName
	oEntry.Item("sPerformedBy").Value = sCurrentUserName
	oEntry.Item("sDecision").Value = wfvariables.sOptions.Value
	'oEntry.Item("dPerformedAt").Value = Date()	
	oEntry.Item("dPerformedAt").Value = getCurrentCustomerLocalDate 'CR1.7
	oEntry.Item("sDate").Value = getCurrentCustomerLocalDate 'CR 1.7
	oEntry.Item("sRoleName").Value = sAuthorRoleName 'CC-000138
	'wfvariables.recUsersSorted.AddListItem oEntry	 'CC-000159
	wfvariables.recUsersSortedLocal.AddListItem oEntry	 'CC-000159
	
	debug2File "addPerformerToUserTable (" & sActivity & ") => Updated recUsers..."	
	debug2File "End - addPerformerToUserTable (" & sActivity & ")"	
End Sub