From af4d2c1c5909eb4137a87278d511932a4f736d0d Mon Sep 17 00:00:00 2001 From: Mark Struchkov Date: Fri, 17 May 2019 18:58:52 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A0=D0=B5=D1=84=D0=B0=D0=BA=D1=82=D0=BE?= =?UTF-8?q?=D1=80=D0=B8=D0=BD=D0=B3=20=D0=BE=D1=82=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=BE=D0=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 7 ++ .../org/sadtech/bot/core/domain/Person.java | 39 ++++++---- .../repository/impl/MailRepositoryList.java | 2 +- .../bot/core/sender/email/EmailConfig.java | 77 +++++++++++++++++++ .../bot/core/sender/email/EmailSent.java | 47 +++++++++++ .../sadtech/bot/core/service/MailService.java | 7 ++ .../core/service/impl/MailServiceImpl.java | 72 +++++++++++++++++ 7 files changed, 235 insertions(+), 16 deletions(-) create mode 100644 src/main/java/org/sadtech/bot/core/sender/email/EmailConfig.java create mode 100644 src/main/java/org/sadtech/bot/core/sender/email/EmailSent.java create mode 100644 src/main/java/org/sadtech/bot/core/service/MailService.java create mode 100644 src/main/java/org/sadtech/bot/core/service/impl/MailServiceImpl.java diff --git a/pom.xml b/pom.xml index 7103068..5533462 100644 --- a/pom.xml +++ b/pom.xml @@ -34,6 +34,7 @@ 2.8.5 0.5.13-SNAPSHOT 1.7.26 + 1.4 @@ -48,6 +49,12 @@ ${slf4j.ver} + + javax.mail + mail + ${mail.ver} + + org.apache.logging.log4j log4j-slf4j-impl diff --git a/src/main/java/org/sadtech/bot/core/domain/Person.java b/src/main/java/org/sadtech/bot/core/domain/Person.java index 80970ee..646d076 100644 --- a/src/main/java/org/sadtech/bot/core/domain/Person.java +++ b/src/main/java/org/sadtech/bot/core/domain/Person.java @@ -9,18 +9,7 @@ public class Person { private String lastName; private Integer sex; private String city; - - public Person() { - - } - - public Person(Integer id, String firstName, String lastName, Integer sex, String city) { - this.id = id; - this.firstName = firstName; - this.lastName = lastName; - this.sex = sex; - this.city = city; - } + private String email; public Integer getId() { return id; @@ -62,21 +51,41 @@ public class Person { this.city = city; } + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + @Override public boolean equals(Object o) { if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; + if (!(o instanceof Person)) return false; Person person = (Person) o; return Objects.equals(id, person.id) && Objects.equals(firstName, person.firstName) && Objects.equals(lastName, person.lastName) && Objects.equals(sex, person.sex) && - Objects.equals(city, person.city); + Objects.equals(city, person.city) && + Objects.equals(email, person.email); } @Override public int hashCode() { - return Objects.hash(id, firstName, lastName, sex, city); + return Objects.hash(id, firstName, lastName, sex, city, email); } + @Override + public String toString() { + return "Person{" + + "id=" + id + + ", firstName='" + firstName + '\'' + + ", lastName='" + lastName + '\'' + + ", sex=" + sex + + ", city='" + city + '\'' + + ", email='" + email + '\'' + + '}'; + } } diff --git a/src/main/java/org/sadtech/bot/core/repository/impl/MailRepositoryList.java b/src/main/java/org/sadtech/bot/core/repository/impl/MailRepositoryList.java index 4231cb1..521b024 100644 --- a/src/main/java/org/sadtech/bot/core/repository/impl/MailRepositoryList.java +++ b/src/main/java/org/sadtech/bot/core/repository/impl/MailRepositoryList.java @@ -32,7 +32,7 @@ public class MailRepositoryList implements EventRepository, MailRepository public List getMailByTime(LocalDateTime timeFrom, LocalDateTime timeTo) { ArrayList rezultMails = new ArrayList<>(); for (int i = mails.size() - 1; i >= 0; i--) { - if (!(mails.get(i).getDate().isBefore(timeFrom) || mails.get(i).getDate().isAfter(timeTo))) { + if (!(mails.get(i).getDate().isBefore(timeFrom) || mails.get(i).getDate().isAfter(timeTo)) && mails.get(i).getDate().equals(timeFrom)) { rezultMails.add(this.mails.get(i)); } else if (mails.get(i).getDate().isBefore(timeFrom)) { break; diff --git a/src/main/java/org/sadtech/bot/core/sender/email/EmailConfig.java b/src/main/java/org/sadtech/bot/core/sender/email/EmailConfig.java new file mode 100644 index 0000000..abfbefe --- /dev/null +++ b/src/main/java/org/sadtech/bot/core/sender/email/EmailConfig.java @@ -0,0 +1,77 @@ +package org.sadtech.bot.core.sender.email; + +import java.util.Properties; + +public class EmailConfig { + + private Properties props = new Properties(); + private String username; + private String password; + + private EmailConfig() { + + } + + public Properties getProps() { + return props; + } + + public String getUsername() { + return username; + } + + public String getPassword() { + return password; + } + + public static Builder builder() { + return new EmailConfig().new Builder(); + } + + public class Builder { + + private Builder() { + + } + + public Builder smtpHost(String smtpHost) { + EmailConfig.this.props.setProperty("mail.smtp.host", smtpHost); + return this; + } + + public Builder smtpSocketFactoryPort(Integer smtpSocketFactoryPortort) { + EmailConfig.this.props.setProperty("mail.smtp.socketFactory.port", smtpSocketFactoryPortort.toString()); + return this; + } + + public Builder smtpSocketFactoryClass(String smtpSocketFactoryClass) { + EmailConfig.this.props.setProperty("mail.smtp.socketFactory.class", smtpSocketFactoryClass); + return this; + } + + public Builder smtpAuth(Boolean smtpAuth) { + EmailConfig.this.props.setProperty("mail.smtp.auth", (smtpAuth) ? "true" : "false"); + return this; + } + + public Builder smtpPort(Integer smtpPort) { + EmailConfig.this.props.setProperty("mail.smtp.port", smtpPort.toString()); + return this; + } + + public Builder email(String username) { + EmailConfig.this.username = username; + return this; + } + + public Builder password(String password) { + EmailConfig.this.password = password; + return this; + } + + public EmailConfig build() { + return EmailConfig.this; + } + + } +} diff --git a/src/main/java/org/sadtech/bot/core/sender/email/EmailSent.java b/src/main/java/org/sadtech/bot/core/sender/email/EmailSent.java new file mode 100644 index 0000000..9f20425 --- /dev/null +++ b/src/main/java/org/sadtech/bot/core/sender/email/EmailSent.java @@ -0,0 +1,47 @@ +package org.sadtech.bot.core.sender.email; + +import org.sadtech.bot.core.domain.BoxAnswer; +import org.sadtech.bot.core.exception.MailSendException; +import org.sadtech.bot.core.sender.Sent; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.mail.*; +import javax.mail.internet.InternetAddress; +import javax.mail.internet.MimeMessage; + +public class EmailSent implements Sent { + + private static final Logger log = LoggerFactory.getLogger(Sent.class); + + private final EmailConfig emailConfig; + + public EmailSent(EmailConfig emailConfig) { + this.emailConfig = emailConfig; + } + + @Override + public void send(Integer personId, String htmlText) { + Session session = Session.getDefaultInstance(emailConfig.getProps(), new Authenticator() { + protected PasswordAuthentication getPasswordAuthentication() { + return new PasswordAuthentication(emailConfig.getUsername(), emailConfig.getPassword()); + } + }); + + try { + Message message = new MimeMessage(session); + message.setFrom(new InternetAddress(emailConfig.getUsername())); + message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(emailConfig.getUsername())); + message.setContent(htmlText, "text/html; charset=utf-8"); + Transport.send(message); + } catch (MessagingException e) { + log.error(e.getMessage()); + throw new MailSendException(); + } + } + + @Override + public void send(Integer personId, BoxAnswer boxAnswer) { + + } +} diff --git a/src/main/java/org/sadtech/bot/core/service/MailService.java b/src/main/java/org/sadtech/bot/core/service/MailService.java new file mode 100644 index 0000000..6a0db7e --- /dev/null +++ b/src/main/java/org/sadtech/bot/core/service/MailService.java @@ -0,0 +1,7 @@ +package org.sadtech.bot.core.service; + +import org.sadtech.bot.core.domain.Mail; + +public interface MailService extends EventService { + +} diff --git a/src/main/java/org/sadtech/bot/core/service/impl/MailServiceImpl.java b/src/main/java/org/sadtech/bot/core/service/impl/MailServiceImpl.java new file mode 100644 index 0000000..4cb0d04 --- /dev/null +++ b/src/main/java/org/sadtech/bot/core/service/impl/MailServiceImpl.java @@ -0,0 +1,72 @@ +package org.sadtech.bot.core.service.impl; + +import org.sadtech.bot.core.domain.Mail; +import org.sadtech.bot.core.repository.MailRepository; +import org.sadtech.bot.core.repository.impl.MailRepositoryList; +import org.sadtech.bot.core.service.MailService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.time.LocalDateTime; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +public class MailServiceImpl implements MailService { + + private static final Logger log = LoggerFactory.getLogger(MailServiceImpl.class); + + private final MailRepository mailRepository; + + public MailServiceImpl() { + this.mailRepository = new MailRepositoryList(); + } + + public MailServiceImpl(MailRepository mailRepository) { + this.mailRepository = mailRepository; + } + + @Override + public void add(Mail mail) { + mailRepository.add(mail); + log.info("Сообщение добавлено в репозиторий"); + log.info(mail.toString()); + } + + @Override + public List getFirstEventByTime(LocalDateTime timeFrom, LocalDateTime timeTo) { + log.info("Запрошены сообщения " + timeFrom + " - " + timeTo); + List mails = mailRepository.getMailByTime(timeFrom, timeTo); + Set people = new HashSet<>(); + List returnMails = new ArrayList<>(); + for (int i = mails.size() - 1; i >= 0; i--) { + if (!people.contains(mails.get(i).getPersonId())) { + returnMails.add(mails.get(i)); + people.add(mails.get(i).getPersonId()); + } + } + return returnMails; + } + + @Override + public List getLastEventByTime(LocalDateTime timeFrom, LocalDateTime timeTo) { + List mails = mailRepository.getMailByTime(timeFrom, timeTo); + Set people = new HashSet<>(); + List returnMails = new ArrayList<>(); + for (Mail mail : mails) { + if (!people.contains(mail.getPersonId())) { + returnMails.add(mail); + people.add(mail.getPersonId()); + } + } + return returnMails; + } + + @Override + public List getEvent(LocalDateTime timeFrom, LocalDateTime timeTo) { + log.info("Запрос на получение сообщений в интервале от " + timeFrom + " до " + timeTo); + return mailRepository.getMailByTime(timeFrom, timeTo); + } + +}