Example usage for java.util LinkedHashSet stream

List of usage examples for java.util LinkedHashSet stream

Introduction

In this page you can find the example usage for java.util LinkedHashSet stream.

Prototype

default Stream<E> stream() 

Source Link

Document

Returns a sequential Stream with this collection as its source.

Usage

From source file:dhbw.clippinggorilla.external.mailserver.MailUtils.java

public static void sendClipping(Clipping clipping) {
    User user = UserUtils.getUser(clipping.getUserid());
    String email = user.getEmail();
    String html = CLIPPING_ROOT_HTML;
    String rows = "";
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd. LLLL. yyyy");
    formatter.withZone(ZoneId.of("Europe/Berlin"));

    rows = clipping.getArticles().entrySet().stream().map((entry) -> {
        InterestProfile userprofile = entry.getKey();
        LinkedHashSet<Article> articleSet = entry.getValue();
        String profileRow = CLIPPING_GROUP_HTML.replace("[GROUPNAME]", userprofile.getName());
        String articleRows = articleSet.stream()
                .map((article) -> CLIPPING_ARTICLE_HTML
                        .replace("[ARTICLE_IMAGE_URL]",
                                article.getUrlToImage().isEmpty()
                                        ? article.getSourceAsSource().getLogo().toExternalForm()
                                        : article.getUrlToImage())
                        .replace("[ARTICLE_TITLE]", article.getTitle())
                        .replace("[ARTICLE_DESCRIPTION]",
                                article.getDescription().length() > 400
                                        ? article.getDescription().substring(0, 400) + "..."
                                        : article.getDescription())
                        .replace("[SOURCE_NAME]", article.getSourceAsSource().getName())
                        .replace("[ARTICLE_DATE]",
                                article.getPublishedAtAsLocalDateTime().toLocalDate().format(formatter))
                        .replace("[AUTHOR]", article.getAuthor()))
                .reduce("", String::concat);
        return profileRow + "\n" + articleRows;
    }).reduce(rows, String::concat);

    rows = clipping.getArticlesFromGroup().entrySet().stream().map((entry) -> {
        GroupInterestProfile groupprofile = entry.getKey();
        LinkedHashSet<Article> articleSet = entry.getValue();
        String profileRow = CLIPPING_GROUP_HTML.replace("[GROUPNAME]", groupprofile.getName());
        String articleRows = articleSet.stream()
                .map((article) -> CLIPPING_ARTICLE_HTML
                        .replace("[ARTICLE_IMAGE_URL]",
                                article.getUrlToImage().isEmpty()
                                        ? article.getSourceAsSource().getLogo().toExternalForm()
                                        : article.getUrlToImage())
                        .replace("[ARTICLE_TITLE]", article.getTitle())
                        .replace("[ARTICLE_DESCRIPTION]",
                                article.getDescription().length() > 400
                                        ? article.getDescription().substring(0, 400) + "..."
                                        : article.getDescription())
                        .replace("[SOURCE_NAME]", article.getSourceAsSource().getName())
                        .replace("[ARTICLE_DATE]",
                                article.getPublishedAtAsLocalDateTime().toLocalDate().format(formatter))
                        .replace("[AUTHOR]", article.getAuthor()))
                .reduce("", String::concat);
        return profileRow + "\n" + articleRows;
    }).reduce(rows, String::concat);

    String clippingText = Language.get(Word.CLIPPING_TEXT).replace("[FIRSTNAME]", user.getFirstName())
            .replace("[LASTNAME]", user.getLastName()).replace("[DATE]", clipping.getDate().format(formatter));
    html = html.replace("[CLIPPING_TEXT]", clippingText).replace("[GROUPS]", rows);
    //        try {
    //            Files.write(Paths.get(System.getProperty("user.home"), "Desktop", "email.html"), html.getBytes("UTF-8"));
    //        } catch (IOException ex) {
    //            Logger.getLogger(MailUtils.class.getName()).log(Level.SEVERE, null, ex);
    //        }/*from w  w  w .  ja  v  a  2  s . co  m*/
    try {
        Mail.send(email, "ClippingGorilla Clippings", "Your email client does not support HTML messages", html);
    } catch (EmailException ex) {
        Log.error("Could not send Clipping to user " + user.getFirstName() + " " + user.getLastName() + " ("
                + user.getUsername() + ")", ex);
    }
}