'**********************************************************
' OpenDocument_MapToArchive
' @description: button function for opening a document, derived by OpenDocument_Default, but with an mapToArchive function included (added with CC-000370)
' @param: objectID (Integer) - the object for which the workflow variables should be archived
' @return nothing
'**********************************************************
Sub OpenDocument_MapToArchive()

	'CC-000784
	If (wfvariables.sDocumentNumber.value = "") And (wfvariables.recStructure.iDocumentID.Value <> 0) Then
		sMsg = getMessageText ( "StandardWF", "HintNoMapToArchiveResetFunction", "Sie haben vermutlich Zurücksetzen im Kontextmenü geklickt. Daher werden keine Daten auf die Indexmaske übertragen.") 
		MsgBox sMsg, vbInformation, "enaio"
		OpenObjectID wfvariables.recStructure.iDocumentID.Value, wfvariables.recStructure.iDocumentType.Value, 1
		Exit Sub
	End If

	'added field length validation for CC-000644
	for a = 0 to wfvariables.Count - 1
		for b = 0 to wfvariables.recConfigWFFields.Count - 1
			if wfvariables.Item(a).Name = wfvariables.recConfigWFFields.Item(b).Item("sVariableName").Value then
				debug2file wfvariables.Item(a).Name & ": " & wfvariables.recConfigWFFields.Item(b).Item("iMaxLength").Value
				if CInt(wfvariables.recConfigWFFields.Item(b).Item("iMaxLength").Value) <> 0 Then
					If Len(wfvariables.Item(a).Value) > CInt(wfvariables.recConfigWFFields.Item(b).Item("iMaxLength").Value) Then
						sMsg = sMsg & getMessageText( "StandardWF", "FieldLengthStart", "Das Feld '") & getMaskFieldNameFromVariable (wfvariables.Item(a).Name) & getMessageText( "StandardWF", "FieldLengthEnd", "' überschreitet die maximale Feldlänge.") & " (" & wfvariables.recConfigWFFields.Item(b).Item("iMaxLength").Value & ")." & vbCrLf
					End If
				End If
			end if	
		Next
	Next
	if sMsg <> "" then 'show error message and don't forward the activity
		msgbox sMsg, vbInformation, "enaio"
		Exit Sub
	end if		

'the flag "bExchangedTemplate" is set to 1 by the button-click event "Exchange template"
	if (wfvariables.recStructure.iDocumentID.Value <> 0) AND (wfvariables.bExchangedTemplate.Value = 0) then 'default value is 0, if we have a document the value must be higher

		'Open the document with writeaccess
		dtrMapToArchive wfvariables.recStructure.iDocumentID.Value

		if wfvariables.recCommon.DocumentDataTransferSolution.value = 1 Then	'word sync
			debug2file "Usage of ENAIO WORD SYNC"
			syncWithWordSync(wfvariables.recStructure.iDocumentID.Value)
		End If

		OpenObjectID wfvariables.recStructure.iDocumentID.Value, wfvariables.recStructure.iDocumentType.Value, 1
	else
		'this flag is set to 1 by the button-click event "Exchange template"
		if wfvariables.bExchangedTemplate.Value = 1 Then
			'check for templates

			if wfvariables.sTemplate.Value = "" then
				sMsg = sMsg & getMessageText ("StandardWF", "ObligatoryFieldStart", "Das Feld '") & getMaskFieldNameFromVariable (wfvariables.sTemplate.Name) & getMessageText ("StandardWF", "ObligatoryFieldEnd", "' ist ein Pflichtfeld.") & vbCrlf 'TODO: dynamic
				msgbox sMsg, vbInformation, "Information"
				'CC-000133 START
				wfvariables.bExchangedTemplate.Value = 0
				changeButtonByVariable getMessageText ("StandardWF", "btReset", "Zurücksetzen"), 0, 1 'deactivate the button 'Zurücksetzen' to change template, language or type values 
				
				dtrMapToArchive wfvariables.recStructure.iDocumentID.Value
				OpenObjectID wfvariables.recStructure.iDocumentID.Value, wfvariables.recStructure.iDocumentType.Value, 1
				'CC-000133 END
				Exit sub
			End If
			'CC-000133 START
			If wfvariables.sTemplate.Value = "n/a" OR wfvariables.recStructure.iTemplatesDocumentID.Value < 1 then 'CC-000567
				sMsg = sMsg & getMessageText ("StandardWF", "TemplateExchangeNotCompleted","Die Vorlage wurde nicht zurückgesetzt bzw. ein neues Dokument importiert. Der Vorlagentausch wird daher abgebrochen.")
				msgbox sMsg, vbInformation, "Information"
				wfvariables.bExchangedTemplate.Value = 0
				changeButtonByVariable getMessageText ("StandardWF", "btReset", "Zurücksetzen"), 0, 1 'deactivate the button 'Zurücksetzen' to change template, language or type values 
				
				dtrMapToArchive wfvariables.recStructure.iDocumentID.Value
				OpenObjectID wfvariables.recStructure.iDocumentID.Value, wfvariables.recStructure.iDocumentType.Value, 1
				Exit sub
			End If
			'CC-000133 END
						
			sDocumentFilePath = storeObjectInCacheByID  (wfvariables.recStructure.iTemplatesDocumentID.Value, 0)
			'replaces the document
			Set oSession = createSession()
			'CC-000251 - AB;28.05.2019 - START
			'ReplaceDocument oSession, wfvariables.recStructure.iDocumentID.Value, wfvariables.recStructure.sDocumentInternalName.Value, sDocumentFilePath
			ReplaceAndUpdateDocument oSession, wfvariables.recStructure.iDocumentID.Value, wfvariables.recStructure.sDocumentInternalName.Value, sDocumentFilePath, true
			'CC-000251 - AB;28.05.2019 - END
			'synchronize the document
			sSynchronizedDocumentPath = SynchronizeData (oSession, wfvariables.recStructure.iDocumentID.Value, wfvariables.recStructure.iDocumentType.Value )
			'replace the document with the synchronized data document
			If sSynchronizedDocumentPath <> "" Then
				ReplaceDocument oSession, wfvariables.recStructure.iDocumentID.Value, wfvariables.recStructure.sDocumentInternalName.Value, sSynchronizedDocumentPath
			End If
			
			'open the new document
			dtrMapToArchive wfvariables.recStructure.iDocumentID.Value
			OpenObjectID wfvariables.recStructure.iDocumentID.Value, wfvariables.recStructure.iDocumentType.Value, 1
			
			'reset the flag to 0
			wfvariables.bExchangedTemplate.Value = 0			
			
			changeButtonByVariable getMessageText ("StandardWF", "btReset", "Zurücksetzen"), 0, 1 'deactivate the button 'Zurücksetzen' to change template, language or type values 
			Set oSession = Nothing
		Else 			
			'validate user input
