Разбиение на модули

This commit is contained in:
Mark Struchkov 2019-01-24 22:20:26 +03:00
parent c0a58e61e1
commit b5a8680f09
36 changed files with 1340 additions and 0 deletions

60
pom.xml Normal file
View File

@ -0,0 +1,60 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.sadtech.vkbot</groupId>
<artifactId>core</artifactId>
<version>0.3-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>7</source>
<target>7</target>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<gson.ver>2.8.5</gson.ver>
<vksdk.ver>0.5.13-SNAPSHOT</vksdk.ver>
<log4j.ver>1.2.17</log4j.ver>
<json.ver>20180813</json.ver>
</properties>
<dependencies>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>${gson.ver}</version>
</dependency>
<dependency>
<groupId>com.vk.api</groupId>
<artifactId>sdk</artifactId>
<version>${vksdk.ver}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.ver}</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>${json.ver}</version>
</dependency>
<dependency>
<groupId>org.sadtech.autoresponder</groupId>
<artifactId>autoresponder</artifactId>
<version>1.1.2-SNAPHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,31 @@
package org.sadtech.vkbot.core;
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.UserXtrCounters;
import java.util.List;
public class VkApi {
private VkApiClient vk;
private GroupActor actor;
public VkApi(VkConnect vkConnect) {
vk = vkConnect.getVkApiClient();
actor = vkConnect.getGroupActor();
}
public UserXtrCounters getUserVk(Integer id) {
List<UserXtrCounters> temp = null;
try {
temp = vk.users().get(actor).userIds(String.valueOf(id)).execute();
} catch (ApiException | ClientException e) {
e.printStackTrace();
}
return temp.get(0);
}
}

View File

@ -0,0 +1,83 @@
package org.sadtech.vkbot.core;
import java.util.Objects;
public class VkConfig {
private String groupToken;
private Integer groupId;
private String serviceToken;
private Integer appId;
private String groupSecretKey;
private String groupPublicKey;
public VkConfig() {
}
public String getGroupToken() {
return groupToken;
}
public void setGroupToken(String groupToken) {
this.groupToken = groupToken;
}
public Integer getGroupId() {
return groupId;
}
public void setGroupId(Integer groupId) {
this.groupId = groupId;
}
public String getServiceToken() {
return serviceToken;
}
public void setServiceToken(String serviceToken) {
this.serviceToken = serviceToken;
}
public Integer getAppId() {
return appId;
}
public void setAppId(Integer appId) {
this.appId = appId;
}
public String getGroupSecretKey() {
return groupSecretKey;
}
public void setGroupSecretKey(String groupSecretKey) {
this.groupSecretKey = groupSecretKey;
}
public String getGroupPublicKey() {
return groupPublicKey;
}
public void setGroupPublicKey(String groupPublicKey) {
this.groupPublicKey = groupPublicKey;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
VkConfig vkConfig = (VkConfig) o;
return Objects.equals(groupToken, vkConfig.groupToken) &&
Objects.equals(groupId, vkConfig.groupId) &&
Objects.equals(serviceToken, vkConfig.serviceToken) &&
Objects.equals(appId, vkConfig.appId) &&
Objects.equals(groupSecretKey, vkConfig.groupSecretKey) &&
Objects.equals(groupPublicKey, vkConfig.groupPublicKey);
}
@Override
public int hashCode() {
return Objects.hash(groupToken, groupId, serviceToken, appId, groupSecretKey, groupPublicKey);
}
}

View File

