diff --git a/src/main/java/org/sadtech/autoresponder/Autoresponder.java b/src/main/java/org/sadtech/autoresponder/Autoresponder.java index da2a281..6e6246c 100644 --- a/src/main/java/org/sadtech/autoresponder/Autoresponder.java +++ b/src/main/java/org/sadtech/autoresponder/Autoresponder.java @@ -1,42 +1,38 @@ package org.sadtech.autoresponder; -import lombok.AllArgsConstructor; -import lombok.NonNull; import org.sadtech.autoresponder.entity.Person; import org.sadtech.autoresponder.entity.Unit; +import org.sadtech.autoresponder.entity.compare.UnitPriorityComparator; import org.sadtech.autoresponder.service.PersonService; import org.sadtech.autoresponder.service.UnitService; -import org.sadtech.autoresponder.submodule.insertwords.InsertWords; +import org.sadtech.autoresponder.submodule.parser.Parser; +import java.util.Collections; import java.util.List; -@AllArgsConstructor public class Autoresponder { private UnitService unitService; private PersonService personService; - public String answer(@NonNull Integer idPerson, @NonNull String message) { - Person person = CheckAndAddPerson(idPerson); + public Autoresponder(UnitService unitService, PersonService personService) { + this.unitService = unitService; + this.personService = personService; + } + + public Unit answer(Integer idPerson, String message) { + Person person = checkAndAddPerson(idPerson); Unit unit; if (person.getUnit() == null) { - unit = unitService.nextUnit(unitService.menuUnit(), message); + unit = nextUnit(unitService.menuUnit(), message); } else { - unit = unitService.nextUnit(person.getUnit(), message); + unit = nextUnit(person.getUnit().getNextUnits(), message); } person.setUnit(unit); - return unit.getAnswer(); + return unit; } - public String answer(@NonNull Integer idPerson, @NonNull String message, @NonNull List words) { - String answer = answer(idPerson, message); - InsertWords insertWords = new InsertWords(); - insertWords.setInText(answer); - insertWords.insert(words); - return insertWords.getOutText(); - } - - private Person CheckAndAddPerson(Integer idPerson) { + private Person checkAndAddPerson(Integer idPerson) { Person person; if (personService.checkPerson(idPerson)) { person = personService.getPersonById(idPerson); @@ -48,4 +44,15 @@ public class Autoresponder { return person; } + private Unit nextUnit(List nextUnits, String message) { + if (nextUnits.size() > 0) { + Parser parser = new Parser(); + parser.setText(message); + parser.parse(); + return nextUnits.stream().filter(nextUnit -> !Collections.disjoint(nextUnit.getKeyWords(), parser.getWords())).max(new UnitPriorityComparator()).get(); + } else { + return null; + } + } + } diff --git a/src/main/java/org/sadtech/autoresponder/entity/Person.java b/src/main/java/org/sadtech/autoresponder/entity/Person.java index ab4a6b3..86fb8d1 100644 --- a/src/main/java/org/sadtech/autoresponder/entity/Person.java +++ b/src/main/java/org/sadtech/autoresponder/entity/Person.java @@ -1,11 +1,48 @@ package org.sadtech.autoresponder.entity; -import lombok.Data; +import java.util.Objects; -@Data public class Person { private Integer id; private Unit unit; + public Person(Integer id, Unit unit) { + this.id = id; + this.unit = unit; + } + + 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; + } + + @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/Unit.java b/src/main/java/org/sadtech/autoresponder/entity/Unit.java index 18f408c..eeccefc 100644 --- a/src/main/java/org/sadtech/autoresponder/entity/Unit.java +++ b/src/main/java/org/sadtech/autoresponder/entity/Unit.java @@ -1,19 +1,83 @@ package org.sadtech.autoresponder.entity; -import lombok.Data; - import java.util.List; +import java.util.Objects; import java.util.Set; -@Data -public class Unit { +public abstract class Unit { - private Integer idUnit; - private List nextUnits; - private String answer; - private Integer priority; private Set keyWords; private Integer matchThreshold; - private Boolean level = false; + private Integer priority; + private Boolean level; + private List nextUnits; + public Unit() { + level=false; + } + + public Unit(Set keyWords, Integer matchThreshold, Integer priority, Boolean level, List nextUnits) { + this.keyWords = keyWords; + this.matchThreshold = matchThreshold; + this.priority = priority; + this.level = level; + this.nextUnits = nextUnits; + } + + public Set getKeyWords() { + return keyWords; + } + + public void setKeyWords(Set keyWords) { + this.keyWords = keyWords; + } + + public Integer getMatchThreshold() { + return matchThreshold; + } + + public void setMatchThreshold(Integer matchThreshold) { + this.matchThreshold = matchThreshold; + } + + public Integer getPriority() { + return priority; + } + + public void setPriority(Integer priority) { + this.priority = priority; + } + + public Boolean getLevel() { + return level; + } + + public void setLevel(Boolean level) { + this.level = level; + } + + public List getNextUnits() { + return nextUnits; + } + + public void setNextUnits(List nextUnits) { + this.nextUnits = nextUnits; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Unit unit = (Unit) o; + return Objects.equals(keyWords, unit.keyWords) && + Objects.equals(matchThreshold, unit.matchThreshold) && + Objects.equals(priority, unit.priority) && + Objects.equals(level, unit.level) && + Objects.equals(nextUnits, unit.nextUnits); + } + + @Override + public int hashCode() { + return Objects.hash(keyWords, matchThreshold, priority, level, nextUnits); + } } diff --git a/src/main/java/org/sadtech/autoresponder/entity/compare/UnitPriorityComparator.java b/src/main/java/org/sadtech/autoresponder/entity/compare/UnitPriorityComparator.java new file mode 100644 index 0000000..6ced573 --- /dev/null +++ b/src/main/java/org/sadtech/autoresponder/entity/compare/UnitPriorityComparator.java @@ -0,0 +1,19 @@ +package org.sadtech.autoresponder.entity.compare; + +import org.sadtech.autoresponder.entity.Unit; + +import java.util.Comparator; + +public class UnitPriorityComparator implements Comparator { + + @Override + public int compare(Unit o1, Unit o2) { + if (o1.getPriority() > o2.getPriority()) { + return -1; + } else if (o1.getPriority().equals(o2.getPriority())) { + return 0; + } + return 1; + } + +} diff --git a/src/main/java/org/sadtech/autoresponder/repository/PersonRepository.java b/src/main/java/org/sadtech/autoresponder/repository/PersonRepository.java index a9c668e..c75b018 100644 --- a/src/main/java/org/sadtech/autoresponder/repository/PersonRepository.java +++ b/src/main/java/org/sadtech/autoresponder/repository/PersonRepository.java @@ -8,8 +8,11 @@ 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/UnitRepository.java b/src/main/java/org/sadtech/autoresponder/repository/UnitRepository.java index 0f4679a..d069a1e 100644 --- a/src/main/java/org/sadtech/autoresponder/repository/UnitRepository.java +++ b/src/main/java/org/sadtech/autoresponder/repository/UnitRepository.java @@ -7,8 +7,6 @@ import java.util.List; public interface UnitRepository { - Unit getUnitById(Integer idUnit); - void addUnit(Unit unit); void addUnits(Collection units); @@ -16,4 +14,5 @@ public interface UnitRepository { void removeUnit(Unit idUnit); List menuUnits(); + } diff --git a/src/main/java/org/sadtech/autoresponder/repository/impl/UnitRepositoryMap.java b/src/main/java/org/sadtech/autoresponder/repository/impl/UnitRepositoryMap.java deleted file mode 100644 index 7ed7c7e..0000000 --- a/src/main/java/org/sadtech/autoresponder/repository/impl/UnitRepositoryMap.java +++ /dev/null @@ -1,42 +0,0 @@ -package org.sadtech.autoresponder.repository.impl; - -import org.sadtech.autoresponder.entity.Unit; -import org.sadtech.autoresponder.repository.UnitRepository; - -import java.util.*; - -public class UnitRepositoryMap implements UnitRepository { - - private Map units = new HashMap<>(); - - @Override - public Unit getUnitById(Integer idUnit) { - return units.get(idUnit); - } - - @Override - public void addUnit(Unit unit) { - units.put(unit.getIdUnit(), unit); - } - - @Override - public void addUnits(Collection units) { - units.addAll(units); - } - - @Override - public void removeUnit(Unit idUnit) { - units.remove(idUnit.getIdUnit()); - } - - @Override - public List menuUnits() { - ArrayList unitsMenu = new ArrayList<>(); - for (Integer integer : units.keySet()) { - if (units.get(integer).getLevel()) { - unitsMenu.add(units.get(integer)); - } - } - return unitsMenu; - } -} diff --git a/src/main/java/org/sadtech/autoresponder/service/UnitService.java b/src/main/java/org/sadtech/autoresponder/service/UnitService.java index c319e0e..45285af 100644 --- a/src/main/java/org/sadtech/autoresponder/service/UnitService.java +++ b/src/main/java/org/sadtech/autoresponder/service/UnitService.java @@ -1,13 +1,14 @@ package org.sadtech.autoresponder.service; import org.sadtech.autoresponder.entity.Unit; +import org.sadtech.autoresponder.repository.UnitRepository; + +import java.util.List; public interface UnitService { - Unit nextUnit(Unit unit, String message); + List menuUnit(); - Unit getUnitById(Integer idUnit); - - Unit menuUnit(); + void addUnitRepository(UnitRepository unitRepository); } diff --git a/src/main/java/org/sadtech/autoresponder/service/impl/PersonServiceImpl.java b/src/main/java/org/sadtech/autoresponder/service/impl/PersonServiceImpl.java index 5325b22..d7933f2 100644 --- a/src/main/java/org/sadtech/autoresponder/service/impl/PersonServiceImpl.java +++ b/src/main/java/org/sadtech/autoresponder/service/impl/PersonServiceImpl.java @@ -1,15 +1,17 @@ package org.sadtech.autoresponder.service.impl; -import lombok.AllArgsConstructor; import org.sadtech.autoresponder.entity.Person; import org.sadtech.autoresponder.repository.PersonRepository; import org.sadtech.autoresponder.service.PersonService; -@AllArgsConstructor public class PersonServiceImpl implements PersonService { private PersonRepository personRepository; + public PersonServiceImpl(PersonRepository personRepository) { + this.personRepository = personRepository; + } + @Override public Person getPersonById(Integer integer) { return personRepository.getPersonById(integer); diff --git a/src/main/java/org/sadtech/autoresponder/service/impl/UnitServiceImpl.java b/src/main/java/org/sadtech/autoresponder/service/impl/UnitServiceImpl.java index aa8f8e0..3a975bb 100644 --- a/src/main/java/org/sadtech/autoresponder/service/impl/UnitServiceImpl.java +++ b/src/main/java/org/sadtech/autoresponder/service/impl/UnitServiceImpl.java @@ -1,52 +1,38 @@ package org.sadtech.autoresponder.service.impl; -import lombok.AllArgsConstructor; -import lombok.NonNull; import lombok.extern.log4j.Log4j; import org.sadtech.autoresponder.entity.Unit; import org.sadtech.autoresponder.repository.UnitRepository; import org.sadtech.autoresponder.service.UnitService; -import org.sadtech.autoresponder.submodule.parser.Parser; import java.util.ArrayList; -import java.util.Collections; +import java.util.List; @Log4j -@AllArgsConstructor public class UnitServiceImpl implements UnitService { - private UnitRepository unitRepository; + private List unitRepositories; - public Unit nextUnit(Unit unit, @NonNull String message) { - ArrayList nextUnits = (ArrayList) unit.getNextUnits(); - if (nextUnits.size() > 0) { - Parser parser = new Parser(); - parser.setText(message); - parser.parse(); - Unit unitReturn = new Unit(); - unitReturn.setPriority(0); - for (Unit nextUnit : nextUnits) { - if (!Collections.disjoint(nextUnit.getKeyWords(), parser.getWords()) && (nextUnit.getPriority() > unitReturn.getPriority())) { - unitReturn = nextUnit; - } - } - return unitReturn; - } else { - return null; + public UnitServiceImpl() { + unitRepositories = new ArrayList<>(); + } + + public UnitServiceImpl(List unitRepositories) { + this.unitRepositories = unitRepositories; + } + + @Override + public List menuUnit() { + List units = new ArrayList<>(); + for (UnitRepository unitRepository : unitRepositories) { + units.addAll(unitRepository.menuUnits()); } - + return units; } @Override - public Unit getUnitById(@NonNull Integer idUnit) { - return unitRepository.getUnitById(idUnit); - } - - @Override - public Unit menuUnit() { - Unit menuUnit = new Unit(); - menuUnit.setNextUnits(unitRepository.menuUnits()); - return menuUnit; + public void addUnitRepository(UnitRepository unitRepository) { + unitRepositories.add(unitRepository); } diff --git a/src/test/java/org/sadtech/autoresponder/AutoresponderTest.java b/src/test/java/org/sadtech/autoresponder/AutoresponderTest.java index 7d79844..29879d2 100644 --- a/src/test/java/org/sadtech/autoresponder/AutoresponderTest.java +++ b/src/test/java/org/sadtech/autoresponder/AutoresponderTest.java @@ -1,127 +1,126 @@ -package org.sadtech.autoresponder; - -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.sadtech.autoresponder.entity.Person; -import org.sadtech.autoresponder.entity.Unit; -import org.sadtech.autoresponder.repository.impl.PersonRepositoryMap; -import org.sadtech.autoresponder.repository.impl.UnitRepositoryMap; -import org.sadtech.autoresponder.service.impl.PersonServiceImpl; -import org.sadtech.autoresponder.service.impl.UnitServiceImpl; - -import java.util.ArrayList; -import java.util.HashSet; - -public class AutoresponderTest { - - private Person person = new Person(); - private Unit unit = new Unit(); - private Unit unit2 = new Unit(); - private Unit unit3 = new Unit(); - private ArrayList units = new ArrayList<>(); - private UnitRepositoryMap unitRepository = new UnitRepositoryMap(); - private PersonRepositoryMap personRepository = new PersonRepositoryMap(); - - private UnitServiceImpl unitService = new UnitServiceImpl(unitRepository); - private PersonServiceImpl personService = new PersonServiceImpl(personRepository); - private Autoresponder autoresponder = new Autoresponder(unitService, personService); - - - @Before - public void before() { - HashSet words = new HashSet<>(); - HashSet words2 = new HashSet<>(); - words.add("тест"); - words2.add("тест"); - words2.add("привет"); - - unit.setIdUnit(1); - unit.setLevel(true); - unit.setPriority(50); - unit.setKeyWords(words); - unit.setAnswer("Здравствуйте, вы написали в нашу компанию!"); - unit.setMatchThreshold(100); - - units.add(unit2); - units.add(unit3); - - unit.setNextUnits(units); - - unit2.setIdUnit(2); - unit2.setAnswer("Ответ с {0} параметрами!"); - unit2.setPriority(60); - unit2.setKeyWords(words); - unit2.setMatchThreshold(100); - - unit3.setIdUnit(3); - unit3.setAnswer("Второй Ответ с {0} параметрами!"); - unit3.setPriority(50); - unit3.setKeyWords(words2); - unit3.setMatchThreshold(100); - - person.setUnit(unit); - person.setId(1); - - unitRepository.addUnit(unit); - unitRepository.addUnit(unit2); - personRepository.addPerson(person); - } - - @Test - public void usualAnswer() { - Assert.assertEquals(autoresponder.answer(person.getId(), "Привет это тест срабатывания"), "Ответ с {0} параметрами!"); - } - - @Test - public void answerOneParameter() { - ArrayList words = new ArrayList<>(); - words.add("одним"); - Assert.assertEquals(autoresponder.answer(person.getId(), "Привет это тест срабатывания", words), "Ответ с одним параметрами!"); - } - - @Test - public void NoAnswer() { - person.setUnit(null); - String test = autoresponder.answer(person.getId(), "Привет это срабатывания"); - Assert.assertNull(test); - } - - @Test - public void answerTwoParameter() { - ArrayList words = new ArrayList<>(); - words.add("одним"); - words.add("двумя"); - unit2.setAnswer("Ответ с {0} и {1}"); - Assert.assertEquals(autoresponder.answer(person.getId(), "Привет это тест срабатывания", words), "Ответ с одним и двумя"); - } - - @Test - public void incorrectSettingsWords() { - ArrayList words = new ArrayList<>(); - words.add("одним"); - words.add("двумя"); - unit2.setAnswer("Ответ с {1} и {3}"); - Assert.assertEquals(autoresponder.answer(person.getId(), "Привет это тест срабатывания", words), "Ответ с двумя и {3}"); - } - - @Test - public void answerRepeatSingleParameter() { - ArrayList words = new ArrayList<>(); - words.add("одним"); - words.add("двумя"); - unit2.setAnswer("Ответ с {1} и {1}"); - Assert.assertEquals(autoresponder.answer(person.getId(), "Привет это тест срабатывания", words), "Ответ с двумя и двумя"); - } - - @Test - public void answerByPriority() { - Assert.assertEquals(autoresponder.answer(person.getId(), "Привет это тест срабатывания"), "Ответ с {0} параметрами!"); - } - - @Test - public void answerNoPerson() { - Assert.assertEquals(autoresponder.answer(100, "Привет это тест срабатывания"), "Здравствуйте, вы написали в нашу компанию!"); - Assert.assertEquals(autoresponder.answer(100, "Привет это тест срабатывания"), "Ответ с {0} параметрами!"); - } +//package org.sadtech.autoresponder; +// +//import org.junit.Assert; +//import org.junit.Before; +//import org.junit.Test; +//import org.sadtech.autoresponder.entity.Person; +//import org.sadtech.autoresponder.entity.Unit; +//import org.sadtech.autoresponder.repository.impl.PersonRepositoryMap; +//import org.sadtech.autoresponder.service.impl.PersonServiceImpl; +//import org.sadtech.autoresponder.service.impl.UnitServiceImpl; +// +//import java.util.ArrayList; +//import java.util.HashSet; +// +//public class AutoresponderTest { +// +// private Person person = new Person(); +// private Unit unit = new Unit(); +// private Unit unit2 = new Unit(); +// private Unit unit3 = new Unit(); +// private ArrayList units = new ArrayList<>(); +// private UnitRepositoryMap unitRepository = new UnitRepositoryMap(); +// private PersonRepositoryMap personRepository = new PersonRepositoryMap(); +// +// private UnitServiceImpl unitService = new UnitServiceImpl(unitRepository); +// private PersonServiceImpl personService = new PersonServiceImpl(personRepository); +// private Autoresponder autoresponder = new Autoresponder(unitService, personService); +// +// +// @Before +// public void before() { +// HashSet words = new HashSet<>(); +// HashSet words2 = new HashSet<>(); +// words.add("тест"); +// words2.add("тест"); +// words2.add("привет"); +// +// unit.setIdUnit(1); +// unit.setLevel(true); +// unit.setPriority(50); +// unit.setKeyWords(words); +// unit.setAnswer("Здравствуйте, вы написали в нашу компанию!"); +// unit.setMatchThreshold(100); +// +// units.add(unit2); +// units.add(unit3); +// +// unit.setNextUnits(units); +// +// unit2.setIdUnit(2); +// unit2.setAnswer("Ответ с {0} параметрами!"); +// unit2.setPriority(60); +// unit2.setKeyWords(words); +// unit2.setMatchThreshold(100); +// +// unit3.setIdUnit(3); +// unit3.setAnswer("Второй Ответ с {0} параметрами!"); +// unit3.setPriority(50); +// unit3.setKeyWords(words2); +// unit3.setMatchThreshold(100); +// +// person.setUnit(unit); +// person.setId(1); +// +// unitRepository.addUnit(unit); +// unitRepository.addUnit(unit2); +// personRepository.addPerson(person); +// } +// +// @Test +// public void usualAnswer() { +// Assert.assertEquals(autoresponder.answer(person.getId(), "Привет это тест срабатывания"), "Ответ с {0} параметрами!"); +// } +// +// @Test +// public void answerOneParameter() { +// ArrayList words = new ArrayList<>(); +// words.add("одним"); +// Assert.assertEquals(autoresponder.answer(person.getId(), "Привет это тест срабатывания", words), "Ответ с одним параметрами!"); +// } +// +// @Test +// public void NoAnswer() { +// person.setUnit(null); +// String test = autoresponder.answer(person.getId(), "Привет это срабатывания"); +// Assert.assertNull(test); +// } +// +// @Test +// public void answerTwoParameter() { +// ArrayList words = new ArrayList<>(); +// words.add("одним"); +// words.add("двумя"); +// unit2.setAnswer("Ответ с {0} и {1}"); +// Assert.assertEquals(autoresponder.answer(person.getId(), "Привет это тест срабатывания", words), "Ответ с одним и двумя"); +// } +// +// @Test +// public void incorrectSettingsWords() { +// ArrayList words = new ArrayList<>(); +// words.add("одним"); +// words.add("двумя"); +// unit2.setAnswer("Ответ с {1} и {3}"); +// Assert.assertEquals(autoresponder.answer(person.getId(), "Привет это тест срабатывания", words), "Ответ с двумя и {3}"); +// } +// +// @Test +// public void answerRepeatSingleParameter() { +// ArrayList words = new ArrayList<>(); +// words.add("одним"); +// words.add("двумя"); +// unit2.setAnswer("Ответ с {1} и {1}"); +// Assert.assertEquals(autoresponder.answer(person.getId(), "Привет это тест срабатывания", words), "Ответ с двумя и двумя"); +// } +// +// @Test +// public void answerByPriority() { +// Assert.assertEquals(autoresponder.answer(person.getId(), "Привет это тест срабатывания"), "Ответ с {0} параметрами!"); +// } +// +// @Test +// public void answerNoPerson() { +// Assert.assertEquals(autoresponder.answer(100, "Привет это тест срабатывания"), "Здравствуйте, вы написали в нашу компанию!"); +// Assert.assertEquals(autoresponder.answer(100, "Привет это тест срабатывания"), "Ответ с {0} параметрами!"); +// } } \ No newline at end of file