'			msgbox "Validate User Input"
			Dim sMsg
	
			if wfvariables.sTemplate.Value = "" then
				sMsg = sMsg & getMessageText ("StandardWF", "ObligatoryFieldStart", "Das Feld '") & getMaskFieldNameFromVariable (wfvariables.sTemplate.Name) & getMessageText ("StandardWF", "ObligatoryFieldEnd", "' ist ein Pflichtfeld.") & vbCrlf 'TODO: dynamic
			End if	
	
			if wfvariables.sTitle.Value = "" then
				sMsg = sMsg & getMessageText ("StandardWF", "ObligatoryFieldStart", "Das Feld '") & getMaskFieldNameFromVariable (wfvariables.sTitle.Name) & getMessageText ("StandardWF", "ObligatoryFieldEnd", "' ist ein Pflichtfeld.") & vbCrlf 'TODO: dynamic
			End if
			
			If sMsg <> "" Then 'show error message and don't create a new document
				msgbox sMsg, vbInformation, "Information"
			Else 'user input is valid, so create the document
				'msgbox "Create Document"
				'check for templates
				sDocumentFilePath = storeObjectInCacheByID  (wfvariables.recStructure.iTemplatesDocumentID.Value, 0)
				'BPLGD-169 - Start
				If IsWriteAccessible(sDocumentFilePath) = False Then
					MsgBox getMessageText( "V1-6", "DocumentInUse", "Das Dokument kann nicht verwendet werden. Bitte prüfen Sie, ob das Dokument aktuell geöffnet ist."), vbInformation + vbOKOnly, "enaio"
					Exit Sub
				End If
				'BPLGD-169 - End
				'CC-000108 - START
				Dim bValidationOK : bValidationOK = True
				bValidationOK = validateListEntriesBeforeDocumentCreation()
				If (bValidationOK = false) Then
					Exit Sub
				End If
				'CC-000108 - END				
				createNewDocumentStructure sDocumentFilePath, 1, true, true 'create the new document, and delete the document source
				wfmaskfields.Item(getMessageText ("StandardWF", "btImportDocument", "Dokument importieren")).Enabled = False 'disable the document import button now
				changeButtonByVariable getMessageText ("StandardWF", "btReset", "Zurücksetzen"), 0, 1 'deactivate the button 'Zurücksetzen' to change template, language or type values

			End If	
		End If
	End If
End Sub