Рефакторинг

This commit is contained in:
Mark Struchkov 2019-05-08 13:32:00 +03:00
parent 6563cf3a53
commit 0819f2d78b
17 changed files with 63 additions and 168 deletions

6
.gitignore vendored Normal file
View File

@ -0,0 +1,6 @@
*.iml
.idea/
**/target/**
*.log
*.properties
.DS_Store

View File

@ -6,7 +6,7 @@
<groupId>org.sadtech.vkbot</groupId> <groupId>org.sadtech.vkbot</groupId>
<artifactId>vkbot-core</artifactId> <artifactId>vkbot-core</artifactId>
<version>1.0-SNAPSHOT</version> <version>0.1.0-RELEASE</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<build> <build>

View File

@ -16,10 +16,10 @@ import java.util.List;
public class VkApi { public class VkApi {
public static final Logger log = Logger.getLogger(String.valueOf(VkApi.class)); private static final Logger log = Logger.getLogger(String.valueOf(VkApi.class));
private VkApiClient vk; private final VkApiClient vk;
private GroupActor actor; private final GroupActor actor;
public VkApi(VkConnect vkConnect) { public VkApi(VkConnect vkConnect) {
vk = vkConnect.getVkApiClient(); vk = vkConnect.getVkApiClient();
@ -66,7 +66,6 @@ public class VkApi {
} }
private boolean checkCity(List<UserXtrCounters> temp) { private boolean checkCity(List<UserXtrCounters> temp) {
log.info("Запрошен город пользователя: " + temp.get(0).getCity().getTitle());
return temp.get(0).getCity() != null; return temp.get(0).getCity() != null;
} }

View File

@ -7,7 +7,7 @@ import com.vk.api.sdk.httpclient.HttpTransportClient;
public class VkConnect { public class VkConnect {
private VkConfig vkConfig; private final VkConfig vkConfig;
private VkApiClient vkApiClient; private VkApiClient vkApiClient;
private GroupActor groupActor; private GroupActor groupActor;

View File

@ -5,7 +5,7 @@ import java.util.regex.Pattern;
public class VkInsertData { public class VkInsertData {
private VkApi vkApi; private final VkApi vkApi;
public VkInsertData(VkConnect vkConnect) { public VkInsertData(VkConnect vkConnect) {
this.vkApi = new VkApi(vkConnect); this.vkApi = new VkApi(vkConnect);

View File

@ -1,26 +0,0 @@
package org.sadtech.vkbot.core.distribution;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.vk.api.sdk.objects.wall.WallComment;
import org.apache.log4j.Logger;
import org.sadtech.vkbot.core.service.distribution.CommentService;
public class CommentSubscriber implements EventSubscribe<JsonObject> {
public static final Logger log = Logger.getLogger(CommentSubscriber.class);
private CommentService commentService;
public CommentSubscriber(CommentService commentService) {
this.commentService = commentService;
}
@Override
public void update(JsonObject object) {
Gson gson = new Gson();
WallComment wallComment = gson.fromJson(object, WallComment.class);
log.info(wallComment);
}
}

View File

@ -9,13 +9,14 @@ import java.util.Map;
public class EventDistributor implements Runnable { public class EventDistributor implements Runnable {
public static final Logger log = Logger.getLogger(EventDistributor.class); private static final Logger log = Logger.getLogger(EventDistributor.class);
private RawEventService rawEventService; private final RawEventService rawEventService;
private Map<String, EventSubscribe> eventDistributionMap = new HashMap<>(); private final Map<String, EventSubscribe> eventDistributionMap = new HashMap<>();
public EventDistributor(RawEventService rawEventService) { public EventDistributor(RawEventService rawEventService) {
this.rawEventService = rawEventService; this.rawEventService = rawEventService;
log.info("EventDistributor инициализирован");
} }
public void update() { public void update() {
@ -41,6 +42,7 @@ public class EventDistributor implements Runnable {
@Override @Override
public void run() { public void run() {
log.info("EventDistributor запущен");
update(); update();
} }
} }

View File

@ -14,11 +14,11 @@ import java.util.Set;
public class MailSubscriber implements EventSubscribe<JsonObject> { public class MailSubscriber implements EventSubscribe<JsonObject> {
public static final Logger log = Logger.getLogger(MailSubscriber.class); private static final Logger log = Logger.getLogger(MailSubscriber.class);
private MailService mailService; private final MailService mailService;
private Set<Integer> admins = new HashSet<>(); private Set<Integer> admins = new HashSet<>();
private Map<String, EventSubscribe<Message>> eventDistributionMap = new HashMap<>(); private final Map<String, EventSubscribe<Message>> eventDistributionMap = new HashMap<>();
public MailSubscriber(MailService mailService) { public MailSubscriber(MailService mailService) {
this.mailService = mailService; this.mailService = mailService;
@ -54,10 +54,10 @@ public class MailSubscriber implements EventSubscribe<JsonObject> {
private Mail createMaail(Message message) { private Mail createMaail(Message message) {
Mail mail = new Mail(); Mail mail = new Mail();
mail.setBody(message.getBody()); mail.setMessage(message.getBody());
mail.setDate(message.getDate()); mail.setDate(message.getDate());
mail.setId(message.getId()); mail.setId(message.getId());
mail.setPeerId(message.getPeerId()); mail.setPersonId(message.getPeerId());
return mail; return mail;
} }

View File

@ -1,30 +0,0 @@
package org.sadtech.vkbot.core.distribution;
import com.vk.api.sdk.objects.messages.Message;
import org.apache.log4j.Logger;
import org.sadtech.vkbot.core.service.distribution.MailService;
public class TerminalSubscriber implements EventSubscribe<Message> {
public static final Logger log = Logger.getLogger(TerminalSubscriber.class);
private MailService mailService;
public TerminalSubscriber(MailSubscriber mailSubscriber, MailService mailService) {
this.mailService = mailService;
}
public MailService getMailService() {
return mailService;
}
public void setMailService(MailService mailService) {
this.mailService = mailService;
}
@Override
public void update(Message object) {
log.info("Сообщение отправленно на добавление в репозиторий команд");
}
}

View File

@ -4,20 +4,13 @@ import com.google.gson.JsonObject;
public class ButtonKeyBoard { public class ButtonKeyBoard {
private String type = "text"; private String type;
private String payload; private String payload;
private String label; private String label;
private ColorButton color = ColorButton.PRIMARY; private ColorButton color;
public ButtonKeyBoard() { private ButtonKeyBoard() {
throw new IllegalStateException();
}
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() { public JsonObject getButton() {
@ -45,22 +38,22 @@ public class ButtonKeyBoard {
} }
public Builder setColor(ColorButton color) { public Builder color(ColorButton color) {
ButtonKeyBoard.this.color = color; ButtonKeyBoard.this.color = color;
return this; return this;
} }
public Builder setLabel(String label) { public Builder label(String label) {
ButtonKeyBoard.this.label = label; ButtonKeyBoard.this.label = label;
return this; return this;
} }
public Builder setPayload(String payload) { public Builder payload(String payload) {
ButtonKeyBoard.this.payload = payload; ButtonKeyBoard.this.payload = payload;
return this; return this;
} }
public Builder setType(String type) { public Builder type(String type) {
ButtonKeyBoard.this.type = type; ButtonKeyBoard.this.type = type;
return this; return this;
} }

View File

@ -8,20 +8,12 @@ import java.util.List;
public class KeyBoard { public class KeyBoard {
private List<LineKeyBoard> lineKeyBoards = new ArrayList<>(); private List<LineKeyBoard> lineKeyBoards;
private boolean oneTime; private boolean oneTime;
public KeyBoard() { private KeyBoard() {
lineKeyBoards = new ArrayList<>();
} throw new IllegalStateException();
public KeyBoard(List<LineKeyBoard> lineKeyBoards, boolean oneTime) {
this.lineKeyBoards = lineKeyBoards;
this.oneTime = oneTime;
}
public void setOneTime(boolean oneTime) {
this.oneTime = oneTime;
} }
public JsonObject getKeyboard() { public JsonObject getKeyboard() {
@ -37,26 +29,26 @@ public class KeyBoard {
return keyboard; return keyboard;
} }
public void addLine(LineKeyBoard lineKeyBoard) {
lineKeyBoards.add(lineKeyBoard);
}
public static Builder builder() { public static Builder builder() {
return new KeyBoard().new Builder(); return new KeyBoard().new Builder();
} }
public void addLine(LineKeyBoard lineKeyBoard) {
lineKeyBoards.add(lineKeyBoard);
}
public class Builder { public class Builder {
private Builder() { private Builder() {
} }
public Builder setLineKeyBoard(LineKeyBoard lineKeyBoard) { public Builder lineKeyBoard(LineKeyBoard lineKeyBoard) {
KeyBoard.this.lineKeyBoards.add(lineKeyBoard); KeyBoard.this.lineKeyBoards.add(lineKeyBoard);
return this; return this;
} }
public Builder setOneTime(boolean oneTime) { public Builder oneTime(boolean oneTime) {
KeyBoard.this.oneTime = oneTime; KeyBoard.this.oneTime = oneTime;
return this; return this;
} }

View File

@ -9,30 +9,26 @@ public class KeyBoardService {
} }
public static KeyBoard keyBoardYesNo() { public static KeyBoard keyBoardYesNo() {
ButtonKeyBoard yesButton = ButtonKeyBoard.builder().setColor(ColorButton.POSITIVE).setLabel("Да").setPayload("{\"button\": \"yes\"}").build(); ButtonKeyBoard yesButton = ButtonKeyBoard.builder().color(ColorButton.POSITIVE).label("Да").payload("{\"button\": \"yes\"}").build();
ButtonKeyBoard noButton = ButtonKeyBoard.builder().setColor(ColorButton.NEGATIVE).setLabel("Нет").setPayload("{\"button\": \"no\"}").build(); ButtonKeyBoard noButton = ButtonKeyBoard.builder().color(ColorButton.NEGATIVE).label("Нет").payload("{\"button\": \"no\"}").build();
LineKeyBoard lineKeyBoard = LineKeyBoard.builder().setButtonKeyBoard(yesButton).setButtonKeyBoard(noButton).build(); LineKeyBoard lineKeyBoard = LineKeyBoard.builder().setButtonKeyBoard(yesButton).setButtonKeyBoard(noButton).build();
KeyBoard keyBoard = KeyBoard.builder().setLineKeyBoard(lineKeyBoard).build(); return KeyBoard.builder().lineKeyBoard(lineKeyBoard).oneTime(true).build();
keyBoard.setOneTime(true);
return keyBoard;
} }
public static KeyBoard verticalMenuString(List<String> labelButtons) { public static KeyBoard verticalMenuString(List<String> labelButtons) {
KeyBoard keyBoard = new KeyBoard(); KeyBoard keyBoard = KeyBoard.builder().oneTime(true).build();
for (String labelButton : labelButtons) { for (String labelButton : labelButtons) {
ButtonKeyBoard buttonKeyBoard = ButtonKeyBoard.builder().setLabel(labelButton).setType("text").setPayload("{\"button\": \"" + labelButton + "\"}").build(); ButtonKeyBoard buttonKeyBoard = ButtonKeyBoard.builder().label(labelButton).type("text").payload("{\"button\": \"" + labelButton + "\"}").build();
keyBoard.addLine(LineKeyBoard.builder().setButtonKeyBoard(buttonKeyBoard).build()); keyBoard.addLine(LineKeyBoard.builder().setButtonKeyBoard(buttonKeyBoard).build());
} }
keyBoard.setOneTime(true);
return keyBoard; return keyBoard;
} }
public static KeyBoard verticalMenuButton(List<ButtonKeyBoard> buttonKeyBoards) { public static KeyBoard verticalMenuButton(List<ButtonKeyBoard> buttonKeyBoards) {
KeyBoard keyBoard = new KeyBoard(); KeyBoard keyBoard = KeyBoard.builder().oneTime(true).build();
for (ButtonKeyBoard buttonKeyBoard : buttonKeyBoards) { for (ButtonKeyBoard buttonKeyBoard : buttonKeyBoards) {
keyBoard.addLine(LineKeyBoard.builder().setButtonKeyBoard(buttonKeyBoard).build()); keyBoard.addLine(LineKeyBoard.builder().setButtonKeyBoard(buttonKeyBoard).build());
} }
keyBoard.setOneTime(true);
return keyBoard; return keyBoard;
} }
} }

View File

@ -16,14 +16,14 @@ import org.sadtech.vkbot.core.VkConnect;
public class EventListenerVk implements Runnable { public class EventListenerVk implements Runnable {
public static final Logger log = Logger.getLogger(EventListenerVk.class); private static final Logger log = Logger.getLogger(EventListenerVk.class);
private VkApiClient vk; private final VkApiClient vk;
private GroupActor actor; private final GroupActor actor;
private static final Integer DEFAULT_WAIT_TIME = 25; private static final Integer DEFAULT_WAIT_TIME = 25;
private RawEventService rawEventService; private final RawEventService rawEventService;
public EventListenerVk(VkConnect vkConnect) { public EventListenerVk(VkConnect vkConnect) {
vk = vkConnect.getVkApiClient(); vk = vkConnect.getVkApiClient();

View File

@ -13,12 +13,12 @@ import org.sadtech.vkbot.core.VkInsertData;
public class MailSenderVk implements Sent { public class MailSenderVk implements Sent {
public static final Logger log = Logger.getLogger(MailSenderVk.class); private static final Logger log = Logger.getLogger(MailSenderVk.class);
private VkApiClient vkApiClient; private final VkApiClient vkApiClient;
private GroupActor groupActor; private final GroupActor groupActor;
private VkInsertData vkInsertData; private final VkInsertData vkInsertData;
public MailSenderVk(VkConnect vkConnect) { public MailSenderVk(VkConnect vkConnect) {
this.vkApiClient = vkConnect.getVkApiClient(); this.vkApiClient = vkConnect.getVkApiClient();

View File

@ -12,10 +12,10 @@ import org.sadtech.vkbot.core.VkConnect;
public class PersonServiceImpl implements PersonService { public class PersonServiceImpl implements PersonService {
public static final Logger log = Logger.getLogger(PersonServiceImpl.class); private static final Logger log = Logger.getLogger(PersonServiceImpl.class);
private PersonRepository personRepository; private final PersonRepository personRepository;
private VkApi vkApi; private final VkApi vkApi;
public PersonServiceImpl(VkConnect vkConnect) { public PersonServiceImpl(VkConnect vkConnect) {
this.personRepository = new PersonRepositoryMap(); this.personRepository = new PersonRepositoryMap();

View File

@ -1,37 +0,0 @@
package org.sadtech.vkbot.core.service.distribution.impl;
import org.sadtech.bot.core.domain.Comment;
import org.sadtech.bot.core.repository.CommentRepository;
import org.sadtech.vkbot.core.service.distribution.CommentService;
import java.util.List;
public class CommentServiceImpl implements CommentService {
private CommentRepository commentRepository;
public CommentServiceImpl(CommentRepository commentRepository) {
this.commentRepository = commentRepository;
}
@Override
public void add(Comment comment) {
commentRepository.add(comment);
}
@Override
public List<Comment> getEvent(Integer timeFrom, Integer timeTo) {
return null;
}
@Override
public List<Comment> getFirstEventByTime(Integer timeFrom, Integer timeTo) {
return null;
}
@Override
public List<Comment> getLastEventByTime(Integer timeFrom, Integer timeTo) {
return null;
}
}

View File

@ -13,9 +13,9 @@ import java.util.Set;
public class MailServiceImpl implements MailService { public class MailServiceImpl implements MailService {
public static final Logger log = Logger.getLogger(MailServiceImpl.class); private static final Logger log = Logger.getLogger(MailServiceImpl.class);
private MailRepository mailRepository; private final MailRepository mailRepository;
public MailServiceImpl() { public MailServiceImpl() {
this.mailRepository = new MailRepositoryList(); this.mailRepository = new MailRepositoryList();
@ -39,9 +39,9 @@ public class MailServiceImpl implements MailService {
Set<Integer> people = new HashSet<>(); Set<Integer> people = new HashSet<>();
List<Mail> returnMails = new ArrayList<>(); List<Mail> returnMails = new ArrayList<>();
for (int i = mails.size() - 1; i >= 0; i--) { for (int i = mails.size() - 1; i >= 0; i--) {
if (!people.contains(mails.get(i).getPeerId())) { if (!people.contains(mails.get(i).getPersonId())) {
returnMails.add(mails.get(i)); returnMails.add(mails.get(i));
people.add(mails.get(i).getPeerId()); people.add(mails.get(i).getPersonId());
} }
} }
return returnMails; return returnMails;
@ -53,9 +53,9 @@ public class MailServiceImpl implements MailService {
Set<Integer> people = new HashSet<>(); Set<Integer> people = new HashSet<>();
List<Mail> returnMails = new ArrayList<>(); List<Mail> returnMails = new ArrayList<>();
for (Mail mail : mails) { for (Mail mail : mails) {
if (!people.contains(mail.getPeerId())) { if (!people.contains(mail.getPersonId())) {
returnMails.add(mail); returnMails.add(mail);
people.add(mail.getPeerId()); people.add(mail.getPersonId());
} }
} }
return returnMails; return returnMails;