final List<Piece> includedPieces = new ArrayList<>(pieces.subList(0, Math.min(pieces.size(), 3)));
if (includedPieces.isEmpty()) {
return;
}
// for now - using the same data for all users. TODO send personalized selection
final EmailDetails baseDetails = new EmailDetails();
baseDetails.setMessageTemplate("digest.vm");
baseDetails.setSubject("Computoser-generated tracks digest for you");
Map<String, Object> model = Maps.newHashMap();
baseDetails.setMessageTemplateModel(model);
baseDetails.setFrom(from);
baseDetails.setHtml(true);
userDao.performBatched(User.class, 100, new PageableOperation<User>() {
@Override
public void execute() {
for (User user : getData()) {
if (user.isReceiveDailyDigest() && StringUtils.isNotBlank(user.getEmail())) {
EmailDetails email = SerializationUtils.clone(baseDetails);
email.setTo(user.getEmail());
email.setCurrentUser(user);
String hmac = SecurityUtils.hmac(user.getEmail(), hmacKey);
email.getMessageTemplateModel().put("pieces", includedPieces);
email.getMessageTemplateModel().put("hmac", hmac);
// needed due to SES restrictions
try {
Thread.sleep(500);
} catch (InterruptedException e) {
throw new IllegalStateException(e);