From c50ce8cd40995cb44e45ac541b46be7908f088f8 Mon Sep 17 00:00:00 2001 From: Mark Struchkov Date: Thu, 31 Jan 2019 00:59:00 +0300 Subject: [PATCH] =?UTF-8?q?=D0=91=D0=BE=D0=BB=D1=8C=D1=88=D0=B8=D0=B5=20?= =?UTF-8?q?=D0=B0=D1=80=D1=85=D0=B8=D1=82=D0=B5=D0=BA=D1=82=D1=83=D1=80?= =?UTF-8?q?=D0=BD=D1=8B=D0=B5=20=D0=B8=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Добавлен новый класс, для хранения терминальных команд от администратора бота * Некоторые интерфесы переписаны под дженерики * Архитектура стала более логичной и продуманной * Рефакторинг --- pom.xml | 5 ++ .../java/org/sadtech/vkbot/core/VkApi.java | 20 ++++- .../core/distribution/EventDistributable.java | 9 ++ .../core/distribution/EventSubscribe.java | 7 ++ .../distribution/impl/EventDistributorVK.java | 44 ++++++++++ .../distribution/impl/MailSubscriber.java | 71 ++++++++++++++++ .../distribution/impl/TerminalSubscriber.java | 33 ++++++++ .../core/{sender => entity}/MailSend.java | 2 +- .../sadtech/vkbot/core/handlers/Handled.java | 13 --- .../vkbot/core/handlers/ProcessableEvent.java | 12 --- .../handlers/impl/DispatcherHandlerVk.java | 44 ---------- .../core/handlers/impl/MailHandlerVk.java | 83 ------------------- .../vkbot/core/listener/EventListenerVk.java | 5 +- .../core/repository/EventRepository.java | 10 +-- .../vkbot/core/repository/MailRepository.java | 2 +- .../repository/impl/EventRepositoryQueue.java | 9 +- .../repository/impl/MailRepositoryList.java | 14 +++- .../repository/impl/PersonRepositoryMap.java | 15 +++- .../impl/TerminalComandRepositoryQueue.java | 27 ++++++ .../vkbot/core/sender/MailSanderVk.java | 1 + .../vkbot/core/sender/MailSandler.java | 2 + .../vkbot/core/service/PersonService.java | 4 + .../core/service/handlers/MailService.java | 3 + .../handlers/impl/MailServiceImpl.java | 27 ++++-- .../core/service/impl/EventServiceImpl.java | 2 +- .../core/service/impl/PersonServiceImpl.java | 29 ++++++- 26 files changed, 307 insertions(+), 186 deletions(-) create mode 100644 src/main/java/org/sadtech/vkbot/core/distribution/EventDistributable.java create mode 100644 src/main/java/org/sadtech/vkbot/core/distribution/EventSubscribe.java create mode 100644 src/main/java/org/sadtech/vkbot/core/distribution/impl/EventDistributorVK.java create mode 100644 src/main/java/org/sadtech/vkbot/core/distribution/impl/MailSubscriber.java create mode 100644 src/main/java/org/sadtech/vkbot/core/distribution/impl/TerminalSubscriber.java rename src/main/java/org/sadtech/vkbot/core/{sender => entity}/MailSend.java (97%) delete mode 100644 src/main/java/org/sadtech/vkbot/core/handlers/Handled.java delete mode 100644 src/main/java/org/sadtech/vkbot/core/handlers/ProcessableEvent.java delete mode 100644 src/main/java/org/sadtech/vkbot/core/handlers/impl/DispatcherHandlerVk.java delete mode 100644 src/main/java/org/sadtech/vkbot/core/handlers/impl/MailHandlerVk.java create mode 100644 src/main/java/org/sadtech/vkbot/core/repository/impl/TerminalComandRepositoryQueue.java diff --git a/pom.xml b/pom.xml index c03d021..69c1e4f 100644 --- a/pom.xml +++ b/pom.xml @@ -56,6 +56,11 @@ json ${json.ver} + + junit + junit + 4.12 + diff --git a/src/main/java/org/sadtech/vkbot/core/VkApi.java b/src/main/java/org/sadtech/vkbot/core/VkApi.java index b066905..cf0a0e9 100644 --- a/src/main/java/org/sadtech/vkbot/core/VkApi.java +++ b/src/main/java/org/sadtech/vkbot/core/VkApi.java @@ -1,15 +1,21 @@ package org.sadtech.vkbot.core; +import com.google.gson.Gson; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; 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.UserMin; import com.vk.api.sdk.objects.users.UserXtrCounters; import java.util.List; public class VkApi { +// public static final Logger log = Logger.getLogger(String.valueOf(VkApi.class)); + private VkApiClient vk; private GroupActor actor; @@ -18,14 +24,20 @@ public class VkApi { actor = vkConnect.getGroupActor(); } - public UserXtrCounters getUserVk(Integer id) { - List temp = null; + public UserMin getUserMini(Integer id) { + UserMin userMin = null; + Gson gson = new Gson(); try { - temp = vk.users().get(actor).userIds(String.valueOf(id)).execute(); + List temp = vk.users().get(actor).userIds(String.valueOf(id)).execute(); + JsonParser parser = new JsonParser(); + JsonObject object = parser.parse(temp.get(0).toString().substring(15)).getAsJsonObject(); + object.add("last_name", object.get("lastName")); + object.add("first_name", object.get("firstName")); + userMin = gson.fromJson(object, UserMin.class); } catch (ApiException | ClientException e) { e.printStackTrace(); } - return temp.get(0); + return userMin; } } diff --git a/src/main/java/org/sadtech/vkbot/core/distribution/EventDistributable.java b/src/main/java/org/sadtech/vkbot/core/distribution/EventDistributable.java new file mode 100644 index 0000000..b814451 --- /dev/null +++ b/src/main/java/org/sadtech/vkbot/core/distribution/EventDistributable.java @@ -0,0 +1,9 @@ +package org.sadtech.vkbot.core.distribution; + +public interface EventDistributable { + + void distribute() throws Exception; + + void registerSubscriber(String key, EventSubscribe o); + +} diff --git a/src/main/java/org/sadtech/vkbot/core/distribution/EventSubscribe.java b/src/main/java/org/sadtech/vkbot/core/distribution/EventSubscribe.java new file mode 100644 index 0000000..bd8dead --- /dev/null +++ b/src/main/java/org/sadtech/vkbot/core/distribution/EventSubscribe.java @@ -0,0 +1,7 @@ +package org.sadtech.vkbot.core.distribution; + +public interface EventSubscribe { + + void update(T object); + +} diff --git a/src/main/java/org/sadtech/vkbot/core/distribution/impl/EventDistributorVK.java b/src/main/java/org/sadtech/vkbot/core/distribution/impl/EventDistributorVK.java new file mode 100644 index 0000000..948d77e --- /dev/null +++ b/src/main/java/org/sadtech/vkbot/core/distribution/impl/EventDistributorVK.java @@ -0,0 +1,44 @@ +package org.sadtech.vkbot.core.distribution.impl; + +import com.google.gson.JsonObject; +import org.apache.log4j.Logger; +import org.sadtech.vkbot.core.distribution.EventDistributable; +import org.sadtech.vkbot.core.distribution.EventSubscribe; +import org.sadtech.vkbot.core.service.EventService; + +import java.util.HashMap; +import java.util.Map; + +public class EventDistributorVK implements EventDistributable, Runnable { + + public static final Logger log = Logger.getLogger(EventDistributorVK.class); + + private EventService eventService; + private Map eventDistributionMap = new HashMap<>(); + + public EventDistributorVK(EventService eventService) { + this.eventService = eventService; + } + + @Override + public void distribute() { + while (true) { + if (eventService.getJsonObjects().peek() != null) { + JsonObject event = eventService.getJsonObjects().poll(); + log.info("Главный дистрибьютор отправил событие дальше"); + eventDistributionMap.get(event.get("type").toString()).update(event); + + } + } + } + + @Override + public void registerSubscriber(String key, EventSubscribe o) { + eventDistributionMap.put(key, o); + } + + @Override + public void run() { + distribute(); + } +} diff --git a/src/main/java/org/sadtech/vkbot/core/distribution/impl/MailSubscriber.java b/src/main/java/org/sadtech/vkbot/core/distribution/impl/MailSubscriber.java new file mode 100644 index 0000000..0d2dcee --- /dev/null +++ b/src/main/java/org/sadtech/vkbot/core/distribution/impl/MailSubscriber.java @@ -0,0 +1,71 @@ +package org.sadtech.vkbot.core.distribution.impl; + +import com.google.gson.Gson; +import com.google.gson.JsonObject; +import com.vk.api.sdk.objects.messages.Message; +import org.apache.log4j.Logger; +import org.sadtech.vkbot.core.distribution.EventDistributable; +import org.sadtech.vkbot.core.distribution.EventSubscribe; +import org.sadtech.vkbot.core.service.handlers.MailService; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +public class MailSubscriber implements EventSubscribe, EventDistributable { + + public static final Logger log = Logger.getLogger(MailSubscriber.class); + + private MailService mailService; + + private Set admins = new HashSet<>(); + private Map eventDistributionMap = new HashMap<>(); + + public MailSubscriber(EventDistributable eventDistributable, MailService mailService) { + this.mailService = mailService; + eventDistributable.registerSubscriber("\"message_new\"", this); + } + + public MailService getMailService() { + return mailService; + } + + public void setMailService(MailService mailService) { + this.mailService = mailService; + } + + public void setAdmins(Set admins) { + this.admins = admins; + } + + public Set getAdmins() { + return admins; + } + + @Override + public void update(JsonObject object) { + log.info("Дистрибьютор получил событие - сообщение от пользователя"); + Gson gson = new Gson(); + Message userMessage = gson.fromJson(object.getAsJsonObject("object"), Message.class); + if (admins.contains(userMessage.getUserId())) { + log.info("Сообщение отправлено в репозиторий команд"); + eventDistributionMap.get("terminal").update(userMessage); + } else { + log.info("Сообщение отправленно на добавление в репозиторий"); + mailService.add(userMessage); + } + } + + + @Override + public void distribute() throws Exception { + + } + + @Override + public void registerSubscriber(String key, EventSubscribe o) { + eventDistributionMap.put(key, o); + } + +} diff --git a/src/main/java/org/sadtech/vkbot/core/distribution/impl/TerminalSubscriber.java b/src/main/java/org/sadtech/vkbot/core/distribution/impl/TerminalSubscriber.java new file mode 100644 index 0000000..d0ce956 --- /dev/null +++ b/src/main/java/org/sadtech/vkbot/core/distribution/impl/TerminalSubscriber.java @@ -0,0 +1,33 @@ +package org.sadtech.vkbot.core.distribution.impl; + +import org.apache.log4j.Logger; +import org.sadtech.vkbot.core.distribution.EventSubscribe; +import org.sadtech.vkbot.core.entity.Mail; +import org.sadtech.vkbot.core.service.handlers.MailService; + +public class TerminalSubscriber implements EventSubscribe { + + public static final Logger log = Logger.getLogger(TerminalSubscriber.class); + + private MailService mailService; + + public TerminalSubscriber(MailSubscriber mailSubscriber, MailService mailService) { + this.mailService = mailService; + mailSubscriber.registerSubscriber("terminal", this); + } + + public MailService getMailService() { + return mailService; + } + + public void setMailService(MailService mailService) { + this.mailService = mailService; + } + + @Override + public void update(Mail object) { + log.info("Сообщение отправленно на добавление в репозиторий команд"); + mailService.add(object); + } + +} diff --git a/src/main/java/org/sadtech/vkbot/core/sender/MailSend.java b/src/main/java/org/sadtech/vkbot/core/entity/MailSend.java similarity index 97% rename from src/main/java/org/sadtech/vkbot/core/sender/MailSend.java rename to src/main/java/org/sadtech/vkbot/core/entity/MailSend.java index d954f60..4982ffb 100644 --- a/src/main/java/org/sadtech/vkbot/core/sender/MailSend.java +++ b/src/main/java/org/sadtech/vkbot/core/entity/MailSend.java @@ -1,4 +1,4 @@ -package org.sadtech.vkbot.core.sender; +package org.sadtech.vkbot.core.entity; public class MailSend { diff --git a/src/main/java/org/sadtech/vkbot/core/handlers/Handled.java b/src/main/java/org/sadtech/vkbot/core/handlers/Handled.java deleted file mode 100644 index 65efdae..0000000 --- a/src/main/java/org/sadtech/vkbot/core/handlers/Handled.java +++ /dev/null @@ -1,13 +0,0 @@ -package org.sadtech.vkbot.core.handlers; - -import org.sadtech.vkbot.core.service.EventService; - -public interface Handled extends Runnable { - - void sortAndSend() throws Exception; - - void registerObserver(String key, ProcessableEvent o); - - EventService getResponsibleService(); - -} diff --git a/src/main/java/org/sadtech/vkbot/core/handlers/ProcessableEvent.java b/src/main/java/org/sadtech/vkbot/core/handlers/ProcessableEvent.java deleted file mode 100644 index e4e0d8c..0000000 --- a/src/main/java/org/sadtech/vkbot/core/handlers/ProcessableEvent.java +++ /dev/null @@ -1,12 +0,0 @@ -package org.sadtech.vkbot.core.handlers; - -import com.google.gson.JsonObject; -import org.sadtech.vkbot.core.service.handlers.SortEventService; - -public interface ProcessableEvent { - - void update(JsonObject object); - - SortEventService getServiceEventData(); - -} diff --git a/src/main/java/org/sadtech/vkbot/core/handlers/impl/DispatcherHandlerVk.java b/src/main/java/org/sadtech/vkbot/core/handlers/impl/DispatcherHandlerVk.java deleted file mode 100644 index 6d33630..0000000 --- a/src/main/java/org/sadtech/vkbot/core/handlers/impl/DispatcherHandlerVk.java +++ /dev/null @@ -1,44 +0,0 @@ -package org.sadtech.vkbot.core.handlers.impl; - -import com.google.gson.JsonObject; -import org.sadtech.vkbot.core.handlers.Handled; -import org.sadtech.vkbot.core.handlers.ProcessableEvent; -import org.sadtech.vkbot.core.service.EventService; - -import java.util.HashMap; -import java.util.Map; - -public class DispatcherHandlerVk implements Handled, Runnable { - - private EventService responsibleService; - private Map processableEventMap = new HashMap<>(); - - public DispatcherHandlerVk(EventService responsibleService) { - this.responsibleService = responsibleService; - } - - @Override - public void sortAndSend() { - while (true) { - if (responsibleService.getJsonObjects().peek() != null) { - JsonObject event = responsibleService.getJsonObjects().poll(); - processableEventMap.get(event.get("type").toString()).update(event); - } - } - } - - @Override - public void registerObserver(String key, ProcessableEvent o) { - processableEventMap.put(key, o); - } - - @Override - public EventService getResponsibleService() { - return responsibleService; - } - - @Override - public void run() { - sortAndSend(); - } -} diff --git a/src/main/java/org/sadtech/vkbot/core/handlers/impl/MailHandlerVk.java b/src/main/java/org/sadtech/vkbot/core/handlers/impl/MailHandlerVk.java deleted file mode 100644 index 5954524..0000000 --- a/src/main/java/org/sadtech/vkbot/core/handlers/impl/MailHandlerVk.java +++ /dev/null @@ -1,83 +0,0 @@ -package org.sadtech.vkbot.core.handlers.impl; - -import com.google.gson.Gson; -import com.google.gson.JsonObject; -import com.vk.api.sdk.objects.messages.Message; -import org.apache.log4j.Logger; -import org.sadtech.vkbot.core.entity.Mail; -import org.sadtech.vkbot.core.entity.Person; -import org.sadtech.vkbot.core.handlers.Handled; -import org.sadtech.vkbot.core.handlers.ProcessableEvent; -import org.sadtech.vkbot.core.service.PersonService; -import org.sadtech.vkbot.core.service.handlers.MailService; -import org.sadtech.vkbot.core.service.handlers.SortEventService; -import org.sadtech.vkbot.core.service.handlers.impl.MailServiceImpl; -import org.sadtech.vkbot.core.service.impl.PersonServiceImpl; - -public class MailHandlerVk implements ProcessableEvent { - - public static final Logger log = Logger.getLogger(MailHandlerVk.class); - - private PersonService personService; - private MailService mailService; - - public MailHandlerVk(Handled dispatcherHandler) { - personService = new PersonServiceImpl(); - mailService = new MailServiceImpl(); - dispatcherHandler.registerObserver("\"message_new\"", this); - } - - public MailHandlerVk(Handled dispatcherHandler, PersonService personService, MailService mailService) { - this.personService = personService; - this.mailService = mailService; - dispatcherHandler.registerObserver("\"message_new\"", this); - } - - public PersonService getPersonService() { - return personService; - } - - public void setPersonService(PersonService personService) { - this.personService = personService; - } - - public MailService getMailService() { - return mailService; - } - - public void setMailService(MailService mailService) { - this.mailService = mailService; - } - - @Override - public void update(JsonObject object) { - Gson gson = new Gson(); - Message userMessage = gson.fromJson(object.getAsJsonObject("object"), Message.class); - if (!personService.checkPerson(userMessage.getUserId())) { - Person person = new Person(); - person.setId(userMessage.getUserId()); - log.info("Пользователь отправлен на добавление в репозиторий"); - personService.add(person); - } - log.info("Сообщение отправленно на добавление в репозиторий"); - mailService.add(createMail(userMessage)); - } - - private Mail createMail(Message message) { - Mail mail = new Mail(); - mail.setDate(message.getDate()); - mail.setBody(message.getBody()); - mail.setId(message.getId()); - if (personService.checkPerson(message.getUserId())) { - mail.setPerson(personService.get(message.getUserId())); - } else { - return null; - } - return mail; - } - - @Override - public SortEventService getServiceEventData() { - return mailService; - } -} diff --git a/src/main/java/org/sadtech/vkbot/core/listener/EventListenerVk.java b/src/main/java/org/sadtech/vkbot/core/listener/EventListenerVk.java index 3a3f988..81b7e68 100644 --- a/src/main/java/org/sadtech/vkbot/core/listener/EventListenerVk.java +++ b/src/main/java/org/sadtech/vkbot/core/listener/EventListenerVk.java @@ -11,7 +11,6 @@ import com.vk.api.sdk.exceptions.ClientException; import com.vk.api.sdk.objects.groups.responses.GetLongPollServerResponse; import org.apache.log4j.Logger; import org.sadtech.vkbot.core.VkConnect; -import org.sadtech.vkbot.core.repository.EventRepository; import org.sadtech.vkbot.core.repository.impl.EventRepositoryQueue; import org.sadtech.vkbot.core.service.EventService; import org.sadtech.vkbot.core.service.impl.EventServiceImpl; @@ -36,10 +35,10 @@ public class EventListenerVk implements EventListener, Runnable { longPoll = new LongPoll(vk); } - public EventListenerVk(VkConnect vkConnect, EventRepository eventRepository) { + public EventListenerVk(VkConnect vkConnect, EventService eventService) { this.vk = vkConnect.getVkApiClient(); this.actor = vkConnect.getGroupActor(); - this.eventService = new EventServiceImpl(eventRepository); + this.eventService = eventService; longPoll = new LongPoll(vk); } diff --git a/src/main/java/org/sadtech/vkbot/core/repository/EventRepository.java b/src/main/java/org/sadtech/vkbot/core/repository/EventRepository.java index d725028..d98fedd 100644 --- a/src/main/java/org/sadtech/vkbot/core/repository/EventRepository.java +++ b/src/main/java/org/sadtech/vkbot/core/repository/EventRepository.java @@ -1,17 +1,13 @@ package org.sadtech.vkbot.core.repository; -import com.google.gson.JsonObject; - import java.util.Queue; -public interface EventRepository { +public interface EventRepository { - void add(JsonObject jsonObject); - - void remove(int id); + void add(T dataObject); void cleanAll(); - Queue getJsonObjects(); + Queue getEventQueue(); } diff --git a/src/main/java/org/sadtech/vkbot/core/repository/MailRepository.java b/src/main/java/org/sadtech/vkbot/core/repository/MailRepository.java index 0f263f8..54bcf12 100644 --- a/src/main/java/org/sadtech/vkbot/core/repository/MailRepository.java +++ b/src/main/java/org/sadtech/vkbot/core/repository/MailRepository.java @@ -6,7 +6,7 @@ import java.util.List; public interface MailRepository { - void add(Mail message); + void add(Mail mail); List getMailByTime(Integer timeFrom, Integer timeTo); diff --git a/src/main/java/org/sadtech/vkbot/core/repository/impl/EventRepositoryQueue.java b/src/main/java/org/sadtech/vkbot/core/repository/impl/EventRepositoryQueue.java index 54d6d65..61ba2b4 100644 --- a/src/main/java/org/sadtech/vkbot/core/repository/impl/EventRepositoryQueue.java +++ b/src/main/java/org/sadtech/vkbot/core/repository/impl/EventRepositoryQueue.java @@ -6,7 +6,7 @@ import org.sadtech.vkbot.core.repository.EventRepository; import java.util.Queue; import java.util.concurrent.ConcurrentLinkedQueue; -public class EventRepositoryQueue implements EventRepository { +public class EventRepositoryQueue implements EventRepository { private Queue jsonObjects = new ConcurrentLinkedQueue<>(); @@ -15,17 +15,12 @@ public class EventRepositoryQueue implements EventRepository { jsonObjects.offer(jsonObject); } - @Override - public void remove(int id) { - jsonObjects.remove(id); - } - @Override public void cleanAll() { jsonObjects.clear(); } - public Queue getJsonObjects() { + public Queue getEventQueue() { return jsonObjects; } } diff --git a/src/main/java/org/sadtech/vkbot/core/repository/impl/MailRepositoryList.java b/src/main/java/org/sadtech/vkbot/core/repository/impl/MailRepositoryList.java index 522fa58..4391bec 100644 --- a/src/main/java/org/sadtech/vkbot/core/repository/impl/MailRepositoryList.java +++ b/src/main/java/org/sadtech/vkbot/core/repository/impl/MailRepositoryList.java @@ -1,12 +1,15 @@ package org.sadtech.vkbot.core.repository.impl; import org.sadtech.vkbot.core.entity.Mail; +import org.sadtech.vkbot.core.repository.EventRepository; import org.sadtech.vkbot.core.repository.MailRepository; import java.util.ArrayList; import java.util.List; +import java.util.Queue; +import java.util.concurrent.ConcurrentLinkedQueue; -public class MailRepositoryList implements MailRepository { +public class MailRepositoryList implements EventRepository, MailRepository { private List messages = new ArrayList<>(); @@ -16,6 +19,15 @@ public class MailRepositoryList implements MailRepository { } @Override + public void cleanAll() { + messages.clear(); + } + + @Override + public Queue getEventQueue() { + return new ConcurrentLinkedQueue<>(messages); + } + public List getMailByTime(Integer timeFrom, Integer timeTo) { ArrayList mails = new ArrayList<>(); for (int i = messages.size() - 1; i >= 0; i--) { diff --git a/src/main/java/org/sadtech/vkbot/core/repository/impl/PersonRepositoryMap.java b/src/main/java/org/sadtech/vkbot/core/repository/impl/PersonRepositoryMap.java index b076236..cca2610 100644 --- a/src/main/java/org/sadtech/vkbot/core/repository/impl/PersonRepositoryMap.java +++ b/src/main/java/org/sadtech/vkbot/core/repository/impl/PersonRepositoryMap.java @@ -1,12 +1,15 @@ package org.sadtech.vkbot.core.repository.impl; import org.sadtech.vkbot.core.entity.Person; +import org.sadtech.vkbot.core.repository.EventRepository; import org.sadtech.vkbot.core.repository.PersonRepository; import java.util.HashMap; import java.util.Map; +import java.util.Queue; +import java.util.concurrent.ConcurrentLinkedQueue; -public class PersonRepositoryMap implements PersonRepository { +public class PersonRepositoryMap implements EventRepository, PersonRepository { private Map personMap = new HashMap<>(); @@ -15,6 +18,16 @@ public class PersonRepositoryMap implements PersonRepository { personMap.put(person.getId(), person); } + @Override + public void cleanAll() { + personMap.clear(); + } + + @Override + public Queue getEventQueue() { + return new ConcurrentLinkedQueue<>(personMap.values()); + } + @Override public Person get(Integer id) { return personMap.get(id); diff --git a/src/main/java/org/sadtech/vkbot/core/repository/impl/TerminalComandRepositoryQueue.java b/src/main/java/org/sadtech/vkbot/core/repository/impl/TerminalComandRepositoryQueue.java new file mode 100644 index 0000000..90e178a --- /dev/null +++ b/src/main/java/org/sadtech/vkbot/core/repository/impl/TerminalComandRepositoryQueue.java @@ -0,0 +1,27 @@ +package org.sadtech.vkbot.core.repository.impl; + +import org.sadtech.vkbot.core.entity.Mail; +import org.sadtech.vkbot.core.repository.EventRepository; + +import java.util.Queue; +import java.util.concurrent.ConcurrentLinkedQueue; + +public class TerminalComandRepositoryQueue implements EventRepository { + + private Queue mailQueue = new ConcurrentLinkedQueue<>(); + + @Override + public void add(Mail dataObject) { + mailQueue.offer(dataObject); + } + + @Override + public void cleanAll() { + mailQueue.clear(); + } + + @Override + public Queue getEventQueue() { + return mailQueue; + } +} diff --git a/src/main/java/org/sadtech/vkbot/core/sender/MailSanderVk.java b/src/main/java/org/sadtech/vkbot/core/sender/MailSanderVk.java index ba2de4e..72978d0 100644 --- a/src/main/java/org/sadtech/vkbot/core/sender/MailSanderVk.java +++ b/src/main/java/org/sadtech/vkbot/core/sender/MailSanderVk.java @@ -7,6 +7,7 @@ import com.vk.api.sdk.exceptions.ClientException; import com.vk.api.sdk.queries.messages.MessagesSendQuery; import org.apache.log4j.Logger; import org.sadtech.vkbot.core.VkConnect; +import org.sadtech.vkbot.core.entity.MailSend; public class MailSanderVk implements MailSandler { diff --git a/src/main/java/org/sadtech/vkbot/core/sender/MailSandler.java b/src/main/java/org/sadtech/vkbot/core/sender/MailSandler.java index 54989e0..511cb76 100644 --- a/src/main/java/org/sadtech/vkbot/core/sender/MailSandler.java +++ b/src/main/java/org/sadtech/vkbot/core/sender/MailSandler.java @@ -1,5 +1,7 @@ package org.sadtech.vkbot.core.sender; +import org.sadtech.vkbot.core.entity.MailSend; + public interface MailSandler { void send(MailSend mailSend); diff --git a/src/main/java/org/sadtech/vkbot/core/service/PersonService.java b/src/main/java/org/sadtech/vkbot/core/service/PersonService.java index d5d9777..7769688 100644 --- a/src/main/java/org/sadtech/vkbot/core/service/PersonService.java +++ b/src/main/java/org/sadtech/vkbot/core/service/PersonService.java @@ -1,6 +1,7 @@ package org.sadtech.vkbot.core.service; import com.vk.api.sdk.objects.users.User; +import com.vk.api.sdk.objects.users.UserMin; import org.sadtech.vkbot.core.entity.Person; public interface PersonService { @@ -9,8 +10,11 @@ public interface PersonService { void add(Person person); + void add(UserMin userMin); + Person get(Integer id); boolean checkPerson(Integer idPerson); + Person createPerson(Integer userId); } diff --git a/src/main/java/org/sadtech/vkbot/core/service/handlers/MailService.java b/src/main/java/org/sadtech/vkbot/core/service/handlers/MailService.java index a78bf27..89ac658 100644 --- a/src/main/java/org/sadtech/vkbot/core/service/handlers/MailService.java +++ b/src/main/java/org/sadtech/vkbot/core/service/handlers/MailService.java @@ -1,9 +1,12 @@ package org.sadtech.vkbot.core.service.handlers; +import com.vk.api.sdk.objects.messages.Message; import org.sadtech.vkbot.core.entity.Mail; public interface MailService extends SortEventService { void add(Mail mail); + void add(Message message); + } diff --git a/src/main/java/org/sadtech/vkbot/core/service/handlers/impl/MailServiceImpl.java b/src/main/java/org/sadtech/vkbot/core/service/handlers/impl/MailServiceImpl.java index 0d9e8c6..209aea2 100644 --- a/src/main/java/org/sadtech/vkbot/core/service/handlers/impl/MailServiceImpl.java +++ b/src/main/java/org/sadtech/vkbot/core/service/handlers/impl/MailServiceImpl.java @@ -1,10 +1,11 @@ package org.sadtech.vkbot.core.service.handlers.impl; +import com.vk.api.sdk.objects.messages.Message; import org.apache.log4j.Logger; import org.sadtech.vkbot.core.entity.Mail; import org.sadtech.vkbot.core.entity.Person; import org.sadtech.vkbot.core.repository.MailRepository; -import org.sadtech.vkbot.core.repository.impl.MailRepositoryList; +import org.sadtech.vkbot.core.service.PersonService; import org.sadtech.vkbot.core.service.handlers.MailService; import java.util.ArrayList; @@ -17,18 +18,31 @@ public class MailServiceImpl implements MailService { public static final Logger log = Logger.getLogger(MailServiceImpl.class); private MailRepository messageRepository; + private PersonService personService; - public MailServiceImpl() { - this.messageRepository = new MailRepositoryList(); - } - - public MailServiceImpl(MailRepository messageRepository) { + public MailServiceImpl(MailRepository messageRepository, PersonService personService) { this.messageRepository = messageRepository; + this.personService = personService; } @Override public void add(Mail mail) { messageRepository.add(mail); + log.info("Сообщение добавлено в репозиторий"); + } + + @Override + public void add(Message message) { + Mail mail = new Mail(); + mail.setDate(message.getDate()); + mail.setBody(message.getBody()); + mail.setId(message.getId()); + if (!personService.checkPerson(message.getUserId())) { + personService.add(personService.createPerson(message.getUserId())); + } + mail.setPerson(personService.get(message.getUserId())); + log.info("Сообщение добавлено в репозиторий"); + messageRepository.add(mail); } @Override @@ -65,4 +79,5 @@ public class MailServiceImpl implements MailService { log.info("Запрос на получение сообщений в интервале от " + timeFrom + " до " + timeTo); return messageRepository.getMailByTime(timeFrom, timeTo); } + } diff --git a/src/main/java/org/sadtech/vkbot/core/service/impl/EventServiceImpl.java b/src/main/java/org/sadtech/vkbot/core/service/impl/EventServiceImpl.java index 9e686f0..e7fc7c2 100644 --- a/src/main/java/org/sadtech/vkbot/core/service/impl/EventServiceImpl.java +++ b/src/main/java/org/sadtech/vkbot/core/service/impl/EventServiceImpl.java @@ -31,7 +31,7 @@ public class EventServiceImpl implements EventService { @Override public Queue getJsonObjects() { - return eventRepository.getJsonObjects(); + return eventRepository.getEventQueue(); } @Override diff --git a/src/main/java/org/sadtech/vkbot/core/service/impl/PersonServiceImpl.java b/src/main/java/org/sadtech/vkbot/core/service/impl/PersonServiceImpl.java index 5a695a8..10a5af3 100644 --- a/src/main/java/org/sadtech/vkbot/core/service/impl/PersonServiceImpl.java +++ b/src/main/java/org/sadtech/vkbot/core/service/impl/PersonServiceImpl.java @@ -1,7 +1,10 @@ package org.sadtech.vkbot.core.service.impl; import com.vk.api.sdk.objects.users.User; +import com.vk.api.sdk.objects.users.UserMin; import org.apache.log4j.Logger; +import org.sadtech.vkbot.core.VkApi; +import org.sadtech.vkbot.core.VkConnect; import org.sadtech.vkbot.core.entity.Person; import org.sadtech.vkbot.core.repository.PersonRepository; import org.sadtech.vkbot.core.repository.impl.PersonRepositoryMap; @@ -12,13 +15,16 @@ public class PersonServiceImpl implements PersonService { public static final Logger log = Logger.getLogger(PersonServiceImpl.class); private PersonRepository personRepository; + private VkApi vkApi; - public PersonServiceImpl() { + public PersonServiceImpl(VkConnect vkConnect) { this.personRepository = new PersonRepositoryMap(); + vkApi = new VkApi(vkConnect); } - public PersonServiceImpl(PersonRepository personRepository) { + public PersonServiceImpl(PersonRepository personRepository, VkConnect vkConnect) { this.personRepository = personRepository; + vkApi = new VkApi(vkConnect); } @Override @@ -37,6 +43,15 @@ public class PersonServiceImpl implements PersonService { log.info("Пользователь добавлен в репозиторий"); } + @Override + public void add(UserMin userMin) { + Person person = new Person(); + person.setId(userMin.getId()); + person.setFirstName(userMin.getFirstName()); + person.setLastName(userMin.getLastName()); + personRepository.add(person); + } + @Override public Person get(Integer id) { return personRepository.get(id); @@ -47,4 +62,14 @@ public class PersonServiceImpl implements PersonService { log.info("Проверка наличия пользователя в репозитории"); return get(idPerson) != null; } + + @Override + public Person createPerson(Integer userId) { + Person person = new Person(); + UserMin userMin = vkApi.getUserMini(userId); + person.setId(userMin.getId()); + person.setLastName(userMin.getLastName()); + person.setFirstName(userMin.getFirstName()); + return person; + } }