From d6b902f2b9e2cd276ec1a57908545ffda734d631 Mon Sep 17 00:00:00 2001 From: Mark Struchkov Date: Sat, 5 Jan 2019 00:45:57 +0300 Subject: [PATCH] =?UTF-8?q?*=20=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=20ENUM=20=D0=B4=D0=BB=D1=8F=20=D0=BE=D0=BF=D1=80=D0=B5?= =?UTF-8?q?=D0=B4=D0=B5=D0=BB=D0=B5=D0=BD=D0=B8=D1=8F=20=D0=B8=D1=81=D1=82?= =?UTF-8?q?=D0=BE=D1=87=D0=BD=D0=B8=D0=BA=D0=B0=20=D1=81=D0=BE=D0=B1=D1=8B?= =?UTF-8?q?=D1=82=D0=B8=D1=8F=20*=20=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=20=D0=BA=D0=B0=D1=81=D1=82=D0=BE=D0=BC=D0=BD=D1=8B?= =?UTF-8?q?=D0=B9=20SQL=20=D0=BC=D0=B5=D1=82=D0=BE=D0=B4=20=D0=B2=D0=BE?= =?UTF-8?q?=D0=B7=D0=B2=D1=80=D0=B0=D1=89=D0=B0=D1=8E=D1=89=D0=B8=D0=B9=20?= =?UTF-8?q?=D1=81=D0=BE=D0=BE=D0=B1=D1=89=D0=B5=D0=BD=D0=B8=D1=8F=20=D0=B8?= =?UTF-8?q?=D0=B7=20=D0=91=D0=94=20=D0=BD=D0=BE=D0=B2=D0=B5=D0=B5=20=D0=B7?= =?UTF-8?q?=D0=B0=D0=BF=D1=80=D0=BE=D1=88=D0=B5=D0=BD=D0=BE=D0=B9=20=D0=B4?= =?UTF-8?q?=D0=B0=D1=82=D1=8B=20*=20=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=20=D0=B3=D0=BB=D0=BE=D0=B1=D0=B0=D0=BB=D1=8C=D0=BD?= =?UTF-8?q?=D1=8B=D0=B9=20=D0=BE=D0=B1=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D1=87?= =?UTF-8?q?=D0=B8=D0=BA=20=D1=81=D0=BE=D0=BE=D0=B1=D1=89=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D0=B9=20*=20=D0=98=D1=81=D1=80=D0=BF=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D1=8B=20=D0=BA=D0=BE=D0=BD=D1=84=D0=B8=D0=B3=D1=83=D1=80?= =?UTF-8?q?=D0=B0=D1=86=D0=B8=D0=B8=20spring,=20=D0=BA=D0=BE=D1=82=D0=BE?= =?UTF-8?q?=D1=80=D1=8B=D0=B5=20=D0=BF=D1=80=D0=B8=D0=B2=D0=BE=D0=B4=D0=B8?= =?UTF-8?q?=D0=BB=D0=B8=20=D0=BA=20=D0=BE=D1=88=D0=B8=D0=B1=D0=BA=D0=B0?= =?UTF-8?q?=D0=BC=20=D0=B2=20=D1=81=D0=BE=D0=B7=D0=B4=D0=B0=D0=BD=D0=B8?= =?UTF-8?q?=D0=B8=20=D0=B1=D0=B8=D0=BD=D0=BE=D0=B2=20*=20=D0=94=D0=BE?= =?UTF-8?q?=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=20=D1=81=D1=82=D0=B0=D1=82?= =?UTF-8?q?=D0=B8=D1=87=D0=B5=D1=81=D0=BA=D0=B8=D0=B9=20=D0=BA=D0=BB=D0=B0?= =?UTF-8?q?=D1=81=D1=81=20=D1=81=20=D0=BC=D0=B5=D1=82=D0=BE=D0=B4=D0=B0?= =?UTF-8?q?=D0=BC=D0=B8=20=D0=B2=D0=BA=20=D0=B0=D0=BF=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 17 ++++++++ .../entity/{Message.java => Messages.java} | 10 ++--- .../database/entity/SocialNetworks.java | 17 -------- .../database/entity/SourceMessage.java | 5 +++ .../consultant/database/entity/User.java | 8 ++-- .../repository/MessageRepository.java | 12 +++++- ...positoriy.java => NextUnitRepository.java} | 3 +- .../repository/SocialNetworksRepositoriy.java | 7 ---- .../database/service/MessageService.java | 8 +++- .../service/SocialNetworksService.java | 4 -- .../service/impl/MessageServiceImpl.java | 11 ++++- .../service/impl/NextUnitServiceImpl.java | 4 +- .../impl/SocialNetworksServiceImpl.java | 14 ------- .../consultant/processing/MessageHandler.java | 40 +++++++++++++++++++ 14 files changed, 100 insertions(+), 60 deletions(-) rename src/main/java/org/sadtech/consultant/database/entity/{Message.java => Messages.java} (56%) delete mode 100644 src/main/java/org/sadtech/consultant/database/entity/SocialNetworks.java create mode 100644 src/main/java/org/sadtech/consultant/database/entity/SourceMessage.java rename src/main/java/org/sadtech/consultant/database/repository/{NextUnitRepositoriy.java => NextUnitRepository.java} (69%) delete mode 100644 src/main/java/org/sadtech/consultant/database/repository/SocialNetworksRepositoriy.java delete mode 100644 src/main/java/org/sadtech/consultant/database/service/SocialNetworksService.java delete mode 100644 src/main/java/org/sadtech/consultant/database/service/impl/SocialNetworksServiceImpl.java create mode 100644 src/main/java/org/sadtech/consultant/processing/MessageHandler.java diff --git a/pom.xml b/pom.xml index 5e55758..4a34740 100644 --- a/pom.xml +++ b/pom.xml @@ -10,6 +10,18 @@ 4.0.0 consultant-core + + + + org.apache.maven.plugins + maven-compiler-plugin + + 8 + 8 + + + + 1.0.2 @@ -43,6 +55,11 @@ spring-data-jpa ${spring.data} + + javax.persistence + javax.persistence-api + 2.2 + \ No newline at end of file diff --git a/src/main/java/org/sadtech/consultant/database/entity/Message.java b/src/main/java/org/sadtech/consultant/database/entity/Messages.java similarity index 56% rename from src/main/java/org/sadtech/consultant/database/entity/Message.java rename to src/main/java/org/sadtech/consultant/database/entity/Messages.java index e729d25..f752173 100644 --- a/src/main/java/org/sadtech/consultant/database/entity/Message.java +++ b/src/main/java/org/sadtech/consultant/database/entity/Messages.java @@ -2,20 +2,20 @@ package org.sadtech.consultant.database.entity; import lombok.Data; -import javax.persistence.Entity; -import javax.persistence.GeneratedValue; -import javax.persistence.Id; +import javax.persistence.*; @Data @Entity -public class Message { +public class Messages { @Id @GeneratedValue private Long idMess; private Long idUser; private String text; - private String date; + private Long date; + @Enumerated(EnumType.STRING) + private SourceMessage source; } diff --git a/src/main/java/org/sadtech/consultant/database/entity/SocialNetworks.java b/src/main/java/org/sadtech/consultant/database/entity/SocialNetworks.java deleted file mode 100644 index d3c78a4..0000000 --- a/src/main/java/org/sadtech/consultant/database/entity/SocialNetworks.java +++ /dev/null @@ -1,17 +0,0 @@ -package org.sadtech.consultant.database.entity; - -import lombok.Data; - -import javax.persistence.Entity; -import javax.persistence.Id; - -@Data -@Entity -public class SocialNetworks { - - @Id - private Long id; - private Long idVk; - private Long idFacebook; - -} diff --git a/src/main/java/org/sadtech/consultant/database/entity/SourceMessage.java b/src/main/java/org/sadtech/consultant/database/entity/SourceMessage.java new file mode 100644 index 0000000..df6497a --- /dev/null +++ b/src/main/java/org/sadtech/consultant/database/entity/SourceMessage.java @@ -0,0 +1,5 @@ +package org.sadtech.consultant.database.entity; + +public enum SourceMessage { + VK, FACEBOOK; +} diff --git a/src/main/java/org/sadtech/consultant/database/entity/User.java b/src/main/java/org/sadtech/consultant/database/entity/User.java index 10271c7..17267fa 100644 --- a/src/main/java/org/sadtech/consultant/database/entity/User.java +++ b/src/main/java/org/sadtech/consultant/database/entity/User.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.*; import java.util.List; @Entity @@ -16,7 +15,10 @@ public class User { private String token; private String lastName; private String city; -// private List socialNetworks; + + @ElementCollection + @CollectionTable(name="SocialNetworks", joinColumns=@JoinColumn(name="id")) + private List socialNetworks; } diff --git a/src/main/java/org/sadtech/consultant/database/repository/MessageRepository.java b/src/main/java/org/sadtech/consultant/database/repository/MessageRepository.java index 51d73ed..cedfccb 100644 --- a/src/main/java/org/sadtech/consultant/database/repository/MessageRepository.java +++ b/src/main/java/org/sadtech/consultant/database/repository/MessageRepository.java @@ -1,8 +1,16 @@ package org.sadtech.consultant.database.repository; -import org.sadtech.consultant.database.entity.Message; +import org.sadtech.consultant.database.entity.Messages; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; -public interface MessageRepository extends JpaRepository { +import java.util.Collection; +import java.util.List; + +public interface MessageRepository extends JpaRepository { + + @Query("SELECT u FROM Messages u WHERE u.date > :date") + Collection getMessagesByRange(@Param("date") Long date); } diff --git a/src/main/java/org/sadtech/consultant/database/repository/NextUnitRepositoriy.java b/src/main/java/org/sadtech/consultant/database/repository/NextUnitRepository.java similarity index 69% rename from src/main/java/org/sadtech/consultant/database/repository/NextUnitRepositoriy.java rename to src/main/java/org/sadtech/consultant/database/repository/NextUnitRepository.java index eff732b..3f3f71f 100644 --- a/src/main/java/org/sadtech/consultant/database/repository/NextUnitRepositoriy.java +++ b/src/main/java/org/sadtech/consultant/database/repository/NextUnitRepository.java @@ -3,8 +3,7 @@ package org.sadtech.consultant.database.repository; import org.sadtech.consultant.database.entity.NextUnit; import org.springframework.data.jpa.repository.JpaRepository; -public interface NextUnitRepositoriy extends JpaRepository { - +public interface NextUnitRepository extends JpaRepository { } diff --git a/src/main/java/org/sadtech/consultant/database/repository/SocialNetworksRepositoriy.java b/src/main/java/org/sadtech/consultant/database/repository/SocialNetworksRepositoriy.java deleted file mode 100644 index 7c606ae..0000000 --- a/src/main/java/org/sadtech/consultant/database/repository/SocialNetworksRepositoriy.java +++ /dev/null @@ -1,7 +0,0 @@ -package org.sadtech.consultant.database.repository; - -import org.sadtech.consultant.database.entity.SocialNetworks; -import org.springframework.data.jpa.repository.JpaRepository; - -public interface SocialNetworksRepositoriy extends JpaRepository { -} diff --git a/src/main/java/org/sadtech/consultant/database/service/MessageService.java b/src/main/java/org/sadtech/consultant/database/service/MessageService.java index c4edb28..9aa136e 100644 --- a/src/main/java/org/sadtech/consultant/database/service/MessageService.java +++ b/src/main/java/org/sadtech/consultant/database/service/MessageService.java @@ -1,8 +1,12 @@ package org.sadtech.consultant.database.service; -import org.sadtech.consultant.database.entity.Message; +import org.sadtech.consultant.database.entity.Messages; + +import java.util.List; public interface MessageService { - void addMessage(Message message); + void addMessage(Messages message); + + List getMessageRange(Long date); } diff --git a/src/main/java/org/sadtech/consultant/database/service/SocialNetworksService.java b/src/main/java/org/sadtech/consultant/database/service/SocialNetworksService.java deleted file mode 100644 index 83d12da..0000000 --- a/src/main/java/org/sadtech/consultant/database/service/SocialNetworksService.java +++ /dev/null @@ -1,4 +0,0 @@ -package org.sadtech.consultant.database.service; - -public interface SocialNetworksService { -} 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 index 2171cda..23f6d65 100644 --- a/src/main/java/org/sadtech/consultant/database/service/impl/MessageServiceImpl.java +++ b/src/main/java/org/sadtech/consultant/database/service/impl/MessageServiceImpl.java @@ -1,18 +1,25 @@ package org.sadtech.consultant.database.service.impl; -import org.sadtech.consultant.database.entity.Message; +import org.sadtech.consultant.database.entity.Messages; 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) { + public void addMessage(Messages 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/NextUnitServiceImpl.java b/src/main/java/org/sadtech/consultant/database/service/impl/NextUnitServiceImpl.java index 282c162..1e93a0f 100644 --- a/src/main/java/org/sadtech/consultant/database/service/impl/NextUnitServiceImpl.java +++ b/src/main/java/org/sadtech/consultant/database/service/impl/NextUnitServiceImpl.java @@ -1,6 +1,6 @@ package org.sadtech.consultant.database.service.impl; -import org.sadtech.consultant.database.repository.NextUnitRepositoriy; +import org.sadtech.consultant.database.repository.NextUnitRepository; import org.sadtech.consultant.database.service.NextUnitService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -9,6 +9,6 @@ import org.springframework.stereotype.Service; public class NextUnitServiceImpl implements NextUnitService { @Autowired - private NextUnitRepositoriy repositoriy; + private NextUnitRepository repository; } diff --git a/src/main/java/org/sadtech/consultant/database/service/impl/SocialNetworksServiceImpl.java b/src/main/java/org/sadtech/consultant/database/service/impl/SocialNetworksServiceImpl.java deleted file mode 100644 index b1b8c05..0000000 --- a/src/main/java/org/sadtech/consultant/database/service/impl/SocialNetworksServiceImpl.java +++ /dev/null @@ -1,14 +0,0 @@ -package org.sadtech.consultant.database.service.impl; - -import org.sadtech.consultant.database.repository.SocialNetworksRepositoriy; -import org.sadtech.consultant.database.service.SocialNetworksService; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; - -@Service -public class SocialNetworksServiceImpl implements SocialNetworksService { - - @Autowired - private SocialNetworksRepositoriy repositoriy; - -} diff --git a/src/main/java/org/sadtech/consultant/processing/MessageHandler.java b/src/main/java/org/sadtech/consultant/processing/MessageHandler.java new file mode 100644 index 0000000..fdd84a8 --- /dev/null +++ b/src/main/java/org/sadtech/consultant/processing/MessageHandler.java @@ -0,0 +1,40 @@ +package org.sadtech.consultant.processing; + +import lombok.extern.log4j.Log4j; +import org.sadtech.consultant.database.entity.Messages; +import org.sadtech.consultant.database.service.MessageService; +import org.springframework.scheduling.annotation.Async; +import org.springframework.stereotype.Component; + +import java.util.Date; +import java.util.List; + +@Log4j +@Component +public class MessageHandler { + + private MessageService service; + private long data; + + public MessageHandler(MessageService service) { + this.service = service; + data = new Date().getTime() / 1000; + } + + @Async + public void processing() { + while (true) { + List messages = service.getMessageRange(data); + for (Messages message : messages) { + System.out.println(message); + data = message.getDate(); + } + try { + Thread.sleep(10000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + } + +}