bug5693: extract Sailti event name from one of the XRR docs referenced if available;

adjusted boat class comparison in Division, using BoatClassMasterdata.
unifyBoatClassNameBasedOnExistingMasterdata so that "N17" compares equal
to "Nacra 17" which otherwise would have been a problem as Sailti
reports the boat class name different across the XRR docs and the
HTML navigational doc
This commit is contained in:
Axel Uhl
2022-03-23 18:30:31 +01:00
parent b433c46ee4
commit f4e52b1b48
8 changed files with 135 additions and 70 deletions
@@ -9,13 +9,16 @@ import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.sap.sailing.xrr.resultimport.ParserFactory;
import com.sap.sailing.xrr.schema.Event;
import com.sap.sailing.xrr.schema.RegattaResults;
import com.sap.sse.common.TimePoint;
import com.sap.sse.util.HttpUrlConnectionHelper;
/**
* From a Sailti event result overview document that consists of pairs of class names and the names / URLs of the XRR documents
@@ -34,42 +37,69 @@ import com.sap.sse.common.TimePoint;
*/
public class SailtiEventResultsParserImpl implements SailtiEventResultsParser {
private static final Logger logger = Logger.getLogger(SailtiEventResultsParserImpl.class.getName());
static final Pattern xrrFileNamePattern = Pattern.compile("XML-(.*)_([0-9][0-9]*)_([0-9][0-9]*)_([0-9]*).xml");
static final Pattern xrrFileNamePattern = Pattern.compile("XML-([^_]*)_([0-9][0-9]*)_([0-9][0-9]*)_([0-9]*).xml");
static final Pattern classAndXrrLinkPattern = Pattern.compile("<p>([^<]*)<br/>\\s*<a *href=\"(([^\"]*)"+xrrFileNamePattern+")\"\\s*>");
private final URL baseUrl;
public SailtiEventResultsParserImpl(URL baseUrl) {
super();
this.baseUrl = baseUrl;
}
/**
* @param is closed before the method returns, also in case of exception
*/
public EventResultDescriptor getEventResult(InputStream is) throws IOException {
EventResultDescriptor result = null;
try {
final BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
List<RegattaResultDescriptor> regattaResults = new ArrayList<>();
String className;
while ((className = br.readLine()) != null) {
final String xrrDocumentUrl = br.readLine();
final Matcher matcher = xrrFileNamePattern.matcher(xrrDocumentUrl);
if (matcher.matches()) {
regattaResults.add(new RegattaResultDescriptor(matcher.group(2), matcher.group(3), className, new URL(xrrDocumentUrl), getTimePoint(matcher)));
try (final BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"))) {
List<RegattaResultDescriptor> regattaResults = new ArrayList<>();
final StringBuilder eventHtml = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
eventHtml.append(line);
}
final Matcher matcher = classAndXrrLinkPattern.matcher(eventHtml.toString());
while (matcher.find()) {
regattaResults.add(new RegattaResultDescriptor(matcher.group(1)+"/"+matcher.group(5)+"/"+matcher.group(6), matcher.group(1), getBoatClassName(matcher),
new URL(baseUrl, getAbsoluteXrrUrlPath(matcher)), getTimePoint(matcher)));
}
final String eventName;
if (regattaResults.isEmpty()) {
eventName = getEventId();
} else {
final URL xrrFinalUrl = regattaResults.iterator().next().getXrrFinalUrl();
final RegattaResults anyXrr = ParserFactory.INSTANCE.createParser(
HttpUrlConnectionHelper.redirectConnection(xrrFinalUrl).getInputStream(), xrrFinalUrl.toString()).parse();
if (anyXrr == null) {
eventName = getEventId();
} else {
eventName = anyXrr.getPersonOrBoatOrTeam().stream().filter(o->(o instanceof Event)).findAny().map(o->((Event) o).getTitle()).get();
}
}
result = new EventResultDescriptor(getEventId(), eventName, regattaResults);
}
result = new EventResultDescriptor(/* TODO bug5693 ID */ ""+new Random().nextDouble(),
/* TODO bug5693 name */ ""+new Random().nextDouble(),
regattaResults);
is.close();
} catch(Exception e) {
logger.log(Level.SEVERE, "Problem parsing Sailti event document", e);
} finally {
is.close();
}
return result;
}
private String getEventId() {
return baseUrl.getPath().substring(baseUrl.getPath().lastIndexOf('/')+1);
}
private String getAbsoluteXrrUrlPath(Matcher matcher) {
return matcher.group(2);
}
String getBoatClassName(Matcher matcher) {
return matcher.group(1);
}
TimePoint getTimePoint(Matcher matcher) throws ParseException {
return TimePoint.of(new SimpleDateFormat("yyyyMMddhhmmssX").parse(matcher.group(4)+"Z"));
return TimePoint.of(new SimpleDateFormat("yyyyMMddhhmmssX").parse(matcher.group(7)+"Z"));
}
}
@@ -4,7 +4,6 @@ import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
@@ -12,11 +11,6 @@ import com.sap.sailing.domain.resultimport.ResultUrlProvider;
import com.sap.sailing.resultimport.ResultDocumentDescriptor;
import com.sap.sailing.resultimport.ResultDocumentProvider;
import com.sap.sailing.resultimport.impl.ResultDocumentDescriptorImpl;
import com.sap.sailing.xrr.resultimport.impl.XRRParserUtil;
import com.sap.sailing.xrr.schema.Division;
import com.sap.sailing.xrr.schema.Event;
import com.sap.sailing.xrr.schema.RegattaResults;
import com.sap.sse.common.TimePoint;
import com.sap.sse.util.HttpUrlConnectionHelper;
/**
@@ -34,34 +28,6 @@ public class SailtiResultDocumentProvider implements ResultDocumentProvider {
this.resultUrlProvider = resultUrlProvider;
}
public List<ResultDocumentDescriptor> resolveResultDocumentDescriptors(RegattaResults xrrParserResult, URL url) {
List<ResultDocumentDescriptor> result = new ArrayList<>();
final TimePoint xrrDocumentDateAndTime = XRRParserUtil.calculateTimePointForRegattaResults(xrrParserResult);
for (Object o : xrrParserResult.getPersonOrBoatOrTeam()) {
if (o instanceof Event) {
Event event = (Event) o;
String eventName = event.getTitle();
for (Object d: event.getRaceOrDivisionOrRegattaSeriesResult()) {
if (d instanceof Division) {
Division division = (Division) d;
String regattaName = division.getTitle();
String boatClass = division.getTitle();
try {
String requestUrl = url.toString() + "&Class=" + URLEncoder.encode(boatClass, "UTF-8");
URL urlByClass = new URL(requestUrl);
result.add(new UrlResultDocumentDescriptorImpl(urlByClass, requestUrl, xrrDocumentDateAndTime,
eventName, regattaName, boatClass));
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
return result;
}
private URL getDocumentUrlForRegatta(RegattaResultDescriptor regattaResult) {
return regattaResult.getXrrFinalUrl();
}
@@ -73,8 +39,8 @@ public class SailtiResultDocumentProvider implements ResultDocumentProvider {
@Override
public Iterable<ResultDocumentDescriptor> getResultDocumentDescriptors() throws IOException {
List<ResultDocumentDescriptor> result = new ArrayList<>();
SailtiEventResultsParserImpl parser = new SailtiEventResultsParserImpl();
for (URL url : resultUrlProvider.getReadableUrls()) {
SailtiEventResultsParserImpl parser = new SailtiEventResultsParserImpl(url);
URLConnection eventResultConn = HttpUrlConnectionHelper.redirectConnection(url);
EventResultDescriptor eventResult = parser.getEventResult((InputStream) eventResultConn.getContent());
addResultsForEvent(result, eventResult);
@@ -15,13 +15,13 @@ import com.sap.sse.util.HttpUrlConnectionHelper;
public class UrlResultDocumentDescriptorImpl implements ResultDocumentDescriptor {
private static final Logger logger = Logger.getLogger(UrlResultDocumentDescriptorImpl.class.getName());
private URL documentURL;
private String documentName;
private TimePoint lastModified;
private String eventName;
private String regattaName;
private String boatClass;
private CompetitorGenderType competitorGenderType;
private final URL documentURL;
private final String documentName;
private final TimePoint lastModified;
private final String eventName;
private final String regattaName;
private final String boatClass;
private final CompetitorGenderType competitorGenderType;
public UrlResultDocumentDescriptorImpl(URL documentURL, String documentName, TimePoint lastModified) {
this(documentURL, documentName, lastModified, null, null, null, null);