diff --git a/src/main/java/org/sadtech/autoresponder/Autoresponder.java b/src/main/java/org/sadtech/autoresponder/Autoresponder.java index 87c07cd..69e2edb 100644 --- a/src/main/java/org/sadtech/autoresponder/Autoresponder.java +++ b/src/main/java/org/sadtech/autoresponder/Autoresponder.java @@ -3,6 +3,7 @@ package org.sadtech.autoresponder; import org.sadtech.autoresponder.compare.UnitPriorityComparator; import org.sadtech.autoresponder.entity.Unit; import org.sadtech.autoresponder.entity.UnitPointer; +import org.sadtech.autoresponder.exception.NotFoundUnitException; import org.sadtech.autoresponder.service.UnitPointerService; import org.sadtech.autoresponder.util.Parser; import org.slf4j.Logger; @@ -22,14 +23,11 @@ public class Autoresponder { private static final Logger log = LoggerFactory.getLogger(Autoresponder.class); private static final UnitPriorityComparator UNIT_PRIORITY_COMPARATOR = new UnitPriorityComparator(); - /* - Начальные unit, по которым происходит поиск, если пользователь только обратился или закончил сценарий. - */ - private final Set menuUnits; + private final Set startUnits; private final UnitPointerService unitPointerService; - public Autoresponder(UnitPointerService unitPointerService, Set menuUnits) { - this.menuUnits = menuUnits; + public Autoresponder(UnitPointerService unitPointerService, Set startUnits) { + this.startUnits = startUnits; this.unitPointerService = unitPointerService; } @@ -43,57 +41,46 @@ public class Autoresponder { public Unit answer(Integer personId, String message) { UnitPointer unitPointer = checkAndAddPerson(personId); Unit unit; - if (unitPointer.getUnit() == null) { - unit = nextUnit(menuUnits, message); // выбирает unit из menuUnits, если пользователь обращается впервые + if (unitPointer.getUnit() == null || unitPointer.getUnit().getNextUnits() == null) { + unit = nextUnit(startUnits, message); // выбирает unit из startUnits, если пользователь обращается впервые } else { - if (unitPointer.getUnit().getNextUnits() == null) { - unit = nextUnit(menuUnits, message); // если пользователь закончил сценарий, то выбирает следующий юнит из menuUnits - } else { - unit = nextUnit(unitPointer.getUnit().getNextUnits(), message); - } - } - if (unit != null) { - unitPointerService.edit(personId, unit); + unit = nextUnit(unitPointer.getUnit().getNextUnits(), message); } + unitPointerService.edit(personId, unit); return unit; } - private UnitPointer checkAndAddPerson(Integer idPerson) { + private Unit nextUnit(Set nextUnits, String message) { + Optional unit = nextUnits.stream() + .filter(nextUnit -> nextUnit.getPattern() != null) + .filter(nextUnit -> patternReg(nextUnit, message)) + .max(UNIT_PRIORITY_COMPARATOR); + + if (!unit.isPresent()) { + unit = nextUnits.stream() + .filter(nextUnit -> textPercentageMatch(nextUnit, Parser.parse(message)) >= nextUnit.getMatchThreshold()) + .max(UNIT_PRIORITY_COMPARATOR); + } + + if (!unit.isPresent()) { + unit = nextUnits.stream() + .filter(nextUnit -> (nextUnit.getPattern() == null && nextUnit.getKeyWords() == null)) + .max(UNIT_PRIORITY_COMPARATOR); + } + return unit.orElseThrow(NotFoundUnitException::new); + } + + private UnitPointer checkAndAddPerson(Integer personId) { UnitPointer unitPointer; - if (unitPointerService.check(idPerson)) { - unitPointer = unitPointerService.getByEntityId(idPerson); + if (unitPointerService.check(personId)) { + unitPointer = unitPointerService.getByEntityId(personId); } else { - unitPointer = new UnitPointer(idPerson); + unitPointer = new UnitPointer(personId); unitPointerService.add(unitPointer); } return unitPointer; } - private Unit nextUnit(Set nextUnits, String message) { - if (nextUnits.size() > 0) { - Optional patternUnits = nextUnits.stream() - .filter(nextUnit -> nextUnit.getPattern() != null) - .filter(nextUnit -> patternReg(nextUnit, message)) - .max(UNIT_PRIORITY_COMPARATOR); - - if (!patternUnits.isPresent()) { - patternUnits = nextUnits.stream() - .filter(nextUnit -> textPercentageMatch(nextUnit, Parser.parse(message)) >= nextUnit.getMatchThreshold()) - .max(UNIT_PRIORITY_COMPARATOR); - } - - if (!patternUnits.isPresent()) { - patternUnits = nextUnits.stream() - .filter(nextUnit -> (nextUnit.getPattern() == null && nextUnit.getKeyWords() == null)) - .max(UNIT_PRIORITY_COMPARATOR); - } - - return patternUnits.orElse(null); - } else { - return null; - } - } - private boolean patternReg(Unit unit, String message) { Pattern pattern = unit.getPattern(); Matcher m = pattern.matcher(message); diff --git a/src/main/java/org/sadtech/autoresponder/exception/NotFoundUnitException.java b/src/main/java/org/sadtech/autoresponder/exception/NotFoundUnitException.java new file mode 100644 index 0000000..bb8be59 --- /dev/null +++ b/src/main/java/org/sadtech/autoresponder/exception/NotFoundUnitException.java @@ -0,0 +1,4 @@ +package org.sadtech.autoresponder.exception; + +public class NotFoundUnitException extends RuntimeException { +}