Skip to content

Commit

Permalink
wip - bifunction
Browse files Browse the repository at this point in the history
  • Loading branch information
jflamy committed Aug 20, 2024
1 parent c7deef5 commit 2397acb
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.List;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Supplier;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -107,7 +108,8 @@
@Route(value = "preparation/documents", layout = OwlcmsLayout.class)
public class DocumentsContent extends BaseContent implements CrudListener<Group>, OwlcmsContent {

private record KitElement(String id, String name, String extension, Path isp, int count, Supplier<JXLSWorkbookStreamSource> writerFactory) {
private record KitElement(String id, String name, String extension, Path isp, int count,
BiFunction<List<Athlete>, Group, JXLSWorkbookStreamSource> writerFactory) {
}

final static Logger logger = (Logger) LoggerFactory.getLogger(DocumentsContent.class);
Expand Down Expand Up @@ -328,7 +330,7 @@ protected List<Athlete> participationFindAll() {
}

private KitElement checkKit(String id, PreCompetitionTemplates templateEnum, BiConsumer<Throwable, String> errorProcessor,
Supplier<JXLSWorkbookStreamSource> writerFactory) {
BiFunction<List<Athlete>, Group, JXLSWorkbookStreamSource> writerFactory) {
try {
String resourceFolder = templateEnum.folder;
resourceFolder = resourceFolder.endsWith("/") ? resourceFolder : (resourceFolder + "/");
Expand Down Expand Up @@ -731,12 +733,19 @@ private Div createWeighInButton() {
return new Div(openDialog);
}

private void doKitElement(KitElement elem, String seq, ZipOutputStream zipOut, Group g, List<Athlete> athletes) throws IOException {
JXLSWorkbookStreamSource xlsWriter = elem.writerFactory.get();
private void doKitElement(KitElement elem, String seq, ZipOutputStream zipOut, Group g, List<Athlete> athletes) throws IOException {
JXLSWorkbookStreamSource xlsWriter = elem.writerFactory.apply(athletes, g);

// apply default if the factory did not set
if (xlsWriter.getGroup() == null) {
xlsWriter.setGroup(g);
}
if (xlsWriter.getSortedAthletes() == null) {
xlsWriter.setSortedAthletes(athletes);
}

InputStream is = Files.newInputStream(elem.isp);
xlsWriter.setInputStream(is);
xlsWriter.setGroup(g);
xlsWriter.setSortedAthletes(athletes);
xlsWriter.setTemplateFileName(elem.name);
InputStream in = xlsWriter.createInputStream();
String name = seq + "_" + elem.id + "_" + g.getName() + "." + elem.extension;
Expand Down Expand Up @@ -769,16 +778,27 @@ private InputStream excelKitElement(List<Group> selectedSessions, List<KitElemen
// for items that are one per session, selected sessions will be non-empty.
Group g = (selectedSessions != null && selectedSessions.size() > 0) ? selectedSessions.get(0) : null;
KitElement elem = elements.get(0);

List<Athlete> athletes = null;
if (g != null) {
athletes = groupAthletes(g, true);
}

JXLSWorkbookStreamSource xlsWriter = elem.writerFactory.get();
// writerFactory can apply custom sorting order to the athletes
JXLSWorkbookStreamSource xlsWriter = elem.writerFactory.apply(athletes, g);
if (xlsWriter.getSortedAthletes() == null) {
// writerFactory did not set them explicitly, set default
xlsWriter.setSortedAthletes(athletes);
}
if (xlsWriter.getGroup() == null) {
// writerFactory did not set them explicitly, set default.
xlsWriter.setGroup(g);
}

InputStream is = Files.newInputStream(elem.isp);
xlsWriter.setInputStream(is);
xlsWriter.setTemplateFileName(elem.name);
if (g != null) {
List<Athlete> athletes = groupAthletes(g, true);
xlsWriter.setGroup(g);
xlsWriter.setSortedAthletes(athletes);
}


if (doneCallback == null) {
Notification n = new Notification(Translator.translate("Documents.ProcessingExcel"));
Expand Down Expand Up @@ -978,7 +998,7 @@ private List<KitElement> prepareBodyweight(PreCompetitionTemplates templateDefin
checkKit("bodyweight",
templateDefinition,
errorProcessor,
() -> {
(a, g) -> {
JXLSStartingListDocs startingXlsWriter = new JXLSStartingListDocs();
startingXlsWriter.setGroup(null);
// get current version of athletes.
Expand All @@ -997,12 +1017,12 @@ private List<KitElement> prepareCards(PreCompetitionTemplates templateDefinition
checkKit("cards",
templateDefinition,
errorProcessor,
() -> {
JXLSCardsDocs xlsWriter = new JXLSCardsDocs();
(a, g) -> {
JXLSCardsDocs xlsWriter = new JXLSCardsDocs();
List<Athlete> athletes = athletesFindAll(true);
athletes.sort(RegistrationOrderComparator.athleteSessionRegistrationOrderComparator);
xlsWriter.setSortedAthletes(athletes);
return xlsWriter;
return xlsWriter;
}));
return elements;
}
Expand All @@ -1013,7 +1033,7 @@ private List<KitElement> prepareCategories(PreCompetitionTemplates template, Lis
checkKit("categories",
template,
errorProcessor,
() -> {
(a, g) -> {
JXLSCategoriesListDocs xlsWriter = new JXLSCategoriesListDocs();
xlsWriter.setGroup(null);
// get current version of athletes.
Expand All @@ -1031,7 +1051,7 @@ private List<KitElement> prepareCheckin(PreCompetitionTemplates template, List<G
checkKit("checkin",
template,
errorProcessor,
() -> {
(a, g) -> {
JXLSStartingListDocs startingXlsWriter = new JXLSStartingListDocs();
// group may have been edited since the page was loaded
startingXlsWriter.setGroup(null);
Expand All @@ -1051,7 +1071,7 @@ private List<KitElement> prepareEmptyProtocol(PreCompetitionTemplates template,
checkKit("weighin",
template,
errorProcessor,
() -> {
(a, g) -> {
JXLSResultSheet rs = new JXLSResultSheet();
rs.setGroup(null);
return rs;
Expand All @@ -1066,14 +1086,12 @@ private List<KitElement> prepareIntroduction(PreCompetitionTemplates template, L
checkKit("introduction",
template,
errorProcessor,
() -> {
(a, g) -> {
JXLSCategoriesListDocs xlsWriter = new JXLSCategoriesListDocs();
xlsWriter.setGroup(null);
xlsWriter.setGroup(g);
// get current version of athletes.
var athletes = athletesFindAll(true);
athletes.sort((a,b) -> ObjectUtils.compare(a.getCategoryCode(), b.getCategoryCode()));
xlsWriter.setSortedAthletes(athletes);
logger.warn("athletes {}", athletes);
a.sort((x, y) -> ObjectUtils.compare(x.getCategoryCode(), y.getCategoryCode()));
xlsWriter.setSortedAthletes(a);
return xlsWriter;
}));
return elements;
Expand All @@ -1086,7 +1104,7 @@ private List<KitElement> prepareJury(PreCompetitionTemplates template, List<Grou
checkKit("weighin",
template,
errorProcessor,
() -> {
(a, g) -> {
JXLSJurySheet rs = new JXLSJurySheet();
rs.setGroup(null);
return rs;
Expand All @@ -1100,7 +1118,7 @@ private List<KitElement> prepareOfficials(List<Group> selectedItems, BiConsumer<
checkKit("officials",
PreCompetitionTemplates.OFFICIALS,
errorProcessor,
() -> {
(a, g) -> {
JXLSStartingListDocs xlsWriter = new JXLSStartingListDocs();
xlsWriter.setGroup(
getGroup() != null ? GroupRepository.getById(getGroup().getId()) : null);
Expand All @@ -1117,23 +1135,23 @@ private List<KitElement> preparePostWeighInKit(List<Group> selectedItems, BiCons
KitElement kit = checkKit("introduction",
PreCompetitionTemplates.INTRODUCTION,
null, // no error processor - ignore this item if no template
() -> new JXLSWeighInSheet());
(a, g) -> new JXLSWeighInSheet());
if (kit != null) {
elements.add(kit);
}

KitElement kit2 = checkKit("emptyProtocol",
PreCompetitionTemplates.EMPTY_PROTOCOL,
null, // no error processor - ignore this item if no template
() -> new JXLSResultSheet());
(a, g) -> new JXLSResultSheet());
if (kit2 != null) {
elements.add(kit2);
}

KitElement kit3 = checkKit("jury",
PreCompetitionTemplates.JURY,
null, // no error processor - ignore this item if no template
() -> new JXLSJurySheet());
(a, g) -> new JXLSJurySheet());
if (kit3 != null) {
elements.add(kit3);
}
Expand All @@ -1146,15 +1164,15 @@ private List<KitElement> preparePreWeighInKit(List<Group> selectedItems, BiConsu
KitElement kit = checkKit("weighin",
PreCompetitionTemplates.WEIGHIN,
null, // no error processor - ignore this item if no template
() -> new JXLSCardsDocs());
(a, g) -> new JXLSCardsDocs());
if (kit != null) {
elements.add(kit);
}

KitElement kit2 = checkKit("cards",
PreCompetitionTemplates.CARDS,
null, // no error processor - ignore this item if no template
() -> new JXLSCardsDocs());
(a, g) -> new JXLSCardsDocs());
if (kit2 != null) {
elements.add(kit2);
}
Expand All @@ -1167,7 +1185,7 @@ private List<KitElement> prepareSchedule(List<Group> selectedItems, BiConsumer<T
checkKit("schedule",
PreCompetitionTemplates.SCHEDULE,
errorProcessor,
() -> {
(a, g) -> {
// schedule is currently a variation on starting list
JXLSStartingListDocs xlsWriter = new JXLSStartingListDocs();
// group may have been edited since the page was loaded
Expand Down Expand Up @@ -1199,7 +1217,7 @@ private List<KitElement> prepareStartList(PreCompetitionTemplates templateDefini
checkKit("startList",
templateDefinition,
errorProcessor,
() -> {
(a, g) -> {
JXLSStartingListDocs xlsWriter = new JXLSStartingListDocs();
// group may have been edited since the page was loaded
xlsWriter.setGroup(null);
Expand All @@ -1218,7 +1236,7 @@ private List<KitElement> prepareTeam(PreCompetitionTemplates template, List<Grou
checkKit("categories",
template,
errorProcessor,
() -> {
(a, g) -> {
JXLSStartingListDocs startingXlsWriter = new JXLSStartingListDocs();
startingXlsWriter.setGroup(null);
// get current version of athletes.
Expand All @@ -1236,7 +1254,7 @@ private List<KitElement> prepareWeighIn(PreCompetitionTemplates template, List<G
checkKit("weighin",
template,
errorProcessor,
() -> {
(a, g) -> {
JXLSWeighInSheet rs = new JXLSWeighInSheet();
rs.setGroup(null);
return rs;
Expand Down
Binary file modified owlcms/src/main/resources/templates/introduction/Introduction.xlsx
Binary file not shown.

0 comments on commit 2397acb

Please sign in to comment.