Skip to content

Commit

Permalink
export all records + messages; closes #722;closes #723
Browse files Browse the repository at this point in the history
  • Loading branch information
jflamy committed Aug 9, 2023
1 parent c36d80f commit 952aeac
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public default HashMap<String, List<String>> readParams(Location location,
if (!r.isIgnoreGroupFromURL()) {
List<String> ageGroupNames = parametersMap.get(AGEGROUP);
if (ageGroupNames != null && ageGroupNames.get(0) != null) {
//TODO urldecode the agegroup ?
//CHECK urldecode the agegroup ?
ageGroup = AgeGroupRepository.findByName(ageGroupNames.get(0));
} else if (fop != null && isVideo(location) && fop.getVideoAgeGroup() != null) {
ageGroup = fop.getVideoAgeGroup();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import app.owlcms.data.records.RecordEvent.MissingAgeGroup;
import app.owlcms.data.records.RecordEvent.MissingGender;
import app.owlcms.data.records.RecordEvent.UnknownIWFBodyWeightCategory;
import app.owlcms.i18n.Translator;
import app.owlcms.utils.LoggerUtils;
import app.owlcms.utils.ResourceWalker;
import app.owlcms.utils.ZipUtils;
Expand Down Expand Up @@ -159,7 +160,7 @@ public static List<String> createRecords(Workbook workbook, String name, String
logger.error("[" + sheet.getSheetName() + "," + cell.getAddress() + "]");
}
}
logger.warn("normal {} {} {}", iRecord, rec.getBwCatUpper(), rec.getBwCatLower());
logger.debug("normal {} {} {}", iRecord, rec.getBwCatUpper(), rec.getBwCatLower());
if (rec.getBwCatUpper() < rec.getBwCatLower()) {
throw new Exception(cellValue
+ " upper limit on bodyweight category should be >= to "+rec.getAgeGrpLower());
Expand All @@ -169,7 +170,7 @@ public static List<String> createRecords(Workbook workbook, String name, String
long cellValue = Math.round(cell.getNumericCellValue());
rec.setBwCatString(Long.toString(cellValue));
rec.setBwCatUpper(Math.toIntExact(cellValue));
logger.warn("illegalstate {} {} {}", iRecord, rec.getBwCatUpper(), rec.getBwCatLower());
logger.debug("illegalstate {} {} {}", iRecord, rec.getBwCatUpper(), rec.getBwCatLower());
if (rec.getBwCatUpper() <= rec.getBwCatLower()) {
throw new Exception(cellValue
+ " upper limit on bodyweight category should be > to "+rec.getBwCatLower());
Expand Down Expand Up @@ -203,34 +204,32 @@ public static List<String> createRecords(Workbook workbook, String name, String
int intExact = Math.toIntExact(cellValue);
if (cellValue < 3000) {
rec.setBirthYear(intExact);
logger.warn("number {}", intExact);
logger.debug("number {}", intExact);
} else {
LocalDate epoch = LocalDate.of(1900, 1, 1);
LocalDate plusDays = epoch.plusDays(intExact - 2);
// Excel quirks: 1 is 1900-01-01 and mistakenly assumes 1900-02-29 existed
rec.setBirthDate(plusDays);
logger.warn("plusDays {}", rec.getRecordDateAsString());
logger.debug("plusDays {}", rec.getRecordDateAsString());
}
} else if (cell.getCellType() == CellType.STRING) {
String cellValue = cell.getStringCellValue();
logger.warn("string value = '{}'", cellValue);
logger.debug("string value = '{}'", cellValue);
try {
LocalDate date = LocalDate.parse(cellValue, ymdFormatter);
rec.setBirthDate(date);
logger.warn("date {}", date);
logger.debug("date {}", date);
} catch (DateTimeParseException e) {
try {
YearMonth date = YearMonth.parse(cellValue, ymFormatter);
rec.setBirthYear(date.getYear());
logger.warn("datemonth {}", date.getYear());
logger.debug("datemonth {}", date.getYear());
} catch (DateTimeParseException e2) {
try {
Year date = Year.parse(cellValue, yFormatter);
rec.setBirthYear(date.getValue());
logger.warn("year {}", date.getValue());
logger.debug("year {}", date.getValue());
} catch (DateTimeParseException e3) {
logger.error(cellValue
+ " not in yyyy-MM-dd or yyyy-MM or yyyy date format");
throw new Exception(cellValue
+ " not in yyyy-MM-dd or yyyy-MM or yyyy date format");
}
Expand All @@ -253,34 +252,32 @@ public static List<String> createRecords(Workbook workbook, String name, String
int intExact = Math.toIntExact(cellValue);
if (cellValue < 3000) {
rec.setRecordYear(intExact);
logger.warn("number {}", intExact);
logger.debug("number {}", intExact);
} else {
LocalDate epoch = LocalDate.of(1900, 1, 1);
LocalDate plusDays = epoch.plusDays(intExact - 2);
// Excel quirks: 1 is 1900-01-01 and mistakenly assumes 1900-02-29 existed
rec.setRecordDate(plusDays);
logger.warn("plusDays {}", rec.getRecordDateAsString());
logger.debug("plusDays {}", rec.getRecordDateAsString());
}
} else if (cell.getCellType() == CellType.STRING) {
String cellValue = cell.getStringCellValue();
logger.warn("string value = '{}'", cellValue);
logger.debug("string value = '{}'", cellValue);
try {
LocalDate date = LocalDate.parse(cellValue, ymdFormatter);
rec.setRecordDate(date);
logger.warn("date {}", date);
logger.debug("date {}", date);
} catch (DateTimeParseException e) {
try {
YearMonth date = YearMonth.parse(cellValue, ymFormatter);
rec.setRecordYear(date.getYear());
logger.warn("datemonth {}", date.getYear());
logger.debug("datemonth {}", date.getYear());
} catch (DateTimeParseException e2) {
try {
Year date = Year.parse(cellValue, yFormatter);
rec.setRecordYear(date.getValue());
logger.warn("year {}", date.getValue());
logger.debug("year {}", date.getValue());
} catch (DateTimeParseException e3) {
logger.error(cellValue
+ " not in yyyy-MM-dd or yyyy-MM or yyyy date format");
throw new Exception(cellValue
+ " not in yyyy-MM-dd or yyyy-MM or yyyy date format");
}
Expand Down Expand Up @@ -329,7 +326,7 @@ public static List<String> createRecords(Workbook workbook, String name, String
comp2.setAgeGroupsFileName(name);
startupLogger.info("inserted {} record entries.", iRecord);
logger.info("inserted {} record entries.", iRecord);
errors.add(Integer.toString(iRecord));
errors.add(Translator.translate("Records.Inserted",iRecord));
return errors;
});
}
Expand All @@ -356,20 +353,24 @@ public static void readFolder(Path recordsPath) throws IOException {

}

public static void readInputStream(InputStream is, String fileName) {
public static List<String> readInputStream(InputStream is, String fileName) {
List<String> errors = new ArrayList<>();
try (Workbook workbook = WorkbookFactory.create(is)) {
logger.info("loading record definition file {} {}", fileName,
FilenameUtils.removeExtension(fileName));
startupLogger.info("loading record definition file {}", fileName);

createRecords(workbook, fileName,
errors = createRecords(workbook, fileName,
FilenameUtils.removeExtension(fileName.toString()));
return errors;
} catch (Exception e) {
logger.error("could not process record definition file {}\n{}", fileName,
LoggerUtils./**/stackTrace(e));
startupLogger.error(
"could not process record definition file {}. See log files for details.",
fileName);
errors.add(Translator.translate("Records.couldNotProcess",fileName));
return errors;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ private static String filteringWhere(Gender gender, Integer age, Double bw, Stri
if (groupName != null) {
whereList.add("(groupNameString = :groupName)");
}
if (newRecords != null) {
if (newRecords != null && newRecords) {
whereList.add("(groupNameString is not null)");
}
if (whereList.size() == 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

import org.vaadin.crudui.crud.CrudOperation;

Expand All @@ -14,6 +15,7 @@
import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.button.ButtonVariant;
import com.vaadin.flow.component.checkbox.Checkbox;
import com.vaadin.flow.component.dialog.Dialog;
import com.vaadin.flow.component.formlayout.FormLayout;
import com.vaadin.flow.component.formlayout.FormLayout.FormItem;
import com.vaadin.flow.component.formlayout.FormLayout.ResponsiveStep;
Expand All @@ -24,6 +26,7 @@
import com.vaadin.flow.component.html.Hr;
import com.vaadin.flow.component.html.Label;
import com.vaadin.flow.component.html.Paragraph;
import com.vaadin.flow.component.html.Pre;
import com.vaadin.flow.component.orderedlayout.FlexComponent.Alignment;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
Expand Down Expand Up @@ -123,6 +126,7 @@ public Component buildNewForm(CrudOperation operation, RecordConfig comp, boolea
FormLayout recordsOrderLayout = recordOrderForm();
FormLayout provisionalLayout = provisionalForm();
FormLayout officialLayout = officialForm();
FormLayout exportLayout = exportAllForm();


TabSheet ts = new TabSheet();
Expand All @@ -132,9 +136,9 @@ public Component buildNewForm(CrudOperation operation, RecordConfig comp, boolea
recordsOrderLayout,
separator(),
officialLayout));
ts.add(Translator.translate("Records.ProvisionalSection"),
ts.add(Translator.translate("Records.manageNewRecords"),
new VerticalLayout(
provisionalLayout));
provisionalLayout, separator(), exportLayout));


VerticalLayout mainLayout = new VerticalLayout(
Expand Down Expand Up @@ -162,14 +166,27 @@ private FormLayout provisionalForm() {
recordsAvailableLayout.add(title);
recordsAvailableLayout.setColspan(title, 2);
Div newRecords = DownloadButtonFactory.createDynamicXLSDownloadButton("records",
Translator.translate("Results.NewRecords"), new JXLSExportRecords(UI.getCurrent()));
Translator.translate("Results.NewRecords"), new JXLSExportRecords(UI.getCurrent(),false));
recordsAvailableLayout.addFormItem(newRecords, Translator.translate("Results.NewRecords"));
recordsAvailableLayout.addFormItem(clearNewRecords,
Translator.translate("Preparation.ClearNewRecordsExplanation"));

return recordsAvailableLayout;
}

private FormLayout exportAllForm() {
FormLayout recordsAvailableLayout = createLayout();
Component title = createTitle("Records.exportAllRecordsTitle");

recordsAvailableLayout.add(title);
recordsAvailableLayout.setColspan(title, 2);
Div newRecords = DownloadButtonFactory.createDynamicXLSDownloadButton("records",
Translator.translate("Records.exportAllRecordsTitle"), new JXLSExportRecords(UI.getCurrent(),true));
recordsAvailableLayout.addFormItem(newRecords, Translator.translate("Records.exportAllRecordsLabel"));

return recordsAvailableLayout;
}

private FormLayout officialForm() {
Button clearNewRecords = new Button(Translator.translate("Records.ClearOfficialRecords"),
buttonClickEvent -> {
Expand All @@ -188,8 +205,22 @@ private FormLayout officialForm() {
uploadRecords.setUploadButton(uploadButton);
uploadRecords.setDropLabel(new Label(Translator.translate("Records.UploadDropZone")));
uploadRecords.addSucceededListener(e -> {
RecordDefinitionReader.readInputStream(receiver.getInputStream(), receiver.getFileName());
UI.getCurrent().getPage().reload();
List<String> errors = RecordDefinitionReader.readInputStream(receiver.getInputStream(), receiver.getFileName());
if (errors.isEmpty()) {
UI.getCurrent().getPage().reload();
} else {
Pre errorsComponent = new Pre();
errorsComponent.add(errors.stream().collect(Collectors.joining(System.lineSeparator())));
Dialog d = new Dialog();
Button okButton = new Button(Translator.translate("OK"),
x -> {
d.close();
UI.getCurrent().getPage().reload();
});
d.add(errorsComponent);
d.getFooter().add(okButton);
d.open();
}
});

FormLayout recordsAvailableLayout = createLayout();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public ResultsNavigationContent() {
getTranslation("TimingStatistics"), new JXLSTimingStats(UI.getCurrent()));
((Button) timingStats.getComponentAt(0)).setWidth("100%");
Div newRecords = DownloadButtonFactory.createDynamicXLSDownloadButton("records",
getTranslation("Results.NewRecords"), new JXLSExportRecords(UI.getCurrent()));
getTranslation("Results.NewRecords"), new JXLSExportRecords(UI.getCurrent(),false));
((Button) newRecords.getComponentAt(0)).setWidth("100%");

FlexibleGridLayout grid1 = HomeNavigationContent.navigationGrid(groupResults);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,15 @@ public class JXLSExportRecords extends JXLSWorkbookStreamSource {
Group group;
private List<RecordEvent> records;
//private List<RecordEvent> bestRecords;
private boolean allRecords;

public JXLSExportRecords(Group group, boolean excludeNotWeighed, UI ui) {
super();
}

public JXLSExportRecords(UI ui) {
public JXLSExportRecords(UI ui, boolean allRecords) {
super();
this.allRecords = allRecords;
}

@Override
Expand Down Expand Up @@ -91,7 +93,7 @@ public List<Athlete> getSortedAthletes() {
}

String groupName = group != null ? group.getName() : null;
records = RecordRepository.findFiltered(null, null, null, groupName, true);
records = RecordRepository.findFiltered(null, null, null, groupName, !allRecords);
records.sort(sortRecords());

// // keep best record if beaten several times
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ protected void setReportingInfo() {
getReportingBeans().put("group", getGroup());

// reuse existing logic for processing records
JXLSExportRecords jxlsExportRecords = new JXLSExportRecords(null);
JXLSExportRecords jxlsExportRecords = new JXLSExportRecords(null,false);
jxlsExportRecords.setGroup(getGroup());
logger.debug("fetching records for session {} category {}", getGroup(), getCategory());
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ public void _09_testOrder() throws IOException {
RecordDefinitionReader.createRecords(wb, streamURI, null);

List<RecordEvent> records = RecordRepository.findFiltered(null, null, null, null, null);
records.sort(new JXLSExportRecords(null).sortRecords());
records.sort(new JXLSExportRecords(null,false).sortRecords());

String results = records.stream().map(RecordEvent::toString).collect(
Collectors.joining(System.lineSeparator(),"",System.lineSeparator()));
Expand All @@ -197,7 +197,7 @@ public void _10_testMessages() throws IOException {
List<String> errors = RecordDefinitionReader.createRecords(wb, streamURI, null);

List<RecordEvent> records = RecordRepository.findFiltered(null, null, null, null, null);
records.sort(new JXLSExportRecords(null).sortRecords());
records.sort(new JXLSExportRecords(null,true).sortRecords());

String results = records.stream().map(RecordEvent::toString).collect(
Collectors.joining(System.lineSeparator(),"",System.lineSeparator()));
Expand Down
Loading

0 comments on commit 952aeac

Please sign in to comment.