'**********************************************************
' createNewDocumentStructure
' @description: creates a new structure for a new document
' @param: sDocumentFilePath
' @param: bDeleteDocument
' @param: bSync - true for datasynchronization, otherwise false
' @param: bShowExchangeTemplateButton
' @return: 
'**********************************************************
Function createNewDocumentStructure (sDocumentFilePath, bDeleteDocument, bSync, bShowExchangeTemplateButton)

	'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 Function
	end if		

	'to improve performance the session will be created initially and transfered to following functions
	'Set oServer = CreateObject("OxSvrSpt.Server") 
	'create a session with an encrypted password
	'Set oSession = oServer.Login(wfvariables.recSession.sTechUser.Value, wfvariables.recSession.sTechPwd.Value, drtSession.ServerName, drtSession.ServerPort, 1)

	'CC-000567 moved the following If-Block before the ProgressDlg
	If wfvariables.recStructure.iDocumentID.Value > 0 Then 'a document was already created
		sRet = MsgBox (getMessageText( "StandardWF", "ReplaceCurrentDocumentQuestion", "Möchten Sie das aktuelle Dokument ersetzen?"), vbYesNo, "enaio" )
		If sRet = vbYes Then
			'delete existing structure
			DeleteFolder wfvariables.recStructure.iFolderID.Value
			DeleteRegister wfvariables.recStructure.iRegisterID.Value
			DeleteDocument wfvariables.recStructure.iDocumentID.Value
		Else
			Exit Function		
		End If	
	End If

	Dim oxvbbas
	Set oxvbbas = createobject("oxvbbas.functions") 
	oxvbbas.ProgressDlgOSAS 0,,,"Erstelle Ablagestruktur / Creating repository..."

	Dim oSession
	Set oSession = createSession
	
	oxvbbas.ProgressDlgOSAS 10
	
	'get the next document number
	wfvariables.sDocumentNumber.Value = getNextDocumentNumber
	wfvariables.sVersion.Value = "1"	
	wfvariables.sTitle2.Value = wfvariables.sTitle.Value 'we got two title fields, we use the title also for the header of the mask
	'wfvariables.dCreatorDate.Value = Date	'set the creation date
	wfvariables.dCreatorDate.Value = getCurrentCustomerLocalDate 'CR1.7	'set the creation date
	wfvariables.sCreatorName.Value = wfclient.UserName	'set the author

	'get the complete (firstname + lastname) username
	For z = 0 To wforganisation.GetOrgObjectById(wfclient.OrgUserID).Attributes.Count - 1
		If wforganisation.GetOrgObjectById(wfclient.OrgUserID).Attributes.Item(z).Name = "Vorname" Then
			sFirstName = wforganisation.GetOrgObjectById(wfclient.OrgUserID).Attributes.Item(z).Value
		End If
		If wforganisation.GetOrgObjectById(wfclient.OrgUserID).Attributes.Item(z).Name = "Nachname" Then
			sLastName = wforganisation.GetOrgObjectById(wfclient.OrgUserID).Attributes.Item(z).Value
		End If		
	Next
	Dim sCurrentUserName : sCurrentUserName = sFirstName & " " & sLastName
	If Trim(sCurrentUserName) <> "" Then 'use the fullname if existing
		wfvariables.sCreatorName.Value = sCurrentUserName
	End If	
	
	oxvbbas.ProgressDlgOSAS 20
	
	sFolder = CreateFolder (oSession) 'create a new folder
	sFolder = split(sFolder,",") 'the result is the objectId and the objectType in a comma separated string
	wfvariables.recStructure.iFolderID.Value = sFolder(0)
	wfvariables.recStructure.iFolderType.Value = sFolder(1)

	oxvbbas.ProgressDlgOSAS 30
	
	sRegister = CreateRegister (oSession, wfvariables.recStructure.iFolderID.Value) 'creates a new register under the folder
	sRegister = split(sRegister,",") 'the result is the objectId and the objectType in a comma separated string
	wfvariables.recStructure.iRegisterID.Value = sRegister(0)
	wfvariables.recStructure.iRegisterType.Value = sRegister(1)
	
	oxvbbas.ProgressDlgOSAS 40	
	
	sProtocolRegister = CreateProtocolRegister (oSession, wfvariables.recStructure.iFolderID.Value, wfvariables.recStructure.iRegisterID.Value) 'creates a new protocol register
	sProtocolRegister = split(sProtocolRegister,",") 'the result is the objectId and the objectType in a comma separated string
	wfvariables.recStructure.iProtocolRegisterID.Value = sProtocolRegister(0)
	wfvariables.recStructure.iProtocolRegisterType.Value = sProtocolRegister(1)	
	
	oxvbbas.ProgressDlgOSAS 50	
	
	sDocument = CreateDocument (oSession, wfvariables.recStructure.iFolderID.Value, wfvariables.recStructure.iRegisterID.Value, sDocumentFilePath, bDeleteDocument) 'creates a new document
	sDocument = split(sDocument,",") 'the result is the objectId and the objectType in a comma separated string	
	wfvariables.recStructure.iDocumentID.Value = sDocument(0)
	wfvariables.recStructure.iDocumentType.Value = sDocument(1)

	oxvbbas.ProgressDlgOSAS 60	

	wfvariables.sStatus.Value = 0 'draft

	If bSync = True Then
		'datatransfer - update the document
		Dim sSynchronizedDocumentPath
		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
	End If	
	
	oxvbbas.ProgressDlgOSAS 90
	
	'add the new document to the workflow file	
	WFFile.AddFileObject wfvariables.recStructure.iDocumentID.Value, wfvariables.recStructure.iDocumentType.Value, True, 1
	For i=0 To wffile.FileObjects.Count - 1
		If wffile.FileObjects.Item(i).ID = objectId Then
			wffile.FileObjects.Item(i).Moveable = False
			wffile.FileObjects.Item(i).Deleteable = False
			Exit For
		End If
	Next

	'deactivate template field
	if isMaskFieldEnabled (wfvariables.sTemplate.Name)  = True Then 'templates are only available if the template field is visible in the configuration file
		changeFieldByVariable wfvariables.sTemplate.Name,0,1
	end if
	'deactivate fields
	'changeFieldByVariable wfvariables.sTitle.Name,0,1 'title		'CC-000137
	changeFieldByVariable wfvariables.sLanguage.Name,0,1 'language
	changeFieldByVariable wfvariables.sType.Name,0,1 'document type

	'activate the button 'Datenblatt öffnen'
	wfmaskfields.Item(getMessageText( "StandardWF", "btOpenDataSheet", "Datenblatt öffnen")).Visible = True	
	wfmaskfields.Item(getMessageText( "StandardWF", "btOpenDataSheet", "Datenblatt öffnen")).Enabled = True

	Dim pError		'save the wf variables to avoid to close the wf-step without saving
	workitem.Save pError
	
	'clean up
	Set oSession = Nothing	
	Set oServer = Nothing
	
	oxvbbas.ProgressDlgOSAS 100
	
	Set oxvbbas = Nothing		
	dtrMapToArchive wfvariables.recStructure.iDocumentID.Value	'CC-000370
	
	'Open the document with writeaccess
	OpenObjectID wfvariables.recStructure.iDocumentID.Value, wfvariables.recStructure.iDocumentType.Value, 1
	
	'now activate the Button 'Change template
	If bShowExchangeTemplateButton = True Then
		changeButtonByVariable getMessageText( "StandardWF", "btSwapTemplate", "Vorlage austauschen"), 1, 1
	End If
End Function