mirror of
https://github.com/eclipse-sailing-analytics/sailing-analytics.git
synced 2026-10-09 13:50:57 +00:00
# moved some code in gateway
This commit is contained in:
1 parent
ca61dc52e5
commit
61dec32f17
6 files changed
+213
-142
No files matched your search
@@ -35,11 +35,11 @@
|
||||
</servlet-mapping>
|
||||
|
||||
<servlet>
|
||||
<servlet-name>RCRaceLogs</servlet-name>
|
||||
<servlet-class>com.sap.sailing.server.gateway.impl.rc.RaceLogsJsonExportServlet</servlet-class>
|
||||
<servlet-name>RCQueryRaceLog</servlet-name>
|
||||
<servlet-class>com.sap.sailing.server.gateway.impl.rc.QueryRaceLogJsonExportServlet</servlet-class>
|
||||
</servlet>
|
||||
<servlet-mapping>
|
||||
<servlet-name>RCRaceLogs</servlet-name>
|
||||
<servlet-name>RCQueryRaceLog</servlet-name>
|
||||
<url-pattern>/rc/racelogs</url-pattern>
|
||||
</servlet-mapping>
|
||||
|
||||
|
||||
+1
-2
@@ -24,8 +24,7 @@ public class EventsJsonExportServlet extends JsonExportServlet {
|
||||
|
||||
JsonSerializer<Event> eventSerializer = createSerializer();
|
||||
JSONArray result = new JSONArray();
|
||||
for (Event event : getService().getAllEvents())
|
||||
{
|
||||
for (Event event : getService().getAllEvents()) {
|
||||
result.add(eventSerializer.serialize(event));
|
||||
}
|
||||
result.writeJSONString(response.getWriter());
|
||||
|
||||
+39
-5
@@ -1,6 +1,7 @@
|
||||
package com.sap.sailing.server.gateway.impl.rc;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.UUID;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
@@ -8,6 +9,8 @@ import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.json.simple.JSONArray;
|
||||
|
||||
import com.sap.sailing.domain.base.CourseArea;
|
||||
import com.sap.sailing.domain.base.Event;
|
||||
import com.sap.sailing.domain.leaderboard.Leaderboard;
|
||||
import com.sap.sailing.server.gateway.impl.JsonExportServlet;
|
||||
import com.sap.sailing.server.gateway.serialization.JsonSerializer;
|
||||
@@ -29,20 +32,51 @@ public class LeaderboardJsonExportServlet extends JsonExportServlet {
|
||||
throws ServletException, IOException {
|
||||
|
||||
String courseAreaFilter = request.getParameter(PARAM_COURSE_AREA_FILTER);
|
||||
if (courseAreaFilter != null) {
|
||||
System.out.println("Filter by course area: " + courseAreaFilter);
|
||||
if (courseAreaFilter == null) {
|
||||
response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Need to set a course area filter.");
|
||||
return;
|
||||
}
|
||||
/// TODO: implement course are filtering
|
||||
UUID courseAreaId = toUUID(courseAreaFilter);
|
||||
if (courseAreaId == null) {
|
||||
response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Course are filter must be valid UUID.");
|
||||
return;
|
||||
}
|
||||
|
||||
CourseArea filterCourseArea = getCourseArea(courseAreaId);
|
||||
|
||||
JsonSerializer<Leaderboard> serializer = createSerializer();
|
||||
JSONArray result = new JSONArray();
|
||||
|
||||
for (Leaderboard leaderboard : getService().getLeaderboards().values()) {
|
||||
|
||||
result.add(serializer.serialize(leaderboard));
|
||||
if (leaderboard.getDefaultCourseArea().equals(filterCourseArea)) {
|
||||
result.add(serializer.serialize(leaderboard));
|
||||
}
|
||||
}
|
||||
|
||||
result.writeJSONString(response.getWriter());
|
||||
}
|
||||
|
||||
private CourseArea getCourseArea(UUID courseAreaId) {
|
||||
for (Event event : getService().getAllEvents()) {
|
||||
if (event.getVenue() == null) {
|
||||
continue;
|
||||
}
|
||||
for (CourseArea courseArea : event.getVenue().getCourseAreas()) {
|
||||
if (courseArea.getId().equals(courseAreaId)) {
|
||||
return courseArea;
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private UUID toUUID(String value) {
|
||||
try {
|
||||
return UUID.fromString(value);
|
||||
} catch (IllegalArgumentException iae) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static JsonSerializer<Leaderboard> createSerializer() {
|
||||
return new LeaderboardJsonSerializer(
|
||||
|
||||
+97
@@ -0,0 +1,97 @@
|
||||
package com.sap.sailing.server.gateway.impl.rc;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.json.simple.JSONObject;
|
||||
import org.json.simple.JSONValue;
|
||||
import org.json.simple.parser.ParseException;
|
||||
|
||||
import com.sap.sailing.domain.base.Fleet;
|
||||
import com.sap.sailing.domain.base.RaceColumn;
|
||||
import com.sap.sailing.domain.leaderboard.Leaderboard;
|
||||
import com.sap.sailing.domain.racelog.RaceLogEvent;
|
||||
import com.sap.sailing.domain.tracking.DynamicTrackedRace;
|
||||
import com.sap.sailing.domain.tracking.DynamicTrackedRegatta;
|
||||
import com.sap.sailing.domain.tracking.TrackedRace;
|
||||
import com.sap.sailing.domain.tracking.TrackedRegatta;
|
||||
import com.sap.sailing.server.gateway.deserialization.JsonDeserializationException;
|
||||
import com.sap.sailing.server.gateway.deserialization.impl.Helpers;
|
||||
import com.sap.sailing.server.gateway.impl.JsonExportServlet;
|
||||
|
||||
|
||||
public class ModifyRaceLogJsonExportServlet extends JsonExportServlet {
|
||||
private static final long serialVersionUID = 2197006949440887925L;
|
||||
|
||||
/// TODO: for presentation purpose only - implementation needed
|
||||
@SuppressWarnings("unused")
|
||||
@Override
|
||||
protected void doPost(HttpServletRequest request, HttpServletResponse response)
|
||||
throws ServletException, IOException {
|
||||
|
||||
/// TODO: split race identifier into its part (leaderboard name, series name, fleet name, race column name)
|
||||
try {
|
||||
Object requestBody = JSONValue.parseWithException(request.getReader());
|
||||
JSONObject requestObject = Helpers.toJSONObjectSafe(requestBody);
|
||||
|
||||
/// TODO: deserialize request (including identifier and to-be-added event)
|
||||
String leaderboardName = "";
|
||||
String seriesName = "";
|
||||
String fleetName = "";
|
||||
String raceColumnName = "";
|
||||
RaceLogEvent raceLogEvent = null;
|
||||
|
||||
Leaderboard leaderboard = getService().getLeaderboardByName(leaderboardName);
|
||||
if (leaderboard == null) {
|
||||
response.sendError(HttpServletResponse.SC_NOT_FOUND, "No such leaderboard found.");
|
||||
return;
|
||||
}
|
||||
|
||||
Fleet fleet = leaderboard.getFleet(fleetName);
|
||||
if (fleet == null) {
|
||||
response.sendError(HttpServletResponse.SC_NOT_FOUND, "No such fleet found.");
|
||||
return;
|
||||
}
|
||||
|
||||
RaceColumn raceColumn = leaderboard.getRaceColumnByName(raceColumnName);
|
||||
if (raceColumn == null) {
|
||||
response.sendError(HttpServletResponse.SC_NOT_FOUND, "No such race column found.");
|
||||
return;
|
||||
}
|
||||
|
||||
TrackedRace trackedRace = raceColumn.getTrackedRace(fleet);
|
||||
if (trackedRace == null) {
|
||||
response.sendError(HttpServletResponse.SC_NOT_FOUND, "No such tracked race found.");
|
||||
return;
|
||||
}
|
||||
|
||||
DynamicTrackedRace dynamicTrackedRace = getDynamicTrackedRace(trackedRace);
|
||||
if (dynamicTrackedRace == null) {
|
||||
response.sendError(
|
||||
HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
|
||||
"Tracked race cannot be modified right now.");
|
||||
return;
|
||||
}
|
||||
|
||||
dynamicTrackedRace.recordRaceLogEvent(raceLogEvent);
|
||||
} catch (ParseException e) {
|
||||
throw new JsonDeserializationException("Invalid JSON in request body.", e);
|
||||
}
|
||||
}
|
||||
|
||||
private DynamicTrackedRace getDynamicTrackedRace(TrackedRace trackedRace) {
|
||||
TrackedRegatta trackedRegatta = trackedRace.getTrackedRegatta();
|
||||
if (trackedRegatta == null) {
|
||||
return null;
|
||||
}
|
||||
DynamicTrackedRegatta dynamicTrackedRegatta = getService().getTrackedRegatta(trackedRegatta.getRegatta());
|
||||
if (dynamicTrackedRegatta == null) {
|
||||
return null;
|
||||
}
|
||||
return dynamicTrackedRegatta.getExistingTrackedRace(trackedRace.getRace());
|
||||
}
|
||||
|
||||
}
|
||||
+73
@@ -0,0 +1,73 @@
|
||||
package com.sap.sailing.server.gateway.impl.rc;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.json.simple.JSONArray;
|
||||
import org.json.simple.JSONValue;
|
||||
import org.json.simple.parser.ParseException;
|
||||
|
||||
import com.sap.sailing.domain.base.Competitor;
|
||||
import com.sap.sailing.domain.racelog.RaceLog;
|
||||
import com.sap.sailing.domain.racelog.RaceLogEvent;
|
||||
import com.sap.sailing.domain.tracking.TrackedRace;
|
||||
import com.sap.sailing.server.gateway.deserialization.JsonDeserializationException;
|
||||
import com.sap.sailing.server.gateway.deserialization.impl.Helpers;
|
||||
import com.sap.sailing.server.gateway.impl.JsonExportServlet;
|
||||
import com.sap.sailing.server.gateway.serialization.JsonSerializer;
|
||||
import com.sap.sailing.server.gateway.serialization.impl.CompetitorIdJsonSerializer;
|
||||
import com.sap.sailing.server.gateway.serialization.impl.racelog.RaceLogCourseAreaChangedEventSerializer;
|
||||
import com.sap.sailing.server.gateway.serialization.impl.racelog.RaceLogEventSerializer;
|
||||
import com.sap.sailing.server.gateway.serialization.impl.racelog.RaceLogFlagEventSerializer;
|
||||
import com.sap.sailing.server.gateway.serialization.impl.racelog.RaceLogStartTimeEventSerializer;
|
||||
|
||||
|
||||
public class QueryRaceLogJsonExportServlet extends JsonExportServlet {
|
||||
private static final long serialVersionUID = 2197006949440887925L;
|
||||
private static final Logger logger = Logger.getLogger(QueryRaceLogJsonExportServlet.class.getName());
|
||||
|
||||
@Override
|
||||
protected void doPost(HttpServletRequest request, HttpServletResponse response)
|
||||
throws ServletException, IOException {
|
||||
try {
|
||||
Object requestBody = JSONValue.parseWithException(request.getReader());
|
||||
JSONArray requestedIdentifiers = Helpers.toJSONArraySafe(requestBody);
|
||||
|
||||
for (Object element : requestedIdentifiers) {
|
||||
logger.info(String.format("Client requests race log for race with identifier %s.", element));
|
||||
/// TODO: deserialize identifier and look up TrackedRace
|
||||
}
|
||||
/// TODO: Return RaceLogs for found TrackedRaces...
|
||||
|
||||
} catch (ParseException e) {
|
||||
throw new JsonDeserializationException("Invalid JSON in request body.", e);
|
||||
}
|
||||
}
|
||||
|
||||
protected JSONArray serizalizeTrack(TrackedRace trackedRace) {
|
||||
JsonSerializer<RaceLogEvent> serializer = createSerializer();
|
||||
JSONArray result = new JSONArray();
|
||||
|
||||
RaceLog log = trackedRace.getRaceLog();
|
||||
log.lockForRead();
|
||||
for (RaceLogEvent event : log.getFixes()) {
|
||||
result.add(serializer.serialize(event));
|
||||
}
|
||||
log.unlockAfterRead();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private static JsonSerializer<RaceLogEvent> createSerializer() {
|
||||
JsonSerializer<Competitor> competitorSerializer = new CompetitorIdJsonSerializer();
|
||||
return new RaceLogEventSerializer(
|
||||
new RaceLogFlagEventSerializer(competitorSerializer),
|
||||
new RaceLogStartTimeEventSerializer(competitorSerializer),
|
||||
new RaceLogCourseAreaChangedEventSerializer(competitorSerializer));
|
||||
}
|
||||
|
||||
}
|
||||
-132
@@ -1,132 +0,0 @@
|
||||
package com.sap.sailing.server.gateway.impl.rc;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.json.simple.JSONArray;
|
||||
import org.json.simple.JSONValue;
|
||||
import org.json.simple.parser.ParseException;
|
||||
|
||||
import com.sap.sailing.domain.base.Competitor;
|
||||
import com.sap.sailing.domain.base.RaceDefinition;
|
||||
import com.sap.sailing.domain.base.Regatta;
|
||||
import com.sap.sailing.domain.common.RegattaNameAndRaceName;
|
||||
import com.sap.sailing.domain.racelog.RaceLogEvent;
|
||||
import com.sap.sailing.domain.tracking.DynamicTrackedRace;
|
||||
import com.sap.sailing.domain.tracking.DynamicTrackedRegatta;
|
||||
import com.sap.sailing.server.gateway.deserialization.JsonDeserializationException;
|
||||
import com.sap.sailing.server.gateway.deserialization.impl.Helpers;
|
||||
import com.sap.sailing.server.gateway.impl.JsonExportServlet;
|
||||
import com.sap.sailing.server.gateway.serialization.JsonSerializer;
|
||||
import com.sap.sailing.server.gateway.serialization.impl.CompetitorIdJsonSerializer;
|
||||
import com.sap.sailing.server.gateway.serialization.impl.racelog.RaceLogCourseAreaChangedEventSerializer;
|
||||
import com.sap.sailing.server.gateway.serialization.impl.racelog.RaceLogEventSerializer;
|
||||
import com.sap.sailing.server.gateway.serialization.impl.racelog.RaceLogFlagEventSerializer;
|
||||
import com.sap.sailing.server.gateway.serialization.impl.racelog.RaceLogStartTimeEventSerializer;
|
||||
|
||||
|
||||
public class RaceLogsJsonExportServlet extends JsonExportServlet {
|
||||
private static final long serialVersionUID = 2197006949440887925L;
|
||||
|
||||
|
||||
@Override
|
||||
protected void doPost(HttpServletRequest request, HttpServletResponse response)
|
||||
throws ServletException, IOException {
|
||||
try {
|
||||
Object requestBody = JSONValue.parseWithException(request.getReader());
|
||||
JSONArray requestedIds = Helpers.toJSONArraySafe(requestBody);
|
||||
|
||||
for (Object element : requestedIds) {
|
||||
System.out.println(element);
|
||||
}
|
||||
|
||||
} catch (ParseException e) {
|
||||
throw new JsonDeserializationException("Invalid JSON in request body.", e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
private DynamicTrackedRace getDynamicTrackedRace(String regattaName, String raceName) {
|
||||
RegattaNameAndRaceName raceAndRegattaIdentifier = new RegattaNameAndRaceName(regattaName, raceName);
|
||||
|
||||
RaceDefinition raceDefinition = getService().getRace(raceAndRegattaIdentifier);
|
||||
Regatta regatta = getService().getRegatta(raceAndRegattaIdentifier);
|
||||
|
||||
DynamicTrackedRegatta trackedRegatta = getService().getTrackedRegatta(regatta);
|
||||
DynamicTrackedRace trackedRace = trackedRegatta.getExistingTrackedRace(raceDefinition);
|
||||
|
||||
return trackedRace;
|
||||
}
|
||||
|
||||
private boolean validateParameter(HttpServletResponse response, String... parameters) throws IOException {
|
||||
for (String parameter : parameters) {
|
||||
if (parameter == null) {
|
||||
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private static JsonSerializer<RaceLogEvent> createSerializer() {
|
||||
JsonSerializer<Competitor> competitorSerializer = new CompetitorIdJsonSerializer();
|
||||
return new RaceLogEventSerializer(
|
||||
new RaceLogFlagEventSerializer(competitorSerializer),
|
||||
new RaceLogStartTimeEventSerializer(competitorSerializer),
|
||||
new RaceLogCourseAreaChangedEventSerializer(competitorSerializer));
|
||||
}
|
||||
/*@Override
|
||||
protected void doGet(HttpServletRequest request, HttpServletResponse response)
|
||||
throws ServletException, IOException {
|
||||
|
||||
TrackedRace trackedRace = getTrackedRace(request);
|
||||
if (trackedRace == null)
|
||||
{
|
||||
response.sendError(HttpServletResponse.SC_NOT_FOUND);
|
||||
return;
|
||||
}
|
||||
|
||||
JSONArray result = serizalizeTrack(trackedRace);
|
||||
result.writeJSONString(response.getWriter());
|
||||
}
|
||||
|
||||
private JSONArray serizalizeTrack(TrackedRace trackedRace) {
|
||||
JsonSerializer<RaceLogEvent> rcEventSerializer = createSerializer();
|
||||
JSONArray result = new JSONArray();
|
||||
|
||||
RaceLog track = trackedRace.getRaceLog();
|
||||
track.lockForRead();
|
||||
for (RaceLogEvent event : track.getFixes()) {
|
||||
result.add(rcEventSerializer.serialize(event));
|
||||
}
|
||||
track.unlockAfterRead();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doPost(HttpServletRequest request, HttpServletResponse response)
|
||||
throws ServletException, IOException {
|
||||
|
||||
String regattaName = request.getParameter(PARAM_NAME_REGATTANAME);
|
||||
String raceName = request.getParameter(PARAM_NAME_RACENAME);
|
||||
if (!validateParameter(response, regattaName, raceName)) return;
|
||||
|
||||
DynamicTrackedRace trackedRace = getDynamicTrackedRace(regattaName, raceName);
|
||||
if (trackedRace == null)
|
||||
{
|
||||
response.sendError(HttpServletResponse.SC_NOT_FOUND);
|
||||
return;
|
||||
}
|
||||
|
||||
// TODO: add event to trackedRace...
|
||||
// trackedRace.recordRaceCommitteeEvent(null);
|
||||
response.sendError(HttpServletResponse.SC_NO_CONTENT);
|
||||
|
||||
}*/
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user