Новый тип контента - сообщения в обсуждениях

This commit is contained in:
Mark Struchkov 2019-05-30 15:48:32 +03:00
parent c5c3b87b4b
commit 8cf244bb1c
17 changed files with 260 additions and 98 deletions

View File

@ -6,7 +6,7 @@
<groupId>org.sadtech.bot</groupId> <groupId>org.sadtech.bot</groupId>
<artifactId>bot-core</artifactId> <artifactId>bot-core</artifactId>
<version>0.6.1-RELEASE</version> <version>0.6.2-SNAPSHOT</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<build> <build>

View File

@ -0,0 +1,41 @@
package org.sadtech.bot.core.domain.content;
import java.util.Objects;
public class BoardComment extends Content {
private Integer topicId;
public BoardComment() {
type = ContentType.BOARD_COMMENT;
}
public Integer getTopicId() {
return topicId;
}
public void setTopicId(Integer topicId) {
this.topicId = topicId;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof BoardComment)) return false;
if (!super.equals(o)) return false;
BoardComment that = (BoardComment) o;
return Objects.equals(topicId, that.topicId);
}
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), topicId);
}
@Override
public String toString() {
return "BoardComment{" +
"topicId=" + topicId +
"} " + super.toString();
}
}

View File

@ -1,9 +1,13 @@
package org.sadtech.bot.core.domain.content; package org.sadtech.bot.core.domain.content;
import java.time.LocalDateTime;
import java.util.Objects; import java.util.Objects;
public abstract class Content { public abstract class Content {
private Integer id;
protected ContentType type;
private LocalDateTime createDate;
private Integer personId; private Integer personId;
private String message; private String message;
@ -14,6 +18,9 @@ public abstract class Content {
public Content(Content source) { public Content(Content source) {
this.personId = source.getPersonId(); this.personId = source.getPersonId();
this.message = source.getMessage(); this.message = source.getMessage();
this.createDate = source.getCreateDate();
this.id = source.getPersonId();
this.type = source.getType();
} }
public Integer getPersonId() { public Integer getPersonId() {
@ -32,17 +39,55 @@ public abstract class Content {
this.message = message; this.message = message;
} }
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public LocalDateTime getCreateDate() {
return createDate;
}
public void setCreateDate(LocalDateTime createDate) {
this.createDate = createDate;
}
public ContentType getType() {
return type;
}
public void setType(ContentType type) {
this.type = type;
}
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (this == o) return true; if (this == o) return true;
if (!(o instanceof Content)) return false; if (!(o instanceof Content)) return false;
Content content = (Content) o; Content content = (Content) o;
return Objects.equals(personId, content.personId) && return Objects.equals(id, content.id) &&
type == content.type &&
Objects.equals(createDate, content.createDate) &&
Objects.equals(personId, content.personId) &&
Objects.equals(message, content.message); Objects.equals(message, content.message);
} }
@Override @Override
public int hashCode() { public int hashCode() {
return Objects.hash(personId, message); return Objects.hash(id, type, createDate, personId, message);
}
@Override
public String toString() {
return "Content{" +
"id=" + id +
", type=" + type +
", createDate=" + createDate +
", personId=" + personId +
", message='" + message + '\'' +
'}';
} }
} }

View File

@ -0,0 +1,7 @@
package org.sadtech.bot.core.domain.content;
public enum ContentType {
MAIL, BOARD_COMMENT
}

View File

@ -2,40 +2,21 @@ package org.sadtech.bot.core.domain.content;
import org.sadtech.bot.core.domain.content.attachment.Attachment; import org.sadtech.bot.core.domain.content.attachment.Attachment;
import java.time.LocalDateTime;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
public class Mail extends Content { public class Mail extends Content {
private Integer id;
private LocalDateTime date;
private List<Attachment> attachments; private List<Attachment> attachments;
public Mail() {
public Mail() {
type = ContentType.MAIL;
} }
public Mail(Mail source) { public Mail(Mail source) {
super(source); super(source);
this.id = source.getId(); this.attachments = source.getAttachments();
this.date = source.getDate();
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public LocalDateTime getDate() {
return date;
}
public void setDate(LocalDateTime date) {
this.date = date;
} }
@ -57,22 +38,18 @@ public class Mail extends Content {
if (!(o instanceof Mail)) return false; if (!(o instanceof Mail)) return false;
if (!super.equals(o)) return false; if (!super.equals(o)) return false;
Mail mail = (Mail) o; Mail mail = (Mail) o;
return Objects.equals(id, mail.id) && return Objects.equals(attachments, mail.attachments);
Objects.equals(date, mail.date) &&
Objects.equals(attachments, mail.attachments);
} }
@Override @Override
public int hashCode() { public int hashCode() {
return Objects.hash(super.hashCode(), id, date, attachments); return Objects.hash(super.hashCode(), attachments);
} }
@Override @Override
public String toString() { public String toString() {
return "Mail{" + return "Mail{" +
"id=" + id + "attachments=" + attachments +
", date=" + date + "} " + super.toString();
", attachments=" + attachments +
'}';
} }
} }

