0

I want to send infomation about users from database through email.

@Repository
public interface UserRepository extends JpaRepository<User, Long> {
    @Query(value = "SELECT user from User user where user.reportDate >= :ago order by user.reportDate desc")
    List<User> findAllWithDateAfter(@Param("ago") LocalDate ago);
}

Classic User entity:

@Entity
public class User {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long id;

    private String name;

    /getters and setters, etc

}

Service:

@Service
public class UserService {
    @Autowired
    private UserRepository userRepository;

    public List<User> getUsersForRequiredDays(int days) {
        LocalDate daysAgoDate = LocalDate.now().minusDays(days);
        return badUtmMarksRepository.findAllWithDateAfter(daysAgoDate);
    }
}

But I want to limit this query. If i'll retrieve more than 30 rows from this query, I want to divide it into parts. Instead of sending one email with 100 rows, I want to send four emails (30/30/30/10). I heard about pagination, but I don't know how to apply it for mail.

I'm sending emails on schedule:

public class ScheduledMailSenderService {

    private final MailSenderService mailSender;

    @Scheduled(cron = "${schedule.cron.update}")
    public void send() {
        log.info("Scheduled sending started");
        try {
            mailSender.send();
        } catch (MessagingException | IOException | TemplateException e) {
            log.error("Error occurred while sending email message:: {}", e.toString());
        }
        log.info("Scheduled sending finished");
    }

}

Method send from senderService (in try block above):

public void send() throws MessagingException, IOException, TemplateException {
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        List<User> users = userService.getUsersForRequiredDays(interval); //im using info from db here
        setMimeMessageSettings(mimeMessage, subject, emailTo, reportTable, from);
        mailSender.send(mimeMessage);
    }
takotsubo
  • 666
  • 6
  • 18
  • 1
    A Google Search with "Spring Data Paging" would have helped you too. Checkout this guide [Pagination and Sorting using Spring Data JPA](https://www.baeldung.com/spring-data-jpa-pagination-sorting "Pagination and Sorting using Spring Data JPA") – ImtiazeA Oct 13 '20 at 08:17
  • Does this answer your question? [How to implement pagination in spring boot with hibernate](https://stackoverflow.com/questions/32434058/how-to-implement-pagination-in-spring-boot-with-hibernate) – Eklavya Oct 13 '20 at 08:48

1 Answers1

1

Your repo method should be:

List<User> findAllWithDateAfter(Pageable pageable, @Param("ago") LocalDate ago);

and when calling the method you should do something like :

@Autowired
UserRepository userRepository; 
...
int size;
int index;

*** fill the size and index variables ***
PageRequest pageRequest = PageRequest.of(index, size);

userRepository.findAllWithDateAfter(pageRequest, your-other-parameter);
Morteza
  • 642
  • 7
  • 17