Skip to content

Commit

Permalink
first pass TO import/export
Browse files Browse the repository at this point in the history
  • Loading branch information
jflamy committed Feb 5, 2025
1 parent 5502a0c commit 4446b02
Show file tree
Hide file tree
Showing 15 changed files with 481 additions and 126 deletions.
1 change: 1 addition & 0 deletions installtools/.classpath
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/jbrsdk-17.0.6-windows-x64-b779.1"/>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
Expand Down
17 changes: 0 additions & 17 deletions owlcms-docker/.classpath
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,5 @@
<attribute name="optional" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="target/generated-sources/annotations">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="ignore_optional_problems" value="true"/>
<attribute name="m2e-apt" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="target/generated-test-sources/test-annotations">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="ignore_optional_problems" value="true"/>
<attribute name="m2e-apt" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
17 changes: 0 additions & 17 deletions owlcms-windows/.classpath
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,5 @@
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="target/generated-sources/annotations">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="ignore_optional_problems" value="true"/>
<attribute name="m2e-apt" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="target/generated-test-sources/test-annotations">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="ignore_optional_problems" value="true"/>
<attribute name="m2e-apt" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
15 changes: 15 additions & 0 deletions owlcms/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -194,11 +194,26 @@
<artifactId>commons-io</artifactId>
<version>2.14.0</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.5</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.21</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-text</artifactId>
<version>1.10.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.26.0</version>
</dependency>

