Search in sources :

Example 1 with DefaultNeedModelWrapper

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

the class MailFileNeedProducer method readNeedFromFile.

@Override
public synchronized Dataset readNeedFromFile(final File file) throws IOException {
    logger.debug("processing as mail file: {} ", file);
    FileInputStream fis = new FileInputStream(file);
    DefaultNeedModelWrapper needModelWrapper = new DefaultNeedModelWrapper("no:uri");
    try {
        MimeMessage emailMessage = new MimeMessage(null, fis);
        MimeMessageParser parser = new MimeMessageParser(emailMessage);
        parser.parse();
        needModelWrapper.setTitle(needContentPropertyType, parser.getSubject());
        String content = null;
        if (parser.hasPlainContent()) {
            content = parser.getPlainContent();
        } else if (parser.hasHtmlContent()) {
            Document doc = Jsoup.parse(parser.getHtmlContent());
            content = doc.text();
        }
        if (content != null) {
            needModelWrapper.setDescription(needContentPropertyType, content);
        }
        logger.debug("mail subject          : {}", parser.getSubject());
        logger.debug("mail has plain content: {}", parser.hasPlainContent());
        logger.debug("mail has html content : {}", parser.hasHtmlContent());
        logger.debug("mail has attachments  : {}", parser.hasAttachments());
        logger.debug("mail plain content    : {}", StringUtils.abbreviate(parser.getPlainContent(), 200));
        logger.debug("mail html content     : {}", StringUtils.abbreviate(parser.getHtmlContent(), 200));
        return needModelWrapper.copyDataset();
    } catch (Exception e) {
        logger.error("could not parse email from file {} ", file, e);
    } finally {
        if (fis != null)
            fis.close();
    }
    return null;
}
Also used : DefaultNeedModelWrapper(won.protocol.util.DefaultNeedModelWrapper) MimeMessage(javax.mail.internet.MimeMessage) MimeMessageParser(org.apache.commons.mail.util.MimeMessageParser) Document(org.jsoup.nodes.Document) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException)

Example 2 with DefaultNeedModelWrapper

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

the class CreateEchoNeedWithFacetsAction method doRun.

@Override
protected void doRun(Event event, EventListener executingListener) throws Exception {
    EventListenerContext ctx = getEventListenerContext();
    String replyText = "";
    if (!(event instanceof NeedCreatedEventForMatcher)) {
        logger.error("CreateEchoNeedWithFacetsAction can only handle NeedCreatedEventForMatcher");
        return;
    }
    final URI reactingToNeedUri = ((NeedCreatedEventForMatcher) event).getNeedURI();
    final Dataset needDataset = ((NeedCreatedEventForMatcher) event).getNeedData();
    DefaultNeedModelWrapper needModelWrapper = new DefaultNeedModelWrapper(needDataset);
    String titleString = needModelWrapper.getSomeTitleFromIsOrAll("en", "de");
    if (titleString != null) {
        replyText = titleString;
    } else {
        replyText = "Your Posting (" + reactingToNeedUri.toString() + ")";
    }
    WonNodeInformationService wonNodeInformationService = ctx.getWonNodeInformationService();
    final URI wonNodeUri = ctx.getNodeURISource().getNodeURI();
    final URI needURI = wonNodeInformationService.generateNeedURI(wonNodeUri);
    needModelWrapper = new DefaultNeedModelWrapper(needURI.toString());
    needModelWrapper.createContentNode(NeedContentPropertyType.IS_AND_SEEKS, needURI.toString());
    needModelWrapper.setTitle(NeedContentPropertyType.IS_AND_SEEKS, "RE: " + replyText);
    needModelWrapper.setDescription(NeedContentPropertyType.IS_AND_SEEKS, "This is a need automatically created by the EchoBot.");
    for (URI facetUri : facets) {
        needModelWrapper.addFacetUri(facetUri.toString());
    }
    final Dataset echoNeedDataset = needModelWrapper.copyDataset();
    logger.debug("creating need on won node {} with content {} ", wonNodeUri, StringUtils.abbreviate(RdfUtils.toString(echoNeedDataset), 150));
    WonMessage createNeedMessage = createWonMessage(wonNodeInformationService, needURI, wonNodeUri, echoNeedDataset);
    // 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);
            ctx.getEventBus().publish(new NeedCreatedEvent(needURI, wonNodeUri, echoNeedDataset, 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);
            ctx.getEventBus().publish(new NeedCreationFailedEvent(wonNodeUri));
        }
    };
    EventBotActionUtils.makeAndSubscribeResponseListener(createNeedMessage, successCallback, failureCallback, ctx);
    logger.debug("registered listeners for response to message URI {}", createNeedMessage.getMessageURI());
    getEventListenerContext().getWonMessageSender().sendWonMessage(createNeedMessage);
    logger.debug("need creation message sent with message URI {}", createNeedMessage.getMessageURI());
}
Also used : EventListenerContext(won.bot.framework.eventbot.EventListenerContext) DefaultNeedModelWrapper(won.protocol.util.DefaultNeedModelWrapper) NeedCreationFailedEvent(won.bot.framework.eventbot.event.NeedCreationFailedEvent) Dataset(org.apache.jena.query.Dataset) NeedCreatedEventForMatcher(won.bot.framework.eventbot.event.impl.matcher.NeedCreatedEventForMatcher) WonMessage(won.protocol.message.WonMessage) WonNodeInformationService(won.protocol.service.WonNodeInformationService) NeedCreationFailedEvent(won.bot.framework.eventbot.event.NeedCreationFailedEvent) FailureResponseEvent(won.bot.framework.eventbot.event.impl.wonmessage.FailureResponseEvent) Event(won.bot.framework.eventbot.event.Event) NeedCreatedEvent(won.bot.framework.eventbot.event.impl.needlifecycle.NeedCreatedEvent) EventListener(won.bot.framework.eventbot.listener.EventListener) URI(java.net.URI) NeedCreatedEvent(won.bot.framework.eventbot.event.impl.needlifecycle.NeedCreatedEvent)

