From 00a964a9fd8287f3643eba9936832c3912d2ea83 Mon Sep 17 00:00:00 2001 From: Mark Struchkov Date: Wed, 9 Jan 2019 17:18:20 +0300 Subject: [PATCH] =?UTF-8?q?=D0=90=D1=80=D1=85=D0=B8=D1=82=D0=B5=D0=BA?= =?UTF-8?q?=D1=82=D1=83=D1=80=D0=BD=D0=BE=D0=B5=20=D1=80=D0=B0=D0=B7=D0=B1?= =?UTF-8?q?=D0=B8=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=BD=D0=B0=20=D0=BD=D0=B5?= =?UTF-8?q?=D0=B7=D0=B0=D0=B2=D0=B8=D1=81=D0=B8=D0=BC=D1=8B=D0=B5=20=D0=BF?= =?UTF-8?q?=D1=80=D0=BE=D0=B5=D0=BA=D1=82=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/sadtech/consultant/MessageSender.java | 7 +++++ .../entity/{Message.java => Mail.java} | 7 ++--- .../consultant/database/entity/Person.java | 3 ++ .../consultant/database/entity/SaveUnit.java | 7 +++-- ...ageRepository.java => MailRepository.java} | 8 +++--- .../database/service/MailService.java | 12 ++++++++ .../database/service/MessageService.java | 12 -------- .../service/impl/MailServiceImpl.java | 25 +++++++++++++++++ .../service/impl/MessageServiceImpl.java | 25 ----------------- .../service/impl/PersonServiceImpl.java | 2 +- .../service/impl/UnitServiceImpl.java | 2 +- .../consultant/processing/MessageHandler.java | 28 +++++++++++++++---- .../processing/MessageLogicService.java | 12 ++++---- 13 files changed, 90 insertions(+), 60 deletions(-) create mode 100644 src/main/java/org/sadtech/consultant/MessageSender.java rename src/main/java/org/sadtech/consultant/database/entity/{Message.java => Mail.java} (68%) rename src/main/java/org/sadtech/consultant/database/repository/{MessageRepository.java => MailRepository.java} (50%) create mode 100644 src/main/java/org/sadtech/consultant/database/service/MailService.java delete mode 100644 src/main/java/org/sadtech/consultant/database/service/MessageService.java create mode 100644 src/main/java/org/sadtech/consultant/database/service/impl/MailServiceImpl.java delete mode 100644 src/main/java/org/sadtech/consultant/database/service/impl/MessageServiceImpl.java diff --git a/src/main/java/org/sadtech/consultant/MessageSender.java b/src/main/java/org/sadtech/consultant/MessageSender.java new file mode 100644 index 0000000..75ba12e --- /dev/null +++ b/src/main/java/org/sadtech/consultant/MessageSender.java @@ -0,0 +1,7 @@ +package org.sadtech.consultant; + +public interface MessageSender { + + void send(Integer idNetSoc, String text); + +} diff --git a/src/main/java/org/sadtech/consultant/database/entity/Message.java b/src/main/java/org/sadtech/consultant/database/entity/Mail.java similarity index 68% rename from src/main/java/org/sadtech/consultant/database/entity/Message.java rename to src/main/java/org/sadtech/consultant/database/entity/Mail.java index 69afff2..5ba4490 100644 --- a/src/main/java/org/sadtech/consultant/database/entity/Message.java +++ b/src/main/java/org/sadtech/consultant/database/entity/Mail.java @@ -6,7 +6,7 @@ import javax.persistence.*; @Data @Entity -public class Message { +public class Mail { @Id @GeneratedValue @@ -14,11 +14,10 @@ public class Message { private String text; private Long date; private String sourceMessage; - private String sex; @ManyToOne - @JoinColumn(name = "idUser", nullable = false) - private Person user; + @JoinColumn(name = "idPerson", nullable = false) + private Person person; } diff --git a/src/main/java/org/sadtech/consultant/database/entity/Person.java b/src/main/java/org/sadtech/consultant/database/entity/Person.java index cf92adf..847e228 100644 --- a/src/main/java/org/sadtech/consultant/database/entity/Person.java +++ b/src/main/java/org/sadtech/consultant/database/entity/Person.java @@ -24,10 +24,13 @@ public class Person { private String city; private String token; + private String email; @ElementCollection @CollectionTable(name = "PersonSocialNetworks") @MapKeyColumn(name = "KeysSocialNetworks", length = 20) private Map socialNetworks = new HashMap<>(); + + } diff --git a/src/main/java/org/sadtech/consultant/database/entity/SaveUnit.java b/src/main/java/org/sadtech/consultant/database/entity/SaveUnit.java index b5df08e..d198992 100644 --- a/src/main/java/org/sadtech/consultant/database/entity/SaveUnit.java +++ b/src/main/java/org/sadtech/consultant/database/entity/SaveUnit.java @@ -2,8 +2,7 @@ package org.sadtech.consultant.database.entity; import lombok.Data; -import javax.persistence.Entity; -import javax.persistence.Id; +import javax.persistence.*; @Entity @Data @@ -13,4 +12,8 @@ public class SaveUnit { private Long id; private Long idStage; + @OneToOne + @JoinColumn(name = "idPerson", nullable = false) + private Person user; + } diff --git a/src/main/java/org/sadtech/consultant/database/repository/MessageRepository.java b/src/main/java/org/sadtech/consultant/database/repository/MailRepository.java similarity index 50% rename from src/main/java/org/sadtech/consultant/database/repository/MessageRepository.java rename to src/main/java/org/sadtech/consultant/database/repository/MailRepository.java index 8dfc122..8e5541c 100644 --- a/src/main/java/org/sadtech/consultant/database/repository/MessageRepository.java +++ b/src/main/java/org/sadtech/consultant/database/repository/MailRepository.java @@ -1,15 +1,15 @@ package org.sadtech.consultant.database.repository; -import org.sadtech.consultant.database.entity.Message; +import org.sadtech.consultant.database.entity.Mail; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; import java.util.Collection; -public interface MessageRepository extends JpaRepository { +public interface MailRepository extends JpaRepository { - @Query("SELECT u FROM Message u WHERE u.date > :date") - Collection getMessagesByRange(@Param("date") Long date); + @Query("SELECT u FROM Mail u WHERE u.date > :date") + Collection getMessagesByRange(@Param("date") Long date); } diff --git a/src/main/java/org/sadtech/consultant/database/service/MailService.java b/src/main/java/org/sadtech/consultant/database/service/MailService.java new file mode 100644 index 0000000..1ca820b --- /dev/null +++ b/src/main/java/org/sadtech/consultant/database/service/MailService.java @@ -0,0 +1,12 @@ +package org.sadtech.consultant.database.service; + +import org.sadtech.consultant.database.entity.Mail; + +import java.util.List; + +public interface MailService { + + void addMessage(Mail message); + + List getMessageRange(Long date); +} diff --git a/src/main/java/org/sadtech/consultant/database/service/MessageService.java b/src/main/java/org/sadtech/consultant/database/service/MessageService.java deleted file mode 100644 index 53e2d37..0000000 --- a/src/main/java/org/sadtech/consultant/database/service/MessageService.java +++ /dev/null @@ -1,12 +0,0 @@ -package org.sadtech.consultant.database.service; - -import org.sadtech.consultant.database.entity.Message; - -import java.util.List; - -public interface MessageService { - - void addMessage(Message message); - - List getMessageRange(Long date); -} diff --git a/src/main/java/org/sadtech/consultant/database/service/impl/MailServiceImpl.java b/src/main/java/org/sadtech/consultant/database/service/impl/MailServiceImpl.java new file mode 100644 index 0000000..6432031 --- /dev/null +++ b/src/main/java/org/sadtech/consultant/database/service/impl/MailServiceImpl.java @@ -0,0 +1,25 @@ +package org.sadtech.consultant.database.service.impl; + +import org.sadtech.consultant.database.entity.Mail; +import org.sadtech.consultant.database.repository.MailRepository; +import org.sadtech.consultant.database.service.MailService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.List; + +@Service +public class MailServiceImpl implements MailService { + + @Autowired + private MailRepository repository; + + public void addMessage(Mail message) { + repository.saveAndFlush(message); + } + + @Override + public List getMessageRange(Long date) { + return (List) repository.getMessagesByRange(date); + } +} diff --git a/src/main/java/org/sadtech/consultant/database/service/impl/MessageServiceImpl.java b/src/main/java/org/sadtech/consultant/database/service/impl/MessageServiceImpl.java deleted file mode 100644 index f5d967b..0000000 --- a/src/main/java/org/sadtech/consultant/database/service/impl/MessageServiceImpl.java +++ /dev/null @@ -1,25 +0,0 @@ -package org.sadtech.consultant.database.service.impl; - -import org.sadtech.consultant.database.entity.Message; -import org.sadtech.consultant.database.repository.MessageRepository; -import org.sadtech.consultant.database.service.MessageService; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; - -import java.util.List; - -@Service -public class MessageServiceImpl implements MessageService { - - @Autowired - private MessageRepository repository; - - public void addMessage(Message message) { - repository.saveAndFlush(message); - } - - @Override - public List getMessageRange(Long date) { - return (List) repository.getMessagesByRange(date); - } -} diff --git a/src/main/java/org/sadtech/consultant/database/service/impl/PersonServiceImpl.java b/src/main/java/org/sadtech/consultant/database/service/impl/PersonServiceImpl.java index 7acd025..936d0d8 100644 --- a/src/main/java/org/sadtech/consultant/database/service/impl/PersonServiceImpl.java +++ b/src/main/java/org/sadtech/consultant/database/service/impl/PersonServiceImpl.java @@ -1,7 +1,7 @@ package org.sadtech.consultant.database.service.impl; -import org.sadtech.consultant.database.entity.Person; import org.sadtech.consultant.database.repository.PersonRepository; +import org.sadtech.consultant.database.entity.Person; import org.sadtech.consultant.database.service.PersonService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; diff --git a/src/main/java/org/sadtech/consultant/database/service/impl/UnitServiceImpl.java b/src/main/java/org/sadtech/consultant/database/service/impl/UnitServiceImpl.java index 1a67158..eeec2d8 100644 --- a/src/main/java/org/sadtech/consultant/database/service/impl/UnitServiceImpl.java +++ b/src/main/java/org/sadtech/consultant/database/service/impl/UnitServiceImpl.java @@ -1,7 +1,7 @@ package org.sadtech.consultant.database.service.impl; -import org.sadtech.consultant.database.entity.Unit; import org.sadtech.consultant.database.repository.UnitRepositoriy; +import org.sadtech.consultant.database.entity.Unit; import org.sadtech.consultant.database.service.UnitService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; diff --git a/src/main/java/org/sadtech/consultant/processing/MessageHandler.java b/src/main/java/org/sadtech/consultant/processing/MessageHandler.java index e486947..d513930 100644 --- a/src/main/java/org/sadtech/consultant/processing/MessageHandler.java +++ b/src/main/java/org/sadtech/consultant/processing/MessageHandler.java @@ -1,12 +1,17 @@ package org.sadtech.consultant.processing; import lombok.extern.log4j.Log4j; -import org.sadtech.consultant.database.entity.Message; +import org.sadtech.consultant.database.entity.Mail; +import org.sadtech.consultant.MessageSender; +import org.sadtech.consultant.database.entity.Person; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Component; +import org.springframework.transaction.annotation.Transactional; import java.util.Date; +import java.util.HashMap; import java.util.List; +import java.util.Map; @Log4j @Component @@ -14,6 +19,7 @@ public class MessageHandler { private MessageLogicService messageLogicService; private PersonLogicService userLogicService; + private Map senderHashMap = new HashMap<>(); private long data; public MessageHandler(MessageLogicService messageLogicService, PersonLogicService userLogicService) { @@ -22,13 +28,25 @@ public class MessageHandler { data = new Date().getTime() / 1000; } + public void addMessageSendler(String type, MessageSender messageSender) { + senderHashMap.put(type, messageSender); + } + @Async + @Transactional public void processing() { + log.info("process"); while (true) { - List messages = messageLogicService.getMessageRange(data); - for (Message message : messages) { - log.info(message); - data = message.getDate(); + List messages = messageLogicService.getMessageRange(data); + if (messages.size()>0) { + for (Mail message : messages) { + log.info(message); + Person person = message.getPerson(); + Integer idNetSoc = person.getSocialNetworks().get(message.getSourceMessage()); + senderHashMap.get(message.getSourceMessage()).send(idNetSoc, "Тестовое сообщение"); + + data = message.getDate(); + } } } } diff --git a/src/main/java/org/sadtech/consultant/processing/MessageLogicService.java b/src/main/java/org/sadtech/consultant/processing/MessageLogicService.java index 1ed910a..3eb2be4 100644 --- a/src/main/java/org/sadtech/consultant/processing/MessageLogicService.java +++ b/src/main/java/org/sadtech/consultant/processing/MessageLogicService.java @@ -1,7 +1,7 @@ package org.sadtech.consultant.processing; -import org.sadtech.consultant.database.entity.Message; -import org.sadtech.consultant.database.service.MessageService; +import org.sadtech.consultant.database.entity.Mail; +import org.sadtech.consultant.database.service.MailService; import org.springframework.stereotype.Component; import java.util.List; @@ -9,17 +9,17 @@ import java.util.List; @Component public class MessageLogicService { - private MessageService messageService; + private MailService messageService; - public MessageLogicService(MessageService messageService) { + public MessageLogicService(MailService messageService) { this.messageService = messageService; } - public void addMessage(Message message) { + public void addMessage(Mail message) { messageService.addMessage(message); } - public List getMessageRange(long data) { + public List getMessageRange(long data) { return messageService.getMessageRange(data); } }