Search in sources :

Example 11 with DefaultNeedModelWrapper

use of won.protocol.util.DefaultNeedModelWrapper in project webofneeds by researchstudio-sat.

the class CreateDebugNeedWithFacetsAction method doRun.

@Override
protected void doRun(Event event, EventListener executingListener) throws Exception {
    String replyText = "";
    URI reactingToNeedUriTmp = null;
    Dataset needDataset = null;
    if (event instanceof NeedSpecificEvent) {
        reactingToNeedUriTmp = ((NeedSpecificEvent) event).getNeedURI();
    } else {
        logger.warn("could not process non-need specific event {}", event);
        return;
    }
    if (event instanceof NeedCreatedEventForMatcher) {
        needDataset = ((NeedCreatedEventForMatcher) event).getNeedData();
    } else if (event instanceof HintDebugCommandEvent) {
        reactingToNeedUriTmp = ((HintDebugCommandEvent) event).getRemoteNeedURI();
    } else if (event instanceof ConnectDebugCommandEvent) {
        reactingToNeedUriTmp = ((ConnectDebugCommandEvent) event).getRemoteNeedURI();
    } else {
        logger.error("CreateEchoNeedWithFacetsAction cannot handle " + event.getClass().getName());
        return;
    }
    final URI reactingToNeedUri = reactingToNeedUriTmp;
    String titleString = null;
    boolean createNeed = true;
    if (needDataset != null) {
        DefaultNeedModelWrapper needModelWrapper = new DefaultNeedModelWrapper(needDataset);
        titleString = needModelWrapper.getSomeTitleFromIsOrAll("en", "de");
        createNeed = needModelWrapper.hasFlag(WON.USED_FOR_TESTING) && !needModelWrapper.hasFlag(WON.NO_HINT_FOR_ME);
    }
    // if create need is false do not continue the debug need creation
    if (!createNeed)
        return;
    if (titleString != null) {
        if (isInitialForConnect) {
            replyText = "Debugging with initial connect: " + titleString;
        } else if (isInitialForHint) {
            replyText = "Debugging with initial hint: " + titleString;
        } else {
            replyText = "Debugging: " + titleString;
        }
    } else {
        replyText = "Debug Need No. " + counter.increment();
    }
    EventListenerContext ctx = getEventListenerContext();
    WonNodeInformationService wonNodeInformationService = ctx.getWonNodeInformationService();
    EventBus bus = ctx.getEventBus();
    final URI wonNodeUri = ctx.getNodeURISource().getNodeURI();
    final URI needURI = wonNodeInformationService.generateNeedURI(wonNodeUri);
    DefaultNeedModelWrapper needModelWrapper = new DefaultNeedModelWrapper(needURI.toString());
    needModelWrapper.setTitle(NeedContentPropertyType.IS_AND_SEEKS, replyText);
    needModelWrapper.setDescription(NeedContentPropertyType.IS_AND_SEEKS, "This is a need automatically created by the DebugBot.");
    for (URI facet : facets) {
        needModelWrapper.addFacetUri(facet.toString());
    }
    final Dataset debugNeedDataset = needModelWrapper.copyDataset();
    final Event origEvent = event;
    logger.debug("creating need on won node {} with content {} ", wonNodeUri, StringUtils.abbreviate(RdfUtils.toString(debugNeedDataset), 150));
    WonMessage createNeedMessage = createWonMessage(wonNodeInformationService, needURI, wonNodeUri, debugNeedDataset);
    // remember the need URI so we can react to success/failure responses
    EventBotActionUtils.rememberInList(ctx, needURI, uriListName);
    EventListener successCallback = new EventListener() {

        @Override
        public void onEvent(Event event) throws Exception {
            logger.debug("need creation successful, new need URI is {}", needURI);
            // save the mapping between the original and the reaction in to the context.
            getEventListenerContext().getBotContextWrapper().addUriAssociation(reactingToNeedUri, needURI);
            if ((origEvent instanceof HintDebugCommandEvent) || isInitialForHint) {
                bus.publish(new NeedCreatedEventForDebugHint(needURI, wonNodeUri, debugNeedDataset, null));
            } else if ((origEvent instanceof ConnectDebugCommandEvent) || isInitialForConnect) {
                bus.publish(new NeedCreatedEventForDebugConnect(needURI, wonNodeUri, debugNeedDataset, null));
            } else {
                bus.publish(new NeedCreatedEvent(needURI, wonNodeUri, debugNeedDataset, null));
            }
        }
    };
    EventListener failureCallback = new EventListener() {

        @Override
        public void onEvent(Event event) throws Exception {
            String textMessage = WonRdfUtils.MessageUtils.getTextMessage(((FailureResponseEvent) event).getFailureMessage());
            logger.debug("need creation failed for need URI {}, original message URI {}: {}", new Object[] { needURI, ((FailureResponseEvent) event).getOriginalMessageURI(), textMessage });
            EventBotActionUtils.removeFromList(ctx, needURI, uriListName);
            bus.publish(new NeedCreationFailedEvent(wonNodeUri));
        }
    };
    EventBotActionUtils.makeAndSubscribeResponseListener(createNeedMessage, successCallback, failureCallback, ctx);
    logger.debug("registered listeners for response to message URI {}", createNeedMessage.getMessageURI());
    ctx.getWonMessageSender().sendWonMessage(createNeedMessage);
    logger.debug("need creation message sent with message URI {}", createNeedMessage.getMessageURI());
}
Also used : DefaultNeedModelWrapper(won.protocol.util.DefaultNeedModelWrapper) EventListenerContext(won.bot.framework.eventbot.EventListenerContext) NeedCreatedEventForDebugConnect(won.bot.framework.eventbot.event.impl.debugbot.NeedCreatedEventForDebugConnect) Dataset(org.apache.jena.query.Dataset) WonNodeInformationService(won.protocol.service.WonNodeInformationService) EventBus(won.bot.framework.eventbot.bus.EventBus) URI(java.net.URI) NeedCreatedEventForDebugHint(won.bot.framework.eventbot.event.impl.debugbot.NeedCreatedEventForDebugHint) ConnectDebugCommandEvent(won.bot.framework.eventbot.event.impl.debugbot.ConnectDebugCommandEvent) NeedSpecificEvent(won.bot.framework.eventbot.event.NeedSpecificEvent) NeedCreationFailedEvent(won.bot.framework.eventbot.event.NeedCreationFailedEvent) HintDebugCommandEvent(won.bot.framework.eventbot.event.impl.debugbot.HintDebugCommandEvent) NeedCreatedEventForMatcher(won.bot.framework.eventbot.event.impl.matcher.NeedCreatedEventForMatcher) WonMessage(won.protocol.message.WonMessage) NeedCreatedEvent(won.bot.framework.eventbot.event.impl.needlifecycle.NeedCreatedEvent) ConnectDebugCommandEvent(won.bot.framework.eventbot.event.impl.debugbot.ConnectDebugCommandEvent) NeedCreationFailedEvent(won.bot.framework.eventbot.event.NeedCreationFailedEvent) HintDebugCommandEvent(won.bot.framework.eventbot.event.impl.debugbot.HintDebugCommandEvent) FailureResponseEvent(won.bot.framework.eventbot.event.impl.wonmessage.FailureResponseEvent) Event(won.bot.framework.eventbot.event.Event) NeedSpecificEvent(won.bot.framework.eventbot.event.NeedSpecificEvent) EventListener(won.bot.framework.eventbot.listener.EventListener) NeedCreatedEvent(won.bot.framework.eventbot.event.impl.needlifecycle.NeedCreatedEvent)

