From 30f4c09bbd39062eb68b9b4c4dda7169644f447f 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 --- core/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 ++++++++++++++++ vk-bot/pom.xml | 12 +---- .../src/main/java/org/sadtech/vkbot/Main.java | 9 ++-- .../main/java/org/sadtech/vkbot/VkApi.java | 47 +++++++++++++++++++ .../java/org/sadtech/vkbot/VkOpenMethod.java | 24 ---------- .../org/sadtech/vkbot/config/DataConfig.java | 5 +- .../sadtech/vkbot/config/SpringConfigVk.java | 10 ++-- .../{listener => }/handlers/Handled.java | 2 +- .../handlers/impl/DispetcherHandlerVk.java | 4 +- .../vkbot/handlers/impl/MessageHandlerVk.java | 46 ++++++++++++++++++ .../handlers/impl/MessageHandlerVk.java | 39 --------------- .../vkbot/listener/impl/EventListenerVk.java | 10 ++-- 25 files changed, 214 insertions(+), 154 deletions(-) rename core/src/main/java/org/sadtech/consultant/database/entity/{Message.java => Messages.java} (56%) delete mode 100644 core/src/main/java/org/sadtech/consultant/database/entity/SocialNetworks.java create mode 100644 core/src/main/java/org/sadtech/consultant/database/entity/SourceMessage.java rename core/src/main/java/org/sadtech/consultant/database/repository/{NextUnitRepositoriy.java => NextUnitRepository.java} (69%) delete mode 100644 core/src/main/java/org/sadtech/consultant/database/repository/SocialNetworksRepositoriy.java delete mode 100644 core/src/main/java/org/sadtech/consultant/database/service/SocialNetworksService.java delete mode 100644 core/src/main/java/org/sadtech/consultant/database/service/impl/SocialNetworksServiceImpl.java create mode 100644 core/src/main/java/org/sadtech/consultant/processing/MessageHandler.java create mode 100644 vk-bot/src/main/java/org/sadtech/vkbot/VkApi.java delete mode 100644 vk-bot/src/main/java/org/sadtech/vkbot/VkOpenMethod.java rename vk-bot/src/main/java/org/sadtech/vkbot/{listener => }/handlers/Handled.java (61%) rename vk-bot/src/main/java/org/sadtech/vkbot/{listener => }/handlers/impl/DispetcherHandlerVk.java (91%) create mode 100644 vk-bot/src/main/java/org/sadtech/vkbot/handlers/impl/MessageHandlerVk.java delete mode 100644 vk-bot/src/main/java/org/sadtech/vkbot/listener/handlers/impl/MessageHandlerVk.java diff --git a/core/pom.xml b/core/pom.xml index 5e55758..4a34740 100644 --- a/core/pom.xml +++ b/core/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/core/src/main/java/org/sadtech/consultant/database/entity/Message.java b/core/src/main/java/org/sadtech/consultant/database/entity/Messages.java similarity index 56% rename from core/src/main/java/org/sadtech/consultant/database/entity/Message.java rename to core/src/main/java/org/sadtech/consultant/database/entity/Messages.java index e729d25..f752173 100644 --- a/core/src/main/java/org/sadtech/consultant/database/entity/Message.java +++ b/core/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/core/src/main/java/org/sadtech/consultant/database/entity/SocialNetworks.java b/core/src/main/java/org/sadtech/consultant/database/entity/SocialNetworks.java deleted file mode 100644 index d3c78a4..0000000 --- a/core/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/core/src/main/java/org/sadtech/consultant/database/entity/SourceMessage.java b/core/src/main/java/org/sadtech/consultant/database/entity/SourceMessage.java new file mode 100644 index 0000000..df6497a --- /dev/null +++ b/core/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/core/src/main/java/org/sadtech/consultant/database/entity/User.java b/core/src/main/java/org/sadtech/consultant/database/entity/User.java index 10271c7..17267fa 100644 --- a/core/src/main/java/org/sadtech/consultant/database/entity/User.java +++ b/core/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/core/src/main/java/org/sadtech/consultant/database/repository/MessageRepository.java b/core/src/main/java/org/sadtech/consultant/database/repository/MessageRepository.java index 51d73ed..cedfccb 100644 --- a/core/src/main/java/org/sadtech/consultant/database/repository/MessageRepository.java +++ b/core/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/core/src/main/java/org/sadtech/consultant/database/repository/NextUnitRepositoriy.java b/core/src/main/java/org/sadtech/consultant/database/repository/NextUnitRepository.java similarity index 69% rename from core/src/main/java/org/sadtech/consultant/database/repository/NextUnitRepositoriy.java rename to core/src/main/java/org/sadtech/consultant/database/repository/NextUnitRepository.java index eff732b..3f3f71f 100644 --- a/core/src/main/java/org/sadtech/consultant/database/repository/NextUnitRepositoriy.java +++ b/core/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/core/src/main/java/org/sadtech/consultant/database/repository/SocialNetworksRepositoriy.java b/core/src/main/java/org/sadtech/consultant/database/repository/SocialNetworksRepositoriy.java deleted file mode 100644 index 7c606ae..0000000 --- a/core/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/core/src/main/java/org/sadtech/consultant/database/service/MessageService.java b/core/src/main/java/org/sadtech/consultant/database/service/MessageService.java index c4edb28..9aa136e 100644 --- a/core/src/main/java/org/sadtech/consultant/database/service/MessageService.java +++ b/core/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/core/src/main/java/org/sadtech/consultant/database/service/SocialNetworksService.java b/core/src/main/java/org/sadtech/consultant/database/service/SocialNetworksService.java deleted file mode 100644 index 83d12da..0000000 --- a/core/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/core/src/main/java/org/sadtech/consultant/database/service/impl/MessageServiceImpl.java b/core/src/main/java/org/sadtech/consultant/database/service/impl/MessageServiceImpl.java index 2171cda..23f6d65 100644 --- a/core/src/main/java/org/sadtech/consultant/database/service/impl/MessageServiceImpl.java +++ b/core/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/core/src/main/java/org/sadtech/consultant/database/service/impl/NextUnitServiceImpl.java b/core/src/main/java/org/sadtech/consultant/database/service/impl/NextUnitServiceImpl.java index 282c162..1e93a0f 100644 --- a/core/src/main/java/org/sadtech/consultant/database/service/impl/NextUnitServiceImpl.java +++ b/core/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/core/src/main/java/org/sadtech/consultant/database/service/impl/SocialNetworksServiceImpl.java b/core/src/main/java/org/sadtech/consultant/database/service/impl/SocialNetworksServiceImpl.java deleted file mode 100644 index b1b8c05..0000000 --- a/core/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/core/src/main/java/org/sadtech/consultant/processing/MessageHandler.java b/core/src/main/java/org/sadtech/consultant/processing/MessageHandler.java new file mode 100644 index 0000000..fdd84a8 --- /dev/null +++ b/core/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(); + } + } + } + +} diff --git a/vk-bot/pom.xml b/vk-bot/pom.xml index 939a596..e3617b7 100644 --- a/vk-bot/pom.xml +++ b/vk-bot/pom.xml @@ -82,9 +82,6 @@ - - - org.springframework spring-core @@ -121,12 +118,6 @@ 5.1.3.RELEASE - - javax.servlet - javax.servlet-api - ${javax.servlet} - - com.google.code.gson gson @@ -154,14 +145,12 @@ mysql mysql-connector-java ${mysql.connector} - runtime org.hibernate hibernate-entitymanager ${hibernate.entitymanager} - runtime org.hibernate @@ -174,6 +163,7 @@ slf4j-nop 1.7.13 + org.sadtech consultant-core diff --git a/vk-bot/src/main/java/org/sadtech/vkbot/Main.java b/vk-bot/src/main/java/org/sadtech/vkbot/Main.java index 012ceb4..69f7e42 100644 --- a/vk-bot/src/main/java/org/sadtech/vkbot/Main.java +++ b/vk-bot/src/main/java/org/sadtech/vkbot/Main.java @@ -1,14 +1,15 @@ package org.sadtech.vkbot; import lombok.extern.log4j.Log4j; -import org.sadtech.vkbot.config.DataConfig; +import org.sadtech.consultant.processing.MessageHandler; import org.sadtech.vkbot.config.SpringConfigVk; import org.sadtech.vkbot.listener.EventListenable; -import org.sadtech.vkbot.listener.handlers.Handled; +import org.sadtech.vkbot.handlers.Handled; import org.springframework.context.annotation.AnnotationConfigApplicationContext; @Log4j public class Main { + public static void main(String[] args) throws Exception { Main main = new Main(); main.run(); @@ -17,11 +18,13 @@ public class Main { public void run() throws Exception { log.info("\n\n\n\n=== Запуск прогарммы ===\n\n"); - AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfigVk.class, DataConfig.class); + AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfigVk.class); EventListenable eventListener = context.getBean(EventListenable.class); eventListener.listen(); Handled dispetcherHandler = context.getBean(Handled.class); dispetcherHandler.sortAndSend(); + MessageHandler messageHandler = context.getBean(MessageHandler.class); + messageHandler.processing(); log.info("\n\n=== Конец программы ===\n\n"); } diff --git a/vk-bot/src/main/java/org/sadtech/vkbot/VkApi.java b/vk-bot/src/main/java/org/sadtech/vkbot/VkApi.java new file mode 100644 index 0000000..659b304 --- /dev/null +++ b/vk-bot/src/main/java/org/sadtech/vkbot/VkApi.java @@ -0,0 +1,47 @@ +package org.sadtech.vkbot; + +import com.google.gson.Gson; +import com.vk.api.sdk.client.VkApiClient; +import com.vk.api.sdk.client.actors.GroupActor; +import com.vk.api.sdk.exceptions.ApiException; +import com.vk.api.sdk.exceptions.ClientException; +import com.vk.api.sdk.objects.users.User; +import com.vk.api.sdk.objects.users.UserFull; +import com.vk.api.sdk.objects.users.UserXtrCounters; +import lombok.extern.log4j.Log4j; +import org.springframework.stereotype.Component; + +import java.util.List; + +@Log4j +@Component +public class VkApi { + + private static VkApiClient vk; + private static GroupActor actor; + + public VkApi(VkApiClient vk, GroupActor actor) { + this.vk = vk; + this.actor = actor; + } + + public static void sendMessage(Integer id, String text) { + try { + vk.messages().send(actor).userId(id).message(text).execute(); + } catch (ApiException | ClientException e) { + e.printStackTrace(); + } + } + + public static String getUserName(Integer id) { + List user = null; + UserFull userFull = null; + try { + user = vk.users().get(actor).userIds(String.valueOf(id)).execute(); + } catch (ApiException | ClientException e) { + e.printStackTrace(); + } + return user.get(0).getLastName() + " " + user.get(0).getFirstName(); + } + +} diff --git a/vk-bot/src/main/java/org/sadtech/vkbot/VkOpenMethod.java b/vk-bot/src/main/java/org/sadtech/vkbot/VkOpenMethod.java deleted file mode 100644 index 6308980..0000000 --- a/vk-bot/src/main/java/org/sadtech/vkbot/VkOpenMethod.java +++ /dev/null @@ -1,24 +0,0 @@ -package org.sadtech.vkbot; - -import com.vk.api.sdk.client.VkApiClient; -import com.vk.api.sdk.client.actors.GroupActor; -import lombok.extern.log4j.Log4j; -import org.springframework.stereotype.Component; - -@Log4j -@Component -public class VkOpenMethod { - - private VkApiClient vk; - private GroupActor actor; - - public VkOpenMethod(VkApiClient vk, GroupActor actor) { - this.vk = vk; - this.actor = actor; - } - - public void sendMessage(Integer id, String text) { - vk.messages().send(actor).userId(id).peerId(id).message(text); - } - -} diff --git a/vk-bot/src/main/java/org/sadtech/vkbot/config/DataConfig.java b/vk-bot/src/main/java/org/sadtech/vkbot/config/DataConfig.java index 4a93c53..3069b6a 100644 --- a/vk-bot/src/main/java/org/sadtech/vkbot/config/DataConfig.java +++ b/vk-bot/src/main/java/org/sadtech/vkbot/config/DataConfig.java @@ -3,7 +3,9 @@ package org.sadtech.vkbot.config; import org.hibernate.jpa.HibernatePersistenceProvider; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; +import org.springframework.data.jpa.repository.config.EnableJpaRepositories; import org.springframework.jdbc.datasource.DriverManagerDataSource; import org.springframework.orm.jpa.JpaTransactionManager; import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; @@ -14,7 +16,8 @@ import java.util.Properties; @Configuration @EnableTransactionManagement - +@ComponentScan("org.sadtech.consultant.database.service") +@EnableJpaRepositories("org.sadtech.consultant.database.repository") public class DataConfig { @Value("${db.driver}") diff --git a/vk-bot/src/main/java/org/sadtech/vkbot/config/SpringConfigVk.java b/vk-bot/src/main/java/org/sadtech/vkbot/config/SpringConfigVk.java index f1f06b4..231aa60 100644 --- a/vk-bot/src/main/java/org/sadtech/vkbot/config/SpringConfigVk.java +++ b/vk-bot/src/main/java/org/sadtech/vkbot/config/SpringConfigVk.java @@ -12,7 +12,6 @@ import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.*; import org.springframework.core.task.TaskExecutor; import org.springframework.scheduling.annotation.EnableAsync; -import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.concurrent.ConcurrentTaskExecutor; import java.util.concurrent.Executors; @@ -20,9 +19,10 @@ import java.util.concurrent.Executors; @Configuration @EnableAsync -@Import({DataConfig.class}) @PropertySource("classpath:config.properties") -@ComponentScan("org.sadtech.vkbot.listener") +@Import({DataConfig.class}) +@ComponentScan({"org.sadtech.vkbot", "org.sadtech.consultant.processing"}) + public class SpringConfigVk { @Value("${vk.groupID}") @@ -74,9 +74,7 @@ public class SpringConfigVk { GetLongPollServerResponse getLongPollServerResponse = null; try { getLongPollServerResponse = vkApiClient().groups().getLongPollServer(groupActor()).execute(); - } catch (ApiException e) { - e.printStackTrace(); - } catch (ClientException e) { + } catch (ApiException | ClientException e) { e.printStackTrace(); } return getLongPollServerResponse; diff --git a/vk-bot/src/main/java/org/sadtech/vkbot/listener/handlers/Handled.java b/vk-bot/src/main/java/org/sadtech/vkbot/handlers/Handled.java similarity index 61% rename from vk-bot/src/main/java/org/sadtech/vkbot/listener/handlers/Handled.java rename to vk-bot/src/main/java/org/sadtech/vkbot/handlers/Handled.java index 7a5513f..f82c1b6 100644 --- a/vk-bot/src/main/java/org/sadtech/vkbot/listener/handlers/Handled.java +++ b/vk-bot/src/main/java/org/sadtech/vkbot/handlers/Handled.java @@ -1,4 +1,4 @@ -package org.sadtech.vkbot.listener.handlers; +package org.sadtech.vkbot.handlers; public interface Handled { diff --git a/vk-bot/src/main/java/org/sadtech/vkbot/listener/handlers/impl/DispetcherHandlerVk.java b/vk-bot/src/main/java/org/sadtech/vkbot/handlers/impl/DispetcherHandlerVk.java similarity index 91% rename from vk-bot/src/main/java/org/sadtech/vkbot/listener/handlers/impl/DispetcherHandlerVk.java rename to vk-bot/src/main/java/org/sadtech/vkbot/handlers/impl/DispetcherHandlerVk.java index ba1c642..03908b5 100644 --- a/vk-bot/src/main/java/org/sadtech/vkbot/listener/handlers/impl/DispetcherHandlerVk.java +++ b/vk-bot/src/main/java/org/sadtech/vkbot/handlers/impl/DispetcherHandlerVk.java @@ -1,11 +1,11 @@ -package org.sadtech.vkbot.listener.handlers.impl; +package org.sadtech.vkbot.handlers.impl; import com.google.gson.JsonObject; import lombok.extern.log4j.Log4j; +import org.sadtech.vkbot.handlers.Handled; import org.sadtech.vkbot.listener.Observable; import org.sadtech.vkbot.listener.Observer; import org.sadtech.vkbot.listener.data.ResponsibleData; -import org.sadtech.vkbot.listener.handlers.Handled; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Component; diff --git a/vk-bot/src/main/java/org/sadtech/vkbot/handlers/impl/MessageHandlerVk.java b/vk-bot/src/main/java/org/sadtech/vkbot/handlers/impl/MessageHandlerVk.java new file mode 100644 index 0000000..dfb6e5d --- /dev/null +++ b/vk-bot/src/main/java/org/sadtech/vkbot/handlers/impl/MessageHandlerVk.java @@ -0,0 +1,46 @@ +package org.sadtech.vkbot.handlers.impl; + +import com.google.gson.Gson; +import com.google.gson.JsonObject; +import com.vk.api.sdk.objects.messages.Message; +import lombok.extern.log4j.Log4j; +import org.sadtech.consultant.database.entity.Messages; +import org.sadtech.consultant.database.entity.SourceMessage; +import org.sadtech.consultant.database.service.MessageService; +import org.sadtech.vkbot.VkApi; +import org.sadtech.vkbot.listener.Observable; +import org.sadtech.vkbot.listener.Observer; +import org.springframework.stereotype.Component; + +@Log4j +@Component + +public class MessageHandlerVk implements Observer { + + private MessageService service; + + public MessageHandlerVk(Observable dispetcherHandler, MessageService messageService) { + this.service = messageService; + dispetcherHandler.registerObserver(this); + } + + @Override + public void update(JsonObject object) { + if (object.get("type").toString().equals("\"message_new\"")) { + Gson gson = new Gson(); + Message message = gson.fromJson(object.getAsJsonObject("object"), Message.class); + send(message); + } + } + + private void send(Message userMessage) { + log.info(userMessage.getBody()); + Messages message = new Messages(); + message.setIdUser(Long.valueOf(userMessage.getUserId())); + message.setText(userMessage.getBody()); + message.setDate(Long.valueOf(userMessage.getDate())); + message.setSource(SourceMessage.VK); + VkApi.sendMessage(userMessage.getUserId(), "Здравствуйте, " + VkApi.getUserName(userMessage.getUserId()) + "!\nВаше сообщение получено!\n"); + service.addMessage(message); + } +} diff --git a/vk-bot/src/main/java/org/sadtech/vkbot/listener/handlers/impl/MessageHandlerVk.java b/vk-bot/src/main/java/org/sadtech/vkbot/listener/handlers/impl/MessageHandlerVk.java deleted file mode 100644 index 79d0881..0000000 --- a/vk-bot/src/main/java/org/sadtech/vkbot/listener/handlers/impl/MessageHandlerVk.java +++ /dev/null @@ -1,39 +0,0 @@ -package org.sadtech.vkbot.listener.handlers.impl; - -import com.google.gson.Gson; -import com.google.gson.JsonObject; -import com.vk.api.sdk.objects.messages.Message; -import lombok.extern.log4j.Log4j; -import org.sadtech.consultant.database.service.MessageService; -import org.sadtech.consultant.database.service.impl.MessageServiceImpl; -import org.sadtech.vkbot.listener.Observable; -import org.sadtech.vkbot.listener.Observer; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; - -@Log4j -@Component -public class MessageHandlerVk implements Observer { - - private Observable dispetcherHandler; -// @Autowired -// private MessageService service; - - public MessageHandlerVk(Observable dispetcherHandler) { - this.dispetcherHandler = dispetcherHandler; - dispetcherHandler.registerObserver(this); - } - - @Override - public void update(JsonObject object) { - if (object.get("type").toString().equals("\"message_new\"")) { - Gson gson = new Gson(); - Message message = gson.fromJson(object.getAsJsonObject("object"), Message.class); - send(message); - } - } - - private void send(Message message) { - log.info(message.getBody()); - } -} diff --git a/vk-bot/src/main/java/org/sadtech/vkbot/listener/impl/EventListenerVk.java b/vk-bot/src/main/java/org/sadtech/vkbot/listener/impl/EventListenerVk.java index 2261bf1..3c2cfa5 100644 --- a/vk-bot/src/main/java/org/sadtech/vkbot/listener/impl/EventListenerVk.java +++ b/vk-bot/src/main/java/org/sadtech/vkbot/listener/impl/EventListenerVk.java @@ -1,7 +1,5 @@ package org.sadtech.vkbot.listener.impl; -import com.google.gson.JsonObject; -import com.google.gson.JsonParser; import com.vk.api.sdk.actions.LongPoll; import com.vk.api.sdk.callback.longpoll.queries.GetLongPollEventsQuery; import com.vk.api.sdk.callback.longpoll.responses.GetLongPollEventsResponse; @@ -10,15 +8,15 @@ import com.vk.api.sdk.client.actors.GroupActor; import com.vk.api.sdk.exceptions.ApiException; import com.vk.api.sdk.exceptions.ClientException; import com.vk.api.sdk.objects.groups.responses.GetLongPollServerResponse; -import jdk.nashorn.internal.parser.JSONParser; import lombok.extern.log4j.Log4j; -import org.json.JSONObject; +import org.sadtech.vkbot.VkApi; import org.sadtech.vkbot.listener.EventListenable; import org.sadtech.vkbot.listener.data.ResponsibleData; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Component; +import java.util.Date; + @Log4j @Component public class EventListenerVk implements EventListenable { @@ -57,8 +55,6 @@ public class EventListenerVk implements EventListenable { responseData.add(eventsResponse.getUpdates().get(0)); log.info(eventsResponse.getUpdates()); String test = "{\"one_time\":false,\"buttons\":[[{\"action\":{\"type\":\"text\",\"payload\":\"{\\\"button\\\": \\\"1\\\"}\",\"label\":\"Red2\"},\"color\":\"negative\"},{\"action\":{\"type\":\"text\",\"payload\":\"{\\\"button\\\": \\\"2\\\"}\",\"label\":\"Green\"},\"color\":\"positive\"}],[{\"action\":{\"type\":\"text\",\"payload\":\"{\\\"button\\\": \\\"3\\\"}\",\"label\":\"White\"},\"color\":\"default\"},{\"action\":{\"type\":\"text\",\"payload\":\"{\\\"button\\\": \\\"4\\\"}\",\"label\":\"Blue\"},\"color\":\"primary\"}]]}"; - vk.messages().send(actor).peerId(244319573).keyboard(test).message("Сообщение получено").execute(); - } longPollEventsQuery = longPoll.getEvents(server.getServer(), server.getKey(), eventsResponse.getTs()).waitTime(20); } while (true);