diff --git a/core/src/main/java/org/sadtech/consultant/database/entity/Messages.java b/core/src/main/java/org/sadtech/consultant/database/entity/Message.java similarity index 59% rename from core/src/main/java/org/sadtech/consultant/database/entity/Messages.java rename to core/src/main/java/org/sadtech/consultant/database/entity/Message.java index f752173..b1d0ff3 100644 --- a/core/src/main/java/org/sadtech/consultant/database/entity/Messages.java +++ b/core/src/main/java/org/sadtech/consultant/database/entity/Message.java @@ -6,16 +6,18 @@ import javax.persistence.*; @Data @Entity -public class Messages { +public class Message { @Id @GeneratedValue private Long idMess; - private Long idUser; private String text; private Long date; - @Enumerated(EnumType.STRING) - private SourceMessage source; + private String sourceMessage; + + @ManyToOne + @JoinColumn(name = "idUser", nullable = false) + private Person user; } diff --git a/core/src/main/java/org/sadtech/consultant/database/entity/NextUnit.java b/core/src/main/java/org/sadtech/consultant/database/entity/NextUnit.java index 64281d3..5e896fe 100644 --- a/core/src/main/java/org/sadtech/consultant/database/entity/NextUnit.java +++ b/core/src/main/java/org/sadtech/consultant/database/entity/NextUnit.java @@ -4,6 +4,7 @@ import lombok.Data; import javax.persistence.Entity; import javax.persistence.Id; +import javax.persistence.Table; @Entity @Data diff --git a/core/src/main/java/org/sadtech/consultant/database/entity/Person.java b/core/src/main/java/org/sadtech/consultant/database/entity/Person.java new file mode 100644 index 0000000..5a3e580 --- /dev/null +++ b/core/src/main/java/org/sadtech/consultant/database/entity/Person.java @@ -0,0 +1,33 @@ +package org.sadtech.consultant.database.entity; + +import lombok.Data; + +import javax.persistence.*; +import java.util.HashMap; +import java.util.Map; + +@Entity +@Data +public class Person { + + @Id + @GeneratedValue + private Long id; + + @Column(length = 20) + private String name; + + @Column(length = 20) + private String lastName; + + @Column(length = 20) + private String city; + + private String token; + + @ElementCollection + @CollectionTable(name="PersonSocialNetworks") + @MapKeyColumn(name="KeysSocialNetworks", length = 20) + private Map socialNetworks = new HashMap<>(); + +} diff --git a/core/src/main/java/org/sadtech/consultant/database/entity/SaveUnit.java b/core/src/main/java/org/sadtech/consultant/database/entity/SaveUnit.java index b5df08e..68c696c 100644 --- a/core/src/main/java/org/sadtech/consultant/database/entity/SaveUnit.java +++ b/core/src/main/java/org/sadtech/consultant/database/entity/SaveUnit.java @@ -4,6 +4,7 @@ import lombok.Data; import javax.persistence.Entity; import javax.persistence.Id; +import javax.persistence.Table; @Entity @Data 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 deleted file mode 100644 index df6497a..0000000 --- a/core/src/main/java/org/sadtech/consultant/database/entity/SourceMessage.java +++ /dev/null @@ -1,5 +0,0 @@ -package org.sadtech.consultant.database.entity; - -public enum SourceMessage { - VK, FACEBOOK; -} diff --git a/core/src/main/java/org/sadtech/consultant/database/entity/Unit.java b/core/src/main/java/org/sadtech/consultant/database/entity/Unit.java index a267686..247df8b 100644 --- a/core/src/main/java/org/sadtech/consultant/database/entity/Unit.java +++ b/core/src/main/java/org/sadtech/consultant/database/entity/Unit.java @@ -4,6 +4,7 @@ import lombok.Data; import javax.persistence.Entity; import javax.persistence.Id; +import javax.persistence.Table; @Entity @Data 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 deleted file mode 100644 index 17267fa..0000000 --- a/core/src/main/java/org/sadtech/consultant/database/entity/User.java +++ /dev/null @@ -1,24 +0,0 @@ -package org.sadtech.consultant.database.entity; - -import lombok.Data; - -import javax.persistence.*; -import java.util.List; - -@Entity -@Data -public class User { - - @Id - private Long id; - private String name; - private String token; - private String lastName; - private String city; - - @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 cedfccb..8dfc122 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,16 +1,15 @@ package org.sadtech.consultant.database.repository; -import org.sadtech.consultant.database.entity.Messages; +import org.sadtech.consultant.database.entity.Message; 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; -import java.util.List; -public interface MessageRepository extends JpaRepository { +public interface MessageRepository extends JpaRepository { - @Query("SELECT u FROM Messages u WHERE u.date > :date") - Collection getMessagesByRange(@Param("date") Long date); + @Query("SELECT u FROM Message u WHERE u.date > :date") + Collection getMessagesByRange(@Param("date") Long date); } diff --git a/core/src/main/java/org/sadtech/consultant/database/repository/PersonRepository.java b/core/src/main/java/org/sadtech/consultant/database/repository/PersonRepository.java new file mode 100644 index 0000000..a00fa83 --- /dev/null +++ b/core/src/main/java/org/sadtech/consultant/database/repository/PersonRepository.java @@ -0,0 +1,13 @@ +package org.sadtech.consultant.database.repository; + +import org.sadtech.consultant.database.entity.Person; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; + +public interface PersonRepository extends JpaRepository { + + @Query("SELECT u FROM Person u INNER JOIN u.socialNetworks p WHERE KEY(p) = :type2 AND VALUE(p) = :socialId") + Person getUserBySocialNetworksId(@Param("type2") String type, @Param("socialId") Integer socialNetworksId); + +} diff --git a/core/src/main/java/org/sadtech/consultant/database/repository/UserRepositoriy.java b/core/src/main/java/org/sadtech/consultant/database/repository/UserRepositoriy.java deleted file mode 100644 index f92f0d1..0000000 --- a/core/src/main/java/org/sadtech/consultant/database/repository/UserRepositoriy.java +++ /dev/null @@ -1,8 +0,0 @@ -package org.sadtech.consultant.database.repository; - -import org.sadtech.consultant.database.entity.User; -import org.springframework.data.jpa.repository.JpaRepository; - -public interface UserRepositoriy 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 9aa136e..53e2d37 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,12 +1,12 @@ package org.sadtech.consultant.database.service; -import org.sadtech.consultant.database.entity.Messages; +import org.sadtech.consultant.database.entity.Message; import java.util.List; public interface MessageService { - void addMessage(Messages message); + void addMessage(Message message); - List getMessageRange(Long date); + List getMessageRange(Long date); } diff --git a/core/src/main/java/org/sadtech/consultant/database/service/PersonService.java b/core/src/main/java/org/sadtech/consultant/database/service/PersonService.java new file mode 100644 index 0000000..db476ea --- /dev/null +++ b/core/src/main/java/org/sadtech/consultant/database/service/PersonService.java @@ -0,0 +1,14 @@ +package org.sadtech.consultant.database.service; + +import org.sadtech.consultant.database.entity.Person; + +import java.util.Map; + +public interface PersonService { + + void addUser(Person user); + void removeUser(Long id); + Person getUserByID(Long id); + Map getSocialNetwork(Long id); + Person getUserBySocialNetworksId(String type, Integer socialNetworksId); +} diff --git a/core/src/main/java/org/sadtech/consultant/database/service/UserService.java b/core/src/main/java/org/sadtech/consultant/database/service/UserService.java deleted file mode 100644 index 3e81573..0000000 --- a/core/src/main/java/org/sadtech/consultant/database/service/UserService.java +++ /dev/null @@ -1,10 +0,0 @@ -package org.sadtech.consultant.database.service; - -import org.sadtech.consultant.database.entity.User; - -public interface UserService { - - void addUser(User user); - void removeUser(Long id); - -} 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 23f6d65..f5d967b 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,6 +1,6 @@ package org.sadtech.consultant.database.service.impl; -import org.sadtech.consultant.database.entity.Messages; +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; @@ -14,12 +14,12 @@ public class MessageServiceImpl implements MessageService { @Autowired private MessageRepository repository; - public void addMessage(Messages message) { + public void addMessage(Message message) { repository.saveAndFlush(message); } @Override - public List getMessageRange(Long date) { - return (List) repository.getMessagesByRange(date); + public List getMessageRange(Long date) { + return (List) repository.getMessagesByRange(date); } } diff --git a/core/src/main/java/org/sadtech/consultant/database/service/impl/PersonServiceImpl.java b/core/src/main/java/org/sadtech/consultant/database/service/impl/PersonServiceImpl.java new file mode 100644 index 0000000..7acd025 --- /dev/null +++ b/core/src/main/java/org/sadtech/consultant/database/service/impl/PersonServiceImpl.java @@ -0,0 +1,41 @@ +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.service.PersonService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.Map; + +@Service +public class PersonServiceImpl implements PersonService { + + @Autowired + private PersonRepository repositoriy; + + @Override + public void addUser(Person user) { + repositoriy.saveAndFlush(user); + } + + @Override + public void removeUser(Long id) { + repositoriy.deleteById(id); + } + + @Override + public Person getUserByID(Long id) { + return repositoriy.getOne(id); + } + + @Override + public Map getSocialNetwork(Long id) { + return repositoriy.getOne(id).getSocialNetworks(); + } + + @Override + public Person getUserBySocialNetworksId(String type, Integer socialNetworksId) { + return repositoriy.getUserBySocialNetworksId(type, socialNetworksId); + } +} diff --git a/core/src/main/java/org/sadtech/consultant/database/service/impl/UserServiceImpl.java b/core/src/main/java/org/sadtech/consultant/database/service/impl/UserServiceImpl.java deleted file mode 100644 index fe6c606..0000000 --- a/core/src/main/java/org/sadtech/consultant/database/service/impl/UserServiceImpl.java +++ /dev/null @@ -1,22 +0,0 @@ -package org.sadtech.consultant.database.service.impl; - -import org.sadtech.consultant.database.entity.User; -import org.sadtech.consultant.database.repository.UserRepositoriy; -import org.sadtech.consultant.database.service.UserService; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; - -@Service -public class UserServiceImpl implements UserService { - - @Autowired - private UserRepositoriy repositoriy; - - public void addUser(User user) { - //@TODO: Написть добавление профиля в бд - } - - public void removeUser(Long id) { - //@TODO: Написать удаление профиля из бд - } -} diff --git a/core/src/main/java/org/sadtech/consultant/processing/MessageHandler.java b/core/src/main/java/org/sadtech/consultant/processing/MessageHandler.java index fdd84a8..232b33e 100644 --- a/core/src/main/java/org/sadtech/consultant/processing/MessageHandler.java +++ b/core/src/main/java/org/sadtech/consultant/processing/MessageHandler.java @@ -1,8 +1,7 @@ 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.sadtech.consultant.database.entity.Message; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Component; @@ -13,27 +12,26 @@ import java.util.List; @Component public class MessageHandler { - private MessageService service; + private MessageLogicService messageLogicService; + private PersonLogicService userLogicService; private long data; - public MessageHandler(MessageService service) { - this.service = service; + public MessageHandler(MessageLogicService messageLogicService, PersonLogicService userLogicService) { + this.messageLogicService = messageLogicService; + this.userLogicService = userLogicService; data = new Date().getTime() / 1000; } @Async public void processing() { while (true) { - List messages = service.getMessageRange(data); - for (Messages message : messages) { + List messages = messageLogicService.getMessageRange(data); + for (Message message : messages) { System.out.println(message); + + data = message.getDate(); } - try { - Thread.sleep(10000); - } catch (InterruptedException e) { - e.printStackTrace(); - } } } diff --git a/core/src/main/java/org/sadtech/consultant/processing/MessageLogicService.java b/core/src/main/java/org/sadtech/consultant/processing/MessageLogicService.java new file mode 100644 index 0000000..1ed910a --- /dev/null +++ b/core/src/main/java/org/sadtech/consultant/processing/MessageLogicService.java @@ -0,0 +1,25 @@ +package org.sadtech.consultant.processing; + +import org.sadtech.consultant.database.entity.Message; +import org.sadtech.consultant.database.service.MessageService; +import org.springframework.stereotype.Component; + +import java.util.List; + +@Component +public class MessageLogicService { + + private MessageService messageService; + + public MessageLogicService(MessageService messageService) { + this.messageService = messageService; + } + + public void addMessage(Message message) { + messageService.addMessage(message); + } + + public List getMessageRange(long data) { + return messageService.getMessageRange(data); + } +} diff --git a/core/src/main/java/org/sadtech/consultant/processing/PersonLogicService.java b/core/src/main/java/org/sadtech/consultant/processing/PersonLogicService.java new file mode 100644 index 0000000..28c672c --- /dev/null +++ b/core/src/main/java/org/sadtech/consultant/processing/PersonLogicService.java @@ -0,0 +1,29 @@ +package org.sadtech.consultant.processing; + +import org.sadtech.consultant.database.entity.Person; +import org.sadtech.consultant.database.service.PersonService; +import org.springframework.stereotype.Component; + +@Component +public class PersonLogicService { + + private PersonService personService; + + public PersonLogicService(PersonService userService) { + this.personService = userService; + } + + public boolean addUser(Person user) { + personService.addUser(user); + return true; + } + + public Person getUserById(Long id) { + return personService.getUserByID(id); + } + + public Person getUserBySocialId(String type, Integer id) { + return personService.getUserBySocialNetworksId(type, id); + } + +} 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 69f7e42..2f5d11e 100644 --- a/vk-bot/src/main/java/org/sadtech/vkbot/Main.java +++ b/vk-bot/src/main/java/org/sadtech/vkbot/Main.java @@ -23,8 +23,8 @@ public class Main { eventListener.listen(); Handled dispetcherHandler = context.getBean(Handled.class); dispetcherHandler.sortAndSend(); - MessageHandler messageHandler = context.getBean(MessageHandler.class); - messageHandler.processing(); +// 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/SourceMessage.java b/vk-bot/src/main/java/org/sadtech/vkbot/SourceMessage.java new file mode 100644 index 0000000..aca1153 --- /dev/null +++ b/vk-bot/src/main/java/org/sadtech/vkbot/SourceMessage.java @@ -0,0 +1,5 @@ +package org.sadtech.vkbot; + +public enum SourceMessage { + VK, FB +} 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 index dfb6e5d..dd1aaa5 100644 --- 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 @@ -2,25 +2,26 @@ 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.consultant.database.entity.Person; +import org.sadtech.consultant.processing.MessageLogicService; +import org.sadtech.consultant.processing.PersonLogicService; +import org.sadtech.consultant.database.entity.Message; +import org.sadtech.vkbot.SourceMessage; 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; + private MessageLogicService messageLogicService; + private PersonLogicService userLogicService; - public MessageHandlerVk(Observable dispetcherHandler, MessageService messageService) { - this.service = messageService; + public MessageHandlerVk(Observable dispetcherHandler, PersonLogicService userLogicService, MessageLogicService messageLogicService) { + this.userLogicService = userLogicService; + this.messageLogicService = messageLogicService; dispetcherHandler.registerObserver(this); } @@ -28,19 +29,28 @@ public class MessageHandlerVk implements Observer { 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); + com.vk.api.sdk.objects.messages.Message message = gson.fromJson(object.getAsJsonObject("object"), com.vk.api.sdk.objects.messages.Message.class); + sendProcessing(message); } } - private void send(Message userMessage) { + private void sendProcessing(com.vk.api.sdk.objects.messages.Message userMessage) { log.info(userMessage.getBody()); - Messages message = new Messages(); - message.setIdUser(Long.valueOf(userMessage.getUserId())); + Message message = new Message(); +// Person user = userLogicService.getUserBySocialId(SourceMessage.VK.name(), Long.valueOf(userMessage.getUserId())); + Person user; + user = new Person(); + user.getSocialNetworks().put(SourceMessage.VK.name(), userMessage.getUserId()); + user.setName("Name"); + user.setLastName("LastName"); + user.setCity("City"); + userLogicService.addUser(user); + log.info(user); + message.setUser(user); 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); + message.setSourceMessage(SourceMessage.VK.name()); + log.info(message); + messageLogicService.addMessage(message); } }