Skip to content

Commit

Permalink
(#21): prepare pathway view support classes f. recipe
Browse files Browse the repository at this point in the history
  • Loading branch information
andi-huber committed Mar 10, 2024
1 parent 89547cb commit 61629a4
Show file tree
Hide file tree
Showing 33 changed files with 385 additions and 124 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,10 @@ public Food act(@Parameter final FoodFacet foodFacet) {
entity.setFoodSubSubgroupCode(subgroup.getFoodSubSubgroupCode());
});

entity.setFacetCode(fd.getFacetCode());
entity.setFoodFacetCode(fd.getFacetCode());
entity.setFacetDisplayOrder(-1);

entity.setDescriptorCode(fd.getCode());
entity.setFoodDescriptorCode(fd.getCode());
entity.setDescriptorDisplayOrder(-1);

entity.setDefaultFlag(null);
Expand Down Expand Up @@ -131,7 +131,7 @@ public List<FoodFacet> choicesFoodFacet() {
private Set<FoodFacet.SecondaryKey> foodFacetCodesAsDefinedByFoodClassification(
final List<FacetDescriptorPathwayForFoodGroup> pathwayForFoodGroup) {
return pathwayForFoodGroup.stream()
.map(FacetDescriptorPathwayForFoodGroup::getFacetCode)
.map(FacetDescriptorPathwayForFoodGroup::getFoodFacetCode)
.map(FoodFacet.SecondaryKey::new)
.collect(Collectors.toSet());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public Food act(@Parameter final FoodFacet foodFacet) {

entity.setFoodCode(mixee.getCode());
entity.setDisplayOrder(-1); // TODO needs post processing
entity.setMandatoryInSequenceOfFacetsCode(foodFacet.getCode());
entity.setSelectedFoodFacetCode(foodFacet.getCode());
repositoryService.persist(entity);
foreignKeyLookupService.clearCache(FacetDescriptorPathwayForFood.class);
postProcessDisplayOrder();
Expand All @@ -91,7 +91,7 @@ public List<FoodFacet> choicesFoodFacet() {
private Set<FoodFacet.SecondaryKey> foodFacetCodesAsDefinedByFoodClassification(
final List<FacetDescriptorPathwayForFoodGroup> pathwayForFoodGroup) {
return pathwayForFoodGroup.stream()
.map(FacetDescriptorPathwayForFoodGroup::getFacetCode)
.map(FacetDescriptorPathwayForFoodGroup::getFoodFacetCode)
.map(FoodFacet.SecondaryKey::new)
.collect(Collectors.toSet());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public List<FoodDescriptor> coll() {
return foodDescriptorsAsDefinedByFoodClassification;
}
final Set<String> facetCodeSubset = facetDescriptorPathwayForFood.stream()
.map(FacetDescriptorPathwayForFood::getMandatoryInSequenceOfFacetsCode)
.map(FacetDescriptorPathwayForFood::getSelectedFoodFacetCode)
.collect(Collectors.toSet());
return foodDescriptorsAsDefinedByFoodClassification.stream()
.filter(foodDescriptor->facetCodeSubset.contains(foodDescriptor.getFacetCode()))
Expand All @@ -91,8 +91,8 @@ public List<FoodDescriptor> coll() {

private FoodDescriptor foodDescriptor(final FacetDescriptorPathwayForFoodGroup groupPathway) {
return foreignKeyLookupService.unique(new FoodDescriptor.SecondaryKey(
groupPathway.getFacetCode(),
groupPathway.getDescriptorCode()));
groupPathway.getFoodFacetCode(),
groupPathway.getFoodDescriptorCode()));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ public FacetDescriptorPathwayForFoodGroup act(
e.setFoodSubSubgroupCode(foodSubSubgroupCode(p.foodSubgroup()));

// cross reference
e.setFacetCode(p.facet().getCode());
e.setFoodFacetCode(p.foodFacet().getCode());
e.setFacetDisplayOrder(p.facetDisplayOrder());
e.setDescriptorCode(p.descriptor().getCode());
e.setFoodDescriptorCode(p.foodDescriptor().getCode());
e.setDescriptorDisplayOrder(p.descriptorDisplayOrder());

// flags
Expand Down Expand Up @@ -122,9 +122,9 @@ public FacetDescriptorPathwayForFoodGroup act(
}
@MemberSupport public List<FoodDescriptor> choicesDescriptor(
final FacetDescriptorPathwayForFoodGroup.Params p) {
return p.facet()!=null
return p.foodFacet()!=null
? repositoryService.allMatches(FoodDescriptor.class,
fd->Objects.equals(fd.getFacetCode(), p.facet().getCode()))
fd->Objects.equals(fd.getFacetCode(), p.foodFacet().getCode()))
: Collections.emptyList();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ public FacetDescriptorPathwayForFoodGroup.Manager act(
// order by descriptor name, descriptor with Other=true comes last
private int compare(final FacetDescriptorPathwayForFoodGroup a, final FacetDescriptorPathwayForFoodGroup b) {
var descA = foreignKeyLookupService.unique(
new FoodDescriptor.SecondaryKey(a.getFacetCode(), a.getDescriptorCode()));
new FoodDescriptor.SecondaryKey(a.getFoodFacetCode(), a.getFoodDescriptorCode()));
var descB = foreignKeyLookupService.unique(
new FoodDescriptor.SecondaryKey(b.getFacetCode(), b.getDescriptorCode()));
new FoodDescriptor.SecondaryKey(b.getFoodFacetCode(), b.getFoodDescriptorCode()));

int c = Integer.compare(descA.getOtherQ().ordinal(), descB.getOtherQ().ordinal());
if(c!=0) return c;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public List<FacetDescriptorPathwayForFood> listFacetDescriptorPathwayForFood(fin
public List<String> facetCodesAllowedForFoodGrouping(final FoodGrouping foodGrouping) {
return effectiveFacetDescriptorPathwayForFoodClassification(foodGrouping)
.stream()
.map(FacetDescriptorPathwayForFoodGroup::getFacetCode)
.map(FacetDescriptorPathwayForFoodGroup::getFoodFacetCode)
.toList();
}

Expand All @@ -145,7 +145,7 @@ public Set<FoodFacet.SecondaryKey> selectedFacetDescriptorPathwayForFoodAsFoodFa
final @Nullable Food food) {
return selectedFacetDescriptorPathwayForFood(food)
.stream()
.map(FacetDescriptorPathwayForFood::getMandatoryInSequenceOfFacetsCode)
.map(FacetDescriptorPathwayForFood::getSelectedFoodFacetCode)
.map(FoodFacet.SecondaryKey::new)
.collect(Collectors.toSet());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
import dita.globodiet.dom.params.food_list.Food;
import dita.globodiet.dom.params.food_list.FoodGroup;
import dita.globodiet.dom.params.food_list.FoodSubgroup;
import dita.globodiet.dom.params.recipe_list.Recipe;
import dita.globodiet.dom.params.recipe_list.RecipeGroup;
import dita.globodiet.dom.params.recipe_list.RecipeSubgroup;
import dita.globodiet.manager.util.GroupingUtils;

@Service
Expand Down Expand Up @@ -74,4 +77,24 @@ public FoodSubgroup foodSubSubgroupToSubgroup(final @NonNull FoodSubgroup foodSu

// -- RECIPE

public Either<RecipeGroup, RecipeSubgroup> recipeClassification(final @NonNull Recipe recipe) {
var sub = recipeSubgroup(recipe);
return sub!=null
? Either.right(sub)
: Either.left(recipeGroup(recipe));
}

public RecipeGroup recipeGroup(final @NonNull Recipe recipe) {
return foreignKeyLookupService.unique(GroupingUtils.recipeGroupKeyForRecipe(recipe));
}

public RecipeGroup recipeGroup(final @NonNull RecipeSubgroup recipeSubgroup) {
return foreignKeyLookupService.unique(GroupingUtils.recipeGroupKeyForSubgroup(recipeSubgroup));
}

@Nullable
public RecipeSubgroup recipeSubgroup(final @NonNull Recipe recipe) {
return foreignKeyLookupService.nullable(GroupingUtils.recipeSubgroupKeyForRecipe(recipe));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package dita.globodiet.manager.services.recipe;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import jakarta.inject.Inject;

import org.springframework.lang.Nullable;
import org.springframework.stereotype.Service;

import org.apache.causeway.applib.services.factory.FactoryService;
import org.apache.causeway.commons.functional.Either;

import lombok.NonNull;

import dita.globodiet.dom.params.classification.RecipeGrouping;
import dita.globodiet.dom.params.pathway.FacetDescriptorPathwayForRecipe;
import dita.globodiet.dom.params.pathway.FacetDescriptorPathwayForRecipeGroup;
import dita.globodiet.dom.params.recipe_description.RecipeFacet;
import dita.globodiet.dom.params.recipe_list.Recipe;
import dita.globodiet.dom.params.recipe_list.RecipeDeps.Recipe_dependentFacetDescriptorPathwayForRecipeMappedByRecipe;
import dita.globodiet.dom.params.recipe_list.RecipeGroup;
import dita.globodiet.dom.params.recipe_list.RecipeGroupDeps.RecipeGroup_dependentFacetDescriptorPathwayForRecipeGroupMappedByRecipeGroup;
import dita.globodiet.dom.params.recipe_list.RecipeSubgroup;
import dita.globodiet.dom.params.recipe_list.RecipeSubgroupDeps.RecipeSubgroup_dependentFacetDescriptorPathwayForRecipeGroupMappedByRecipeSubgroup;
import dita.globodiet.manager.services.grouping.GroupingHelperService;

@Service
public class RecipeFacetHelperService {

@Inject private FactoryService factoryService;
@Inject private GroupingHelperService groupingHelperService;

public List<FacetDescriptorPathwayForRecipeGroup> effectiveFacetDescriptorPathwayForRecipeClassification(
final @NonNull RecipeGrouping recipeGrouping) {
final @NonNull Either<RecipeGroup, RecipeSubgroup> recipeClassification = recipeGrouping.toEither();
final List<FacetDescriptorPathwayForRecipeGroup> facetDescriptorPathwayForFoodGroup = recipeClassification
.fold(
recipeGroup->listFacetDescriptorPathwayForRecipeGroup(recipeGroup),
recipeSubgroup->{
var lookupResult = listFacetDescriptorPathwayForRecipeSubgroup(recipeSubgroup);
// if lookup was too specific, fallback to top-level = FoodGroup
return lookupResult.isEmpty()
? listFacetDescriptorPathwayForRecipeGroup(groupingHelperService.recipeGroup(recipeClassification.rightIfAny()))
: lookupResult;
});

return facetDescriptorPathwayForFoodGroup;
}

public RecipeGrouping effectiveGroupingUsedForFacetDescriptorPathway(final @NonNull Recipe recipe) {
var recipeClassification = groupingHelperService.recipeClassification(recipe);
final RecipeGrouping recipeGrouping = recipeClassification
.fold(
recipeGroup->(RecipeGrouping)recipeGroup,
recipeSubgroup->{
var groupingResult = recipeSubgroup; // assignment is non final
var lookupResult = listFacetDescriptorPathwayForRecipeSubgroup(recipeSubgroup);
// if lookup was too specific, fallback to top-level = FoodGroup
return lookupResult.isEmpty()
? (RecipeGrouping)groupingHelperService.recipeGroup(recipeClassification.rightIfAny())
: groupingResult;
});
return recipeGrouping;
}

public List<FacetDescriptorPathwayForRecipeGroup> effectiveFacetDescriptorPathwayForRecipeClassificationHonoringDisplayOrder(
final @NonNull RecipeGrouping recipeGrouping) {
return effectiveFacetDescriptorPathwayForRecipeClassification(recipeGrouping).stream()
.sorted(displayOrder())
.toList();
}

public List<FacetDescriptorPathwayForRecipeGroup> effectiveFacetDescriptorPathwayForRecipeClassificationHonoringDisplayOrder(
final @NonNull Recipe recipe) {
return effectiveFacetDescriptorPathwayForRecipeClassificationHonoringDisplayOrder(
effectiveGroupingUsedForFacetDescriptorPathway(recipe));
}

public List<FacetDescriptorPathwayForRecipe> listFacetDescriptorPathwayForRecipe(final @NonNull Recipe recipe) {
return factoryService.mixin(Recipe_dependentFacetDescriptorPathwayForRecipeMappedByRecipe.class, recipe)
.coll();
}

public List<String> facetCodesAllowedForRecipeGrouping(final RecipeGrouping recipeGrouping) {
return effectiveFacetDescriptorPathwayForRecipeClassification(recipeGrouping)
.stream()
.map(FacetDescriptorPathwayForRecipeGroup::getRecipeFacetCode)
.toList();
}

public List<FacetDescriptorPathwayForRecipe> selectedFacetDescriptorPathwayForRecipe(
final @Nullable Recipe recipe) {
if(recipe==null) return Collections.emptyList();
var mixin = factoryService.mixin(Recipe_dependentFacetDescriptorPathwayForRecipeMappedByRecipe.class, recipe);
return mixin.coll();
}

public Set<RecipeFacet.SecondaryKey> selectedFacetDescriptorPathwayForRecipeAsRecipeFacetSecondaryKeySet(
final @Nullable Recipe recipe) {
return selectedFacetDescriptorPathwayForRecipe(recipe)
.stream()
.map(FacetDescriptorPathwayForRecipe::getSelectedRecipeFacetCode)
.map(RecipeFacet.SecondaryKey::new)
.collect(Collectors.toSet());
}

// -- HELPER

private List<FacetDescriptorPathwayForRecipeGroup> listFacetDescriptorPathwayForRecipeGroup(
final RecipeGroup recipeGroup) {
return factoryService.mixin(
RecipeGroup_dependentFacetDescriptorPathwayForRecipeGroupMappedByRecipeGroup.class,
recipeGroup)
.coll();
}

private List<FacetDescriptorPathwayForRecipeGroup> listFacetDescriptorPathwayForRecipeSubgroup(
final RecipeSubgroup recipeSubgroup) {
return factoryService.mixin(
RecipeSubgroup_dependentFacetDescriptorPathwayForRecipeGroupMappedByRecipeSubgroup.class,
recipeSubgroup)
.coll();
}

private Comparator<FacetDescriptorPathwayForRecipeGroup> displayOrder() {
return Comparator.comparing(FacetDescriptorPathwayForRecipeGroup::getFacetDisplayOrder)
.thenComparing(FacetDescriptorPathwayForRecipeGroup::getDescriptorDisplayOrder);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ private Can<RuleViolation> checkForGroup() {
entry.getFoodGroupCode(),
dita.commons.format.FormatUtils.emptyToDash(entry.getFoodSubgroupCode()),
dita.commons.format.FormatUtils.emptyToDash(entry.getFoodSubSubgroupCode()),
entry.getFacetCode());
entry.getFoodFacetCode());
displayOrdersByFacet1.putElement(foodGroupingPlusFacet, entry.getFacetDisplayOrder());
displayOrdersByFacet2.putElement(foodGroupingPlusFacet, entry.getDescriptorDisplayOrder());
urlByFoodGroupingPlusFacet.putElement(foodGroupingPlusFacet, deepLinkService.deepLinkFor(entry));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ private Can<RuleViolation> checkForFood() {

var entries = repositoryService.allInstances(FacetDescriptorPathwayForFood.class);
for(var entry : entries) {
var referencedFacetCode = entry.getMandatoryInSequenceOfFacetsCode();
var referencedFacetCode = entry.getSelectedFoodFacetCode();
if(referencedFacetCode==null) continue; //TODO should we report?
var allowedFacetCodes = facetCodesAllowedByFoodKey.getOrElseEmpty(entry.getFoodCode());
// assert referencedFacetCode exists within effective group
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,15 @@
import dita.globodiet.dom.params.food_list.Food;
import dita.globodiet.dom.params.food_list.FoodGroup;
import dita.globodiet.dom.params.food_list.FoodSubgroup;
import dita.globodiet.dom.params.recipe_list.Recipe;
import dita.globodiet.dom.params.recipe_list.RecipeGroup;
import dita.globodiet.dom.params.recipe_list.RecipeSubgroup;

@UtilityClass
public class GroupingUtils {

// -- FOOD

public FoodGroup.SecondaryKey foodGroupKeyForFood(final @NonNull Food food) {
return new FoodGroup.SecondaryKey(food.getFoodGroupCode());
}
Expand Down Expand Up @@ -75,4 +80,20 @@ public FoodSubgroup.SecondaryKey maskSubSubgroup(final @NonNull FoodSubgroup.Sec
return key;
}

// -- RECIPE

public RecipeGroup.SecondaryKey recipeGroupKeyForRecipe(final @NonNull Recipe recipe) {
return new RecipeGroup.SecondaryKey(recipe.getRecipeGroupCode());
}

public RecipeGroup.SecondaryKey recipeGroupKeyForSubgroup(final @NonNull RecipeSubgroup recipeSubgroup) {
return new RecipeGroup.SecondaryKey(recipeSubgroup.getRecipeGroupCode());
}

public RecipeSubgroup.SecondaryKey recipeSubgroupKeyForRecipe(final @NonNull Recipe recipe) {
return new RecipeSubgroup.SecondaryKey(
recipe.getRecipeGroupCode(),
recipe.getRecipeSubgroupCode());
}

}
Loading

0 comments on commit 61629a4

Please sign in to comment.