Skip to content

Commit

Permalink
recompute eligibility categories correctly; closes #1088; closes #1087
Browse files Browse the repository at this point in the history
  • Loading branch information
jflamy committed Sep 4, 2024
1 parent 3bef80e commit f659c4c
Showing 1 changed file with 44 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.apache.commons.lang3.BooleanUtils;
Expand Down Expand Up @@ -150,8 +149,7 @@ public Athlete add(Athlete athlete) {
/**
* Create a binder with our special validation status handling
*
* @see org.vaadin.crudui.form.AbstractAutoGeneratedCrudFormFactory#buildBinder(org.vaadin.crudui.crud.CrudOperation,
* java.lang.Object)
* @see org.vaadin.crudui.form.AbstractAutoGeneratedCrudFormFactory#buildBinder(org.vaadin.crudui.crud.CrudOperation, java.lang.Object)
*/
@Override
public Binder<Athlete> buildBinder(CrudOperation operation, Athlete ignored) {
Expand All @@ -164,8 +162,7 @@ public Binder<Athlete> buildBinder(CrudOperation operation, Athlete ignored) {
/**
* Change the caption to show the current athlete name and group
*
* @see org.vaadin.crudui.form.impl.form.factory.DefaultCrudFormFactory#buildCaption(org.vaadin.crudui.
* crudGrid.CrudOperation, java.lang.Object)
* @see org.vaadin.crudui.form.impl.form.factory.DefaultCrudFormFactory#buildCaption(org.vaadin.crudui. crudGrid.CrudOperation, java.lang.Object)
*/
@Override
public String buildCaption(CrudOperation operation, Athlete a) {
Expand All @@ -177,9 +174,9 @@ public String buildCaption(CrudOperation operation, Athlete a) {
}

/**
* @see app.owlcms.nui.crudui.OwlcmsCrudFormFactory#buildFooter(org.vaadin.crudui.crud.CrudOperation,
* java.lang.Object, com.vaadin.flow.component.ComponentEventListener,
* com.vaadin.flow.component.ComponentEventListener, com.vaadin.flow.component.ComponentEventListener)
* @see app.owlcms.nui.crudui.OwlcmsCrudFormFactory#buildFooter(org.vaadin.crudui.crud.CrudOperation, java.lang.Object,
* com.vaadin.flow.component.ComponentEventListener, com.vaadin.flow.component.ComponentEventListener,
* com.vaadin.flow.component.ComponentEventListener)
*/
@Override
public Component buildFooter(CrudOperation operation,
Expand Down Expand Up @@ -248,10 +245,9 @@ public Component buildFooter(CrudOperation operation,
}

/**
* @see app.owlcms.nui.crudui.OwlcmsCrudFormFactory#buildNewForm(org.vaadin.crudui.crud.CrudOperation,
* java.lang.Object, boolean, com.vaadin.flow.component.ComponentEventListener,
* @see app.owlcms.nui.crudui.OwlcmsCrudFormFactory#buildNewForm(org.vaadin.crudui.crud.CrudOperation, java.lang.Object, boolean,
* com.vaadin.flow.component.ComponentEventListener, com.vaadin.flow.component.ComponentEventListener,
* com.vaadin.flow.component.button.Button[])
* com.vaadin.flow.component.ComponentEventListener, com.vaadin.flow.component.button.Button[])
*/
@Override
public Component buildNewForm(CrudOperation operation, Athlete aFromList, boolean readOnly,
Expand Down Expand Up @@ -345,8 +341,7 @@ public Athlete update(Athlete athlete) {
/**
* Add the field-level validations
*
* @see org.vaadin.crudui.form.AbstractAutoGeneratedCrudFormFactory#bindField(com.vaadin.flow.component.HasValue,
* java.lang.String, java.lang.Class)
* @see org.vaadin.crudui.form.AbstractAutoGeneratedCrudFormFactory#bindField(com.vaadin.flow.component.HasValue, java.lang.String, java.lang.Class)
*/
@Override
protected void bindField(@SuppressWarnings("rawtypes") HasValue field, String property, Class<?> propertyType,
Expand All @@ -364,7 +359,7 @@ protected void validateBodyWeight(Binder.BindingBuilder<Athlete, Double> binding
protected void validateCategory(Binder.BindingBuilder<Athlete, Category> bindingBuilder) {
// we have age and bodyweight, is registration category selected
Validator<Category> v0 = Validator.from((category) -> {
logger.debug("v0 {} {}",category, isIgnoreErrors());
logger.debug("v0 {} {}", category, isIgnoreErrors());
if (isIgnoreErrors()) {
return true;
}
Expand Down Expand Up @@ -482,18 +477,18 @@ protected void validateCategory(Binder.BindingBuilder<Athlete, Category> binding
}
logger.debug("v4 {} {}", category, category.getName(), category.getCode());
try {
// Category matchingEligible = null;
// Category matchingEligible = null;
// Set<Category> eligibles = getEditedAthlete().getEligibleCategories();
List<Category> eligibles = this.allEligible;
if (eligibles == null || eligibles.isEmpty()) {
return category == null;
}
// for (Category eligible : eligibles) {
// if (eligible.getCode() == category.getCode()) {
// matchingEligible = eligible;
// break;
// }
// }
// for (Category eligible : eligibles) {
// if (eligible.getCode() == category.getCode()) {
// matchingEligible = eligible;
// break;
// }
// }
Set<Category> selectedEligibles = eligibleField.getValue();
return !selectedEligibles.isEmpty();
} catch (Exception e) {
Expand Down Expand Up @@ -523,7 +518,7 @@ protected void validateCategory(Binder.BindingBuilder<Athlete, Category> binding

// category is required
Validator<Category> v6 = Validator.from((category) -> {
logger.debug("v6 ignoreErrors={}",isIgnoreErrors());
logger.debug("v6 ignoreErrors={}", isIgnoreErrors());
try {
return isIgnoreErrors() ? true : category != null;
} catch (Exception e) {
Expand Down Expand Up @@ -1091,7 +1086,7 @@ private void filterCategories(Category category, boolean initCategories) {
this.categoryField,
this.qualifyingTotalField);

logger.debug("**** allEligibles: {} selectedCategories {}",allEligible, selectedCategories);
logger.debug("**** allEligibles: {} selectedCategories {}", allEligible, selectedCategories);
Stream<Category> filter = this.allEligible.stream().filter(c -> c.sameAsAny(selectedCategories));
Category category2 = filter.findFirst().orElse(null);
setCategoryFieldValue(category2);
Expand Down Expand Up @@ -1208,41 +1203,41 @@ private void recomputeCategories(

Category cat = categoryField.getValue();
Integer age = getAgeFromFields();
logger.warn("cat={} age={}", cat, age);
// logger.debug("cat={} age={}", cat, age);
List<Championship> previousChampionships = championshipsForCategories(eligibleField.getValue());
logger.warn("previous championships {} {} {}", eligibleField.getValue(), previousChampionships, LoggerUtils.whereFrom());
// logger.debug("previous championships {} {} {}", eligibleField.getValue(), previousChampionships, LoggerUtils.whereFrom());
if (bodyWeightField.getValue() != null) {
if (genderField.getValue() != null && age != null) {
// body weight, gender, date
this.allEligible = findEligibleCategories(genderField, getAgeFromFields(), bodyWeightField,
categoryField, qualifyingTotalField2);
logger.warn("cat {} eli {}", cat, this.allEligible);
if (cat != null && categoryIsEligible(cat, this.allEligible)) {
// logger.debug("cat {} eli {}", cat, this.allEligible);
if (cat != null && categoryIsEligible(cat, this.allEligible) && cat.getMaximumWeight() < 999) {
// current registration category is amongst eligibles. Don't recompute anything.
logger.warn("leave alone");
// logger.debug("leave alone");
if (Config.getCurrent().featureSwitch("bestMatchCategories")) {
Category bestMatchCategory = bestMatch(this.allEligible);
updateCategoryFields(bestMatchCategory, categoryField, eligibleField, qualifyingTotalField2,
this.allEligible, this.allEligible, false);
}
} else {
logger.warn("recompute, cat={} allEligible = {}", cat, this.allEligible);
// logger.debug("recompute, cat={} allEligible = {}", cat, this.allEligible);
// category is null or not within eligibles, recompute

List<Category> filteredEligibles = allEligible.stream()
.filter(e -> previousChampionships.isEmpty() || previousChampionships.contains(e.getAgeGroup().getChampionship())).toList();
logger.warn("eligibilty filtered on championship {}",filteredEligibles);
.filter(e -> previousChampionships.isEmpty() || previousChampionships.contains(e.getAgeGroup().getChampionship())).toList();
// logger.debug("eligibilty filtered on championship {}",filteredEligibles);

Category bestMatchCategory = bestMatch(filteredEligibles);
updateCategoryFields(bestMatchCategory, categoryField, eligibleField, qualifyingTotalField2,
filteredEligibles, this.allEligible, true);
// Category bestMatchCategory = bestMatch(this.allEligible);
// updateCategoryFields(bestMatchCategory, categoryField, eligibleField, qualifyingTotalField2,
// this.allEligible, true);
filteredEligibles, this.allEligible, true);

// Category bestMatchCategory = bestMatch(this.allEligible);
// updateCategoryFields(bestMatchCategory, categoryField, eligibleField, qualifyingTotalField2,
// this.allEligible, true);
}
} else {
logger.debug/* edit */("bw, but need age and gender");
// logger.debug("bw, but need age and gender");
// cannot compute eligibility and category
}
} else {
Expand All @@ -1255,13 +1250,16 @@ private void recomputeCategories(
if (ageFromFields != null && ageFromFields > 5 && ageFromFields < 120) {
this.allEligible = CategoryRepository.doFindEligibleCategories(this.getEditedAthlete(), gender,
ageFromFields, bw, qualifyingTotal);
logger.warn("cat-based allEligible {} {}", cat, this.allEligible);
// logger.debug("cat-based allEligible {} {}", cat, this.allEligible);
// Category bestMatchCategory = bestMatch(this.allEligible);
List<Category> filteredEligibles = allEligible.stream()
.filter(e -> previousChampionships.isEmpty() || previousChampionships.contains(e.getAgeGroup().getChampionship())).toList();
// logger.debug("eligibilty2 filtered on championship {}",filteredEligibles);
updateCategoryFields(cat, categoryField, eligibleField, qualifyingTotalField2,
filteredEligibles,
this.allEligible,
this.allEligible,
//true);
false);
true);
// false);
}
} else if (genderField.getValue() != null && age != null) {
// use age and qualifying total
Expand Down Expand Up @@ -1379,7 +1377,7 @@ private void updateCategoryFields(Category bestMatch, ComboBox<Category> categor
List<Category> allEligibles,
boolean recomputeEligibles) {

logger.warn("updating category fields {}",LoggerUtils.stackTrace());
//logger.debug("updating category fields {}",LoggerUtils.stackTrace());
LinkedHashSet<Category> newEligibles = new LinkedHashSet<>();
Set<Category> prevEligibles;
if (recomputeEligibles) {
Expand All @@ -1388,9 +1386,7 @@ private void updateCategoryFields(Category bestMatch, ComboBox<Category> categor
} else {
prevEligibles = eligibleField.getValue();
}
logger.warn("updateCategoryFields {} {} - {} {} {}",
categoryField.getValue(), bestMatch, prevEligibles, filteredEligibles,
LoggerUtils.whereFrom());
//logger.debug("updateCategoryFields {} {} - {} {} {}", categoryField.getValue(), bestMatch, prevEligibles, filteredEligibles, LoggerUtils.whereFrom());

if (prevEligibles != null) {
// update the list of eligible categories. Must use the matching items in
Expand All @@ -1405,8 +1401,7 @@ private void updateCategoryFields(Category bestMatch, ComboBox<Category> categor
}
}
}
logger.debug/* edit */("updateCategoryFields filteredEligibles {}",
filteredEligibles.stream().map(v -> v.shortDump()).collect(Collectors.toList()));
//logger.debug("updateCategoryFields filteredEligibles {}", filteredEligibles.stream().map(v -> v.shortDump()).collect(Collectors.toList()));

// List<Category> pertinentCategories = CategoryRepository.findByGenderAgeBW(getGenderFieldValue(),
// getAgeFromFields(), null);
Expand Down

0 comments on commit f659c4c

Please sign in to comment.