After a BizTalk mishap a customer wanted to recreate the xml files for all AIF Messages in a certain timeframe. The following job does exactly this.
static void AIFMessagesRecreate(Args _args)
{
AifMessageLog AifMessageLog;
AifDocumentLog aifDocumentLog;
AifMessage aifMessage;
Dialog dialog = new Dialog("Recreate AIF Messages to file");
int i;
Filename filePath;
DialogField dlgFilePath;
Query q = new Query();
QueryRun qr;
QueryBuildDatasource qbds;
#file
TextIo diskFile;
//copied and enhanced - Source AifMessage::serialize()
AifMessageXml serialize(AifMessage message, AifDocumentXml _AifDocumentXml)
{
#Aif
AifMessageXml messageXml;
XmlTextWriter xmlTextWriter;
XmlTextReader xmlTextReader;
AifDocumentXmlNamespace documentNamespace;
AifXmlEncoding encoding;
;
encoding = AifUtil::updateEncodingAttribute(message.encoding());
documentNamespace = #MessageNamespace;
xmlTextWriter = XmlTextWriter::newXml();
xmlTextWriter.formatting(XmlFormatting::None);
AifUtil::writeXmlDeclaration(xmlTextWriter, encoding);
//Write the Envelope element
xmlTextWriter.writeStartElement2(#MessageEnvelope, documentNamespace);
//Write the Header element
xmlTextWriter.writeStartElement(#MessageHeader);
xmlTextWriter.writeElementString(#MessageId, guid2str(message.messageId()));
// The source endpoint user is not sent on outbound messages for security reasons
if (message.direction() != AifMessageDirection::Outbound)
xmlTextWriter.writeElementString(#MessageSourceUser, message.sourceEndpointUserId());
xmlTextWriter.writeElementString(#MessageSourceEndpoint, message.sourceEndpointId());
xmlTextWriter.writeElementString(#MessageDestEndpoint, message.destinationEndpointId());
xmlTextWriter.writeElementString(#MessageAction, message.externalAction());
if(message.requestMessageId())
xmlTextWriter.writeElementString(#MessageRequestId, guid2str(message.requestMessageId()));
//End the Header
xmlTextWriter.writeEndElement();
//Write the Body element
xmlTextWriter.writeStartElement(#MessageBody);
try
{
xmlTextReader = XmlTextReader::newXml(_AifDocumentXml);
xmlTextReader.whitespaceHandling(XmlWhitespaceHandling::None);
//Move past the declaration
xmlTextReader.moveToContent();
xmlTextWriter.writeRaw(xmlTextReader.readOuterXml());
xmlTextReader.close();
}
catch(Exception::Error)
{
//Unable to serialize the contents of the Xml property.
throw error(strfmt('@SYS89763'));
}
//End the Body
xmlTextWriter.writeEndElement();
//End the Envelope
xmlTextWriter.writeEndElement();
messageXml = xmlTextWriter.writeToString();
xmlTextWriter.close();
return messageXml;
}
;
dlgFilePath = dialog.addFieldValue(typeId(FilePath),'C:\\Temp\\');
if(dialog.run())
{
filePath = dlgFilePath.value();
qbds = q.addDataSource(tablenum(AifMessageLog));
qbds.addRange(fieldnum(AifMessageLog,createdDateTime));
qbds.addRange(fieldnum(AifMessageLog,DestinationEndpointId));
qr = new QueryRun(q);
if(qr.prompt())
{
while (qr.next())
{
if(qr.changed(tablenum(AifMessageLog)))
{
aifMessageLog = qr.get(tablenum(AifMessageLog));
select firstonly aifDocumentLog where aifDocumentLog.MessageId == aifMessageLog.MessageId;
aifMessage = new AIFMessage(AifMessageLog.MessageId,AifMessageLog.SourceEndpointUserId,aifMessageLog.SubmittingUserId);
aifMessage.destinationEndpointId(AifMessageLog.DestinationEndpointId);
aifMessage.sourceEndpointId(AifMessageLog.SourceEndpointId);
diskFile = new TextIo(filePath + guid2str(AifMessageLog.MessageId) + ".xml", #io_Write, 65001); //UTF-8
diskFile.write(serialize(aifMessage,aifDocumentLog.DocumentXml));
diskFile = null;
i++;
}
}
}
}
info(strfmt("%1 files saved",i));
}

Leave a Reply