diff --git a/pom.xml b/pom.xml index add015f..d58ffb5 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ org.sadtech.bot bot-core - 0.5.1-RELEASE + 0.6.0-RELEASE jar @@ -33,7 +33,8 @@ 2.8.5 0.5.13-SNAPSHOT - 1.2.17 + 1.7.26 + 1.4 @@ -43,9 +44,15 @@ ${gson.ver} - log4j - log4j - ${log4j.ver} + org.slf4j + slf4j-api + ${slf4j.ver} + + + + javax.mail + mail + ${mail.ver} diff --git a/src/main/java/org/sadtech/bot/core/domain/BoxAnswer.java b/src/main/java/org/sadtech/bot/core/domain/BoxAnswer.java index e944b40..3640594 100644 --- a/src/main/java/org/sadtech/bot/core/domain/BoxAnswer.java +++ b/src/main/java/org/sadtech/bot/core/domain/BoxAnswer.java @@ -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; diff --git a/src/main/java/org/sadtech/bot/core/domain/Content.java b/src/main/java/org/sadtech/bot/core/domain/Content.java index 96ae304..1059a82 100644 --- a/src/main/java/org/sadtech/bot/core/domain/Content.java +++ b/src/main/java/org/sadtech/bot/core/domain/Content.java @@ -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); } } diff --git a/src/main/java/org/sadtech/bot/core/domain/Mail.java b/src/main/java/org/sadtech/bot/core/domain/Mail.java index 0f5e67e..a69d39f 100644 --- a/src/main/java/org/sadtech/bot/core/domain/Mail.java +++ b/src/main/java/org/sadtech/bot/core/domain/Mail.java @@ -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 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 getAttachments() { + return attachments; + } + + public void setAttachments(List 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 + + '}'; } } diff --git a/src/main/java/org/sadtech/bot/core/domain/MailSend.java b/src/main/java/org/sadtech/bot/core/domain/MailSend.java deleted file mode 100644 index 8238818..0000000 --- a/src/main/java/org/sadtech/bot/core/domain/MailSend.java +++ /dev/null @@ -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 + - '}'; - } - -} 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/domain/attachment/Attachment.java b/src/main/java/org/sadtech/bot/core/domain/attachment/Attachment.java new file mode 100644 index 0000000..1c7de29 --- /dev/null +++ b/src/main/java/org/sadtech/bot/core/domain/attachment/Attachment.java @@ -0,0 +1,10 @@ +package org.sadtech.bot.core.domain.attachment; + +public abstract class Attachment { + + AttachmentType type; + + public AttachmentType getType() { + return type; + } +} diff --git a/src/main/java/org/sadtech/bot/core/domain/attachment/AttachmentType.java b/src/main/java/org/sadtech/bot/core/domain/attachment/AttachmentType.java new file mode 100644 index 0000000..afce805 --- /dev/null +++ b/src/main/java/org/sadtech/bot/core/domain/attachment/AttachmentType.java @@ -0,0 +1,5 @@ +package org.sadtech.bot.core.domain.attachment; + +public enum AttachmentType { + AUDIO_MESSAGE +} diff --git a/src/main/java/org/sadtech/bot/core/domain/attachment/AudioMessage.java b/src/main/java/org/sadtech/bot/core/domain/attachment/AudioMessage.java new file mode 100644 index 0000000..ef6583a --- /dev/null +++ b/src/main/java/org/sadtech/bot/core/domain/attachment/AudioMessage.java @@ -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 + + '}'; + } +} diff --git a/src/main/java/org/sadtech/bot/core/domain/keyboard/ButtonColor.java b/src/main/java/org/sadtech/bot/core/domain/keyboard/ButtonColor.java new file mode 100644 index 0000000..49263a2 --- /dev/null +++ b/src/main/java/org/sadtech/bot/core/domain/keyboard/ButtonColor.java @@ -0,0 +1,7 @@ +package org.sadtech.bot.core.domain.keyboard; + +public enum ButtonColor { + + PRIMARY, DEFAULT, NEGATIVE, POSITIVE + +} diff --git a/src/main/java/org/sadtech/bot/core/domain/keyboard/KeyBoard.java b/src/main/java/org/sadtech/bot/core/domain/keyboard/KeyBoard.java new file mode 100644 index 0000000..12dcf6e --- /dev/null +++ b/src/main/java/org/sadtech/bot/core/domain/keyboard/KeyBoard.java @@ -0,0 +1,48 @@ +package org.sadtech.bot.core.domain.keyboard; + +import java.util.ArrayList; +import java.util.List; + +public class KeyBoard { + + private List keyBoardLines = new ArrayList<>(); + private boolean oneTime = true; + + private KeyBoard() { + + } + + public List 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; + } + + } +} diff --git a/src/main/java/org/sadtech/bot/core/domain/keyboard/KeyBoardButton.java b/src/main/java/org/sadtech/bot/core/domain/keyboard/KeyBoardButton.java new file mode 100644 index 0000000..f59dff1 --- /dev/null +++ b/src/main/java/org/sadtech/bot/core/domain/keyboard/KeyBoardButton.java @@ -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); + } +} diff --git a/src/main/java/org/sadtech/bot/core/domain/keyboard/KeyBoardLine.java b/src/main/java/org/sadtech/bot/core/domain/keyboard/KeyBoardLine.java new file mode 100644 index 0000000..236ccc9 --- /dev/null +++ b/src/main/java/org/sadtech/bot/core/domain/keyboard/KeyBoardLine.java @@ -0,0 +1,41 @@ +package org.sadtech.bot.core.domain.keyboard; + +import java.util.ArrayList; +import java.util.List; + +public class KeyBoardLine { + + private List keyBoardButtons = new ArrayList<>(); + + public KeyBoardLine() { + + } + + public KeyBoardLine(List keyBoardButtons) { + this.keyBoardButtons = keyBoardButtons; + } + + public List 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; + } + } +} diff --git a/src/main/java/org/sadtech/bot/core/exception/MailSendException.java b/src/main/java/org/sadtech/bot/core/exception/MailSendException.java new file mode 100644 index 0000000..f627380 --- /dev/null +++ b/src/main/java/org/sadtech/bot/core/exception/MailSendException.java @@ -0,0 +1,5 @@ +package org.sadtech.bot.core.exception; + +public class MailSendException extends RuntimeException { + +} diff --git a/src/main/java/org/sadtech/bot/core/filter/Filter.java b/src/main/java/org/sadtech/bot/core/filter/Filter.java new file mode 100644 index 0000000..b64ab2e --- /dev/null +++ b/src/main/java/org/sadtech/bot/core/filter/Filter.java @@ -0,0 +1,9 @@ +package org.sadtech.bot.core.filter; + +import org.sadtech.bot.core.domain.Content; + +public interface Filter { + + void doFilter(T content); + +} diff --git a/src/main/java/org/sadtech/bot/core/repository/MailRepository.java b/src/main/java/org/sadtech/bot/core/repository/MailRepository.java index 0ae005f..697cb6f 100644 --- a/src/main/java/org/sadtech/bot/core/repository/MailRepository.java +++ b/src/main/java/org/sadtech/bot/core/repository/MailRepository.java @@ -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 getMailByTime(Integer timeFrom, Integer timeTo); + List getMailByTime(LocalDateTime timeFrom, LocalDateTime timeTo); } 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 6012f54..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 @@ -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, MailRepository { - private final List messages = new ArrayList<>(); + private final List 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 getEventQueue() { - return new ConcurrentLinkedQueue<>(messages); + return new ConcurrentLinkedQueue<>(mails); } - public List getMailByTime(Integer timeFrom, Integer timeTo) { - ArrayList 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 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)) && 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; } diff --git a/src/main/java/org/sadtech/bot/core/sender/Sent.java b/src/main/java/org/sadtech/bot/core/sender/Sent.java index 63a33ba..120e56c 100644 --- a/src/main/java/org/sadtech/bot/core/sender/Sent.java +++ b/src/main/java/org/sadtech/bot/core/sender/Sent.java @@ -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); } 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/EventService.java b/src/main/java/org/sadtech/bot/core/service/EventService.java index b62c1a9..fcb7a1e 100644 --- a/src/main/java/org/sadtech/bot/core/service/EventService.java +++ b/src/main/java/org/sadtech/bot/core/service/EventService.java @@ -1,15 +1,16 @@ package org.sadtech.bot.core.service; +import java.time.LocalDateTime; import java.util.List; public interface EventService { void add(T event); - List getEvent(Integer timeFrom, Integer timeTo); + List getEvent(LocalDateTime timeFrom, LocalDateTime timeTo); - List getFirstEventByTime(Integer timeFrom, Integer timeTo); + List getFirstEventByTime(LocalDateTime timeFrom, LocalDateTime timeTo); - List getLastEventByTime(Integer timeFrom, Integer timeTo); + List getLastEventByTime(LocalDateTime timeFrom, LocalDateTime timeTo); } diff --git a/src/main/java/org/sadtech/bot/core/service/KeyBoards.java b/src/main/java/org/sadtech/bot/core/service/KeyBoards.java new file mode 100644 index 0000000..a9b54d7 --- /dev/null +++ b/src/main/java/org/sadtech/bot/core/service/KeyBoards.java @@ -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 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 keyBoardButtons) { + KeyBoard.Builder keyBoard = KeyBoard.builder().oneTime(true); + for (KeyBoardButton keyBoardButton : keyBoardButtons) { + keyBoard.lineKeyBoard(KeyBoardLine.builder().setButtonKeyBoard(keyBoardButton).build()); + } + return keyBoard.build(); + } +} 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); + } + +} diff --git a/src/main/java/org/sadtech/bot/core/service/impl/RawEventServiceImpl.java b/src/main/java/org/sadtech/bot/core/service/impl/RawEventServiceImpl.java index 7232f89..c592287 100644 --- a/src/main/java/org/sadtech/bot/core/service/impl/RawEventServiceImpl.java +++ b/src/main/java/org/sadtech/bot/core/service/impl/RawEventServiceImpl.java @@ -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;