Search in sources :

Example 1 with ReplaceCommandEvent

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