<!-- event bus -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class TechnicalOfficialReader {
private static final String FEDERATION_ID = "FederationId";

public static List<TechnicalOfficial> importFromXLS(InputStream is) {
logger.warn("importFromXLS");
List<TechnicalOfficial> officials = new ArrayList<>();

try {
Expand All @@ -41,9 +42,14 @@ public static List<TechnicalOfficial> importFromXLS(InputStream is) {
Row row = sheet.getRow(i);
if (row == null) continue;

logger.warn("row[{}] {}", i, colIndices);
TechnicalOfficial official = readRow(row, colIndices);

if (official != null) {
officials.add(official);
var mergedOff = TechnicalOfficialRepository.save(official);
officials.add(mergedOff);
} else {
break;
}
}
workbook.close();
Expand Down Expand Up @@ -74,15 +80,17 @@ private static int[] findColumnIndices(Row headerRow) {

private static TechnicalOfficial readRow(Row row, int[] colIndices) {
// Check required fields
if (isEmptyCell(row.getCell(colIndices[0])) ||
isEmptyCell(row.getCell(colIndices[1])) ||
isEmptyCell(row.getCell(colIndices[2]))) {
if (isEmptyCell(row.getCell(colIndices[0]))) {
return null;
}

String lastName = getCellValueAsString(row.getCell(colIndices[0]));
String firstName = getCellValueAsString(row.getCell(colIndices[1]));
TOLevel level = TOLevel.valueOf(getCellValueAsString(row.getCell(colIndices[2])));
TOLevel level = null;
try {
level = TOLevel.valueOf(getCellValueAsString(row.getCell(colIndices[2])));
} catch (IllegalArgumentException e) {
}
String iwfId = getCellValueAsString(row.getCell(colIndices[3]));
String federation = getCellValueAsString(row.getCell(colIndices[4]));
String federationId = getCellValueAsString(row.getCell(colIndices[5]));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.slf4j.LoggerFactory;

import app.owlcms.data.jpa.JPAService;
import app.owlcms.utils.LoggerUtils;
import ch.qos.logback.classic.Logger;

/**
Expand All @@ -33,6 +34,7 @@ public static void delete(TechnicalOfficial to) {
}

public static List<TechnicalOfficial> findAll() {
logger.warn("====== findall {}",LoggerUtils.stackTrace());
return JPAService
.runInTransaction(em -> em.createQuery("select c from TechnicalOfficial c order by c.id", TechnicalOfficial.class)
.getResultList());
Expand All @@ -52,10 +54,11 @@ public static TechnicalOfficial getById(Long id, EntityManager em) {
TechnicalOfficial.class);
query.setParameter("id", id);

return (TechnicalOfficial) query.getResultList().stream().findFirst().orElse(null);
return query.getResultList().stream().findFirst().orElse(null);
}

public static TechnicalOfficial save(TechnicalOfficial technicalOfficial) {
logger.warn("saving {}", technicalOfficial);
TechnicalOfficial nTechnicalOfficial = JPAService.runInTransaction(em -> em.merge(technicalOfficial));
return nTechnicalOfficial;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package app.owlcms.data.technicalofficial;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.LoggerFactory;

import ch.qos.logback.classic.Logger;

public class TechnicalOfficialWriter {
private final static Logger logger = (Logger) LoggerFactory.getLogger(TechnicalOfficialWriter.class);

public static InputStream write() {
try (Workbook workbook = new XSSFWorkbook()) {
Sheet sheet = workbook.createSheet("Technical Officials");

// Create header style
CellStyle headerStyle = workbook.createCellStyle();
Font headerFont = workbook.createFont();
headerFont.setBold(true);
headerStyle.setFont(headerFont);

// Create headers
Row headerRow = sheet.createRow(0);
String[] headers = {"LastName", "FirstName", "Level", "IWFId", "Federation", "FederationId"};
for (int i = 0; i < headers.length; i++) {
Cell cell = headerRow.createCell(i);
cell.setCellValue(headers[i]);
cell.setCellStyle(headerStyle);
}

// Add data rows
List<TechnicalOfficial> officials = TechnicalOfficialRepository.findAll();
int rowNum = 1;
for (TechnicalOfficial official : officials) {
Row row = sheet.createRow(rowNum++);
row.createCell(0).setCellValue(official.getLastName() != null ? official.getLastName() : "");
row.createCell(1).setCellValue(official.getFirstName() != null ? official.getFirstName() : "");
row.createCell(2).setCellValue(official.getLevel() != null ? official.getLevel().toString() : "");
row.createCell(3).setCellValue(official.getIwfId() != null ? official.getIwfId() : "");
row.createCell(4).setCellValue(official.getFederation() != null ? official.getFederation() : "");
row.createCell(5).setCellValue(official.getFederationId() != null ? official.getFederationId() : "");
}

// Autosize columns
for (int i = 0; i < headers.length; i++) {
sheet.autoSizeColumn(i);
}

// Write to byte array
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
workbook.write(outputStream);
return new ByteArrayInputStream(outputStream.toByteArray());

} catch (IOException e) {
logger.error("Error writing technical officials: {}", e);
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.grid.Grid;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.html.Hr;
import com.vaadin.flow.component.html.NativeLabel;
import com.vaadin.flow.component.icon.Icon;
import com.vaadin.flow.component.icon.VaadinIcon;
Expand All @@ -32,9 +33,10 @@
import app.owlcms.nui.crudui.OwlcmsCrudFormFactory;
import app.owlcms.nui.crudui.OwlcmsCrudGrid;
import app.owlcms.nui.crudui.OwlcmsGridLayout;
import app.owlcms.nui.shared.DownloadButtonFactory;
import app.owlcms.nui.shared.OwlcmsContent;
import app.owlcms.nui.shared.OwlcmsLayout;
import app.owlcms.spreadsheet.JXLSExportRecords;
import app.owlcms.spreadsheet.JXLSExportTechnicalOfficials;
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;

Expand All @@ -54,14 +56,14 @@ public class TechnicalOfficialContent extends BaseContent implements CrudListene
private OwlcmsCrudFormFactory<TechnicalOfficial> editingFormFactory;
private OwlcmsLayout routerLayout;
private FlexLayout topBar;
private GridCrud<TechnicalOfficial> crud;

/**
* Instantiates the TechnicalOfficial crudGrid.
*/
public TechnicalOfficialContent() {
OwlcmsCrudFormFactory<TechnicalOfficial> crudFormFactory = createFormFactory();
GridCrud<TechnicalOfficial> crud = createGrid(crudFormFactory);
// defineFilters(crudGrid);
crud = createGrid(crudFormFactory);
fillHW(crud, this);
}

Expand All @@ -78,32 +80,43 @@ public TechnicalOfficial add(TechnicalOfficial domainObjectToAdd) {
public FlexLayout createMenuArea() {
this.topBar = new FlexLayout();

// Export current officials button using the age groups pattern
Div exportOfficials = DownloadButtonFactory.createDynamicXLSXDownloadButton(
"TechnicalOfficials",
Translator.translate("TechnicalOfficials.Export"),
new XLSXTechnicalOfficialsExport());
exportOfficials.getStyle().set("margin-left", "1em");

Button uploadCustom = new Button(Translator.translate("TechnicalOfficials.Upload"),
new Icon(VaadinIcon.UPLOAD_ALT),
buttonClickEvent -> {
AgeGroupsFileUploadDialog ageGroupsFileUploadDialog = new AgeGroupsFileUploadDialog();
ageGroupsFileUploadDialog.setCallback(() -> loadOfficials());
ageGroupsFileUploadDialog.open();
TechnicalOfficialsUploadDialog dialog = new TechnicalOfficialsUploadDialog();
dialog.setCallback(() -> loadOfficials());
dialog.open();
});

var recordsWriter1 = new JXLSExportRecords(UI.getCurrent(), true, true);
var toAssignmentsWriter = new JXLSExportTechnicalOfficials(UI.getCurrent(), true, true);
JXLSDownloader dd1 = new JXLSDownloader(
() -> {
return recordsWriter1;
return toAssignmentsWriter;
},
"/templates/records",
"/templates/toAssignments",
Competition::getComputedTechnicalOfficialsTemplateFileName,
Competition::setTechnicalOfficialsTemplateFileName,
Translator.translate("Records.exportCurrentRecordsTitle"),
Translator.translate("TechnicalOfficials.ExportAssignementReports"),
Translator.translate("Download"));
Div allRecords1 = new Div();
Button downloadButton = dd1.createDownloadButton();
downloadButton.setWidthFull();
allRecords1.add(downloadButton);

FlexLayout buttons = new FlexLayout(
new NativeLabel(Translator.translate("AgeGroups.Custom")),
allRecords1, uploadCustom);
new NativeLabel(Translator.translate("TechnicalOfficials.ImportExport")),
exportOfficials,
uploadCustom,
hr(),
new NativeLabel(Translator.translate("TechnicalOfficials.AssignmentReports")),
allRecords1);
buttons.getStyle().set("flex-wrap", "wrap");
buttons.getStyle().set("gap", "1ex");
buttons.getStyle().set("margin-left", "5em");
Expand All @@ -117,11 +130,20 @@ public FlexLayout createMenuArea() {
return this.topBar;
}

private Object loadOfficials() {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'loadOfficials'");
private Hr hr() {
Hr hr = new Hr();
hr.setWidthFull();
hr.getStyle().set("margin", "0");
hr.getStyle().set("padding", "0");
return hr;
}

private Object loadOfficials() {
logger.warn("refreshing");
crud.refreshGrid();
return null;
}

@Override
public void delete(TechnicalOfficial domainObjectToDelete) {
this.editingFormFactory.delete(domainObjectToDelete);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package app.owlcms.nui.preparation;

import java.io.InputStream;

import org.slf4j.LoggerFactory;

import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.dialog.Dialog;
import com.vaadin.flow.component.html.NativeLabel;
import com.vaadin.flow.component.upload.Upload;
import com.vaadin.flow.component.upload.receivers.MemoryBuffer;

import app.owlcms.data.technicalofficial.TechnicalOfficialReader;
import app.owlcms.i18n.Translator;
import ch.qos.logback.classic.Logger;

@SuppressWarnings("serial")
public class TechnicalOfficialsUploadDialog extends Dialog {
Logger logger = (Logger) LoggerFactory.getLogger(TechnicalOfficialsUploadDialog.class);
private Runnable callback;
private MemoryBuffer buffer = new MemoryBuffer();
private Upload upload;

public TechnicalOfficialsUploadDialog() {
Button uploadButton = new Button(Translator.translate("TechnicalOfficials.Upload"));
upload = new Upload(buffer);
upload.setUploadButton(uploadButton);
upload.setDropLabel(new NativeLabel(Translator.translate("TechnicalOfficials.UploadDropZone")));
upload.addSucceededListener(e -> {
try {
InputStream is = buffer.getInputStream();
TechnicalOfficialReader.importFromXLS(is);
logger.warn("imported");
close();
if (callback != null) {
callback.run();
}
} catch (Exception ex) {
// Handle error
}
});
add(upload);
}

public void setCallback(Runnable callback) {
this.callback = callback;
}
}
Loading

0 comments on commit 4446b02

Please sign in to comment.