View File

@ -0,0 +1,14 @@
package org.sadtech.bot.core.repository;
import org.sadtech.bot.core.domain.content.Content;
import java.time.LocalDateTime;
import java.util.List;
public interface ContentRepository<T extends Content> {
Integer add(T content);
List<T> findByTime(LocalDateTime timeFrom, LocalDateTime timeTo);
}

View File

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

View File

@ -0,0 +1,36 @@
package org.sadtech.bot.core.repository.impl;
import org.sadtech.bot.core.domain.content.BoardComment;
import org.sadtech.bot.core.repository.ContentRepository;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class BoardCommentRepositoryMap implements ContentRepository<BoardComment> {
private final Map<Integer, BoardComment> saveMap = new HashMap<>();
private Integer count = 0;
@Override
public Integer add(BoardComment comment) {
comment.setId(count);
saveMap.put(count, comment);
return count++;
}
@Override
public List<BoardComment> findByTime(LocalDateTime timeFrom, LocalDateTime timeTo) {
ArrayList<BoardComment> rezultMails = new ArrayList<>();
for (int i = saveMap.size() - 1; i >= 0; i--) {
if (!(saveMap.get(i).getCreateDate().isBefore(timeFrom) || saveMap.get(i).getCreateDate().isAfter(timeTo)) && saveMap.get(i).getCreateDate().equals(timeFrom)) {
rezultMails.add(this.saveMap.get(i));
} else if (saveMap.get(i).getCreateDate().isBefore(timeFrom)) {
break;
}
}
return rezultMails;
}
}

View File

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

View File

@ -0,0 +1,6 @@
package org.sadtech.bot.core.service;
import org.sadtech.bot.core.domain.content.BoardComment;
public interface BoardCommentService extends ContentService<BoardComment> {
}

View File

