use of won.bot.framework.eventbot.event.impl.command.replace.ReplaceCommandFailureEvent in project webofneeds by researchstudio-sat.
the class ExecuteReplaceCommandAction method doRun.
@Override
protected void doRun(Event event, EventListener executingListener) throws Exception {
if (!(event instanceof ReplaceCommandEvent)) {
return;
}
ReplaceCommandEvent replaceCommandEvent = (ReplaceCommandEvent) event;
Dataset atomDataset = replaceCommandEvent.getAtomDataset();
if (atomDataset == null) {
logger.warn("ReplaceCommandEvent did not contain an atom model, aborting atom creation");
getEventListenerContext().getEventBus().publish(new ReplaceAbortedEvent(null, replaceCommandEvent, "CreateAtomCommandEvent did not contain an atom model, aborting atom creation"));
return;
}
Resource atomResource = WonRdfUtils.AtomUtils.getAtomResource(atomDataset);
if (!atomResource.isURIResource()) {
throw new IllegalArgumentException("atom resource in dataset is not an URI");
}
URI atomURI = URI.create(atomResource.getURI());
RdfUtils.replaceBaseURI(atomDataset, atomResource.getURI(), true);
RdfUtils.replaceBaseResource(atomDataset, atomResource, true);
// AtomModelWrapper atomModelWrapper = new AtomModelWrapper(atomDataset);
// URI wonNodeURI =
// getEventListenerContext().getWonNodeInformationService().getWonNodeUri(atomURI);
final URI wonNodeUri = getEventListenerContext().getWonNodeInformationService().getWonNodeUri(atomURI);
logger.debug("replacing atom content on won node {} with content {} ", wonNodeUri, StringUtils.abbreviate(RdfUtils.toString(atomDataset), 150));
RdfUtils.renameResourceWithPrefix(atomDataset, atomResource.getURI(), atomURI.toString());
WonMessage replaceMessage = createWonMessage(atomURI, wonNodeUri, atomDataset);
replaceMessage = getEventListenerContext().getWonMessageSender().prepareMessage(replaceMessage);
EventListener successCallback = event12 -> {
logger.debug("atom content replacement successful for atom URI {}", atomURI);
getEventListenerContext().getEventBus().publish(new ReplaceCommandSuccessEvent(atomURI, replaceCommandEvent));
};
EventListener failureCallback = event1 -> {
String textMessage = WonRdfUtils.MessageUtils.getTextMessage(((FailureResponseEvent) event1).getFailureMessage());
logger.debug("atom content replacement failed for atom URI {}, original message URI {}: {}", new Object[] { atomURI, ((FailureResponseEvent) event1).getOriginalMessageURI(), textMessage });
getEventListenerContext().getEventBus().publish(new ReplaceCommandFailureEvent(atomURI, replaceCommandEvent, textMessage));
};
EventBotActionUtils.makeAndSubscribeResponseListener(replaceMessage, successCallback, failureCallback, getEventListenerContext());
logger.debug("registered listeners for response to message URI {}", replaceMessage.getMessageURI());
getEventListenerContext().getWonMessageSender().sendMessage(replaceMessage);
logger.debug("atom content replacement message sent with message URI {}", replaceMessage.getMessageURI());
}
Aggregations