Search in sources :

Example 1 with AtomCreationAbortedEvent

use of won.bot.framework.eventbot.event.impl.command.create.AtomCreationAbortedEvent 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());
}
Also used : WonMessageBuilderException(won.protocol.exception.WonMessageBuilderException) LoggerFactory(org.slf4j.LoggerFactory) StringUtils(org.apache.commons.lang3.StringUtils) CreateAtomCommandSuccessEvent(won.bot.framework.eventbot.event.impl.command.create.CreateAtomCommandSuccessEvent) WonMessage(won.protocol.message.WonMessage) WonMessageBuilder(won.protocol.message.builder.WonMessageBuilder) Resource(org.apache.jena.rdf.model.Resource) URI(java.net.URI) Dataset(org.apache.jena.query.Dataset) EventListenerContext(won.bot.framework.eventbot.EventListenerContext) WonNodeInformationService(won.protocol.service.WonNodeInformationService) CreateAtomCommandFailureEvent(won.bot.framework.eventbot.event.impl.command.create.CreateAtomCommandFailureEvent) WONMATCH(won.protocol.vocabulary.WONMATCH) Logger(org.slf4j.Logger) AtomCreationAbortedEvent(won.bot.framework.eventbot.event.impl.command.create.AtomCreationAbortedEvent) MethodHandles(java.lang.invoke.MethodHandles) BaseEventBotAction(won.bot.framework.eventbot.action.BaseEventBotAction) EventBotActionUtils(won.bot.framework.eventbot.action.EventBotActionUtils) FailureResponseEvent(won.bot.framework.eventbot.event.impl.wonmessage.FailureResponseEvent) WonRdfUtils(won.protocol.util.WonRdfUtils) AtomModelWrapper(won.protocol.util.AtomModelWrapper) Event(won.bot.framework.eventbot.event.Event) RdfUtils(won.protocol.util.RdfUtils) CreateAtomCommandEvent(won.bot.framework.eventbot.event.impl.command.create.CreateAtomCommandEvent) EventListener(won.bot.framework.eventbot.listener.EventListener) EventListenerContext(won.bot.framework.eventbot.EventListenerContext) Dataset(org.apache.jena.query.Dataset) CreateAtomCommandFailureEvent(won.bot.framework.eventbot.event.impl.command.create.CreateAtomCommandFailureEvent) Resource(org.apache.jena.rdf.model.Resource) WonNodeInformationService(won.protocol.service.WonNodeInformationService) URI(java.net.URI) AtomCreationAbortedEvent(won.bot.framework.eventbot.event.impl.command.create.AtomCreationAbortedEvent) CreateAtomCommandSuccessEvent(won.bot.framework.eventbot.event.impl.command.create.CreateAtomCommandSuccessEvent) WonMessage(won.protocol.message.WonMessage) AtomModelWrapper(won.protocol.util.AtomModelWrapper) CreateAtomCommandEvent(won.bot.framework.eventbot.event.impl.command.create.CreateAtomCommandEvent) EventListener(won.bot.framework.eventbot.listener.EventListener) FailureResponseEvent(won.bot.framework.eventbot.event.impl.wonmessage.FailureResponseEvent)

Aggregations

MethodHandles (java.lang.invoke.MethodHandles)1 URI (java.net.URI)1 StringUtils (org.apache.commons.lang3.StringUtils)1 Dataset (org.apache.jena.query.Dataset)1 Resource (org.apache.jena.rdf.model.Resource)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1 EventListenerContext (won.bot.framework.eventbot.EventListenerContext)1 BaseEventBotAction (won.bot.framework.eventbot.action.BaseEventBotAction)1 EventBotActionUtils (won.bot.framework.eventbot.action.EventBotActionUtils)1 Event (won.bot.framework.eventbot.event.Event)1 AtomCreationAbortedEvent (won.bot.framework.eventbot.event.impl.command.create.AtomCreationAbortedEvent)1 CreateAtomCommandEvent (won.bot.framework.eventbot.event.impl.command.create.CreateAtomCommandEvent)1 CreateAtomCommandFailureEvent (won.bot.framework.eventbot.event.impl.command.create.CreateAtomCommandFailureEvent)1 CreateAtomCommandSuccessEvent (won.bot.framework.eventbot.event.impl.command.create.CreateAtomCommandSuccessEvent)1 FailureResponseEvent (won.bot.framework.eventbot.event.impl.wonmessage.FailureResponseEvent)1 EventListener (won.bot.framework.eventbot.listener.EventListener)1 WonMessageBuilderException (won.protocol.exception.WonMessageBuilderException)1 WonMessage (won.protocol.message.WonMessage)1 WonMessageBuilder (won.protocol.message.builder.WonMessageBuilder)1