* Generic-Response-Reply document or a 'response-reply' document specific to the message object/action
* being processed (e.g. InstitutionalIdentity-Response-Reply).
*/
public final String buildReplyDocument(Element senderControlArea, Document replyDoc) {
XMLOutputter xmlOut = new XMLOutputter();
Document localReplyDoc = (Document)replyDoc.clone();
String msgAction = senderControlArea.getAttribute(MESSAGE_ACTION).getValue();
Element replyControlArea = getControlArea(localReplyDoc.getRootElement());
localReplyDoc.getRootElement().removeContent(replyControlArea);
// A DataArea will only exist for some reply documents,
// later when we rebuild the reply document, we'll check to see
// if this element is null before we try to add it back into
// the document. We need to do this so when we re-build the document
// we can add the ControlArea and DataAreas in the proper order...
Element replyDataArea = localReplyDoc.getRootElement().getChild(DATA_AREA);
if (replyDataArea != null) {
localReplyDoc.getRootElement().removeChild(DATA_AREA);
}
replyControlArea.removeChild("Result");
replyControlArea.removeChild("Datetime");
replyControlArea.removeChild("Sender");
Result aResult = new Result();
ProcessedMessageId processedMsgId = new ProcessedMessageId();
Element eRequestSender = senderControlArea.getChild("Sender");
processedMsgId.setProducerId(eRequestSender.getChild("MessageId").
getChild("ProducerId").getText());
processedMsgId.setSenderAppId(eRequestSender.getChild("MessageId").
getChild("SenderAppId").getText());
processedMsgId.setMessageSeq(eRequestSender.getChild("MessageId").
getChild("MessageSeq").getText());
aResult.setAction(msgAction);
aResult.setStatus("success");
aResult.setProcessedMessageId(processedMsgId);
// Set the sender element (the replier)
Sender sender = new Sender();
MessageId replierMsgId = new MessageId();
replierMsgId.setProducerId(eRequestSender.getChild("MessageId").
getChild("ProducerId").getText());
replierMsgId.setSenderAppId(getAppName());
replierMsgId.setMessageSeq(eRequestSender.getChild("MessageId").
getChild("MessageSeq").getText());
sender.setMessageId(replierMsgId);
Authentication auth = new Authentication();
auth.setAuthUserId(getAppName());
auth.setAuthUserSignature(getAppName());
sender.setAuthentication(auth);
Element eSender = null;
try {
XmlLayout xmlLayout = (XmlLayout)sender.getOutputLayoutManager("xml");
sender.setOutputLayoutManager(xmlLayout);
eSender = (Element)sender.buildOutputFromObject();
}
catch (Exception e) {
logger.fatal("[buildReplyDocument] Exception occurred building an Element from the Sender object. Exception: " + e.getMessage());
return xmlOut.outputString(replyDoc);
}
// Set the datetime element (for the replier)
Datetime dt = new Datetime();
Element eDatetime = null;
try {
eDatetime = (Element)dt.buildOutputFromObject();
}
catch (Exception e) {
logger.fatal("[buildReplyDocument] Exception occurred building an Element from the Datetime object. Exception: " + e.getMessage());
return xmlOut.outputString(replyDoc);
}
try {
replyControlArea.addContent(eSender);
replyControlArea.addContent(eDatetime);
replyControlArea.addContent((Element)aResult.buildOutputFromObject());
localReplyDoc.getRootElement().addContent(replyControlArea);
if (replyDataArea != null) {
localReplyDoc.getRootElement().addContent(replyDataArea);
}
}
catch (Exception e) {
logger.fatal("Error building reply document, returning Reply Document passed in");
logger.fatal(e.getMessage(), e);
return xmlOut.outputString(replyDoc);
}
return xmlOut.outputString(localReplyDoc);
}