mirror of
https://github.com/eclipse-sailing-analytics/sailing-analytics.git
synced 2026-11-06 11:02:25 +00:00
adjusted result importers so they use available abstract base classes from com.sap.sailing.resultimport
This commit is contained in:
1 parent
6211fce442
commit
1bf0732bc9
5 files changed
+13
-242
No files matched your search
+1
-5
@@ -1,5 +1,6 @@
|
||||
package com.sap.sailing.winregatta.resultimport.test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import java.io.File;
|
||||
@@ -39,9 +40,4 @@ public class BarbadosResultImportTest {
|
||||
assertNotNull(regattaResults);
|
||||
assertEquals("505", regattaResults.getMetadata().get("boatclassName"));
|
||||
}
|
||||
|
||||
private void assertEquals(String string, String string2) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
}
|
||||
-123
@@ -1,123 +0,0 @@
|
||||
package com.sap.sailing.winregatta.resultimport.test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.sap.sailing.resultimport.CompetitorEntry;
|
||||
import com.sap.sailing.resultimport.CompetitorRow;
|
||||
import com.sap.sailing.resultimport.RegattaResults;
|
||||
import com.sap.sailing.winregatta.resultimport.impl.CompetitorResultsXlsImporter;
|
||||
|
||||
public class XlsImportTest {
|
||||
private static final String RESOURCES = "resources/";
|
||||
private static final String SAMPLE1_NAME = "Erg_Drachen_Wannseewoche_2012_Beispiel.xlsx";
|
||||
private static final String SAMPLE2_NAME = "Erg_505er.xlsx";
|
||||
private static final String SAMPLE3_NAME = "Erg_Drachen_IDM_2012.xlsx";
|
||||
|
||||
private InputStream getInputStream(String filename) throws FileNotFoundException, IOException {
|
||||
return new FileInputStream(getFile(filename));
|
||||
}
|
||||
|
||||
private File getFile(String filename) {
|
||||
return new File(RESOURCES + filename);
|
||||
}
|
||||
|
||||
private InputStream getSample1InputStream() throws FileNotFoundException, IOException {
|
||||
return getInputStream(SAMPLE1_NAME);
|
||||
}
|
||||
|
||||
private InputStream getSample2InputStream() throws FileNotFoundException, IOException {
|
||||
return getInputStream(SAMPLE2_NAME);
|
||||
}
|
||||
|
||||
private InputStream getSample3InputStream() throws FileNotFoundException, IOException {
|
||||
return getInputStream(SAMPLE3_NAME);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLoadingSampleXLS() throws Exception {
|
||||
CompetitorResultsXlsImporter resultlistFromXlsImporter = new CompetitorResultsXlsImporter();
|
||||
|
||||
InputStream sample1InputStream = getSample1InputStream();
|
||||
RegattaResults regattaResults = resultlistFromXlsImporter.getRegattaResults(sample1InputStream,
|
||||
CompetitorResultsXlsImporter.IMPORT_TEMPLATE_WITH_RANKS_DRACHEN, "Erg_Drachen");
|
||||
|
||||
assertNotNull(regattaResults);
|
||||
assertNotNull(regattaResults.getMetadata());
|
||||
assertNotNull(regattaResults.getCompetitorResults());
|
||||
assertEquals(19, regattaResults.getCompetitorResults().size());
|
||||
|
||||
Map<String, String> metadata = regattaResults.getMetadata();
|
||||
assertNotNull(metadata.get("boatClass"));
|
||||
assertEquals("Drachen", metadata.get("boatClass"));
|
||||
assertNotNull(metadata.get("eventName"));
|
||||
assertEquals("Wannsee Woche", metadata.get("eventName"));
|
||||
|
||||
sample1InputStream.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLoadingSampleWithDiscardingsXLS() throws Exception {
|
||||
CompetitorResultsXlsImporter resultlistFromXlsImporter = new CompetitorResultsXlsImporter();
|
||||
InputStream sample2InputStream = getSample2InputStream();
|
||||
RegattaResults regattaResults = resultlistFromXlsImporter.getRegattaResults(sample2InputStream,
|
||||
CompetitorResultsXlsImporter.IMPORT_TEMPLATE_505, "Erg_505er");
|
||||
|
||||
assertNotNull(regattaResults);
|
||||
assertNotNull(regattaResults.getMetadata());
|
||||
assertNotNull(regattaResults.getCompetitorResults());
|
||||
|
||||
assertEquals(33, regattaResults.getCompetitorResults().size());
|
||||
|
||||
boolean oneRowWithInvalidDiscarding = false;
|
||||
for(CompetitorRow row: regattaResults.getCompetitorResults()) {
|
||||
Iterable<CompetitorEntry> entries = row.getRankAndMaxPointsReasonAndPointsAndDiscarded();
|
||||
int discardCountPerRow = 0;
|
||||
for(CompetitorEntry entry: entries) {
|
||||
if( entry.isDiscarded()) {
|
||||
discardCountPerRow++;
|
||||
}
|
||||
}
|
||||
if(discardCountPerRow != 1) {
|
||||
oneRowWithInvalidDiscarding = true;
|
||||
}
|
||||
}
|
||||
assertFalse(oneRowWithInvalidDiscarding);
|
||||
|
||||
Map<String, String> metadata = regattaResults.getMetadata();
|
||||
assertNotNull(metadata.get("boatClass"));
|
||||
assertEquals("505er", metadata.get("boatClass"));
|
||||
|
||||
sample2InputStream.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLoadingDrachenIDM2012XLS() throws Exception {
|
||||
CompetitorResultsXlsImporter resultlistFromXlsImporter = new CompetitorResultsXlsImporter();
|
||||
InputStream sample3InputStream = getSample3InputStream();
|
||||
RegattaResults regattaResults = resultlistFromXlsImporter.getRegattaResults(sample3InputStream,
|
||||
CompetitorResultsXlsImporter.IMPORT_TEMPLATE_WITHOUT_RANKS_DRACHEN, "Erg_Drachen");
|
||||
|
||||
assertNotNull(regattaResults);
|
||||
assertNotNull(regattaResults.getMetadata());
|
||||
assertNotNull(regattaResults.getCompetitorResults());
|
||||
|
||||
assertEquals(43, regattaResults.getCompetitorResults().size());
|
||||
|
||||
Map<String, String> metadata = regattaResults.getMetadata();
|
||||
assertNotNull(metadata.get("boatClass"));
|
||||
assertEquals("Drachen", metadata.get("boatClass"));
|
||||
|
||||
sample3InputStream.close();
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -75,7 +75,7 @@ public class BarbadosResultSpreadsheet {
|
||||
@Override
|
||||
public Map<String, String> getMetadata() {
|
||||
Map<String, String> result = new HashMap<>();
|
||||
result.put("boatClass", "505");
|
||||
result.put("boatclassName", "505");
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
-73
@@ -1,73 +0,0 @@
|
||||
package com.sap.sailing.winregatta.resultimport.impl;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
|
||||
import org.xml.sax.SAXException;
|
||||
|
||||
import net.sf.jxls.reader.ReaderBuilder;
|
||||
import net.sf.jxls.reader.ReaderConfig;
|
||||
import net.sf.jxls.reader.XLSReadStatus;
|
||||
import net.sf.jxls.reader.XLSReader;
|
||||
|
||||
import com.sap.sailing.resultimport.CompetitorRow;
|
||||
import com.sap.sailing.resultimport.RegattaResults;
|
||||
|
||||
public class CompetitorResultsXlsImporter {
|
||||
public static final String IMPORT_TEMPLATE_WITH_RANKS_DRACHEN = "ImportTemplateWithRanks_Drachen.xml";
|
||||
public static final String IMPORT_TEMPLATE_WITHOUT_RANKS_DRACHEN = "ImportTemplateWithoutRanks_Drachen.xml";
|
||||
public static final String IMPORT_TEMPLATE_505 = "ImportTemplate_505er.xml";
|
||||
|
||||
private static final String RESOURCES_PATH = "/exceltemplates";
|
||||
|
||||
public CompetitorResultsXlsImporter() {
|
||||
}
|
||||
|
||||
public RegattaResults getRegattaResults(InputStream excelFileInputStream, String importTemplateName,
|
||||
String excelSheetName) throws IOException, InvalidFormatException, SAXException {
|
||||
InputStream templateFileInputStream = getClass().getResourceAsStream(RESOURCES_PATH + "/" + importTemplateName);
|
||||
|
||||
final List<CompetitorRow> results = new ArrayList<CompetitorRow>();
|
||||
final RegattaInfo regattaInfo = new RegattaInfo();
|
||||
HashMap<String, Object> beans = new HashMap<String, Object>();
|
||||
beans.put("competitors", results);
|
||||
beans.put("regattaInfo", regattaInfo);
|
||||
|
||||
readXlsSheetGeneric(excelFileInputStream, excelSheetName, new BufferedInputStream(templateFileInputStream),
|
||||
excelSheetName, beans, CompetitorRowImpl.class);
|
||||
|
||||
return new RegattaResults() {
|
||||
@Override
|
||||
public Map<String, String> getMetadata() {
|
||||
return regattaInfo.toMap();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<CompetitorRow> getCompetitorResults() {
|
||||
return results;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private void readXlsSheetGeneric(InputStream xlsIs, String sheetName, InputStream readerXlsConfig,
|
||||
String readerName, Map<String, Object> beans, Class<?> sheetMappingRootClass) throws IOException,
|
||||
SAXException, InvalidFormatException {
|
||||
ReaderConfig.getInstance().setSkipErrors(true);
|
||||
XLSReader mainReader = ReaderBuilder.buildFromXML(readerXlsConfig);
|
||||
Object sheetReader = mainReader.getSheetReaders().get(readerName);
|
||||
mainReader.getSheetReaders().put(sheetName, sheetReader);
|
||||
InputStream inputXLS = new BufferedInputStream(xlsIs);
|
||||
XLSReadStatus readStatus = mainReader.read(inputXLS, beans);
|
||||
xlsIs.close();
|
||||
if (!readStatus.isStatusOK()) {
|
||||
throw new IllegalStateException("Reading xls was not successful according to readStatus: " + readStatus);
|
||||
}
|
||||
}
|
||||
}
|
||||
+11
-40
@@ -1,17 +1,15 @@
|
||||
package com.sap.sailing.winregatta.resultimport.impl;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import com.sap.sailing.domain.base.impl.MillisecondsTimePoint;
|
||||
import com.sap.sailing.domain.common.RegattaScoreCorrections;
|
||||
import com.sap.sailing.domain.common.TimePoint;
|
||||
import com.sap.sailing.domain.common.impl.Util.Pair;
|
||||
import com.sap.sailing.domain.common.impl.Util.Triple;
|
||||
import com.sap.sailing.resultimport.RegattaResults;
|
||||
import com.sap.sailing.resultimport.ResultDocumentProvider;
|
||||
import com.sap.sailing.resultimport.impl.AbstractDocumentBasedScoreCorrectionProvider;
|
||||
@@ -32,21 +30,13 @@ public class ScoreCorrectionProviderImpl extends AbstractDocumentBasedScoreCorre
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Set<Pair<String, TimePoint>>> getHasResultsForBoatClassFromDateByEventName() {
|
||||
public Map<String, Set<Pair<String, TimePoint>>> getHasResultsForBoatClassFromDateByEventName() throws Exception {
|
||||
Map<String, Set<Pair<String, TimePoint>>> result = new HashMap<String, Set<Pair<String,TimePoint>>>();
|
||||
CompetitorResultsXlsImporter importer = new CompetitorResultsXlsImporter();
|
||||
for (URL url : getAllUrls()) {
|
||||
URLConnection conn;
|
||||
try {
|
||||
conn = url.openConnection();
|
||||
TimePoint lastModified = new MillisecondsTimePoint(conn.getLastModified());
|
||||
RegattaResults regattaResult = importer.getRegattaResults((InputStream) conn.getContent(),
|
||||
CompetitorResultsXlsImporter.IMPORT_TEMPLATE_WITHOUT_RANKS_DRACHEN, sheetName);
|
||||
final String boatClassName = getBoatClassName(regattaResult);
|
||||
result.put(boatClassName, Collections.singleton(new Pair<String, TimePoint>(boatClassName, lastModified)));
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
for (Triple<InputStream, String, TimePoint> inputStreamAndNameAndLastModified : getResultDocumentProvider().getDocumentsAndNamesAndLastModified()) {
|
||||
RegattaResults regattaResults = new BarbadosResultSpreadsheet(inputStreamAndNameAndLastModified.getA()).getRegattaResults();
|
||||
TimePoint lastModified = inputStreamAndNameAndLastModified.getC();
|
||||
final String boatClassName = getBoatClassName(regattaResults);
|
||||
result.put(boatClassName, Collections.singleton(new Pair<String, TimePoint>(boatClassName, lastModified)));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
@@ -56,34 +46,15 @@ public class ScoreCorrectionProviderImpl extends AbstractDocumentBasedScoreCorre
|
||||
return metadata.get("boatClass");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterable<URL> getAllUrls() {
|
||||
return Collections.unmodifiableSet(allUrls);
|
||||
}
|
||||
|
||||
@Override
|
||||
public RegattaScoreCorrections getScoreCorrections(String eventName, String boatClassName,
|
||||
TimePoint timePoint) throws Exception {
|
||||
CompetitorResultsXlsImporter importer = new CompetitorResultsXlsImporter();
|
||||
for (URL url : getAllUrls()) {
|
||||
final URLConnection conn = url.openConnection();
|
||||
RegattaResults regattaResult = importer.getRegattaResults((InputStream) conn.getContent(),
|
||||
CompetitorResultsXlsImporter.IMPORT_TEMPLATE_WITHOUT_RANKS_DRACHEN, sheetName);
|
||||
if ((boatClassName == null && getBoatClassName(regattaResult) == null) ||
|
||||
boatClassName.equals(getBoatClassName(regattaResult))) {
|
||||
return new RegattaScoreCorrectionsImpl(this, regattaResult);
|
||||
for (Triple<InputStream, String, TimePoint> inputStreamAndNameAndLastModified : getResultDocumentProvider().getDocumentsAndNamesAndLastModified()) {
|
||||
RegattaResults regattaResults = new BarbadosResultSpreadsheet(inputStreamAndNameAndLastModified.getA()).getRegattaResults();
|
||||
if ((boatClassName == null && getBoatClassName(regattaResults) == null) || boatClassName.equals(getBoatClassName(regattaResults))) {
|
||||
return new RegattaScoreCorrectionsImpl(this, regattaResults);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerResultUrl(URL url) {
|
||||
allUrls.add(url);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeResultUrl(URL url) {
|
||||
allUrls.remove(url);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user