Рефакторинг отправок
This commit is contained in:
parent
b94ca5827f
commit
af4d2c1c59
7
pom.xml
7
pom.xml
@ -34,6 +34,7 @@
|
||||
<gson.ver>2.8.5</gson.ver>
|
||||
<vksdk.ver>0.5.13-SNAPSHOT</vksdk.ver>
|
||||
<slf4j.ver>1.7.26</slf4j.ver>
|
||||
<mail.ver>1.4</mail.ver>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
@ -48,6 +49,12 @@
|
||||
<version>${slf4j.ver}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>javax.mail</groupId>
|
||||
<artifactId>mail</artifactId>
|
||||
<version>${mail.ver}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.logging.log4j</groupId>
|
||||
<artifactId>log4j-slf4j-impl</artifactId>
|
||||
|
@ -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 + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
|
@ -32,7 +32,7 @@ public class MailRepositoryList implements EventRepository<Mail>, MailRepository
|
||||
public List<Mail> getMailByTime(LocalDateTime timeFrom, LocalDateTime timeTo) {
|
||||
ArrayList<Mail> 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;
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -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) {
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package org.sadtech.bot.core.service;
|
||||
|
||||
import org.sadtech.bot.core.domain.Mail;
|
||||
|
||||
public interface MailService extends EventService<Mail> {
|
||||
|
||||
}
|
@ -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<Mail> getFirstEventByTime(LocalDateTime timeFrom, LocalDateTime timeTo) {
|
||||
log.info("Запрошены сообщения " + timeFrom + " - " + timeTo);
|
||||
List<Mail> mails = mailRepository.getMailByTime(timeFrom, timeTo);
|
||||
Set<Integer> people = new HashSet<>();
|
||||
List<Mail> 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<Mail> getLastEventByTime(LocalDateTime timeFrom, LocalDateTime timeTo) {
|
||||
List<Mail> mails = mailRepository.getMailByTime(timeFrom, timeTo);
|
||||
Set<Integer> people = new HashSet<>();
|
||||
List<Mail> returnMails = new ArrayList<>();
|
||||
for (Mail mail : mails) {
|
||||
if (!people.contains(mail.getPersonId())) {
|
||||
returnMails.add(mail);
|
||||
people.add(mail.getPersonId());
|
||||
}
|
||||
}
|
||||
return returnMails;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Mail> getEvent(LocalDateTime timeFrom, LocalDateTime timeTo) {
|
||||
log.info("Запрос на получение сообщений в интервале от " + timeFrom + " до " + timeTo);
|
||||
return mailRepository.getMailByTime(timeFrom, timeTo);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user