use of org.w3._2007.rif.Do in project nhin-d by DirectProject.
the class DirectXdMailet method service.
/*
* (non-Javadoc)
*
* @see org.apache.mailet.base.GenericMailet#service(org.apache.mailet.Mail)
*/
@Override
public void service(Mail mail) throws MessagingException {
LOGGER.info("Servicing DirectXdMailet");
if (StringUtils.isBlank(endpointUrl)) {
LOGGER.error("DirectXdMailet endpoint URL cannot be empty or null.");
throw new MessagingException("DirectXdMailet endpoint URL cannot be empty or null.");
}
boolean successfulTransaction = false;
final MimeMessage msg = mail.getMessage();
final boolean isReliableAndTimely = TxUtil.isReliableAndTimelyRequested(msg);
final NHINDAddressCollection initialRecipients = getMailRecipients(mail);
final NHINDAddressCollection xdRecipients = new NHINDAddressCollection();
final NHINDAddress sender = getMailSender(mail);
Tx txToTrack = null;
// Get recipients and create a collection of Strings
final List<String> recipAddresses = new ArrayList<String>();
for (NHINDAddress addr : initialRecipients) {
recipAddresses.add(addr.getAddress());
}
// Service XD* addresses
if (getResolver().hasXdEndpoints(recipAddresses)) {
LOGGER.info("Recipients include XD endpoints");
try {
//List<Address> xdAddresses = new ArrayList<Address>();
for (String s : getResolver().getXdEndpoints(recipAddresses)) {
//xdAddresses.add((new MailAddress(s)).toInternetAddress());
xdRecipients.add(new NHINDAddress(s));
}
txToTrack = this.getTxToTrack(msg, sender, xdRecipients);
// Replace recipients with only XD* addresses
//msg.setRecipients(RecipientType.TO, xdAddresses.toArray(new Address[0]));
msg.setRecipients(RecipientType.TO, xdRecipients.toArray(new Address[0]));
// Transform MimeMessage into ProvideAndRegisterDocumentSetRequestType object
ProvideAndRegisterDocumentSetRequestType request = getMimeXDSTransformer().transform(msg);
for (String directTo : recipAddresses) {
String response = getDocumentRepository().forwardRequest(endpointUrl, request, directTo, sender.toString());
if (!isSuccessful(response)) {
LOGGER.error("DirectXdMailet failed to deliver XD message.");
LOGGER.error(response);
} else {
successfulTransaction = true;
if (isReliableAndTimely && txToTrack != null && txToTrack.getMsgType() == TxMessageType.IMF) {
// send MDN dispatch for messages the recipients that were successful
final Collection<NotificationMessage> notifications = notificationProducer.produce(new Message(msg), xdRecipients.toInternetAddressCollection());
if (notifications != null && notifications.size() > 0) {
LOGGER.debug("Sending MDN \"dispathed\" messages");
// create a message for each notification and put it on James "stack"
for (NotificationMessage message : notifications) {
try {
getMailetContext().sendMail(message);
} catch (Throwable t) {
// don't kill the process if this fails
LOGGER.error("Error sending MDN dispatched message.", t);
}
}
}
}
}
}
} catch (Throwable e) {
LOGGER.error("DirectXdMailet delivery failure", e);
}
}
// this basically sets the message back to it's original state with SMTP addresses only
if (getResolver().hasSmtpEndpoints(recipAddresses)) {
LOGGER.info("Recipients include SMTP endpoints");
mail.setRecipients(getSmtpRecips(recipAddresses));
} else {
LOGGER.info("Recipients do not include SMTP endpoints");
// No SMTP addresses, ghost it
mail.setState(Mail.GHOST);
}
if (!successfulTransaction) {
if (txToTrack != null && txToTrack.getMsgType() == TxMessageType.IMF) {
// for good measure, send DSN messages back to the original sender on failure
// create a DSN message
this.sendDSN(txToTrack, xdRecipients, false);
}
}
}
use of org.w3._2007.rif.Do in project hale by halestudio.
the class TestModelRifToRifTranslator method checkDoElements.
private void checkDoElements(Do do1) {
assertNotNull(do1.getActionVar());
assertTrue(do1.getActionVar().size() >= 1);
// count number of news and frames in the collection
int numNews = 0;
int numFrames = 0;
for (ActionVar v : do1.getActionVar()) {
assertNotNull(v);
if (v.getNew() != null) {
numNews++;
checkActionVarTypeNew(v);
}
if (v.getFrame() != null) {
numFrames++;
checkActionVarTypeFrame(v);
}
}
assertThat(numNews, is(equalTo(1)));
int numAsserts = 0;
for (Object action : do1.getActions().getACTION()) {
assertTrue(action instanceof org.w3._2007.rif.Assert);
numAsserts++;
org.w3._2007.rif.Assert a = (org.w3._2007.rif.Assert) action;
assertNotNull(a);
checkAssertTarget(a);
}
// CHECKSTYLE:OFF
assertThat(numAsserts, is(equalTo(3)));
// CHECKSTYLE:ON
}
use of org.w3._2007.rif.Do in project hale by halestudio.
the class ModelRifToRifTranslator method buildSentence.
private Sentence buildSentence(ModelSentence s) {
// sort variables within sentence
final Sentence sentence = factory.createSentence();
final Implies implies = factory.createImplies();
sentence.setImplies(implies);
final If if1 = factory.createIf();
implies.setIf(if1);
final Exists exists = factory.createExists();
if1.setExists(exists);
ThenPart then = factory.createThenPart();
implies.setThen(then);
Do do1 = factory.createDo();
then.setDo(do1);
processChildren(s, exists, do1);
return sentence;
}
use of org.w3._2007.rif.Do in project hale by halestudio.
the class ModelRifToRifTranslator method recurseChildren.
private void recurseChildren(final Exists exists, Do do1, RifVariable variable, ModelSentence sentence, boolean isSource) {
List<RifVariable> children = sentence.findChildren(variable);
if (isSource) {
exists.getDeclare().add(createSourceDeclare(variable));
if (variable.getType().equals(Type.INSTANCE)) {
exists.getFormula().getAnd().getFormula().add(createSourceInstanceMembershipFormula(sentence, variable));
}
} else {
// if (!children.isEmpty()) // problem?
do1.getActionVar().add(createTargetVariableDeclare(variable));
if (variable.getType() == Type.INSTANCE) {
do1.getActions().getACTION().add(createTargetInstanceMembershipFormula(do1.getActions(), sentence, variable));
}
}
if (!children.isEmpty()) {
if (isSource) {
Frame frame = initialiseFrame(variable);
for (RifVariable child : children) {
recurseChildren(exists, do1, child, sentence, isSource);
createBindingSlot(child, frame);
}
Formula frameFormula = factory.createFormula();
frameFormula.setFrame(frame);
exists.getFormula().getAnd().getFormula().add(frameFormula);
} else {
for (RifVariable child : children) {
recurseChildren(exists, do1, child, sentence, isSource);
}
}
}
}
use of org.w3._2007.rif.Do in project hale by halestudio.
the class ModelRifToRifTranslator method processChildren.
private void processChildren(ModelSentence s, final Exists exists, Do do1) {
Formula existsFormula = factory.createFormula();
And and = factory.createAnd();
existsFormula.setAnd(and);
exists.setFormula(existsFormula);
Actions actions = factory.createDoActions();
do1.setActions(actions);
for (RifVariable instanceVariable : s.getVariables(Type.INSTANCE)) {
// source instance variables
if (!instanceVariable.isActionVar()) {
recurseChildren(exists, do1, instanceVariable, s, true);
} else // target instance variables
{
recurseChildren(exists, do1, instanceVariable, s, false);
}
}
Map<RifVariable, Frame> map = new LinkedHashMap<RifVariable, Frame>();
for (PropertyMapping mapping : s.getPropertyMappings()) {
RifVariable contextVariable = mapping.getTarget().getContextVariable();
Frame match = map.get(contextVariable);
if (match == null) {
map.put(contextVariable, initialiseFrame(contextVariable));
}
}
for (StaticAssignment staticAssignment : s.getStaticAssignments()) {
RifVariable contextVariable = staticAssignment.getTarget().getContextVariable();
Frame match = map.get(contextVariable);
if (match == null) {
map.put(contextVariable, initialiseFrame(contextVariable));
}
}
for (PropertyMapping mapping : s.getPropertyMappings()) {
Frame frame = map.get(mapping.getTarget().getContextVariable());
createAssignmentSlot(mapping, frame);
}
for (StaticAssignment staticAssignment : s.getStaticAssignments()) {
Frame frame = map.get(staticAssignment.getTarget().getContextVariable());
createStaticAssignmentSlot(staticAssignment, frame);
}
for (ModelRifMappingCondition mappingCondition : s.getMappingConditions()) {
createFilter(exists.getFormula().getAnd().getFormula(), mappingCondition);
}
for (Frame frame : map.values()) {
Assert assert1 = factory.createAssert();
Target target = factory.createAssertTarget();
target.setFrame(frame);
assert1.setTarget(target);
actions.getACTION().add(assert1);
}
}
Aggregations