use of won.bot.framework.eventbot.event.impl.command.create.CreateAtomCommandFailureEvent in project webofneeds by researchstudio-sat.
the class ExecuteCreateAtomCommandAction method doRun.
@Override
protected void doRun(Event event, EventListener executingListener) throws Exception {
if (!(event instanceof CreateAtomCommandEvent)) {
return;
}
EventListenerContext ctx = getEventListenerContext();
CreateAtomCommandEvent createAtomCommandEvent = (CreateAtomCommandEvent) event;
Dataset atomDataset = createAtomCommandEvent.getAtomDataset();
if (atomDataset == null) {
logger.warn("CreateAtomCommandEvent did not contain an atom model, aborting atom creation");
ctx.getEventBus().publish(new AtomCreationAbortedEvent(null, null, createAtomCommandEvent, "CreateAtomCommandEvent did not contain an atom model, aborting atom creation"));
return;
}
URI atomUriFromProducer = null;
Resource atomResource = WonRdfUtils.AtomUtils.getAtomResource(atomDataset);
if (atomResource.isURIResource()) {
atomUriFromProducer = URI.create(atomResource.getURI());
RdfUtils.replaceBaseURI(atomDataset, atomResource.getURI(), true);
} else {
RdfUtils.replaceBaseResource(atomDataset, atomResource, true);
}
final URI atomUriBeforeCreation = atomUriFromProducer;
AtomModelWrapper atomModelWrapper = new AtomModelWrapper(atomDataset);
final Dataset atomDatasetWithSockets = atomModelWrapper.copyDatasetWithoutSysinfo();
final URI wonNodeUri = ctx.getNodeURISource().getNodeURI();
logger.debug("creating atom on won node {} with content {} ", wonNodeUri, StringUtils.abbreviate(RdfUtils.toString(atomDatasetWithSockets), 150));
WonNodeInformationService wonNodeInformationService = ctx.getWonNodeInformationService();
final URI atomURI = wonNodeInformationService.generateAtomURI(wonNodeUri);
RdfUtils.renameResourceWithPrefix(atomDataset, atomResource.getURI(), atomURI.toString());
WonMessage createAtomMessage = createWonMessage(atomURI, atomDatasetWithSockets);
createAtomMessage = ctx.getWonMessageSender().prepareMessage(createAtomMessage);
// remember the atom URI so we can react to success/failure responses
ctx.getBotContextWrapper().rememberAtomUri(atomURI);
EventListener successCallback = event12 -> {
logger.debug("atom creation successful, new atom URI is {}", atomURI);
ctx.getEventBus().publish(new CreateAtomCommandSuccessEvent(atomURI, atomUriBeforeCreation, createAtomCommandEvent));
};
EventListener failureCallback = event1 -> {
String textMessage = WonRdfUtils.MessageUtils.getTextMessage(((FailureResponseEvent) event1).getFailureMessage());
logger.debug("atom creation failed for atom URI {}, original message URI {}: {}", new Object[] { atomURI, ((FailureResponseEvent) event1).getOriginalMessageURI(), textMessage });
ctx.getEventBus().publish(new CreateAtomCommandFailureEvent(atomURI, atomUriBeforeCreation, createAtomCommandEvent, textMessage));
ctx.getBotContextWrapper().removeAtomUri(atomURI);
};
EventBotActionUtils.makeAndSubscribeResponseListener(createAtomMessage, successCallback, failureCallback, ctx);
logger.debug("registered listeners for response to message URI {}", createAtomMessage.getMessageURI());
ctx.getWonMessageSender().sendMessage(createAtomMessage);
logger.debug("atom creation message sent with message URI {}", createAtomMessage.getMessageURI());
}
Aggregations