@ -0,0 +1,58 @@
package org.sadtech.vkbot.core;
import com.vk.api.sdk.client.VkApiClient;
import com.vk.api.sdk.client.actors.GroupActor;
import com.vk.api.sdk.client.actors.ServiceActor;
import com.vk.api.sdk.httpclient.HttpTransportClient;
public class VkConnect {
private VkConfig vkConfig;
private VkApiClient vkApiClient;
private GroupActor groupActor;
private ServiceActor serviceActor;
public VkConnect(VkConfig vkConfig) {
this.vkConfig = vkConfig;
}
public VkApiClient getVkApiClient() {
if (vkApiClient != null) {
return vkApiClient;
} else {
initVkApiClient();
return vkApiClient;
}
}
public GroupActor getGroupActor() {
if (groupActor != null) {
return groupActor;
} else {
initGroupActor();
return groupActor;
}
}
public ServiceActor getServiceActor() {
if (serviceActor != null) {
return serviceActor;
} else {
initServiceActor();
return serviceActor;
}
}
private void initVkApiClient() {
vkApiClient = new VkApiClient(HttpTransportClient.getInstance());
}
private void initGroupActor() {
groupActor = new GroupActor(vkConfig.getGroupId(), vkConfig.getGroupToken());
}
private void initServiceActor() {
serviceActor = new ServiceActor(vkConfig.getAppId(), vkConfig.getServiceToken());
}
}

View File

@ -0,0 +1,70 @@
package org.sadtech.vkbot.core.entity;
import java.util.Objects;
public class Mail {
private Integer id;
private Integer date;
private Person person;
private String body;
public Mail() {
}
public Mail(Integer id, Integer date, Person userId, String body) {
this.id = id;
this.date = date;
this.person = userId;
this.body = body;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getDate() {
return date;
}
public void setDate(Integer date) {
this.date = date;
}
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Mail mail = (Mail) o;
return Objects.equals(id, mail.id) &&
Objects.equals(date, mail.date) &&
Objects.equals(person, mail.person) &&
Objects.equals(body, mail.body);
}
@Override
public int hashCode() {
return Objects.hash(id, date, person, body);
}
}

View File

@ -0,0 +1,81 @@
package org.sadtech.vkbot.core.entity;
import java.util.Objects;
public class Person {
private Integer id;
private String firstName;
private String lastName;
private Integer sex;
private String city;
public Person() {
}
public Person(Integer id, String firstName, String lastName, Integer sex, String city) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.sex = sex;
this.city = city;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Integer getSex() {
return sex;
}
public void setSex(Integer sex) {
this.sex = sex;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Person person = (Person) o;
return Objects.equals(id, person.id) &&
Objects.equals(firstName, person.firstName) &&
Objects.equals(lastName, person.lastName) &&
Objects.equals(sex, person.sex) &&
Objects.equals(city, person.city);
}
@Override
public int hashCode() {
return Objects.hash(id, firstName, lastName, sex, city);
}
}

View File

@ -0,0 +1,10 @@
package org.sadtech.vkbot.core.entity.unit;
import org.sadtech.vkbot.autoresponder.DataService;
public class SaveAnswer extends TextAnswer {
private SaveAnswer prevSaveAnswer;
private DataService dataService;
}

View File

@ -0,0 +1,16 @@
package org.sadtech.vkbot.core.entity.unit;
import org.sadtech.vkbot.core.keyboard.KeyBoard;
public class TextAndKeyBoardAnswer extends TextAnswer {
KeyBoard keyBoard;
public KeyBoard getKeyBoard() {
return keyBoard;
}
public void setKeyBoard(KeyBoard keyBoard) {
this.keyBoard = keyBoard;
}
}

View File

@ -0,0 +1,16 @@
package org.sadtech.vkbot.core.entity.unit;
import org.sadtech.autoresponder.entity.Unit;
public class TextAnswer extends Unit {
private String answer;
public String getAnswer() {
return answer;
}
public void setAnswer(String answer) {
this.answer = answer;
}
}

View File

@ -0,0 +1,13 @@
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();
}

View File

@ -0,0 +1,12 @@
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();
}

View File

@ -0,0 +1,44 @@
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<String, ProcessableEvent> 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();
}
}

View File

@ -0,0 +1,83 @@
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;
}
}

View File