Example 12 with DefaultNeedModelWrapper

use of won.protocol.util.DefaultNeedModelWrapper in project webofneeds by researchstudio-sat.

the class WonMimeMessageGenerator method generateWonMimeMessage.

private WonMimeMessage generateWonMimeMessage(MimeMessage msgToRespondTo, Template template, VelocityContext velocityContext, URI remoteNeedUri) throws MessagingException, UnsupportedEncodingException {
    Dataset remoteNeedRDF = eventListenerContext.getLinkedDataSource().getDataForResource(remoteNeedUri);
    DefaultNeedModelWrapper needModelWrapper = new DefaultNeedModelWrapper(remoteNeedRDF);
    MimeMessage answerMessage = (MimeMessage) msgToRespondTo.reply(false);
    answerMessage.setFrom(new InternetAddress(sentFrom, sentFromName));
    answerMessage.setText("");
    answerMessage.setSubject(answerMessage.getSubject() + " <-> " + StringUtils.trim(needModelWrapper.getSomeTitleFromIsOrAll("en", "de")));
    // We need to create an instance of our own MimeMessage Implementation in order to have the Unique Message Id set before sending
    WonMimeMessage wonAnswerMessage = new WonMimeMessage(answerMessage);
    wonAnswerMessage.updateMessageID();
    String messageId = wonAnswerMessage.getMessageID();
    // put variables (e.g. Message-Id) for the footer into the context and create mail body using the template
    putCommandFooter(velocityContext, wonAnswerMessage);
    StringWriter writer = new StringWriter();
    template.merge(velocityContext, writer);
    answerMessage.setText(writer.toString());
    // create a new won mime message with the right body and message id set
    wonAnswerMessage = new WonMimeMessage(answerMessage);
    wonAnswerMessage.setMessageId(messageId);
    return wonAnswerMessage;
}
Also used : DefaultNeedModelWrapper(won.protocol.util.DefaultNeedModelWrapper) InternetAddress(javax.mail.internet.InternetAddress) StringWriter(java.io.StringWriter) MimeMessage(javax.mail.internet.MimeMessage)