@ -5,13 +5,11 @@ import org.sadtech.bot.core.domain.content.Content;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.List; import java.util.List;
public interface EventService<T extends Content> { public interface ContentService<T extends Content> {
void add(T event); void add(T event);
List<T> getEvent(LocalDateTime timeFrom, LocalDateTime timeTo); List<T> getByTime(LocalDateTime timeFrom, LocalDateTime timeTo);
List<T> getFirstEventByTime(LocalDateTime timeFrom, LocalDateTime timeTo);
List<T> getLastEventByTime(LocalDateTime timeFrom, LocalDateTime timeTo); List<T> getLastEventByTime(LocalDateTime timeFrom, LocalDateTime timeTo);

View File

@ -2,6 +2,6 @@ package org.sadtech.bot.core.service;
import org.sadtech.bot.core.domain.content.Mail; import org.sadtech.bot.core.domain.content.Mail;
public interface MailService extends EventService<Mail> { public interface MailService extends ContentService<Mail> {
} }

View File

@ -0,0 +1,49 @@
package org.sadtech.bot.core.service.impl;
import org.sadtech.bot.core.domain.content.BoardComment;
import org.sadtech.bot.core.repository.ContentRepository;
import org.sadtech.bot.core.service.BoardCommentService;
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 BoardCommentServiceImpl implements BoardCommentService {
private static final Logger log = LoggerFactory.getLogger(BoardCommentServiceImpl.class);
private final ContentRepository<BoardComment> commentRepository;
public BoardCommentServiceImpl(ContentRepository<BoardComment> commentRepository) {
this.commentRepository = commentRepository;
}
@Override
public void add(BoardComment event) {
commentRepository.add(event);
}
@Override
public List<BoardComment> getByTime(LocalDateTime timeFrom, LocalDateTime timeTo) {
return null;
}
@Override
public List<BoardComment> getLastEventByTime(LocalDateTime timeFrom, LocalDateTime timeTo) {
log.info("Запрошены последние комментарии к обсуждению {} - {} ", timeFrom, timeTo);
List<BoardComment> mails = commentRepository.findByTime(timeFrom, timeTo);
Set<Integer> people = new HashSet<>();
List<BoardComment> 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;
}
}

View File

@ -1,7 +1,7 @@
package org.sadtech.bot.core.service.impl; package org.sadtech.bot.core.service.impl;
import org.sadtech.bot.core.domain.content.Mail; import org.sadtech.bot.core.domain.content.Mail;
import org.sadtech.bot.core.repository.MailRepository; import org.sadtech.bot.core.repository.ContentRepository;
import org.sadtech.bot.core.service.MailService; import org.sadtech.bot.core.service.MailService;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -16,9 +16,9 @@ public class MailServiceImpl implements MailService {
private static final Logger log = LoggerFactory.getLogger(MailServiceImpl.class); private static final Logger log = LoggerFactory.getLogger(MailServiceImpl.class);
private final MailRepository mailRepository; private final ContentRepository<Mail> mailRepository;
public MailServiceImpl(MailRepository mailRepository) { public MailServiceImpl(ContentRepository<Mail> mailRepository) {
this.mailRepository = mailRepository; this.mailRepository = mailRepository;
} }
@ -29,9 +29,9 @@ public class MailServiceImpl implements MailService {
} }
@Override @Override
public List<Mail> getFirstEventByTime(LocalDateTime timeFrom, LocalDateTime timeTo) { public List<Mail> getLastEventByTime(LocalDateTime timeFrom, LocalDateTime timeTo) {
log.info("Запрошены сообщения {} - {} ", timeFrom, timeTo); log.info("Запрошены последние сообщения {} - {} ", timeFrom, timeTo);
List<Mail> mails = mailRepository.getMailByTime(timeFrom, timeTo); List<Mail> mails = mailRepository.findByTime(timeFrom, timeTo);
Set<Integer> people = new HashSet<>(); Set<Integer> people = new HashSet<>();
List<Mail> returnMails = new ArrayList<>(); List<Mail> returnMails = new ArrayList<>();
for (int i = mails.size() - 1; i >= 0; i--) { for (int i = mails.size() - 1; i >= 0; i--) {
@ -44,23 +44,9 @@ public class MailServiceImpl implements MailService {
} }
@Override @Override
public List<Mail> getLastEventByTime(LocalDateTime timeFrom, LocalDateTime timeTo) { public List<Mail> getByTime(LocalDateTime timeFrom, LocalDateTime timeTo) {
List<Mail> mails = mailRepository.getMailByTime(timeFrom, timeTo); log.info("Запрошены все сообщения {} - {} ", timeFrom, timeTo);
Set<Integer> people = new HashSet<>(); return mailRepository.findByTime(timeFrom, timeTo);
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

@ -0,0 +1,27 @@
package org.sadtech.bot.core.service.sender;
import org.sadtech.bot.core.domain.BoxAnswer;
import org.sadtech.bot.core.domain.content.BoardComment;
import org.sadtech.bot.core.domain.content.Content;
import org.sadtech.bot.core.exception.MailSendException;
public class SenderBox {
private SenderBox() {
throw new IllegalStateException("Utility Class");
}
public static void sent(Sent sent, Content content, BoxAnswer boxAnswer) {
switch (content.getType()) {
case MAIL:
sent.send(content.getPersonId(), boxAnswer);
break;
case BOARD_COMMENT:
sent.send(((BoardComment) content).getTopicId(), content.getPersonId(), boxAnswer);
break;
default:
throw new MailSendException();
}
}
}

View File

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

View File

@ -21,7 +21,7 @@ public class EmailSent implements Sent {
} }
@Override @Override
public void send(Integer personId, String htmlText) { public void send(Integer personId, BoxAnswer boxAnswer) {
Session session = Session.getDefaultInstance(emailConfig.getProps(), new Authenticator() { Session session = Session.getDefaultInstance(emailConfig.getProps(), new Authenticator() {
@Override @Override
protected PasswordAuthentication getPasswordAuthentication() { protected PasswordAuthentication getPasswordAuthentication() {
@ -33,7 +33,7 @@ public class EmailSent implements Sent {
Message message = new MimeMessage(session); Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(emailConfig.getUsername())); message.setFrom(new InternetAddress(emailConfig.getUsername()));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(emailConfig.getUsername())); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(emailConfig.getUsername()));
message.setContent(htmlText, "text/html; charset=utf-8"); message.setContent(boxAnswer.getMessage(), "text/html; charset=utf-8");
Transport.send(message); Transport.send(message);
} catch (MessagingException e) { } catch (MessagingException e) {
log.error(e.getMessage()); log.error(e.getMessage());
@ -42,7 +42,7 @@ public class EmailSent implements Sent {
} }
@Override @Override
public void send(Integer personId, BoxAnswer boxAnswer) { public void send(Integer contentId, Integer personId, BoxAnswer boxAnswer) {
throw new MailSendException(); throw new MailSendException();
} }
} }