@ -0,0 +1,74 @@
package org.sadtech.vkbot.core.keyboard;
import org.json.JSONObject;
public class ButtonKeyBoard {
private String type = "text";
private String payload;
private String label;
private ColorButton color = ColorButton.DEFAULT;
public ButtonKeyBoard() {
}
public ButtonKeyBoard(String type, String payload, String label, ColorButton color) {
this.type = type;
this.payload = payload;
this.label = label;
this.color = color;
}
public JSONObject getButton() {
JSONObject newButton = new JSONObject();
newButton.put("color", color.toString().toLowerCase());
newButton.put("action", generateAction());
return newButton;
}
private JSONObject generateAction() {
JSONObject action = new JSONObject();
action.put("type", type);
action.put("payload", payload);
action.put("label", label);
return action;
}
public static Builder builder() {
return new ButtonKeyBoard().new Builder();
}
public class Builder {
private Builder() {
}
public Builder setColor(ColorButton color) {
ButtonKeyBoard.this.color = color;
return this;
}
public Builder setLabel(String label) {
ButtonKeyBoard.this.label = label;
return this;
}
public Builder setPayload(String payload) {
ButtonKeyBoard.this.payload = payload;
return this;
}
public Builder setType(String type) {
ButtonKeyBoard.this.type = type;
return this;
}
public ButtonKeyBoard build() {
return ButtonKeyBoard.this;
}
}
}

View File

@ -0,0 +1,7 @@
package org.sadtech.vkbot.core.keyboard;
public enum ColorButton {
PRIMARY, DEFAULT, NEGATIVE, POSITIVE
}

View File

@ -0,0 +1,58 @@
package org.sadtech.vkbot.core.keyboard;
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
public class KeyBoard {
private List<LineKeyBoard> lineKeyBoards = new ArrayList<>();
public KeyBoard() {
}
public KeyBoard(List<LineKeyBoard> lineKeyBoards) {
this.lineKeyBoards = lineKeyBoards;
}
public JSONObject getKeyboard(Boolean one_time) {
JSONObject keyboard = new JSONObject();
keyboard.put("one_time", one_time);
JSONArray menuLine = new JSONArray();
for (LineKeyBoard lineKeyboard : lineKeyBoards) {
menuLine.put(lineKeyboard.getLine());
}
keyboard.put("buttons", menuLine);
return keyboard;
}
public void addLine(LineKeyBoard lineKeyBoard) {
lineKeyBoards.add(lineKeyBoard);
}
public static Builder builder() {
return new KeyBoard().new Builder();
}
public class Builder {
private Builder() {
}
public Builder setLineKeyBoard(LineKeyBoard lineKeyBoard) {
KeyBoard.this.lineKeyBoards.add(lineKeyBoard);
return this;
}
public KeyBoard build() {
return KeyBoard.this;
}
}
}

View File

@ -0,0 +1,31 @@
package org.sadtech.vkbot.core.keyboard;
import org.json.JSONObject;
import java.util.List;
public class KeyBoardService {
private JSONObject keyBoardYesNo = generateKeyBoardYesNo();
private JSONObject generateKeyBoardYesNo() {
ButtonKeyBoard yesButton = ButtonKeyBoard.builder().setColor(ColorButton.POSITIVE).setLabel("Да").setPayload("{\"button\": \"yes\"}").build();
ButtonKeyBoard noButton = ButtonKeyBoard.builder().setColor(ColorButton.NEGATIVE).setLabel("Нет").setPayload("{\"button\": \"no\"}").build();
LineKeyBoard lineKeyBoard = LineKeyBoard.builder().setButtonKeyBoard(yesButton).setButtonKeyBoard(noButton).build();
KeyBoard keyBoard = KeyBoard.builder().setLineKeyBoard(lineKeyBoard).build();
return keyBoard.getKeyboard(true);
}
public static JSONObject verticalMenu(List<String> labelButtons) {
KeyBoard keyBoard = new KeyBoard();
for (String labelButton : labelButtons) {
ButtonKeyBoard buttonKeyBoard = ButtonKeyBoard.builder().setLabel(labelButton).setType("text").setPayload("{\"button\": \"" + labelButton + "\"}").build();
keyBoard.addLine(LineKeyBoard.builder().setButtonKeyBoard(buttonKeyBoard).build());
}
return keyBoard.getKeyboard(true);
}
public JSONObject getKeyBoardYesNo() {
return keyBoardYesNo;
}
}

