use of won.bot.framework.eventbot.event.AtomCreationFailedEvent in project webofneeds by researchstudio-sat.
the class CreateAtomAction method doRun.
@Override
protected void doRun(Event event, EventListener executingListener) throws Exception {
EventListenerContext ctx = getEventListenerContext();
BotContextWrapper botContextWrapper = ctx.getBotContextWrapper();
if (ctx.getAtomProducer().isExhausted()) {
logger.info("the bot's atom producer is exhausted.");
ctx.getEventBus().publish(new AtomProducerExhaustedEvent());
return;
}
final Dataset atomDataset = ctx.getAtomProducer().create();
if (atomDataset == null) {
logger.warn("atomproducer failed to produce 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(Prefixer.setPrefixes(atomDatasetWithSockets)), 150));
WonNodeInformationService wonNodeInformationService = ctx.getWonNodeInformationService();
final URI atomURI = wonNodeInformationService.generateAtomURI(wonNodeUri);
WonMessage createAtomMessage = createWonMessage(atomURI, atomDatasetWithSockets);
createAtomMessage = ctx.getWonMessageSender().prepareMessage(createAtomMessage);
// remember the atom URI so we can react to success/failure responses
botContextWrapper.rememberAtomUri(atomURI);
EventListener successCallback = event12 -> {
logger.debug("atom creation successful, new atom URI is {}", atomURI);
ctx.getEventBus().publish(new AtomCreatedEvent(atomURI, wonNodeUri, atomDatasetWithSockets, null, atomUriBeforeCreation));
};
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 });
botContextWrapper.removeAtomUri(atomURI);
ctx.getEventBus().publish(new AtomCreationFailedEvent(wonNodeUri, atomUriBeforeCreation));
};
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());
}
use of won.bot.framework.eventbot.event.AtomCreationFailedEvent in project webofneeds by researchstudio-sat.
the class CreateAtomWithSocketsAction method doRun.
@Override
protected void doRun(Event event, EventListener executingListener) throws Exception {
EventListenerContext ctx = getEventListenerContext();
if (ctx.getAtomProducer().isExhausted()) {
logger.info("the bot's atom producer is exhausted.");
ctx.getEventBus().publish(new AtomProducerExhaustedEvent());
return;
}
final Dataset atomDataset = ctx.getAtomProducer().create();
if (atomDataset == null) {
logger.warn("atomproducer failed to produce 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);
int i = 1;
for (URI socket : sockets) {
atomModelWrapper.addSocket(atomUriBeforeCreation.toString() + "#socket" + i, socket.toString());
i++;
}
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(Prefixer.setPrefixes(atomDatasetWithSockets)), 150));
WonNodeInformationService wonNodeInformationService = ctx.getWonNodeInformationService();
final URI atomURI = wonNodeInformationService.generateAtomURI(wonNodeUri);
WonMessage createAtomMessage = createWonMessage(atomURI, wonNodeUri, 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 AtomCreatedEvent(atomURI, wonNodeUri, atomDatasetWithSockets, null, atomUriBeforeCreation));
};
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.getBotContextWrapper().removeAtomUri(atomURI);
ctx.getEventBus().publish(new AtomCreationFailedEvent(wonNodeUri, atomUriBeforeCreation));
};
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