Merge branch 'release/release-0.6.0'

This commit is contained in:
Mark Struchkov 2019-05-18 13:12:59 +03:00
commit 01f28fcce0
25 changed files with 634 additions and 126 deletions

17
pom.xml
View File

@ -6,7 +6,7 @@
<groupId>org.sadtech.bot</groupId>
<artifactId>bot-core</artifactId>
<version>0.5.1-RELEASE</version>
<version>0.6.0-RELEASE</version>
<packaging>jar</packaging>
<build>
@ -33,7 +33,8 @@
<properties>
<gson.ver>2.8.5</gson.ver>
<vksdk.ver>0.5.13-SNAPSHOT</vksdk.ver>
<log4j.ver>1.2.17</log4j.ver>
<slf4j.ver>1.7.26</slf4j.ver>
<mail.ver>1.4</mail.ver>
</properties>
<dependencies>
@ -43,9 +44,15 @@
<version>${gson.ver}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.ver}</version>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.ver}</version>
</dependency>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>${mail.ver}</version>
</dependency>
<dependency>

View File

@ -1,11 +1,13 @@
package org.sadtech.bot.core.domain;
import org.sadtech.bot.core.domain.keyboard.KeyBoard;
import java.util.Objects;
public class BoxAnswer {
private String message;
private String keyboard;
private KeyBoard keyboard;
private Float lat;
private Float aLong;
private Integer stickerId;
@ -14,7 +16,7 @@ public class BoxAnswer {
}
public BoxAnswer(BoxAnswer target) {
private BoxAnswer(BoxAnswer target) {
if (target != null) {
this.message = target.getMessage();
this.keyboard = target.getKeyboard();
@ -33,11 +35,11 @@ public class BoxAnswer {
this.message = message;
}
public String getKeyboard() {
public KeyBoard getKeyboard() {
return keyboard;
}
public void setKeyboard(String keyboard) {
public void setKeyboard(KeyBoard keyboard) {
this.keyboard = keyboard;
}
@ -69,6 +71,45 @@ public class BoxAnswer {
return new BoxAnswer(this);
}
public static Builder builder() {
return new BoxAnswer().new Builder();
}
public class Builder {
private Builder() {
}
public Builder message(String message) {
BoxAnswer.this.message = message;
return this;
}
public Builder keyBoard(KeyBoard keyBoard) {
BoxAnswer.this.keyboard = keyBoard;
return this;
}
public Builder lat(Float lat) {
BoxAnswer.this.lat = lat;
return this;
}
public Builder aLong(Float aLong) {
BoxAnswer.this.aLong = aLong;
return this;
}
public Builder steckerId(Integer stickerId) {
BoxAnswer.this.stickerId = stickerId;
return this;
}
public BoxAnswer build() {
return BoxAnswer.this;
}
}
@Override
public boolean equals(Object o) {
if (this == o) return true;

View File

@ -5,6 +5,7 @@ import java.util.Objects;
public abstract class Content {
private Integer personId;
private String message;
public Content() {
@ -12,6 +13,7 @@ public abstract class Content {
public Content(Content source) {
this.personId = source.getPersonId();
this.message = source.getMessage();
}
public Integer getPersonId() {
@ -22,16 +24,25 @@ public abstract class Content {
this.personId = personId;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Content)) return false;
Content content = (Content) o;
return Objects.equals(personId, content.personId);
return Objects.equals(personId, content.personId) &&
Objects.equals(message, content.message);
}
@Override
public int hashCode() {
return Objects.hash(personId);
return Objects.hash(personId, message);
}
}

View File

@ -1,12 +1,16 @@
package org.sadtech.bot.core.domain;
import org.sadtech.bot.core.domain.attachment.Attachment;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
public class Mail extends Content {
private Integer id;
private Integer date;
private String message;
private LocalDateTime date;
private List<Attachment> attachments;
public Mail() {
@ -16,7 +20,6 @@ public class Mail extends Content {
super(source);
this.id = source.getId();
this.date = source.getDate();
this.message = source.getMessage();
}
public Integer getId() {
@ -27,27 +30,27 @@ public class Mail extends Content {
this.id = id;
}
public Integer getDate() {
public LocalDateTime getDate() {
return date;
}
public void setDate(Integer date) {
public void setDate(LocalDateTime date) {
this.date = date;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Mail prototype() {
return new Mail(this);
}
public List<Attachment> getAttachments() {
return attachments;
}
public void setAttachments(List<Attachment> attachments) {
this.attachments = attachments;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
@ -56,11 +59,20 @@ public class Mail extends Content {
Mail mail = (Mail) o;
return Objects.equals(id, mail.id) &&
Objects.equals(date, mail.date) &&
Objects.equals(message, mail.message);
Objects.equals(attachments, mail.attachments);
}
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), id, date, message);
return Objects.hash(super.hashCode(), id, date, attachments);
}
@Override
public String toString() {
return "Mail{" +
"id=" + id +
", date=" + date +
", attachments=" + attachments +
'}';
}
}

View File

@ -1,66 +0,0 @@
package org.sadtech.bot.core.domain;
public class MailSend {
private String message;
private String keyboard;
private Float lat;
private Float aLong;
private Integer stickerId;
public MailSend() {
}
public String getKeyboard() {
return keyboard;
}
public void setKeyboard(String keyboard) {
this.keyboard = keyboard;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Float getLat() {
return lat;
}
public void setLat(Float lat) {
this.lat = lat;
}
public Float getaLong() {
return aLong;
}
public void setaLong(Float aLong) {
this.aLong = aLong;
}
public Integer getStickerId() {
return stickerId;
}
public void setStickerId(Integer stickerId) {
this.stickerId = stickerId;
}
@Override
public String toString() {
return "MailSend{" +
"message='" + message + '\'' +
", keyboard='" + keyboard + '\'' +
", lat=" + lat +
", aLong=" + aLong +
", stickerId=" + stickerId +
'}';
}
}

View File

@ -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 + '\'' +
'}';
}
}

View File

@ -0,0 +1,10 @@
package org.sadtech.bot.core.domain.attachment;
public abstract class Attachment {
AttachmentType type;
public AttachmentType getType() {
return type;
}
}

View File

@ -0,0 +1,5 @@
package org.sadtech.bot.core.domain.attachment;
public enum AttachmentType {
AUDIO_MESSAGE
}

View File

@ -0,0 +1,42 @@
package org.sadtech.bot.core.domain.attachment;
import java.net.URL;
import java.util.Objects;
public class AudioMessage extends Attachment {
private URL linkOdd;
public AudioMessage() {
type = AttachmentType.AUDIO_MESSAGE;
}
public URL getLinkOdd() {
return linkOdd;
}
public void setLinkOdd(URL linkOdd) {
this.linkOdd = linkOdd;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof AudioMessage)) return false;
AudioMessage that = (AudioMessage) o;
return Objects.equals(linkOdd, that.linkOdd);
}
@Override
public int hashCode() {
return Objects.hash(linkOdd);
}
@Override
public String toString() {
return "AudioMessage{" +
"linkOdd=" + linkOdd +
", type=" + type +
'}';
}
}

View File

@ -0,0 +1,7 @@
package org.sadtech.bot.core.domain.keyboard;
public enum ButtonColor {
PRIMARY, DEFAULT, NEGATIVE, POSITIVE
}

View File

@ -0,0 +1,48 @@
package org.sadtech.bot.core.domain.keyboard;
import java.util.ArrayList;
import java.util.List;
public class KeyBoard {
private List<KeyBoardLine> keyBoardLines = new ArrayList<>();
private boolean oneTime = true;
private KeyBoard() {
}
public List<KeyBoardLine> getKeyBoardLines() {
return keyBoardLines;
}
public boolean isOneTime() {
return oneTime;
}
public static Builder builder() {
return new KeyBoard().new Builder();
}
public class Builder {
private Builder() {
}
public Builder lineKeyBoard(KeyBoardLine keyBoardLine) {
KeyBoard.this.keyBoardLines.add(keyBoardLine);
return this;
}
public Builder oneTime(boolean oneTime) {
KeyBoard.this.oneTime = oneTime;
return this;
}
public KeyBoard build() {
return KeyBoard.this;
}
}
}

View File

@ -0,0 +1,81 @@
package org.sadtech.bot.core.domain.keyboard;
import com.google.gson.JsonObject;
import java.util.Objects;
public class KeyBoardButton {
private String payload;
private String label;
private ButtonColor color = ButtonColor.DEFAULT;
public KeyBoardButton() {
}
public String getPayload() {
return payload;
}
public String getLabel() {
return label;
}
public ButtonColor getColor() {
return color;
}
private JsonObject generateAction() {
JsonObject action = new JsonObject();
action.addProperty("payload", payload);
action.addProperty("label", label);
return action;
}
public static Builder builder() {
return new KeyBoardButton().new Builder();
}
public class Builder {
private Builder() {
}
public Builder color(ButtonColor color) {
KeyBoardButton.this.color = color;
return this;
}
public Builder label(String label) {
KeyBoardButton.this.label = label;
return this;
}
public Builder payload(String payload) {
KeyBoardButton.this.payload = payload;
return this;
}
public KeyBoardButton build() {
return KeyBoardButton.this;
}
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof KeyBoardButton)) return false;
KeyBoardButton that = (KeyBoardButton) o;
return Objects.equals(payload, that.payload) &&
Objects.equals(label, that.label) &&
color == that.color;
}
@Override
public int hashCode() {
return Objects.hash(payload, label, color);
}
}

View File

@ -0,0 +1,41 @@
package org.sadtech.bot.core.domain.keyboard;
import java.util.ArrayList;
import java.util.List;
public class KeyBoardLine {
private List<KeyBoardButton> keyBoardButtons = new ArrayList<>();
public KeyBoardLine() {
}
public KeyBoardLine(List<KeyBoardButton> keyBoardButtons) {
this.keyBoardButtons = keyBoardButtons;
}
public List<KeyBoardButton> getKeyBoardButtons() {
return keyBoardButtons;
}
public static Builder builder() {
return new KeyBoardLine().new Builder();
}
public class Builder {
private Builder() {
}
public Builder setButtonKeyBoard(KeyBoardButton keyBoardButton) {
KeyBoardLine.this.keyBoardButtons.add(keyBoardButton);
return this;
}
public KeyBoardLine build() {
return KeyBoardLine.this;
}
}
}

View File

@ -0,0 +1,5 @@
package org.sadtech.bot.core.exception;
public class MailSendException extends RuntimeException {
}

View File

@ -0,0 +1,9 @@
package org.sadtech.bot.core.filter;
import org.sadtech.bot.core.domain.Content;
public interface Filter<T extends Content> {
void doFilter(T content);
}

View File

@ -2,12 +2,13 @@ package org.sadtech.bot.core.repository;
import org.sadtech.bot.core.domain.Mail;
import java.time.LocalDateTime;
import java.util.List;
public interface MailRepository {
void add(Mail mail);
List<Mail> getMailByTime(Integer timeFrom, Integer timeTo);
List<Mail> getMailByTime(LocalDateTime timeFrom, LocalDateTime timeTo);
}

View File

@ -4,6 +4,7 @@ import org.sadtech.bot.core.domain.Mail;
import org.sadtech.bot.core.repository.EventRepository;
import org.sadtech.bot.core.repository.MailRepository;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Queue;
@ -11,33 +12,33 @@ import java.util.concurrent.ConcurrentLinkedQueue;
public class MailRepositoryList implements EventRepository<Mail>, MailRepository {
private final List<Mail> messages = new ArrayList<>();
private final List<Mail> mails = new ArrayList<>();
@Override
public void add(Mail mail) {
messages.add(mail);
mails.add(mail);
}
@Override
public void cleanAll() {
messages.clear();
mails.clear();
}
@Override
public Queue<Mail> getEventQueue() {
return new ConcurrentLinkedQueue<>(messages);
return new ConcurrentLinkedQueue<>(mails);
}
public List<Mail> getMailByTime(Integer timeFrom, Integer timeTo) {
ArrayList<Mail> mails = new ArrayList<>();
for (int i = messages.size() - 1; i >= 0; i--) {
if (messages.get(i).getDate() >= timeFrom && messages.get(i).getDate() < timeTo) {
mails.add(messages.get(i));
} else if (messages.get(i).getDate() < timeFrom) {
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)) && mails.get(i).getDate().equals(timeFrom)) {
rezultMails.add(this.mails.get(i));
} else if (mails.get(i).getDate().isBefore(timeFrom)) {
break;
}
}
return mails;
return rezultMails;
}

View File

@ -4,8 +4,8 @@ import org.sadtech.bot.core.domain.BoxAnswer;
public interface Sent {
void send(Integer idPerson, String message);
void send(Integer personId, String message);
void send(Integer idPerson, BoxAnswer boxAnswer);
void send(Integer personId, BoxAnswer boxAnswer);
}

View File

@ -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;
}
}
}

View File

@ -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) {
}
}