View File

@ -0,0 +1,47 @@
package org.sadtech.vkbot.core.keyboard;
import org.json.JSONArray;
import java.util.ArrayList;
import java.util.List;
public class LineKeyBoard {
private List<ButtonKeyBoard> buttonKeyBoards = new ArrayList<>();
public LineKeyBoard() {
}
public LineKeyBoard(List<ButtonKeyBoard> buttonKeyBoards) {
this.buttonKeyBoards = buttonKeyBoards;
}
public JSONArray getLine() {
JSONArray line = new JSONArray();
for (ButtonKeyBoard buttonKeyBoard : buttonKeyBoards) {
line.put(buttonKeyBoard.getButton());
}
return line;
}
public static Builder builder() {
return new LineKeyBoard().new Builder();
}
public class Builder {
private Builder() {
}
public Builder setButtonKeyBoard(ButtonKeyBoard buttonKeyBoard) {
LineKeyBoard.this.buttonKeyBoards.add(buttonKeyBoard);
return this;
}
public LineKeyBoard build() {
return LineKeyBoard.this;
}
}
}

View File

@ -0,0 +1,7 @@
package org.sadtech.vkbot.core.listener;
public interface EventListenable extends Runnable {
void listen() throws Exception;
}

View File

@ -0,0 +1,85 @@
package org.sadtech.vkbot.core.listener.impl;
import com.google.gson.JsonObject;
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;
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.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;
import org.sadtech.vkbot.core.listener.EventListenable;
public class EventListenerVk implements EventListenable, Runnable {
public static final Logger log = Logger.getLogger(EventListenerVk.class);
private VkApiClient vk;
private GroupActor actor;
private EventService eventService;
private GetLongPollEventsQuery longPollEventsQuery;
private LongPoll longPoll;
private GetLongPollServerResponse server;
public EventListenerVk(VkConnect vkConnect) {
vk = vkConnect.getVkApiClient();
actor = vkConnect.getGroupActor();
eventService = new EventServiceImpl(new EventRepositoryQueue());
longPoll = new LongPoll(vk);
}
public EventListenerVk(VkConnect vkConnect, EventRepository eventRepository) {
this.vk = vkConnect.getVkApiClient();
this.actor = vkConnect.getGroupActor();
this.eventService = new EventServiceImpl(eventRepository);
longPoll = new LongPoll(vk);
}
public EventService getEventService() {
return eventService;
}
public void listen() throws ClientException, ApiException {
initServer();
log.info("LongPollServer инициализирован");
do {
GetLongPollEventsResponse eventsResponse;
eventsResponse = longPollEventsQuery.execute();
if (eventsResponse.getUpdates().toArray().length != 0) {
log.info("Полученно событие от ВК");
log.info(eventsResponse.getUpdates());
for (JsonObject update : eventsResponse.getUpdates()) {
eventService.add(update);
}
}
longPollEventsQuery = longPoll.getEvents(server.getServer(), server.getKey(), eventsResponse.getTs()).waitTime(20);
} while (true);
}
private void initServer() throws ClientException, ApiException {
server = vk.groups().getLongPollServer(actor).execute();
String key = server.getKey();
String serverUrl = server.getServer();
Integer ts = server.getTs();
longPoll = new LongPoll(vk);
longPollEventsQuery = longPoll.getEvents(serverUrl, key, ts).waitTime(20);
}
@Override
public void run() {
try {
listen();
} catch (ClientException | ApiException e) {
e.printStackTrace();
}
}
}

