Добавил возможность устанавливать юниты, которые всегда будут участвовать в обработке. Добавил возможность устанавливать юнит для ответов, когда никакой юнит для ответа не найден

This commit is contained in:
Struchkov Mark 2022-07-15 08:45:14 +03:00
parent 2a9413ab46
commit 6d9454fd03
6 changed files with 44 additions and 14 deletions

View File

@ -13,6 +13,8 @@ public @interface Unit {
String value() default "";
boolean mainUnit() default false;
boolean main() default false;
boolean global() default false;
}

View File

@ -31,4 +31,6 @@ public interface StorylineService<T extends Message> {
Optional<MainUnit> getUnitByName(String unitName);
void setDefaultUnit(String unitName);
}

View File

@ -80,6 +80,10 @@ public class GeneralAutoResponder<T extends Message> {
this.errorHandler = errorHandler;
}
public void setDefaultUnit(String unitName) {
storyLineService.setDefaultUnit(unitName);
}
public void processingNewMessage(T newMessage) {
if (newMessage != null) {
final boolean state = personSettingService.getStateProcessingByPersonId(newMessage.getPersonId()).orElse(true);
@ -107,7 +111,8 @@ public class GeneralAutoResponder<T extends Message> {
modifiable.forEach(m -> m.change(message));
}
final Set<MainUnit> units = storyLineService.getNextUnitByPersonId(message.getPersonId());
final Optional<MainUnit> optAnswer = Responder.nextUnit(message.getText(), units);
final Optional<MainUnit> optAnswer = Responder.nextUnit(message.getText(), units)
.or(storyLineService::getDefaultUnit);
if (optAnswer.isPresent()) {
final MainUnit answer = optAnswer.get();
if (checkPermission(answer.getAccessibility(), message)) {

View File

@ -12,8 +12,8 @@ import java.util.Set;
public class Storyline {
private MainUnit defaultUnit;
private final Set<MainUnit> startingUnits = new HashSet<>();
private final Set<MainUnit> globalUnits = new HashSet<>();
private final Map<String, MainUnit> units = new HashMap<>();
public Storyline(Set<MainUnit> startingUnits, Map<String, MainUnit> units) {
@ -21,12 +21,12 @@ public class Storyline {
this.units.putAll(units);
}
public void setDefaultUnit(MainUnit defaultUnit) {
this.defaultUnit = defaultUnit;
public void addGlobalUnits(Set<MainUnit> globalUnits) {
this.globalUnits.addAll(globalUnits);
}
public Optional<MainUnit> getDefaultUnit() {
return Optional.ofNullable(defaultUnit);
public Set<MainUnit> getGlobalUnits() {
return globalUnits;
}
/**

View File

@ -2,9 +2,8 @@ package dev.struchkov.godfather.core;
import dev.struchkov.godfather.context.domain.UnitDefinition;
import dev.struchkov.godfather.context.domain.annotation.Unit;
import dev.struchkov.godfather.context.exception.UnitConfigException;
import dev.struchkov.godfather.context.domain.unit.LazyUnit;
import dev.struchkov.godfather.context.domain.unit.MainUnit;
import dev.struchkov.godfather.context.exception.UnitConfigException;
import dev.struchkov.haiti.utils.Inspector;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -31,7 +30,9 @@ public class StorylineMaker {
private final Map<String, UnitDefinition> unitDefinitions = new HashMap<>();
private final Map<String, MainUnit> unitMap = new HashMap<>();
private final Set<String> mainUnits = new HashSet<>();
private final Set<String> globalUnits = new HashSet<>();
public StorylineMaker(List<Object> unitConfigurations) {
this.configurations.addAll(unitConfigurations);
@ -50,7 +51,10 @@ public class StorylineMaker {
try {
createUnitMap();
final Set<MainUnit> mainUnit = getMainUnit();
return new Storyline(mainUnit, unitMap);
final Set<MainUnit> globalUnit = getGlobalUnit();
final Storyline storyline = new Storyline(mainUnit, unitMap);
storyline.addGlobalUnits(globalUnit);
return storyline;
} catch (IllegalAccessException | InvocationTargetException e) {
log.error(e.getMessage(), e);
}
@ -64,6 +68,12 @@ public class StorylineMaker {
.collect(Collectors.toSet());
}
private Set<MainUnit> getGlobalUnit() {
return globalUnits.stream()
.map(unitMap::get)
.collect(Collectors.toSet());
}
private void createUnitMap() throws IllegalAccessException, InvocationTargetException {
for (UnitDefinition unitDefinition : unitDefinitions.values()) {
if (!unitMap.containsKey(unitDefinition.getName())) {
@ -118,9 +128,12 @@ public class StorylineMaker {
unitDefinition.setMethod(method);
unitDefinition.setObjectConfig(config);
if (unitConfig.mainUnit()) {
if (unitConfig.main()) {
mainUnits.add(unitName);
}
if (unitConfig.global()) {
globalUnits.add(unitName);
}
final Parameter[] nextUnits = method.getParameters();
if (nextUnits.length > 0) {

View File

@ -27,6 +27,7 @@ public class StorylineMailService implements StorylineService<Mail> {
private final UnitPointerService unitPointerService;
private final StorylineRepository storylineRepository;
private final Storyline storyLine;
private String defaultUnitName;
public StorylineMailService(
UnitPointerService unitPointerService,
@ -60,8 +61,9 @@ public class StorylineMailService implements StorylineService<Mail> {
if (optMainUnits.isEmpty()) {
storylineRepository.cleanHistoryByPersonId(personId);
}
return optMainUnits
.orElse(storyLine.getStartingUnits());
final Set<MainUnit> nextUnits = optMainUnits.orElse(storyLine.getStartingUnits());
nextUnits.addAll(storyLine.getGlobalUnits());
return nextUnits;
}
@Override
@ -88,7 +90,13 @@ public class StorylineMailService implements StorylineService<Mail> {
@Override
public Optional<MainUnit> getDefaultUnit() {
return storyLine.getDefaultUnit();
if (defaultUnitName == null) return Optional.empty();
return storyLine.getUnit(defaultUnitName);
}
@Override
public void setDefaultUnit(String defaultUnit) {
defaultUnitName = defaultUnit;
}
//TODO [22.06.2022]: Временное решение для ленивой инициализации