diff --git a/pom.xml b/pom.xml index ded628d..158fbb0 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ org.sadtech.autoresponder autoresponder - 1.4.0-RELEASE + 1.5.0-RELEASE jar @@ -22,18 +22,8 @@ - - - uPagge - Struchkov Mark - upagge@mail.ru - - - 1.2.17 - 4.12 - 2.23.4 @@ -42,22 +32,15 @@ log4j ${log4j} - - - - - junit - junit - ${junit.ver} - test - - - org.mockito - mockito-core - ${mocito.ver} - test - - + + + uPagge + Struchkov Mark + upagge@mail.ru + SADTECH + + + \ No newline at end of file diff --git a/src/main/java/org/sadtech/autoresponder/Autoresponder.java b/src/main/java/org/sadtech/autoresponder/Autoresponder.java index f7c1a43..1dc1ace 100644 --- a/src/main/java/org/sadtech/autoresponder/Autoresponder.java +++ b/src/main/java/org/sadtech/autoresponder/Autoresponder.java @@ -2,10 +2,10 @@ package org.sadtech.autoresponder; import org.apache.log4j.Logger; import org.sadtech.autoresponder.compare.UnitPriorityComparator; -import org.sadtech.autoresponder.entity.Person; +import org.sadtech.autoresponder.entity.UnitPointer; import org.sadtech.autoresponder.entity.Unit; -import org.sadtech.autoresponder.service.PersonService; -import org.sadtech.autoresponder.submodule.parser.Parser; +import org.sadtech.autoresponder.service.UnitPointerService; +import org.sadtech.autoresponder.util.Parser; import java.util.HashSet; import java.util.Optional; @@ -15,54 +15,54 @@ import java.util.regex.Pattern; public class Autoresponder { - public static final Logger log = Logger.getLogger(Autoresponder.class); + private static final Logger log = Logger.getLogger(Autoresponder.class); private Set menuUnits; - private PersonService personService; + private UnitPointerService unitPointerService; - public Autoresponder(PersonService personService) { - this.personService = personService; + public Autoresponder(UnitPointerService unitPointerService) { + this.unitPointerService = unitPointerService; } public void setMenuUnits(Set menuUnits) { this.menuUnits = menuUnits; } - public PersonService getPersonService() { - return personService; + public UnitPointerService getUnitPointerService() { + return unitPointerService; } - public void setPersonService(PersonService personService) { - this.personService = personService; + public void setUnitPointerService(UnitPointerService unitPointerService) { + this.unitPointerService = unitPointerService; } public Unit answer(Integer idPerson, String message) { - Person person = checkAndAddPerson(idPerson); + UnitPointer unitPointer = checkAndAddPerson(idPerson); Unit unit; - if (person.getUnit() == null) { + if (unitPointer.getUnit() == null) { unit = nextUnit(menuUnits, message); } else { - if (person.getUnit().getNextUnits() == null) { + if (unitPointer.getUnit().getNextUnits() == null) { unit = nextUnit(menuUnits, message); } else { - unit = nextUnit(person.getUnit().getNextUnits(), message); + unit = nextUnit(unitPointer.getUnit().getNextUnits(), message); } } if (unit != null) { - person.setUnit(unit); + unitPointer.setUnit(unit); } return unit; } - private Person checkAndAddPerson(Integer idPerson) { - Person person; - if (personService.checkPerson(idPerson)) { - person = personService.getPersonById(idPerson); + private UnitPointer checkAndAddPerson(Integer idPerson) { + UnitPointer unitPointer; + if (unitPointerService.check(idPerson)) { + unitPointer = unitPointerService.getByEntityId(idPerson); } else { - person = new Person(idPerson); - personService.addPerson(person); + unitPointer = new UnitPointer(idPerson); + unitPointerService.add(unitPointer); } - return person; + return unitPointer; } private Unit nextUnit(Set nextUnits, String message) { @@ -71,14 +71,15 @@ public class Autoresponder { Optional patternUnits = nextUnits.stream().filter(nextUnit -> nextUnit.getPattern() != null).filter(nextUnit -> patternReg(nextUnit, message)).max(unitPriorityComparator); if (!patternUnits.isPresent()) { - Parser parser = new Parser(); - parser.setText(message); - parser.parse(); - patternUnits = nextUnits.stream().filter(nextUnit -> textPercentageMatch(nextUnit, parser.getWords()) >= nextUnit.getMatchThreshold()).max(unitPriorityComparator); + patternUnits = nextUnits.stream() + .filter(nextUnit -> textPercentageMatch(nextUnit, Parser.parse(message)) >= nextUnit.getMatchThreshold()) + .max(unitPriorityComparator); } if (!patternUnits.isPresent()) { - patternUnits = nextUnits.stream().filter(nextUnit -> (nextUnit.getPattern() == null && nextUnit.getKeyWords() == null)).max(unitPriorityComparator); + patternUnits = nextUnits.stream() + .filter(nextUnit -> (nextUnit.getPattern() == null && nextUnit.getKeyWords() == null)) + .max(unitPriorityComparator); } return patternUnits.orElse(null); @@ -90,10 +91,7 @@ public class Autoresponder { private boolean patternReg(Unit unit, String message) { Pattern pattern = unit.getPattern(); Matcher m = pattern.matcher(message); - while (m.find()) { - return true; - } - return false; + return m.find(); } private Double textPercentageMatch(Unit unit, Set words) { diff --git a/src/main/java/org/sadtech/autoresponder/entity/Person.java b/src/main/java/org/sadtech/autoresponder/entity/Person.java deleted file mode 100644 index 92f5a4e..0000000 --- a/src/main/java/org/sadtech/autoresponder/entity/Person.java +++ /dev/null @@ -1,58 +0,0 @@ -package org.sadtech.autoresponder.entity; - -import java.util.Objects; - -public class Person { - - private Integer id; - private Unit unit; - private Boolean replyStatus; - - public Person(Integer id, Unit unit) { - this.id = id; - this.unit = unit; - replyStatus = true; - } - - public Person(Integer id) { - this.id = id; - } - - public Integer getId() { - return id; - } - - public void setId(Integer id) { - this.id = id; - } - - public Unit getUnit() { - return unit; - } - - public void setUnit(Unit unit) { - this.unit = unit; - } - - public Boolean getReplyStatus() { - return replyStatus; - } - - public void setReplyStatus(Boolean replyStatus) { - this.replyStatus = replyStatus; - } - - @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(unit, person.unit); - } - - @Override - public int hashCode() { - return Objects.hash(id, unit); - } -} diff --git a/src/main/java/org/sadtech/autoresponder/entity/UnitPointer.java b/src/main/java/org/sadtech/autoresponder/entity/UnitPointer.java new file mode 100644 index 0000000..37b40b9 --- /dev/null +++ b/src/main/java/org/sadtech/autoresponder/entity/UnitPointer.java @@ -0,0 +1,48 @@ +package org.sadtech.autoresponder.entity; + +import java.util.Objects; + +public class UnitPointer { + + private Integer entityId; + private Unit unit; + + public UnitPointer(Integer entityId, Unit unit) { + this.entityId = entityId; + this.unit = unit; + } + + public UnitPointer(Integer entityId) { + this.entityId = entityId; + } + + public Integer getEntityId() { + return entityId; + } + + public void setEntityId(Integer entityId) { + this.entityId = entityId; + } + + public Unit getUnit() { + return unit; + } + + public void setUnit(Unit unit) { + this.unit = unit; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + UnitPointer unitPointer = (UnitPointer) o; + return Objects.equals(entityId, unitPointer.entityId) && + Objects.equals(unit, unitPointer.unit); + } + + @Override + public int hashCode() { + return Objects.hash(entityId, unit); + } +} diff --git a/src/main/java/org/sadtech/autoresponder/repository/PersonRepository.java b/src/main/java/org/sadtech/autoresponder/repository/PersonRepository.java deleted file mode 100644 index 35312f5..0000000 --- a/src/main/java/org/sadtech/autoresponder/repository/PersonRepository.java +++ /dev/null @@ -1,17 +0,0 @@ -package org.sadtech.autoresponder.repository; - -import org.sadtech.autoresponder.entity.Person; - -import java.util.Map; - -public interface PersonRepository { - - void addPerson(Person person); - - void removePerson(Person person); - - void addPersonAll(Map personCollection); - - Person getPersonById(Integer idPerson); - -} diff --git a/src/main/java/org/sadtech/autoresponder/repository/PersonRepositoryMap.java b/src/main/java/org/sadtech/autoresponder/repository/PersonRepositoryMap.java deleted file mode 100644 index 74b75fc..0000000 --- a/src/main/java/org/sadtech/autoresponder/repository/PersonRepositoryMap.java +++ /dev/null @@ -1,31 +0,0 @@ -package org.sadtech.autoresponder.repository; - -import org.sadtech.autoresponder.entity.Person; - -import java.util.HashMap; -import java.util.Map; - -public class PersonRepositoryMap implements PersonRepository { - - private Map people = new HashMap<>(); - - @Override - public void addPerson(Person person) { - people.put(person.getId(), person); - } - - @Override - public void removePerson(Person person) { - people.remove(person.getId()); - } - - @Override - public void addPersonAll(Map personCollection) { - people.putAll(personCollection); - } - - @Override - public Person getPersonById(Integer idPerson) { - return people.get(idPerson); - } -} diff --git a/src/main/java/org/sadtech/autoresponder/repository/UnitPointerRepository.java b/src/main/java/org/sadtech/autoresponder/repository/UnitPointerRepository.java new file mode 100644 index 0000000..dd40da0 --- /dev/null +++ b/src/main/java/org/sadtech/autoresponder/repository/UnitPointerRepository.java @@ -0,0 +1,17 @@ +package org.sadtech.autoresponder.repository; + +import org.sadtech.autoresponder.entity.UnitPointer; + +import java.util.Map; + +public interface UnitPointerRepository { + + Integer add(UnitPointer unitPointer); + + void remove(Integer entityId); + + void addAll(Map unitPointerMap); + + UnitPointer findByEntityId(Integer entityId); + +} diff --git a/src/main/java/org/sadtech/autoresponder/repository/UnitPointerRepositoryMap.java b/src/main/java/org/sadtech/autoresponder/repository/UnitPointerRepositoryMap.java new file mode 100644 index 0000000..0d2c3f1 --- /dev/null +++ b/src/main/java/org/sadtech/autoresponder/repository/UnitPointerRepositoryMap.java @@ -0,0 +1,32 @@ +package org.sadtech.autoresponder.repository; + +import org.sadtech.autoresponder.entity.UnitPointer; + +import java.util.HashMap; +import java.util.Map; + +public class UnitPointerRepositoryMap implements UnitPointerRepository { + + private Map unitPointerMap = new HashMap<>(); + + @Override + public Integer add(UnitPointer unitPointer) { + unitPointerMap.put(unitPointer.getEntityId(), unitPointer); + return unitPointer.getEntityId(); + } + + @Override + public void remove(Integer entityId) { + unitPointerMap.remove(entityId); + } + + @Override + public void addAll(Map unitPointerMap) { + this.unitPointerMap.putAll(unitPointerMap); + } + + @Override + public UnitPointer findByEntityId(Integer entityId) { + return unitPointerMap.get(entityId); + } +} diff --git a/src/main/java/org/sadtech/autoresponder/service/PersonService.java b/src/main/java/org/sadtech/autoresponder/service/PersonService.java deleted file mode 100644 index ae963e4..0000000 --- a/src/main/java/org/sadtech/autoresponder/service/PersonService.java +++ /dev/null @@ -1,13 +0,0 @@ -package org.sadtech.autoresponder.service; - -import org.sadtech.autoresponder.entity.Person; - -public interface PersonService { - - Person getPersonById(Integer integer); - - void addPerson(Person person); - - boolean checkPerson(Integer idPerson); - -} diff --git a/src/main/java/org/sadtech/autoresponder/service/PersonServiceImpl.java b/src/main/java/org/sadtech/autoresponder/service/PersonServiceImpl.java deleted file mode 100644 index 2aba89f..0000000 --- a/src/main/java/org/sadtech/autoresponder/service/PersonServiceImpl.java +++ /dev/null @@ -1,37 +0,0 @@ -package org.sadtech.autoresponder.service; - -import org.apache.log4j.Logger; -import org.sadtech.autoresponder.entity.Person; -import org.sadtech.autoresponder.repository.PersonRepository; -import org.sadtech.autoresponder.repository.PersonRepositoryMap; - -public class PersonServiceImpl implements PersonService { - - private 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 Person getPersonById(Integer integer) { - return personRepository.getPersonById(integer); - } - - @Override - public void addPerson(Person person) { - personRepository.addPerson(person); - log.info("Пользователь отправлен в репозиторий"); - } - - @Override - public boolean checkPerson(Integer idPerson) { - return personRepository.getPersonById(idPerson) != null; - } -} diff --git a/src/main/java/org/sadtech/autoresponder/service/UnitPointerService.java b/src/main/java/org/sadtech/autoresponder/service/UnitPointerService.java new file mode 100644 index 0000000..e819854 --- /dev/null +++ b/src/main/java/org/sadtech/autoresponder/service/UnitPointerService.java @@ -0,0 +1,13 @@ +package org.sadtech.autoresponder.service; + +import org.sadtech.autoresponder.entity.UnitPointer; + +public interface UnitPointerService { + + void add(UnitPointer unitPointer); + + boolean check(Integer entityId); + + UnitPointer getByEntityId(Integer entityId); + +} diff --git a/src/main/java/org/sadtech/autoresponder/service/UnitPointerServiceImpl.java b/src/main/java/org/sadtech/autoresponder/service/UnitPointerServiceImpl.java new file mode 100644 index 0000000..ca6f9ad --- /dev/null +++ b/src/main/java/org/sadtech/autoresponder/service/UnitPointerServiceImpl.java @@ -0,0 +1,37 @@ +package org.sadtech.autoresponder.service; + +import org.apache.log4j.Logger; +import org.sadtech.autoresponder.entity.UnitPointer; +import org.sadtech.autoresponder.repository.UnitPointerRepository; +import org.sadtech.autoresponder.repository.UnitPointerRepositoryMap; + +public class UnitPointerServiceImpl implements UnitPointerService { + + private static final Logger log = Logger.getLogger(UnitPointerServiceImpl.class); + + private UnitPointerRepository unitPointerRepository; + + public UnitPointerServiceImpl() { + this.unitPointerRepository = new UnitPointerRepositoryMap(); + } + + public UnitPointerServiceImpl(UnitPointerRepository unitPointerRepository) { + this.unitPointerRepository = unitPointerRepository; + } + + @Override + public UnitPointer getByEntityId(Integer entityId) { + return unitPointerRepository.findByEntityId(entityId); + } + + @Override + public void add(UnitPointer unitPointer) { + unitPointerRepository.add(unitPointer); + log.info("Пользователь отправлен в репозиторий"); + } + + @Override + public boolean check(Integer entityId) { + return unitPointerRepository.findByEntityId(entityId) != null; + } +} diff --git a/src/main/java/org/sadtech/autoresponder/submodule/parser/Parser.java b/src/main/java/org/sadtech/autoresponder/util/Parser.java similarity index 57% rename from src/main/java/org/sadtech/autoresponder/submodule/parser/Parser.java rename to src/main/java/org/sadtech/autoresponder/util/Parser.java index 5db9916..91ffdc1 100644 --- a/src/main/java/org/sadtech/autoresponder/submodule/parser/Parser.java +++ b/src/main/java/org/sadtech/autoresponder/util/Parser.java @@ -1,4 +1,4 @@ -package org.sadtech.autoresponder.submodule.parser; +package org.sadtech.autoresponder.util; import java.util.HashSet; import java.util.Set; @@ -7,23 +7,18 @@ import java.util.regex.Pattern; public class Parser { - private Set words = new HashSet<>(); - private String text; - - public Set getWords() { - return words; + private Parser() { + throw new IllegalStateException("Utility Class"); } - public void setText(String text) { - this.text = text; - } - - public void parse() { + public static Set parse(String text) { Pattern p = Pattern.compile("[а-яА-Я0-9]+"); Matcher m = p.matcher(text); + Set words = new HashSet<>(); while (m.find()) { words.add(m.group().toLowerCase()); } + return words; } }