View File

@ -0,0 +1,17 @@
package org.sadtech.vkbot.core.repository;
import com.google.gson.JsonObject;
import java.util.Queue;
public interface EventRepository {
void add(JsonObject jsonObject);
void remove(int id);
void cleanAll();
Queue<JsonObject> getJsonObjects();
}

View File

@ -0,0 +1,13 @@
package org.sadtech.vkbot.core.repository;
import org.sadtech.vkbot.core.entity.Mail;
import java.util.List;
public interface MailRepository {
void add(Mail message);
List<Mail> getMailByTime(Integer timeFrom, Integer timeTo);
}

View File

@ -0,0 +1,11 @@
package org.sadtech.vkbot.core.repository;
import org.sadtech.vkbot.core.entity.Person;
public interface PersonRepository {
void add(Person person);
Person get(Integer id);
}

View File

@ -0,0 +1,31 @@
package org.sadtech.vkbot.core.repository.impl;
import com.google.gson.JsonObject;
import org.sadtech.vkbot.core.repository.EventRepository;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
public class EventRepositoryQueue implements EventRepository {
private Queue<JsonObject> jsonObjects = new ConcurrentLinkedQueue<>();
@Override
public void add(JsonObject jsonObject) {
jsonObjects.offer(jsonObject);
}
@Override
public void remove(int id) {
jsonObjects.remove(id);
}
@Override
public void cleanAll() {
jsonObjects.clear();
}
public Queue<JsonObject> getJsonObjects() {
return jsonObjects;
}
}

View File

@ -0,0 +1,32 @@
package org.sadtech.vkbot.core.repository.impl;
import org.sadtech.vkbot.core.entity.Mail;
import org.sadtech.vkbot.core.repository.MailRepository;
import java.util.ArrayList;
import java.util.List;
public class MailRepositoryList implements MailRepository {
private List<Mail> messages = new ArrayList<>();
@Override
public void add(Mail mail) {
messages.add(mail);
}
@Override
public List<Mail> getMailByTime(Integer timeFrom, Integer timeTo) {
ArrayList<Mail> mails = new ArrayList<>();
for (int i = messages.size() - 1; i >= 0; i--) {
if (messages.get(i).getDate() >= timeFrom && messages.get(i).getDate() < timeTo) {
mails.add(messages.get(i));
} else if (messages.get(i).getDate() < timeFrom) {
break;
}
}
return mails;
}
}

View File

@ -0,0 +1,22 @@
package org.sadtech.vkbot.core.repository.impl;
import org.sadtech.vkbot.core.entity.Person;
import org.sadtech.vkbot.core.repository.PersonRepository;
import java.util.HashMap;
import java.util.Map;
public class PersonRepositoryMap implements PersonRepository {
private Map<Integer, Person> personMap = new HashMap<>();
@Override
public void add(Person person) {
personMap.put(person.getId(), person);
}
@Override
public Person get(Integer id) {
return personMap.get(id);
}
}

View File

@ -0,0 +1,24 @@
package org.sadtech.vkbot.core.repository.unit;
import org.sadtech.vkbot.core.entity.unit.TextAndKeyBoardAnswer;
import org.sadtech.autoresponder.repository.UnitRepository;
import java.util.List;
public class TextAndKeyBoardAnswerRepository implements UnitRepository<TextAndKeyBoardAnswer> {
@Override
public void addUnit(TextAndKeyBoardAnswer unit) {
}
@Override
public void addUnits(List<TextAndKeyBoardAnswer> units) {
}
@Override
public List<TextAndKeyBoardAnswer> menuUnits() {
return null;
}
}

View File

