<commit_before>package pl.tomaszdziurko.jvm_bloggers.mailing;

import com.google.common.base.Strings;

import lombok.NoArgsConstructor;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.stringtemplate.v4.ST;

import pl.tomaszdziurko.jvm_bloggers.blog_posts.domain.BlogPost;
import pl.tomaszdziurko.jvm_bloggers.blogs.domain.Blog;
import pl.tomaszdziurko.jvm_bloggers.blogs.domain.BlogType;
import pl.tomaszdziurko.jvm_bloggers.click_counter.RedirectLinkGenerator;
import pl.tomaszdziurko.jvm_bloggers.metadata.MetadataKeys;
import pl.tomaszdziurko.jvm_bloggers.metadata.MetadataRepository;
import pl.tomaszdziurko.jvm_bloggers.newsletter_issues.domain.NewsletterIssue;
import pl.tomaszdziurko.jvm_bloggers.utils.UriUtmComponentsBuilder;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static java.util.Collections.emptyList;
import static pl.tomaszdziurko.jvm_bloggers.utils.UriUtmComponentsBuilder.DEFAULT_UTM_CAMPAING;
import static pl.tomaszdziurko.jvm_bloggers.utils.UriUtmComponentsBuilder.DEFAULT_UTM_SOURCE;


@Component
@NoArgsConstructor
public class BlogSummaryMailGenerator {

    private static final int DAYS_IN_THE_PAST = 7;
    private static final char TEMPLATE_DELIMITER = '$';
    private static final String UTM_MEDIUM = "newsletter";

    private MetadataRepository metadataRepository;
    private RedirectLinkGenerator linkGenerator;

    @Autowired
    public BlogSummaryMailGenerator(
        MetadataRepository metadataRepository,
        RedirectLinkGenerator linkGenerator
    ) {
        this.metadataRepository = metadataRepository;
        this.linkGenerator = linkGenerator;
    }

    public String prepareMailContent(NewsletterIssue newsletterIssue) {
        String greeting = getValueForSection(MetadataKeys.MAILING_GREETING);
        String heading = getValueForSection(MetadataKeys.HEADING_TEMPLATE);
        String mainSection = prepareMainSectionWithBlogs(newsletterIssue);
        String varia = getValueForSection(MetadataKeys.VARIA_TEMPLATE);
        String signature = getValueForSection(MetadataKeys.MAILING_SIGNATURE);

        return greeting + heading + mainSection + varia + signature;
    }

    private String getValueForSection(String key) {
        return metadataRepository.findByName(key).getValue();
    }

    private String prepareMainSectionWithBlogs(NewsletterIssue newsletterIssue) {
        List<Blog> blogsAddedSinceLastNewsletter = newsletterIssue.getNewBlogs();
        List<BlogPost> newApprovedPosts = newsletterIssue.getBlogPosts();

        Map<BlogType, List<BlogPost>> newBlogPostsByType = newApprovedPosts<commit_msg>Modifies mail generation not to rely on new lines from mailing template sections, but instead of it to put them during e-mail creation.<commit_after>package pl.tomaszdziurko.jvm_bloggers.mailing;

import com.google.common.base.Strings;

import lombok.NoArgsConstructor;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.stringtemplate.v4.ST;

import pl.tomaszdziurko.jvm_bloggers.blog_posts.domain.BlogPost;
import pl.tomaszdziurko.jvm_bloggers.blogs.domain.Blog;
import pl.tomaszdziurko.jvm_bloggers.blogs.domain.BlogType;
import pl.tomaszdziurko.jvm_bloggers.click_counter.RedirectLinkGenerator;
import pl.tomaszdziurko.jvm_bloggers.metadata.MetadataKeys;
import pl.tomaszdziurko.jvm_bloggers.metadata.MetadataRepository;
import pl.tomaszdziurko.jvm_bloggers.newsletter_issues.domain.NewsletterIssue;
import pl.tomaszdziurko.jvm_bloggers.utils.UriUtmComponentsBuilder;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static java.util.Collections.emptyList;
import static pl.tomaszdziurko.jvm_bloggers.utils.UriUtmComponentsBuilder.DEFAULT_UTM_CAMPAING;
import static pl.tomaszdziurko.jvm_bloggers.utils.UriUtmComponentsBuilder.DEFAULT_UTM_SOURCE;


@Component
@NoArgsConstructor
public class BlogSummaryMailGenerator {

    private static final int DAYS_IN_THE_PAST = 7;
    private static final char TEMPLATE_DELIMITER = '$';
    private static final String UTM_MEDIUM = "newsletter";

    private static final String NEW_LINE = "<br/>";

    private MetadataRepository metadataRepository;
    private RedirectLinkGenerator linkGenerator;

    @Autowired
    public BlogSummaryMailGenerator(
        MetadataRepository metadataRepository,
        RedirectLinkGenerator linkGenerator
    ) {
        this.metadataRepository = metadataRepository;
        this.linkGenerator = linkGenerator;
    }

    public String prepareMailContent(NewsletterIssue newsletterIssue) {
        String greeting = getValueForSection(MetadataKeys.MAILING_GREETING);
        String heading = newsletterIssue.getHeading();
        String mainSection = prepareMainSectionWithBlogs(newsletterIssue);
        String varia = newsletterIssue.getVaria();
        String signature = getValueForSection(MetadataKeys.MAILING_SIGNATURE);

        return appendNewLinesIfNotEmpty(greeting, 2)
            + appendNewLinesIfNotEmpty(heading, 2)
            + appendNewLinesIfNotEmpty(mainSection, 2)
            + prepareVariaSection(varia, 2)
            + NEW_LINE + signature;
    }

    private String getValueForSection(String key) {
        return metadataRepository.findByName(key).getValue();
    }

    private String appendNewLinesIfNotEmpty(String text, int numberOfNewLines) {
        if (StringUtils.isEmpty(text)) {
            return text;
        } else {
            return text + StringUtils.repeat(NEW_LINE, numberOfNewLines);
        }
    }

    private String prepareVariaSection(String variaContent, int numberOfNewLines) {
        if (StringUtils.isEmpty(variaContent)) {
            return variaContent;
        } else {
            return "Varia:" + NEW_LINE + variaContent
                + StringUtils.repeat(NEW_LINE, numberOfNewLines);
        }
    }

    private String prepareMainSectionWithBlogs(NewsletterIssue newsletterIssue) {
        List<Blog> blogsAddedSinceLastNewsletter = newsletterIssue.getNewBlogs();
        List<BlogPost> newApprovedPosts = newsletterIssue.getBlogPosts();

        Map<BlogType, List<BlogPost>> newBlogPostsByType = newApprovedPosts
{'fraction_non_alphanumeric': 0.07283931110759993, 'fraction_numerical': 0.0014220255964607362, 'mean_word_length': 4.631672597864768, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4233746', 'n_tokens_mistral': 1891, 'n_tokens_neox': 1807, 'n_words': 320}