View File

@ -1,15 +1,16 @@
package org.sadtech.bot.core.service;
import java.time.LocalDateTime;
import java.util.List;
public interface EventService<T> {
void add(T event);
List<T> getEvent(Integer timeFrom, Integer timeTo);
List<T> getEvent(LocalDateTime timeFrom, LocalDateTime timeTo);
List<T> getFirstEventByTime(Integer timeFrom, Integer timeTo);
List<T> getFirstEventByTime(LocalDateTime timeFrom, LocalDateTime timeTo);
List<T> getLastEventByTime(Integer timeFrom, Integer timeTo);
List<T> getLastEventByTime(LocalDateTime timeFrom, LocalDateTime timeTo);
}

View File

@ -0,0 +1,39 @@
package org.sadtech.bot.core.service;
import org.sadtech.bot.core.domain.keyboard.ButtonColor;
import org.sadtech.bot.core.domain.keyboard.KeyBoard;
import org.sadtech.bot.core.domain.keyboard.KeyBoardButton;
import org.sadtech.bot.core.domain.keyboard.KeyBoardLine;
import java.util.List;
public class KeyBoards {
private KeyBoards() {
throw new IllegalStateException();
}
public static KeyBoard keyBoardYesNo() {
KeyBoardButton yesButton = KeyBoardButton.builder().color(ButtonColor.POSITIVE).label("Да").payload("{\"button\": \"yes\"}").build();
KeyBoardButton noButton = KeyBoardButton.builder().color(ButtonColor.NEGATIVE).label("Нет").payload("{\"button\": \"no\"}").build();
KeyBoardLine keyBoardLine = KeyBoardLine.builder().setButtonKeyBoard(yesButton).setButtonKeyBoard(noButton).build();
return KeyBoard.builder().lineKeyBoard(keyBoardLine).oneTime(true).build();
}
public static KeyBoard verticalMenuString(List<String> labelButtons) {
KeyBoard.Builder keyBoard = KeyBoard.builder().oneTime(true);
for (String labelButton : labelButtons) {
KeyBoardButton keyBoardButton = KeyBoardButton.builder().label(labelButton).payload("{\"button\": \"" + labelButton + "\"}").build();
keyBoard.lineKeyBoard(KeyBoardLine.builder().setButtonKeyBoard(keyBoardButton).build());
}
return keyBoard.build();
}
public static KeyBoard verticalMenuButton(List<KeyBoardButton> keyBoardButtons) {
KeyBoard.Builder keyBoard = KeyBoard.builder().oneTime(true);
for (KeyBoardButton keyBoardButton : keyBoardButtons) {
keyBoard.lineKeyBoard(KeyBoardLine.builder().setButtonKeyBoard(keyBoardButton).build());
}
return keyBoard.build();
}
}

View File

@ -0,0 +1,7 @@
package org.sadtech.bot.core.service;
import org.sadtech.bot.core.domain.Mail;
public interface MailService extends EventService<Mail> {
}

View File

@ -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);
}
}

View File

@ -1,15 +1,16 @@
package org.sadtech.bot.core.service.impl;
import com.google.gson.JsonObject;
import org.apache.log4j.Logger;
import org.sadtech.bot.core.repository.EventRepository;
import org.sadtech.bot.core.service.RawEventService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Queue;
public class RawEventServiceImpl implements RawEventService {
private static final Logger log = Logger.getLogger(RawEventServiceImpl.class);
private static final Logger log = LoggerFactory.getLogger(RawEventServiceImpl.class);
private final EventRepository eventRepository;