@ -0,0 +1,35 @@
package org.sadtech.vkbot.core.repository.unit;
import org.sadtech.vkbot.core.entity.unit.TextAnswer;
import org.sadtech.autoresponder.repository.UnitRepository;
import java.util.ArrayList;
import java.util.List;
public class TextAnswerRepository implements UnitRepository<TextAnswer> {
private List<TextAnswer> textAnswers;
public TextAnswerRepository() {
textAnswers = new ArrayList<>();
}
public TextAnswerRepository(List<TextAnswer> textAnswers) {
this.textAnswers = textAnswers;
}
@Override
public void addUnit(TextAnswer unit) {
textAnswers.add(unit);
}
@Override
public void addUnits(List<TextAnswer> units) {
textAnswers.addAll(units);
}
@Override
public List<TextAnswer> menuUnits() {
return textAnswers;
}
}

View File

@ -0,0 +1,48 @@
package org.sadtech.vkbot.core.sender;
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 org.sadtech.vkbot.core.VkConnect;
import org.sadtech.vkbot.core.entity.Person;
import org.sadtech.vkbot.core.keyboard.KeyBoard;
public class MailSanderVk {
private Person person;
private VkApiClient vkApiClient;
private GroupActor groupActor;
public MailSanderVk(VkConnect vkConnect) {
this.vkApiClient = vkConnect.getVkApiClient();
this.groupActor = vkConnect.getGroupActor();
}
public MailSanderVk(Person person, VkConnect vkConnect) {
this.person = person;
this.vkApiClient = vkConnect.getVkApiClient();
this.groupActor = vkConnect.getGroupActor();
}
public void setPerson(Person person) {
this.person = person;
}
public void sendText(String messageText) {
try {
vkApiClient.messages().send(groupActor).userId(person.getId()).message(messageText).execute();
} catch (ApiException | ClientException e) {
e.printStackTrace();
}
}
public void sendTextandKeyBoard(String message, KeyBoard keyBoard) {
try {
vkApiClient.messages().send(groupActor).userId(person.getId()).message(message).keyboard(keyBoard.getKeyboard(true).toString()).execute();
} catch (ApiException | ClientException e) {
e.printStackTrace();
}
}
}

View File

@ -0,0 +1,20 @@
package org.sadtech.vkbot.core.service;
import com.google.gson.JsonObject;
import org.sadtech.vkbot.core.repository.EventRepository;
import java.util.Queue;
public interface EventService {
void cleanAll();
void add(JsonObject jsonObject);
Queue<JsonObject> getJsonObjects();
EventRepository getEventRepository();
void replacementEventRepository(EventRepository eventRepository);
}

View File

@ -0,0 +1,16 @@
package org.sadtech.vkbot.core.service;
import com.vk.api.sdk.objects.users.User;
import org.sadtech.vkbot.core.entity.Person;
public interface PersonService {
void add(User person);
void add(Person person);
Person get(Integer id);
boolean checkPerson(Integer idPerson);
}

View File

@ -0,0 +1,9 @@
package org.sadtech.vkbot.core.service.handlers;
import org.sadtech.vkbot.core.entity.Mail;
public interface MailService extends SortEventService<Mail> {
void add(Mail mail);
}

View File

@ -0,0 +1,13 @@
package org.sadtech.vkbot.core.service.handlers;
import java.util.List;
public interface SortEventService<T> {
List<T> getEvent(Integer timeFrom, Integer timeTo);
List<T> getFirstMailByTime(Integer timeFrom, Integer timeTo);
List<T> getLastMailByTime(Integer timeFrom, Integer timeTo);
}

View File

