<?xml version="1.0" encoding="utf-8"?>
<ebics-users>
<% __ids__.each({ u = com.axelor.inject.Beans.get('com.axelor.apps.bankpayment.db.repo.EbicsUserRepository' as Class).find(it)

   allEbicsRequestLog = com.axelor.inject.Beans.get('com.axelor.apps.bankpayment.db.repo.EbicsRequestLogRepository' as Class).all()

	bankCerts = "" 
	u.ebicsPartner?.ebicsBank?.ebicsCertificateList.each( {c->
	    bankCerts += "<ebicsCertificate>"
		bankCerts += "<typeSelect>${c.typeSelect}</typeSelect>"
		bankCerts += "<pem>${c.pemString}</pem>"
		bankCerts += "</ebicsCertificate>"
	})
	
	boEbicsPartnerService = ""
	u.ebicsPartner?.boEbicsPartnerServiceList.each( {bo->
	    boEbicsPartnerService += "<boEbicsPartnerService>"
		boEbicsPartnerService += "  <importId>${bo.importId}</importId>"
		boEbicsPartnerService += "  <bankOrderFileFormat>"
		boEbicsPartnerService += "     <name>${bo.bankOrderFileFormat?.name}</name>"
		boEbicsPartnerService += "     <orderTypeSelect>${bo.bankOrderFileFormat?.orderTypeSelect}</orderTypeSelect>"
		boEbicsPartnerService += "     <orderFileFormatSelect>${bo.bankOrderFileFormat?.orderFileFormatSelect}</orderFileFormatSelect>"
		boEbicsPartnerService += "     <description>${bo.bankOrderFileFormat?.description}</description>"
		boEbicsPartnerService += "  </bankOrderFileFormat>"
		boEbicsPartnerService += "  <ebicsCodification>${bo.ebicsCodification}</ebicsCodification>"
		boEbicsPartnerService += "</boEbicsPartnerService>"
	})

	bsEbicsPartnerService = ""
	u.ebicsPartner?.bsEbicsPartnerServiceList.each( {bs->
	    bsEbicsPartnerService += "<bsEbicsPartnerService>"
		bsEbicsPartnerService += "  <importId>${bs.importId}</importId>"
		bsEbicsPartnerService += "  <bankStatementFileFormat>"
		bsEbicsPartnerService += "     <name>${bs.bankStatementFileFormat?.name}</name>"
		bsEbicsPartnerService += "     <statementFileFormatSelect>${bs.bankStatementFileFormat?.statementFileFormatSelect}</statementFileFormatSelect>"
		bsEbicsPartnerService += "     <description>${bs.bankStatementFileFormat?.description}</description>"
		bsEbicsPartnerService += "  </bankStatementFileFormat>"
		bsEbicsPartnerService += "  <ebicsCodification>${bs.ebicsCodification}</ebicsCodification>"
		bsEbicsPartnerService += "</bsEbicsPartnerService>"
	})
	
	receiverBankDetails = ""
	u.ebicsPartner?.receiverBankDetailsSet.each( {b->
	    receiverBankDetails += "<bankDetails>"
		receiverBankDetails += "<importId>${b.importId}</importId>"
		receiverBankDetails += "<partnerFullName>${b.fullName}</partnerFullName>"
		receiverBankDetails += "<ownerName>${b.ownerName}</ownerName>"
		receiverBankDetails += "<bank>${b.bank?.code}</bank>"
		receiverBankDetails += "<bankCode>${b.bankCode}</bankCode>"
		receiverBankDetails += "<iban>${b.iban}</iban>"
		receiverBankDetails += "<sortCode>${b.sortCode}</sortCode>"
		receiverBankDetails += "<accountNbr>${b.accountNbr}</accountNbr>"
		receiverBankDetails += "<bbanKey>${b.bbanKey}</bbanKey>"
		receiverBankDetails += "<ibanBic>${b.ibanBic}</ibanBic>"
		receiverBankDetails += "<fullName>${b.fullName}</fullName>"
		receiverBankDetails += "<active>${b.active}</active>"
		receiverBankDetails += "<bankAddress>"
		receiverBankDetails += "<code>${b.bankAddress?.code}</code>"
		receiverBankDetails += "<address>${b.bankAddress?.address}</address>"
		receiverBankDetails += "<label>${b.bankAddress?.label}</label>"
		receiverBankDetails += "</bankAddress>"
		receiverBankDetails += "</bankDetails>"
	})
	bankDetails = ""
	u.ebicsPartner?.bankDetailsSet.each( {b->
	    bankDetails += "<bankDetails>"
		bankDetails += "<importId>${b.importId}</importId>"
		bankDetails += "<partnerFullName>${b.fullName}</partnerFullName>"
		bankDetails += "<ownerName>${b.ownerName}</ownerName>"
		bankDetails += "<bank>${b.bank?.code}</bank>"
		bankDetails += "<bankCode>${b.bankCode}</bankCode>"
		bankDetails += "<iban>${b.iban}</iban>"
		bankDetails += "<sortCode>${b.sortCode}</sortCode>"
		bankDetails += "<accountNbr>${b.accountNbr}</accountNbr>"
		bankDetails += "<bbanKey>${b.bbanKey}</bbanKey>"
		bankDetails += "<ibanBic>${b.ibanBic}</ibanBic>"
		bankDetails += "<fullName>${b.fullName}</fullName>"
		bankDetails += "<active>${b.active}</active>"
		bankDetails += "<bankAddress>"
		bankDetails += "<code>${b.bankAddress?.code}</code>"
		bankDetails += "<address>${b.bankAddress?.address}</address>"
		bankDetails += "<label>${b.bankAddress?.label}</label>"
		bankDetails += "</bankAddress>"
		bankDetails += "</bankDetails>"
	})
	

	allEbicsRequestLog = allEbicsRequestLog.filter('self.ebicsUser.id in ?1',it)

	ebicsRequestLog = ""
	allEbicsRequestLog.fetch().each( { log ->
		ebicsRequestLog +="<ebicsRequestLog>"
		ebicsRequestLog +="<ebicsUser>${log.ebicsUser?.userId}</ebicsUser>"
		ebicsRequestLog +="<requestType>${log.requestType}</requestType>"
		ebicsRequestLog +="<requestTime>${log.requestTime}</requestTime>"
		ebicsRequestLog +="<responseCode>${log.responseCode}</responseCode>"
		ebicsRequestLog +="<requestTraceText>${log.requestTraceText}</requestTraceText>"
		ebicsRequestLog +="<responseTraceText>${log.responseTraceText}</responseTraceText>"
		ebicsRequestLog +="</ebicsRequestLog>"
		 })
	out << 
	"""
	<ebics-user>
		<name>${u.name}</name>
		<userId>${u.userId}</userId>
		<password>${u.password}</password>
		<userTypeSelect>${u.userTypeSelect}</userTypeSelect>
		<statusSelect>${u.statusSelect}</statusSelect>
		<ebicsPartner>
			<partnerId>${u.ebicsPartner?.partnerId}</partnerId>
			<ebicsTypeSelect>${u.ebicsPartner?.ebicsTypeSelect}</ebicsTypeSelect>
			<testMode>${u.ebicsPartner?.testMode}</testMode>
			<boEbicsPartnerServiceList>
				$boEbicsPartnerService
			</boEbicsPartnerServiceList>
			<bsEbicsPartnerServiceList>
				$bsEbicsPartnerService
			</bsEbicsPartnerServiceList>
			<pSRBankStatementFileFormat>
				<importId>${u.ebicsPartner?.pSRBankStatementFileFormat?.importId}</importId>
				<statementFileFormatSelect>${u.ebicsPartner?.pSRBankStatementFileFormat?.statementFileFormatSelect}</statementFileFormatSelect>
				<name>${u.ebicsPartner?.pSRBankStatementFileFormat?.name}</name>
				<description>${u.ebicsPartner?.pSRBankStatementFileFormat?.description}</description>
			</pSRBankStatementFileFormat>
			<transportEbicsUserId>${u.ebicsPartner?.transportEbicsUser?.userId}</transportEbicsUserId>
			<bankStatementGetModeSelect>${u.ebicsPartner?.bankStatementGetModeSelect}</bankStatementGetModeSelect>
			<bankStatementLastExeDateT>${u.ebicsPartner?.bankStatementLastExeDateT}</bankStatementLastExeDateT>
			<filterReceiverBD>${u.ebicsPartner?.filterReceiverBD}</filterReceiverBD>
			<receiverBankDetailsSet>
				$receiverBankDetails
			</receiverBankDetailsSet>
			<usePSR>${u.ebicsPartner?.usePSR}</usePSR>
			<bankDetailsSet>
				$bankDetails
			</bankDetailsSet>
			<ebicsBank>
				<fullName>${u.ebicsPartner?.ebicsBank?.fullName}</fullName>
				<hostId>${u.ebicsPartner?.ebicsBank?.hostId}</hostId>
				<name>${u.ebicsPartner?.ebicsBank?.name}</name>
				<url>${u.ebicsPartner?.ebicsBank?.url}</url>
				<fax>${u.ebicsPartner?.ebicsBank?.fax}</fax>
				<emailAddress>${u.ebicsPartner?.ebicsBank?.emailAddress}</emailAddress>
				<language>${u.ebicsPartner?.ebicsBank?.language?.code}</language>
				<protocolSelect>${u.ebicsPartner?.ebicsBank?.protocolSelect}</protocolSelect>
				<certValidityPeriodSelect>${u.ebicsPartner?.ebicsBank?.certValidityPeriodSelect}</certValidityPeriodSelect>
				<useX509ExtensionBasicConstraints>${u.ebicsPartner?.ebicsBank?.useX509ExtensionBasicConstraints}</useX509ExtensionBasicConstraints>
				<useX509ExtensionSubjectKeyIdentifier>${u.ebicsPartner?.ebicsBank?.useX509ExtensionSubjectKeyIdentifier}</useX509ExtensionSubjectKeyIdentifier>
				<useX509ExtensionAuthorityKeyIdentifier>${u.ebicsPartner?.ebicsBank?.useX509ExtensionAuthorityKeyIdentifier}</useX509ExtensionAuthorityKeyIdentifier>
				<useX509ExtensionExtendedKeyUsage>${u.ebicsPartner?.ebicsBank?.useX509ExtensionExtendedKeyUsage}</useX509ExtensionExtendedKeyUsage>
				<bank>
					<bankName>${u.ebicsPartner?.ebicsBank?.bank.bankName}</bankName>
					<code>${u.ebicsPartner?.ebicsBank?.bank.code}</code>
					<bankDetailsTypeSelect>${u.ebicsPartner?.ebicsBank?.bank.bankDetailsTypeSelect}</bankDetailsTypeSelect>
				</bank>
				<ebicsCertificateList>
				 $bankCerts
				</ebicsCertificateList>
			</ebicsBank>
		</ebicsPartner>
		<securityMedium>${u.securityMedium}</securityMedium>
		<nextOrderId>${u.nextOrderId}</nextOrderId>
		<associatedUserCode>${u.associatedUser?.code}</associatedUserCode>
		<dn>${u.dn}</dn>
		<a005Certificate>
			<pem>${u.a005Certificate?.pemString}</pem>
			<privateKey>${org.apache.commons.codec.binary.Base64.encodeBase64String(u.a005Certificate?.privateKey)}</privateKey>
		</a005Certificate>
		<e002Certificate>
			<pem>${u.e002Certificate?.pemString}</pem>
			<privateKey>${org.apache.commons.codec.binary.Base64.encodeBase64String(u.e002Certificate?.privateKey)}</privateKey>
		</e002Certificate>
		<x002Certificate>
			<pem>${u.x002Certificate?.pemString}</pem>
			<privateKey>${org.apache.commons.codec.binary.Base64.encodeBase64String(u.x002Certificate?.privateKey)}</privateKey>
		</x002Certificate>
	</ebics-user>

	<ebicsRequestLogList>
		 $ebicsRequestLog
	</ebicsRequestLogList>
	"""
}) %>

</ebics-users>

