<commit_before>package com.dstevens.user;

import java.math.BigDecimal;
import java.time.Instant;
import java.time.Year;
import java.util.Date;
import java.util.function.Supplier;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import com.dstevens.mail.MailMessage;
import com.dstevens.mail.MailMessageFactory;
import com.dstevens.suppliers.ClockSupplier;
import com.dstevens.user.patronage.Patronage;
import com.dstevens.user.patronage.PatronagePaymentReceipt;
import com.dstevens.user.patronage.PaymentType;

import static com.dstevens.collections.Sets.set;

@Service
public class AccountCreator {

	private static final Logger logger = Logger.getLogger(AccountCreator.class);
	
	private final UserDao userDao;
	private final MailMessageFactory messageFactory;
	private final ClockSupplier clockSupplier;
	private final Supplier<PasswordEncoder> passwordEncoderSupplier;

	@Autowired
	public AccountCreator(UserDao userDao, Supplier<PasswordEncoder> passwordEncoderSupplier, MailMessageFactory messageFactory, ClockSupplier clockSupplier) {
		this.userDao = userDao;
		this.passwordEncoderSupplier = passwordEncoderSupplier;
		this.messageFactory = messageFactory;
		this.clockSupplier = clockSupplier;
	}
	
	public User createUser(String email,
			               String password,
			               String firstName,
			               String lastName,
			               String originalUsername,
			               String paymentReceiptIdentifier) {
		User user = new User(email, passwordEncoderSupplier.get().encode(password), set(Role.USER)).withFirstName(firstName).withLastName(lastName);
		Instant now = clockSupplier.get().instant();
		if(!StringUtils.isBlank(originalUsername)) {
			Patronage patronage = new Patronage(Year.now(clockSupplier.get()).getValue(), Date.from(now), null);
			patronage = patronage.withOriginalUsername(originalUsername.trim());
			if(!StringUtils.isBlank(paymentReceiptIdentifier)) {
				patronage = patronage.withPayment(new PatronagePaymentReceipt(PaymentType.PAYPAL, new BigDecimal("20.00"), paymentReceiptIdentifier, Date.from(now)));
			}
			user = user.withPatronage(patronage);
		}
		User newUser = userDao.save(user);
		sendConfirmatoryEmailTo(email);
		sendAdminEmailFor(newUser);
		return newUser;
	}
	
	private void sendAdminEmailFor(User user) {
		StringBuilder body = new StringBuilder();
		body.append(user.getEmail() + " has just created an account on the database.");
		if(user.getPatronage() != null) {
			body.append("\nTheir original username in the old database is " + user.getPatronage().getOriginalUsername());
			if(!user.getPatronage().getPayments().isEmpty()) {
				body.append("\nTheir paypal receipt id for paying for patronage is " + user.getPatronage().getPayments().get(0).getPaymentReceiptIdentifier());
			}
		}
		send(messageFactory.message().
		     from("<EMAIL>", "UT Database").
		     to("<EMAIL>").
		     subject("A new Underground Theater User Account has been created: " + user.getEmail()).
		     body(body.toString()));
	}

	private void sendConfirmatoryEmailTo(String email) {
		send(messageFactory.message().
		     from("<EMAIL>", "UT Database").
		     to(email).
		     subject("Your Underground Theater User Account has been created").
		     body("Thank you for creating an account with Underground Theater's character database, The Green Room!"));
	}
	
	private void send(MailMessage mailMessage) {
		try {
			mailMessage.send();
		} catch(Exception e) {
			logger.error("Failed to send " + mailMessage, e);
		}
	}
}<commit_msg>Refactoring controllers into distinct sub-packages, and services out of those packages<commit_after>package com.dstevens.user;

import java.math.BigDecimal;
import java.time.Instant;
import java.time.Year;
import java.util.Date;
import java.util.function.Supplier;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import com.dstevens.mail.MailMessage;
import com.dstevens.mail.MailMessageFactory;
import com.dstevens.suppliers.ClockSupplier;
import com.dstevens.user.patronage.Patronage;
import com.dstevens.user.patronage.PatronagePaymentReceipt;
import com.dstevens.user.patronage.PaymentType;

import static com.dstevens.collections.Sets.set;

@Service
public class AccountCreator {

	private static final Logger logger = Logger.getLogger(AccountCreator.class);
	
	private final UserDao userDao;
	private final MailMessageFactory messageFactory;
	private final ClockSupplier clockSupplier;
	private final Supplier<PasswordEncoder> passwordEncoderSupplier;

	@Autowired
	public AccountCreator(UserDao userDao, Supplier<PasswordEncoder> passwordEncoderSupplier, MailMessageFactory messageFactory, ClockSupplier clockSupplier) {
		this.userDao = userDao;
		this.passwordEncoderSupplier = passwordEncoderSupplier;
		this.messageFactory = messageFactory;
		this.clockSupplier = clockSupplier;
	}
	
	public User createUser(String email,
			               String password,
			               String firstName,
			               String lastName,
			               String originalUsername,
			               String paymentReceiptIdentifier) {
		User user = new User(email, passwordEncoderSupplier.get().encode(password), set(Role.USER)).withFirstName(firstName).withLastName(lastName);
		Instant now = clockSupplier.get().instant();
		if(!StringUtils.isBlank(originalUsername)) {
			Patronage patronage = new Patronage(Year.now(clockSupplier.get()).getValue(), Date.from(now), null);
			patronage = patronage.withOriginalUsername(originalUsername.trim());
			if(!StringUtils.isBlank(paymentReceiptIdentifier)) {
				patronage = patronage.withPayment(new PatronagePaymentReceipt(PaymentType.PAYPAL, new BigDecimal("20.00"), paymentReceiptIdentifier, Date.from(now)));
			}
			user = user.withPatronage(patronage);
		}
		User newUser = userDao.save(user);
		sendConfirmatoryEmailTo(newUser);
		sendAdminEmailFor(newUser);
		return newUser;
	}
	
	private void sendAdminEmailFor(User user) {
		StringBuilder body = new StringBuilder();
		body.append(user.getEmail() + " has just created an account on the database.");
		if(user.getPatronage() != null) {
			body.append("\nTheir original username in the old database is " + user.getPatronage().getOriginalUsername());
			if(!user.getPatronage().getPayments().isEmpty()) {
				body.append("\nTheir paypal receipt id for paying for patronage is " + user.getPatronage().getPayments().get(0).getPaymentReceiptIdentifier());
			}
		}
		send(messageFactory.message().
		     from("<EMAIL>", "UT Database").
		     to("<EMAIL>").
		     subject("A new Underground Theater User Account has been created: " + user.getEmail()).
		     body(body.toString()));
	}

	private void sendConfirmatoryEmailTo(User user) {
		send(messageFactory.message().
		     from("<EMAIL>", "UT Database").
		     to(user.getEmail()).
		     subject("Your Underground Theater User Account has been created").
		     body("Thank you for creating an account with Underground Theater's character database, The Green Room!"));
	}
	
	private void send(MailMessage mailMessage) {
		try {
			mailMessage.send();
		} catch(Exception e) {
			logger.error("Failed to send " + mailMessage, e);
		}
	}
}
{'fraction_non_alphanumeric': 0.10820499203398831, 'fraction_numerical': 0.0018587360594795538, 'mean_word_length': 4.737242955064737, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '1682916', 'n_tokens_mistral': 2331, 'n_tokens_neox': 2138, 'n_words': 468}