Example 13 with DefaultNeedModelWrapper

use of won.protocol.util.DefaultNeedModelWrapper in project webofneeds by researchstudio-sat.

the class WonMimeMessageGenerator method putRemoteNeedInfo.

/**
 * Responsible for filling inc/remote-need-info.vm template
 * @param velocityContext context to put template-vars in
 * @param remoteNeedUri uri for the remote need
 */
private void putRemoteNeedInfo(VelocityContext velocityContext, URI remoteNeedUri) {
    Dataset remoteNeedRDF = eventListenerContext.getLinkedDataSource().getDataForResource(remoteNeedUri);
    DefaultNeedModelWrapper needModelWrapper = new DefaultNeedModelWrapper(remoteNeedRDF);
    velocityContext.put("remoteNeedTitle", StringUtils.trim(needModelWrapper.getSomeTitleFromIsOrAll("en", "de")).replaceAll("\\n", "\n" + QUOTE_CHAR));
    velocityContext.put("remoteNeedDescription", StringUtils.trim(needModelWrapper.getSomeDescription(NeedContentPropertyType.ALL, "en", "de")).replaceAll("\\n", "\n" + QUOTE_CHAR));
    Collection<String> tags = needModelWrapper.getTags(NeedContentPropertyType.ALL);
    velocityContext.put("remoteNeedTags", tags.size() > 0 ? tags : null);
    velocityContext.put("remoteNeedUri", remoteNeedUri);
}
Also used : DefaultNeedModelWrapper(won.protocol.util.DefaultNeedModelWrapper)

Example 14 with DefaultNeedModelWrapper

use of won.protocol.util.DefaultNeedModelWrapper in project webofneeds by researchstudio-sat.

the class TelegramMessageGenerator method getConnectMessage.

public SendMessage getConnectMessage(Long chatId, URI remoteNeedUri, URI yourNeedUri) {
    Dataset remoteNeedRDF = eventListenerContext.getLinkedDataSource().getDataForResource(remoteNeedUri);
    DefaultNeedModelWrapper needModelWrapper = new DefaultNeedModelWrapper(remoteNeedRDF);
    String title = needModelWrapper.getSomeTitleFromIsOrAll("en", "de");
    String description = needModelWrapper.getSomeDescription(NeedContentPropertyType.ALL, "en", "de");
    SendMessage sendMessage = new SendMessage();
    sendMessage.setChatId(chatId);
    String text = "<b>Someone wants to connect with you!\n\n</b><a href='" + remoteNeedUri + "'>" + title + "\n\n</a>";
    if (description != null) {
        text = text + "<em>" + description + "</em>";
    }
    sendMessage.setText(text);
    sendMessage.setReplyMarkup(getConnectionActionKeyboard("Accept", "Deny"));
    sendMessage.enableHtml(true);
    return sendMessage;
}
Also used : DefaultNeedModelWrapper(won.protocol.util.DefaultNeedModelWrapper) Dataset(org.apache.jena.query.Dataset) SendMessage(org.telegram.telegrambots.api.methods.send.SendMessage)

Aggregations

DefaultNeedModelWrapper (won.protocol.util.DefaultNeedModelWrapper)14 Dataset (org.apache.jena.query.Dataset)9 URI (java.net.URI)5 EventListenerContext (won.bot.framework.eventbot.EventListenerContext)4 Event (won.bot.framework.eventbot.event.Event)4 FailureResponseEvent (won.bot.framework.eventbot.event.impl.wonmessage.FailureResponseEvent)4 EventListener (won.bot.framework.eventbot.listener.EventListener)4 WonMessage (won.protocol.message.WonMessage)4 WonNodeInformationService (won.protocol.service.WonNodeInformationService)4 MimeMessage (javax.mail.internet.MimeMessage)3 SendMessage (org.telegram.telegrambots.api.methods.send.SendMessage)3 WonURI (won.bot.framework.eventbot.action.impl.mail.model.WonURI)3 StringWriter (java.io.StringWriter)2 MessagingException (javax.mail.MessagingException)2 InternetAddress (javax.mail.internet.InternetAddress)2 MailBotContextWrapper (won.bot.framework.bot.context.MailBotContextWrapper)2 NeedCreationFailedEvent (won.bot.framework.eventbot.event.NeedCreationFailedEvent)2 NeedCreatedEventForMatcher (won.bot.framework.eventbot.event.impl.matcher.NeedCreatedEventForMatcher)2 NeedCreatedEvent (won.bot.framework.eventbot.event.impl.needlifecycle.NeedCreatedEvent)2 NeedContentPropertyType (won.protocol.model.NeedContentPropertyType)2