@ -0,0 +1,67 @@
package org.sadtech.vkbot.core.service.handlers.impl;
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.handlers.MailService;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class MailServiceImpl implements MailService {
public static final Logger log = Logger.getLogger(MailServiceImpl.class);
private MailRepository messageRepository;
public MailServiceImpl() {
this.messageRepository = new MailRepositoryList();
}
public MailServiceImpl(MailRepository messageRepository) {
this.messageRepository = messageRepository;
}
@Override
public void add(Mail mail) {
messageRepository.add(mail);
}
@Override
public List<Mail> getFirstMailByTime(Integer timeFrom, Integer timeTo) {
List<Mail> mails = messageRepository.getMailByTime(timeFrom, timeTo);
Set<Person> people = new HashSet<>();
List<Mail> returnMails = new ArrayList<>();
for (int i = mails.size() - 1; i >= 0; i--) {
if (!people.contains(mails.get(i).getPerson())) {
returnMails.add(mails.get(i));
people.add(mails.get(i).getPerson());
}
}
return returnMails;
}
@Override
public List<Mail> getLastMailByTime(Integer timeFrom, Integer timeTo) {
List<Mail> mails = messageRepository.getMailByTime(timeFrom, timeTo);
Set<Person> people = new HashSet<>();
List<Mail> returnMails = new ArrayList<>();
for (Mail mail : mails) {
if (!people.contains(mail.getPerson())) {
returnMails.add(mail);
people.add(mail.getPerson());
}
}
return returnMails;
}
@Override
public List<Mail> getEvent(Integer timeFrom, Integer timeTo) {
log.info("Запрос на получение сообщений в интервале от " + timeFrom + " до " + timeTo);
return messageRepository.getMailByTime(timeFrom, timeTo);
}
}

View File

@ -0,0 +1,46 @@
package org.sadtech.vkbot.core.service.impl;
import com.google.gson.JsonObject;
import org.apache.log4j.Logger;
import org.sadtech.vkbot.core.repository.EventRepository;
import org.sadtech.vkbot.core.service.EventService;
import java.util.Queue;
public class EventServiceImpl implements EventService {
public static final Logger log = Logger.getLogger(EventServiceImpl.class);
private EventRepository eventRepository;
public EventServiceImpl(EventRepository eventRepository) {
this.eventRepository = eventRepository;
}
@Override
public void cleanAll() {
eventRepository.cleanAll();
log.info("Репозиторий событий очищен");
}
@Override
public void add(JsonObject jsonObject) {
eventRepository.add(jsonObject);
log.info("Событие отправленно в репозиторий");
}
@Override
public Queue<JsonObject> getJsonObjects() {
return eventRepository.getJsonObjects();
}
@Override
public EventRepository getEventRepository() {
return eventRepository;
}
@Override
public void replacementEventRepository(EventRepository eventRepository) {
this.eventRepository = eventRepository;
}
}

View File

@ -0,0 +1,50 @@
package org.sadtech.vkbot.core.service.impl;
import com.vk.api.sdk.objects.users.User;
import org.apache.log4j.Logger;
import org.sadtech.vkbot.core.entity.Person;
import org.sadtech.vkbot.core.repository.PersonRepository;
import org.sadtech.vkbot.core.repository.impl.PersonRepositoryMap;
import org.sadtech.vkbot.core.service.PersonService;
public class PersonServiceImpl implements PersonService {
public static final Logger log = Logger.getLogger(PersonServiceImpl.class);
private PersonRepository personRepository;
public PersonServiceImpl() {
this.personRepository = new PersonRepositoryMap();
}
public PersonServiceImpl(PersonRepository personRepository) {
this.personRepository = personRepository;
}
@Override
public void add(User user) {
Person person = new Person();
person.setId(user.getId());
person.setLastName(user.getLastName());
person.setFirstName(user.getFirstName());
personRepository.add(person);
log.info("Пользователь добавлен в репозиторий");
}
@Override
public void add(Person person) {
personRepository.add(person);
log.info("Пользователь добавлен в репозиторий");
}
@Override
public Person get(Integer id) {
return personRepository.get(id);
}
@Override
public boolean checkPerson(Integer idPerson) {
log.info("Проверка наличия пользователя в репозитории");
return get(idPerson) != null;
}
}