Example 3 with DefaultNeedModelWrapper

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

the class TelegramCreateAction method doRun.

@Override
protected void doRun(Event event, EventListener executingListener) throws Exception {
    EventListenerContext ctx = getEventListenerContext();
    if (event instanceof TelegramCreateNeedEvent && ctx.getBotContextWrapper() instanceof TelegramBotContextWrapper) {
        TelegramBotContextWrapper botContextWrapper = (TelegramBotContextWrapper) ctx.getBotContextWrapper();
        TelegramCreateNeedEvent telegramCreateNeedEvent = (TelegramCreateNeedEvent) event;
        String[] parameters = telegramCreateNeedEvent.getStrings();
        Long chatId = telegramCreateNeedEvent.getChat().getId();
        if (chatId == null) {
            logger.error("no chatid present");
            return;
        }
        try {
            NeedContentPropertyType type = telegramContentExtractor.getNeedContentType(parameters[0]);
            if (type == null) {
                throw new InvalidParameterException("no valid type was given");
            }
            String title = null;
            if (parameters.length > 1) {
                title = parameters[1];
            }
            if (title == null) {
                throw new InvalidParameterException("no valid title was given");
            }
            // MAKE THOSE ATTRIBUTES DIFFERENT AND EDITABLE
            boolean isUsedForTesting = true;
            boolean isDoNotMatch = false;
            WonNodeInformationService wonNodeInformationService = ctx.getWonNodeInformationService();
            final URI wonNodeUri = ctx.getNodeURISource().getNodeURI();
            final URI needURI = wonNodeInformationService.generateNeedURI(wonNodeUri);
            DefaultNeedModelWrapper wrapper = new DefaultNeedModelWrapper(needURI.toString());
            wrapper.setTitle(type, title);
            for (URI facet : facets) {
                wrapper.addFacetUri(facet.toString());
            }
            Dataset needDataset = wrapper.copyDataset();
            logger.debug("creating need on won node {} with content {} ", wonNodeUri, StringUtils.abbreviate(RdfUtils.toString(needDataset), 150));
            WonMessage createNeedMessage = createWonMessage(wonNodeInformationService, needURI, wonNodeUri, needDataset, isUsedForTesting, isDoNotMatch);
            EventBotActionUtils.rememberInList(ctx, needURI, uriListName);
            botContextWrapper.addChatIdWonURIRelation(chatId, new WonURI(needURI, UriType.NEED));
            botContextWrapper.addURIChatIdRelation(needURI, chatId);
            EventListener successCallback = new EventListener() {

                @Override
                public void onEvent(Event event) throws Exception {
                    logger.debug("need creation successful, new need URI is {}", needURI);
                    logger.debug("created need was from sender: " + botContextWrapper.getChatIdForURI(needURI));
                    try {
                        Message message = telegramCreateNeedEvent.getAbsSender().sendMessage(wonTelegramBotHandler.getTelegramMessageGenerator().getCreatedNeedMessage(chatId, needURI));
                        botContextWrapper.addMessageIdWonURIRelation(message.getMessageId(), new WonURI(needURI, UriType.NEED));
                    } catch (TelegramApiException te) {
                        logger.error(te.getMessage());
                    }
                }
            };
            EventListener failureCallback = new EventListener() {

                @Override
                public void onEvent(Event event) throws Exception {
                    String textMessage = WonRdfUtils.MessageUtils.getTextMessage(((FailureResponseEvent) event).getFailureMessage());
                    logger.error("need creation failed for need URI {}, original message URI {}: {}", new Object[] { needURI, ((FailureResponseEvent) event).getOriginalMessageURI(), textMessage });
                    EventBotActionUtils.removeFromList(getEventListenerContext(), needURI, uriListName);
                }
            };
            EventBotActionUtils.makeAndSubscribeResponseListener(createNeedMessage, successCallback, failureCallback, getEventListenerContext());
            logger.debug("registered listeners for response to message URI {}", createNeedMessage.getMessageURI());
            getEventListenerContext().getWonMessageSender().sendWonMessage(createNeedMessage);
            logger.debug("need creation message sent with message URI {}", createNeedMessage.getMessageURI());
        } catch (Exception e) {
            try {
                logger.error(e.getMessage());
                telegramCreateNeedEvent.getAbsSender().sendMessage(wonTelegramBotHandler.getTelegramMessageGenerator().getErrorMessage(chatId));
            } catch (TelegramApiException te) {
                logger.error(te.getMessage());
            }
        }
    }
}
Also used : EventListenerContext(won.bot.framework.eventbot.EventListenerContext) DefaultNeedModelWrapper(won.protocol.util.DefaultNeedModelWrapper) Message(org.telegram.telegrambots.api.objects.Message) WonMessage(won.protocol.message.WonMessage) TelegramCreateNeedEvent(won.bot.framework.eventbot.event.impl.telegram.TelegramCreateNeedEvent) Dataset(org.apache.jena.query.Dataset) WonNodeInformationService(won.protocol.service.WonNodeInformationService) NeedContentPropertyType(won.protocol.model.NeedContentPropertyType) WonURI(won.bot.framework.eventbot.action.impl.mail.model.WonURI) URI(java.net.URI) TelegramApiException(org.telegram.telegrambots.exceptions.TelegramApiException) InvalidParameterException(java.security.InvalidParameterException) TelegramApiException(org.telegram.telegrambots.exceptions.TelegramApiException) InvalidParameterException(java.security.InvalidParameterException) WonURI(won.bot.framework.eventbot.action.impl.mail.model.WonURI) TelegramBotContextWrapper(won.bot.framework.bot.context.TelegramBotContextWrapper) WonMessage(won.protocol.message.WonMessage) TelegramCreateNeedEvent(won.bot.framework.eventbot.event.impl.telegram.TelegramCreateNeedEvent) FailureResponseEvent(won.bot.framework.eventbot.event.impl.wonmessage.FailureResponseEvent) Event(won.bot.framework.eventbot.event.Event) EventListener(won.bot.framework.eventbot.listener.EventListener)

Example 4 with DefaultNeedModelWrapper

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

the class TelegramMessageGenerator method getHintMessage.

public SendMessage getHintMessage(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>We found a Match for 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("Request", "Close"));
    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)

Example 5 with DefaultNeedModelWrapper

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

the class TelegramMessageGenerator method getCreatedNeedMessage.

public SendMessage getCreatedNeedMessage(Long chatId, URI needURI) {
    Dataset createdNeedRDF = eventListenerContext.getLinkedDataSource().getDataForResource(needURI);
    DefaultNeedModelWrapper needModelWrapper = new DefaultNeedModelWrapper(createdNeedRDF);
    String title = needModelWrapper.getSomeTitleFromIsOrAll("en", "de");
    String description = needModelWrapper.getSomeDescription(NeedContentPropertyType.ALL, "en", "de");
    SendMessage sendMessage = new SendMessage();
    sendMessage.setChatId(chatId);
    String text = "<b>We created a Need for you!\n\n</b><a href='" + needURI + "'>" + title + "\n\n</a>";
    if (description != null) {
        text = text + "<em>" + description + "</em>";
    }
    sendMessage.setText(text);
    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