Saving local changes.

This commit is contained in:
Leon Radeck committed 2017-08-19 03:08:01 +02:00
1 parent 0774be020a
commit 246d5bb897
1 file changed
+329 -264
@@ -10,12 +10,13 @@ import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;import java.util.Spliterator;
import java.util.Map;
import java.util.Spliterator;
import java.util.UUID;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
@@ -35,6 +36,7 @@ import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.shiro.SecurityUtils;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
@@ -49,6 +51,8 @@ import com.sap.sailing.domain.common.ScoringSchemeType;
import com.sap.sailing.domain.common.dto.FleetDTO;
import com.sap.sailing.domain.common.dto.RegattaCreationParametersDTO;
import com.sap.sailing.domain.common.dto.SeriesCreationParametersDTO;
import com.sap.sailing.domain.common.security.Permission;
import com.sap.sailing.domain.common.security.Permission.Mode;
import com.sap.sailing.domain.leaderboard.Leaderboard;
import com.sap.sailing.domain.leaderboard.LeaderboardGroup;
import com.sap.sailing.domain.leaderboard.RegattaLeaderboard;
@@ -72,77 +76,113 @@ import com.sap.sse.common.impl.MillisecondsTimePoint;
import com.sap.sse.shared.media.ImageDescriptor;
import com.sap.sse.shared.media.VideoDescriptor;
@Path("/v1/events")
public class EventsResource extends AbstractSailingServerResource {
private static final Logger logger = Logger.getLogger(EventsResource.class.getName());
private static final String DEFAULT_EVENT_IS_PUBLIC = "false";
private static final String DEFAULT_EVENT_CREATE_LEADERBOARD_GROUP = "false";
private static final String DEFAULT_EVENT_CREATE_REGATTA = "false";
private static final String DEFAULT_LG_RANKING_METRIC = "ONE_DESIGN";
private static final String DEFAULT_LG_OVERALL_LB_SCORING_SCHEME = "LOW_POINT";
private static final String DEFAULT_LG_DISPLAY_NAME = null;
private static final String DEFAULT_LG_DISPLAY_GROUPS_IN_REVERSE_ORDER = "false";
private static final List<String> DEFAULT_LG_LEADERBOARD_NAMES = new ArrayList<String>();
private static final List<Integer> DEFAULT_LG_OVERALL_LB_DISCARD_THRESHOLDS = new ArrayList<Integer>();
private static final String DEFAULT_COURSE_AREA_NAME = "Default";
private static final String DEFAULT_REGATTA_IS_PERSISTENT = "true";
private static final String DEFAULT_REGATTA_BUOY_ZONE_RADIUS_IN_HULL_LENGTH = "3.0";
private static final String DEFAULT_REGATTA_CONTROL_TRACKING_FROM_START_AND_FINISH_TIMES = "false";
private static final String DEFAULT_REGATTA_USE_START_TIME_INTERFERENCE = "true";
private static final String DEFAULT_REGATTA_START_DATE = null;
private static final String DEFAULT_REGATTA_END_DATE = null;
private static final String DEFAULT_REGATTA_COURSE_AREA_ID = null;
private static final String DEFAULT_SERIES_NAME = "Default";
private static final String DEFAULT_FLEET_NAME = "Default";
private static final SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy");
// SecurityUtils.getSubject().checkPermission(Permission.LEADERBOARD.getStringPermissionForObjects(Mode.UPDATE, leaderboardName));
@POST
@Path("/createEvent")
@Consumes("text/plain")
@Produces("text/plain")
public Response createEvent(
@FormParam("eventName") @NotNull String eventName,
public Response createEvent(@FormParam("eventName") String eventName,
@FormParam("eventDescription") String eventDescription,
@FormParam("startDate") String startDateString,
@FormParam("endDate") String endDateString,
@FormParam("venueName") @NotNull String venueName,
@FormParam("isPublic") @DefaultValue("false") boolean isPublic,
@FormParam("endDate") String endDateString,
@FormParam("venueName") String venueName,
@FormParam("isPublic") @DefaultValue(DEFAULT_EVENT_IS_PUBLIC) String isPublicParam,
@FormParam("officialWebsiteURL") String officialWebsiteURLString,
@FormParam("baseURL") String baseURLString,
@FormParam("leaderboardGroupIds") List<String> leaderboardGroupIdsList)
throws ParseException, MalformedURLException {
@FormParam("leaderboardGroupIds") List<String> leaderboardGroupIdsList,
@FormParam("createLeaderboardGroup") @DefaultValue(DEFAULT_EVENT_CREATE_LEADERBOARD_GROUP) String createLeaderboardGroupParam,
@FormParam("createRegatta") @DefaultValue(DEFAULT_EVENT_CREATE_REGATTA) String createRegattaParam,
@FormParam("boatClassName") String boatClassName)
{
SecurityUtils.getSubject().checkPermission(Permission.EVENT.getStringPermission(Mode.CREATE));
logger.fine(SecurityUtils.getSubject().getSession().toString());
boolean createRegatta = Boolean.parseBoolean(createRegattaParam);
boolean isPublic = Boolean.parseBoolean(isPublicParam);
boolean createLeaderboardGroup = Boolean.parseBoolean(createLeaderboardGroupParam);
if(eventName == null){
return isRequired("eventName");
if (eventName == null) {
return getParameterMissingResponse("eventName");
}
if (venueName == null) {
return getParameterMissingResponse("venueName");
}
if(venueName == null){
return isRequired("venueName");
if (createRegatta) {
if (boatClassName == null) {
return getParameterMissingResponse("boatClassName");
}
}
if(eventDescription == null){
if (eventDescription == null) {
eventDescription = eventName;
}
MillisecondsTimePoint startDate = null;
try{
startDate = startDateString != null ? new MillisecondsTimePoint(df.parse(startDateString)) : new MillisecondsTimePoint(new Date());
try {
startDate = startDateString != null ? new MillisecondsTimePoint(df.parse(startDateString))
: new MillisecondsTimePoint(new Date());
} catch (ParseException e) {
return getInvalidDateFormatResponse(startDateString);
}
catch (ParseException e){
return invalidDateFormat(startDateString);
}
MillisecondsTimePoint endDate = null;
try{
endDate = endDateString != null ? new MillisecondsTimePoint(df.parse(endDateString)) : new MillisecondsTimePoint(addOneWeek(startDate.asDate()));
try {
endDate = endDateString != null ? new MillisecondsTimePoint(df.parse(endDateString))
: new MillisecondsTimePoint(addOneWeek(startDate.asDate()));
} catch (ParseException e) {
return getInvalidDateFormatResponse(endDateString);
}
catch (ParseException e){
return invalidDateFormat(endDateString);
}
URL officialWebsiteURL = null;
try{
URL officialWebsiteURL = null;
try {
officialWebsiteURL = officialWebsiteURLString != null ? new URL(officialWebsiteURLString) : null;
} catch (MalformedURLException e) {
return getInvalidURLFormatResponse(officialWebsiteURLString);
}
catch (MalformedURLException e){
return invalidURLFormat(officialWebsiteURLString);
}
URL baseURL = null;
try{
URL baseURL = null;
try {
baseURL = baseURLString != null ? new URL(baseURLString) : null;
} catch (MalformedURLException e) {
return getInvalidURLFormatResponse(baseURLString);
}
catch (MalformedURLException e){
return invalidURLFormat(baseURLString);
}
List<UUID> leaderboardGroupIds = leaderboardGroupIdsList.stream().map(id -> UUID.fromString(id))
.collect(Collectors.toList());
UUID eventId = UUID.randomUUID();
// ignoring sailorsInfoWebsiteURLs, images, videos
@@ -150,106 +190,113 @@ public class EventsResource extends AbstractSailingServerResource {
Iterable<ImageDescriptor> images = Collections.<ImageDescriptor> emptyList();
Iterable<VideoDescriptor> videos = Collections.<VideoDescriptor> emptyList();
Event event = getService().apply(
new CreateEvent(eventName, eventDescription, startDate, endDate, venueName, isPublic, eventId,
Event event = getService()
.apply(new CreateEvent(eventName, eventDescription, startDate, endDate, venueName, isPublic, eventId,
officialWebsiteURL, baseURL, sailorsInfoWebsiteURLs, images, videos, leaderboardGroupIds));
addCourseArea(event.getId().toString(), "Default");
addRegatta(event.getName(),"A_CAT",null,event.getId().toString(),null,true,"LOW_POINT",null,3.0,true,false,"ONE_DESIGN");
addLeaderboard(event.getName(), new ArrayList<Integer>());
// addLeaderboardGroup(event.getId().toString(), event.getName(), event.getDescription(), event.getName(), false, new ArrayList<String>(), null, null);
addCourseArea(event.getId().toString(), DEFAULT_COURSE_AREA_NAME);
if (createLeaderboardGroup) {
addLeaderboardGroup(event.getId().toString(), event.getName(), event.getName(), DEFAULT_LG_DISPLAY_NAME, DEFAULT_LG_DISPLAY_GROUPS_IN_REVERSE_ORDER,
DEFAULT_LG_LEADERBOARD_NAMES, DEFAULT_LG_OVERALL_LB_DISCARD_THRESHOLDS, DEFAULT_LG_OVERALL_LB_SCORING_SCHEME);
}
if (createRegatta) {
addRegatta(event.getName(), boatClassName, DEFAULT_REGATTA_START_DATE, event.getId().toString(), DEFAULT_REGATTA_END_DATE,
DEFAULT_REGATTA_IS_PERSISTENT, DEFAULT_LG_OVERALL_LB_SCORING_SCHEME, DEFAULT_REGATTA_COURSE_AREA_ID,
DEFAULT_REGATTA_BUOY_ZONE_RADIUS_IN_HULL_LENGTH,
DEFAULT_REGATTA_USE_START_TIME_INTERFERENCE,
DEFAULT_REGATTA_CONTROL_TRACKING_FROM_START_AND_FINISH_TIMES,
DEFAULT_LG_RANKING_METRIC);
}
return ok(event.getId().toString(), MediaType.TEXT_PLAIN);
}
@POST
@Path("/addRegatta")
@Consumes("text/plain")
@Produces("text/plain")
public Response addRegatta(
@FormParam("regattaName") @NotNull String regattaName,
@FormParam("boatClassName") @NotNull String boatClassNameParam,
@FormParam("startDate") String startDateString,
public Response addRegatta(@FormParam("regattaName") String regattaName,
@FormParam("boatClassName") String boatClassNameParam,
@FormParam("startDate") String startDateString,
@FormParam("eventId") String eventIdParam,
@FormParam("endDate") String endDateString,
@FormParam("persistent") @DefaultValue("true") boolean persistent,
@FormParam("scoringScheme") @DefaultValue("LOW_POINT") String scoringSchemeParam,
@FormParam("persistent") @DefaultValue(DEFAULT_REGATTA_IS_PERSISTENT) String isPersistentParam,
@FormParam("scoringScheme") @DefaultValue(DEFAULT_LG_OVERALL_LB_SCORING_SCHEME) String scoringSchemeParam,
@FormParam("courseAreaId") String courseAreaIdParam,
@FormParam("buoyZoneRadiusInHullLengths") @DefaultValue("3.0") double buoyZoneRadiusInHullLengths,
@FormParam("useStartTimeInterference") @DefaultValue("true") boolean useStartTimeInterference,
@FormParam("controlTrackingFromStartAndFinishTimes") @DefaultValue("false") boolean controlTrackingFromStartAndFinishTimes,
@FormParam("rankingMetric") @DefaultValue("ONE_DESIGN") String rankingMetricParam)
throws ParseException {
@FormParam("buoyZoneRadiusInHullLengths") @DefaultValue(DEFAULT_REGATTA_BUOY_ZONE_RADIUS_IN_HULL_LENGTH) String buoyZoneRadiusInHullLengthsParam,
@FormParam("useStartTimeInterference") @DefaultValue(DEFAULT_REGATTA_USE_START_TIME_INTERFERENCE) String useStartTimeInterferenceParam,
@FormParam("controlTrackingFromStartAndFinishTimes") @DefaultValue(DEFAULT_REGATTA_CONTROL_TRACKING_FROM_START_AND_FINISH_TIMES) String controlTrackingFromStartAndFinishTimesParam,
@FormParam("rankingMetric") @DefaultValue(DEFAULT_LG_RANKING_METRIC) String rankingMetricParam)
{
if(regattaName == null){
return isRequired("regattaName");
boolean isPersistent = Boolean.parseBoolean(isPersistentParam);
boolean controlTrackingFromStartAndFinishTimes = Boolean.parseBoolean(controlTrackingFromStartAndFinishTimesParam);
boolean useStartTimeInterference = Boolean.parseBoolean(useStartTimeInterferenceParam);
double buoyZoneRadiusInHullLengths = Double.parseDouble(buoyZoneRadiusInHullLengthsParam);
if (regattaName == null) {
return getParameterMissingResponse("regattaName");
}
if(boatClassNameParam == null){
return isRequired("boatClassName");
if (boatClassNameParam == null) {
return getParameterMissingResponse("boatClassName");
}
if(eventIdParam == null && courseAreaIdParam == null){
return atLeastOneParameterRequired(Arrays.asList("eventId", "courseAreaId"));
if (eventIdParam == null && courseAreaIdParam == null) {
return getMissingParametersResponse(Arrays.asList("eventId", "courseAreaId"));
}
UUID courseAreaId = null;
if (eventIdParam != null) {
UUID eventId = null;
Event event = null;
try {
eventId = UUID.fromString(eventIdParam);
} catch (IllegalArgumentException e) {
return invalidIdFormat(eventIdParam);
return getInvalidIdFormatResponse(eventIdParam);
}
try {
event = getService().getEvent(eventId);
} catch (IllegalArgumentException e) {
event = getService().getEvent(eventId);
if (event == null){
return getBadEventErrorResponse(eventIdParam);
}
CourseArea courseArea = StreamSupport.stream(event.getVenue().getCourseAreas().spliterator(), false)
.filter(c -> c.getName().equals("Default")).findFirst().orElse(null);
}
CourseArea courseArea = getDefaultCourseArea(event);
courseAreaId = courseArea != null ? courseArea.getId() : null;
}
if (courseAreaIdParam != null) {
try {
courseAreaId = UUID.fromString(courseAreaIdParam);
} catch (IllegalArgumentException e) {
return invalidIdFormat(courseAreaIdParam);
return getInvalidIdFormatResponse(courseAreaIdParam);
}
}
String boatClassName = null;
try{
try {
boatClassName = BoatClassMasterdata.valueOf(boatClassNameParam).name();
}
catch(IllegalArgumentException e){
} catch (IllegalArgumentException e) {
String correctValues = getEnumValuesAsString(BoatClassMasterdata.class);
return valueNotKnown(boatClassNameParam, correctValues);
return getValueUnknownResponse(boatClassNameParam, correctValues);
}
MillisecondsTimePoint startDate = null;
try{
startDate = startDateString != null ? new MillisecondsTimePoint(df.parse(startDateString)) : null;
try { startDate = startDateString != null ? new MillisecondsTimePoint(df.parse(startDateString)) : new MillisecondsTimePoint(new Date());;
} catch (ParseException e) {
return getInvalidDateFormatResponse(startDateString);
}
catch (ParseException e){
return invalidDateFormat(startDateString);
}
MillisecondsTimePoint endDate = null;
try{
endDate = endDateString != null ? new MillisecondsTimePoint(df.parse(endDateString)) : null;
try {
endDate = endDateString != null ? new MillisecondsTimePoint(df.parse(endDateString)) : new MillisecondsTimePoint(addOneWeek(startDate.asDate()));;
} catch (ParseException e) {
return getInvalidDateFormatResponse(endDateString);
}
catch (ParseException e){
return invalidDateFormat(endDateString);
}
ScoringScheme scoringScheme = null;
ScoringSchemeType type = null;
if (scoringSchemeParam != null) {
@@ -258,107 +305,113 @@ public class EventsResource extends AbstractSailingServerResource {
scoringScheme = getService().getBaseDomainFactory().createScoringScheme(type);
} catch (IllegalArgumentException e) {
String correctValues = getEnumValuesAsString(ScoringSchemeType.class);
return valueNotKnown(scoringSchemeParam, correctValues);
return getValueUnknownResponse(scoringSchemeParam, correctValues);
}
}
RankingMetrics rankingMetric = null;
if (rankingMetricParam != null) {
try {
rankingMetric = RankingMetrics.valueOf(rankingMetricParam);
} catch (IllegalArgumentException e) {
String correctValues = getEnumValuesAsString(RankingMetrics.class);
return valueNotKnown(rankingMetricParam, correctValues);
return getValueUnknownResponse(rankingMetricParam, correctValues);
}
}
UUID regattaId = UUID.randomUUID();
// using default seriesCreationParameters
RegattaCreationParametersDTO regattaCreationParametersDTO = new RegattaCreationParametersDTO(
createDefaultSeriesCreationParameters());
AddSpecificRegatta operation = new AddSpecificRegatta(regattaName, boatClassName, startDate, endDate,
regattaId, regattaCreationParametersDTO, persistent, scoringScheme, courseAreaId,
buoyZoneRadiusInHullLengths, useStartTimeInterference, controlTrackingFromStartAndFinishTimes,
rankingMetric);
AddSpecificRegatta operation = new AddSpecificRegatta(regattaName, boatClassName, startDate, endDate, regattaId,
regattaCreationParametersDTO, isPersistent, scoringScheme, courseAreaId, buoyZoneRadiusInHullLengths,
useStartTimeInterference, controlTrackingFromStartAndFinishTimes, rankingMetric);
Regatta regatta = getService().apply(operation);
addLeaderboard(regatta.getName(), new ArrayList<Integer>());
return ok(regatta.getId().toString(), MediaType.TEXT_PLAIN);
}
private CourseArea getDefaultCourseArea(Event event) {
return stream(event.getVenue().getCourseAreas().spliterator())
.filter(c -> c.getName().equals(DEFAULT_COURSE_AREA_NAME)).findFirst().orElse(null);
}
@POST
@Path("/addCourseArea")
@Consumes("text/plain")
@Produces("text/plain")
public Response addCourseArea(
@FormParam("eventId") @NotNull String eventIdParam,
@FormParam("courseAreaName") @DefaultValue("Default") @NotNull String courseAreaName) {
if(eventIdParam == null){
return isRequired("eventId");
public Response addCourseArea(@FormParam("eventId") String eventIdParam,
@FormParam("courseAreaName") @DefaultValue(DEFAULT_SERIES_NAME) String courseAreaName) {
if (eventIdParam == null) {
return getParameterMissingResponse("eventId");
}
if(courseAreaName == null){
return isRequired("courseAreaName");
if (courseAreaName == null) {
return getParameterMissingResponse("courseAreaName");
}
UUID eventId = null;
try {
eventId = UUID.fromString(eventIdParam);
} catch (IllegalArgumentException e) {
return invalidIdFormat(eventIdParam);
return getInvalidIdFormatResponse(eventIdParam);
}
String[] courseAreaNames = new String[]{ courseAreaName };
UUID[] courseAreaIds = new UUID[] {UUID.randomUUID()};
String[] courseAreaNames = new String[] { courseAreaName };
UUID[] courseAreaIds = new UUID[] { UUID.randomUUID() };
CourseArea[] courseAreas = null;
CourseArea result = null;
try{
try {
courseAreas = getService().apply(new AddCourseAreas(eventId, courseAreaNames, courseAreaIds));
result = courseAreas[0];
}
catch(IllegalArgumentException e){
} catch (IllegalArgumentException e) {
return getBadEventErrorResponse(eventId.toString());
}
return result != null ? ok(result.getId().toString(), MediaType.TEXT_PLAIN) : error("",Status.INTERNAL_SERVER_ERROR, MediaType.TEXT_PLAIN);
return ok(result.getId().toString(), MediaType.TEXT_PLAIN);
}
@POST
@Path("/addLeaderboard")
@Consumes("text/plain")
@Produces("text/plain")
public Response addLeaderboard(
@FormParam("regattaName") @NotNull String regattaName,
public Response addLeaderboard(@FormParam("regattaName") String regattaName,
@FormParam("discardThreshold") List<Integer> discardThresholdsParam) {
if(regattaName == null){
return isRequired("regattaName");
if (regattaName == null) {
return getParameterMissingResponse("regattaName");
}
if(getService().getRegatta(new RegattaName(regattaName)) == null){
return couldNotFind(Regatta.class.getSimpleName() , regattaName);
if (getService().getRegatta(new RegattaName(regattaName)) == null) {
return getBadObjectErrorResponse(Regatta.class.getSimpleName(), regattaName);
}
int[] discardThresholds = discardThresholdsParam != null ? discardThresholdsParam.stream().mapToInt(i->i).toArray() : null;
int[] discardThresholds = discardThresholdsParam != null
? discardThresholdsParam.stream().mapToInt(i -> i).toArray() : null;
final RegattaLeaderboard leaderboard = getService()
.apply(new CreateRegattaLeaderboard(new RegattaName(regattaName), regattaName, discardThresholds));
if(leaderboard == null){
return getObjectAlreadyExistsResponse(RegattaLeaderboard.class, regattaName);
}
// searching for default leadboardgroup of event
// how to do this easier?
LeaderboardGroup foundLg = null;
for(Event event : getService().getAllEvents()){
for (Event event : getService().getAllEvents()) {
Iterable<CourseArea> courseAreas = event.getVenue().getCourseAreas();
for(CourseArea courseArea : courseAreas){
if(courseArea.getId().equals(leaderboard.getRegatta().getDefaultCourseArea().getId())){
for(LeaderboardGroup lg : event.getLeaderboardGroups()){
for (CourseArea courseArea : courseAreas) {
if (courseArea.getId().equals(leaderboard.getRegatta().getDefaultCourseArea().getId())) {
for (LeaderboardGroup lg : event.getLeaderboardGroups()) {
// if default leaderboard
if(lg.getName().equals(event.getName())){
if (lg.getName().equals(event.getName())) {
foundLg = lg;
lg.addLeaderboard(leaderboard);
}
@@ -366,17 +419,19 @@ public class EventsResource extends AbstractSailingServerResource {
}
}
}
// check if there is a default leaderboard group
if(foundLg != null){
List<String> leaderboards = stream(foundLg.getLeaderboards().spliterator()).map(lg -> lg.getName()).collect(Collectors.toList());
// how to get overallLeaderboard[...] parameters?
getService().updateLeaderboardGroup(foundLg.getName(), foundLg.getName(), foundLg.getDescription(), foundLg.getDisplayName(), leaderboards, null, null);
if (foundLg != null) {
List<String> leaderboards = stream(foundLg.getLeaderboards().spliterator()).map(lg -> lg.getName())
.collect(Collectors.toList());
// how to get overallLeaderboard[...] parameters?
getService().updateLeaderboardGroup(foundLg.getName(), foundLg.getName(), foundLg.getDescription(),
foundLg.getDisplayName(), leaderboards, null, null);
}
return ok(leaderboard.getName(), MediaType.TEXT_PLAIN);
}
private <T> Stream<T> stream(Spliterator<T> spliterator) {
return StreamSupport.stream(spliterator, false);
}
@@ -389,93 +444,95 @@ public class EventsResource extends AbstractSailingServerResource {
@FormParam("leaderboardGroupName") String leaderboardGroupName,
@FormParam("leaderboardGroupDescription") String leaderboardGroupDescription,
@FormParam("leaderboardGroupDisplayName") String leaderboardGroupDisplayName,
@FormParam("displayGroupsInReverseOrder") boolean displayGroupsInReverseOrder,
@FormParam("displayGroupsInReverseOrder") String displayGroupsInReverseOrderParam,
@FormParam("leaderboardNames") List<String> leaderboardNames,
@FormParam("overallLeaderboardDiscardThresholds") List<Integer> overallLeaderboardDiscardThresholdsParam,
@FormParam("overallLeaderboardDiscardThresholds") List<Integer> overallLeaderboardDiscardThresholdsParam,
@FormParam("overallLeaderboardScoringSchemeType") String overallLeaderboardScoringSchemeTypeParam) {
if(eventIdParam == null){
return isRequired("eventId");
if (eventIdParam == null) {
return getParameterMissingResponse("eventId");
}
if(leaderboardGroupDescription == null){
if (leaderboardGroupDescription == null) {
leaderboardGroupDescription = leaderboardGroupName;
}
boolean displayGroupsInReverseOrder = Boolean.parseBoolean(displayGroupsInReverseOrderParam);
UUID eventId = null;
try {
eventId = UUID.fromString(eventIdParam);
} catch (IllegalArgumentException e) {
return invalidIdFormat(eventIdParam);
return getInvalidIdFormatResponse(eventIdParam);
}
Event event = getService().getEvent(eventId);
if(event == null){
if (event == null) {
return getBadEventErrorResponse(eventId.toString());
}
if(leaderboardGroupName == null){
if (leaderboardGroupName == null) {
leaderboardGroupName = event.getName();
}
ScoringSchemeType overallLeaderboardScoringSchemeType = null;
ScoringSchemeType overallLeaderboardScoringSchemeType = null;
if (overallLeaderboardScoringSchemeTypeParam != null) {
try {
overallLeaderboardScoringSchemeType = ScoringSchemeType.valueOf(overallLeaderboardScoringSchemeTypeParam);
overallLeaderboardScoringSchemeType = ScoringSchemeType
.valueOf(overallLeaderboardScoringSchemeTypeParam);
} catch (IllegalArgumentException e) {
String correctValues = getEnumValuesAsString(ScoringSchemeType.class);
return valueNotKnown(overallLeaderboardScoringSchemeTypeParam, correctValues);
return getValueUnknownResponse(overallLeaderboardScoringSchemeTypeParam, correctValues);
}
}
int[] overallLeaderboardDiscardThresholds = overallLeaderboardDiscardThresholdsParam != null ? overallLeaderboardDiscardThresholdsParam.stream().mapToInt(i->i).toArray() : null;
int[] overallLeaderboardDiscardThresholds = overallLeaderboardDiscardThresholdsParam != null
? overallLeaderboardDiscardThresholdsParam.stream().mapToInt(i -> i).toArray() : new int[0];
LeaderboardGroup leaderboardGroup = null;
try{
leaderboardGroup = getService()
.apply(new CreateLeaderboardGroup(leaderboardGroupName, leaderboardGroupDescription,
leaderboardGroupDisplayName, displayGroupsInReverseOrder, leaderboardNames, overallLeaderboardDiscardThresholds, overallLeaderboardScoringSchemeType));
}
catch(IllegalArgumentException e){
return leadeboardNotFound(leaderboardNames);
try {
leaderboardGroup = getService().apply(new CreateLeaderboardGroup(leaderboardGroupName,
leaderboardGroupDescription, leaderboardGroupDisplayName, displayGroupsInReverseOrder,
leaderboardNames, overallLeaderboardDiscardThresholds, overallLeaderboardScoringSchemeType));
} catch (IllegalArgumentException e) {
return getObjectAlreadyExistsResponse(Leaderboard.class, leaderboardGroupName);
}
List<UUID> newLeaderboardGroupIds = new ArrayList<>();
StreamSupport.stream(event.getLeaderboardGroups().spliterator(), false).forEach(lg -> newLeaderboardGroupIds.add(lg.getId()));
StreamSupport.stream(event.getLeaderboardGroups().spliterator(), false)
.forEach(lg -> newLeaderboardGroupIds.add(lg.getId()));
newLeaderboardGroupIds.add(leaderboardGroup.getId());
getService().updateEvent(event.getId(), event.getName(), event.getDescription(), event.getStartDate(), event.getEndDate(),
event.getVenue().getName(), event.isPublic(), newLeaderboardGroupIds, event.getOfficialWebsiteURL(), event.getBaseURL(),
event.getSailorsInfoWebsiteURLs(), event.getImages(), event.getVideos());
getService().updateEvent(event.getId(), event.getName(), event.getDescription(), event.getStartDate(),
event.getEndDate(), event.getVenue().getName(), event.isPublic(), newLeaderboardGroupIds,
event.getOfficialWebsiteURL(), event.getBaseURL(), event.getSailorsInfoWebsiteURLs(), event.getImages(),
event.getVideos());
return ok(leaderboardGroup.getId().toString(), MediaType.TEXT_PLAIN);
}
private <E extends Enum<E>> String getEnumValuesAsString(Class<E> e) {
return EnumSet.allOf(e).stream().map(en -> en.name()).collect(Collectors.joining(", "));
}
private LinkedHashMap<String, SeriesCreationParametersDTO> createDefaultSeriesCreationParameters() {
final LinkedHashMap<String, SeriesCreationParametersDTO> seriesCreationParameters = new LinkedHashMap<>();
seriesCreationParameters.put("Default", new SeriesCreationParametersDTO(
Arrays.asList(new FleetDTO("Default", 0, null)), false, false, false, false, null, false, null));
seriesCreationParameters.put(DEFAULT_SERIES_NAME, new SeriesCreationParametersDTO(
Arrays.asList(new FleetDTO(DEFAULT_FLEET_NAME, 0, null)), false, false, false, false, null, false, null));
return seriesCreationParameters;
}
private Response ok(String message, String mediaType) {
return Response.ok(message).header("Content-Type", mediaType + ";charset=UTF-8").build();
}
private Response error(String message, Status status, String mediaType) {
return Response.status(status).header("Content-Type", mediaType + ";charset=UTF-8").entity(message).build();
}
@GET
@Produces("application/json;charset=UTF-8")
public Response getEvents(@QueryParam("showNonPublic") String showNonPublic) {
// TODO bug2589, bug3504: the following will require EVENT:READ permission; it requires cross-server links to be authentication aware...
// TODO bug2589, bug3504: the following will require EVENT:READ permission; it requires cross-server links to be
// authentication aware...
// SecurityUtils.getSubject().checkPermission(Permission.EVENT.getStringPermission(Permission.Mode.READ));
JsonSerializer<EventBase> eventSerializer = new EventBaseJsonSerializer(new VenueJsonSerializer(new CourseAreaJsonSerializer()), new LeaderboardGroupBaseJsonSerializer());
JsonSerializer<EventBase> eventSerializer = new EventBaseJsonSerializer(
new VenueJsonSerializer(new CourseAreaJsonSerializer()), new LeaderboardGroupBaseJsonSerializer());
JSONArray result = new JSONArray();
for (EventBase event : getService().getAllEvents()) {
if ((showNonPublic != null && Boolean.valueOf(showNonPublic)) || event.isPublic()) {
@@ -490,8 +547,10 @@ public class EventsResource extends AbstractSailingServerResource {
@Produces("application/json;charset=UTF-8")
@Path("{eventId}")
public Response getEvent(@PathParam("eventId") String eventId) {
// TODO bug2589, bug3504: the following will require EVENT:READ permission; it requires cross-server links to be authentication aware...
// SecurityUtils.getSubject().checkPermission(Permission.EVENT.getStringPermissionForObjects(Permission.Mode.READ, eventId));
// TODO bug2589, bug3504: the following will require EVENT:READ permission; it requires cross-server links to be
// authentication aware...
// SecurityUtils.getSubject().checkPermission(Permission.EVENT.getStringPermissionForObjects(Permission.Mode.READ,
// eventId));
Response response;
UUID eventUuid;
try {
@@ -503,8 +562,8 @@ public class EventsResource extends AbstractSailingServerResource {
if (event == null) {
response = getBadEventErrorResponse(eventId);
} else {
JsonSerializer<EventBase> eventSerializer = new EventBaseJsonSerializer(new VenueJsonSerializer(
new CourseAreaJsonSerializer()), new LeaderboardGroupBaseJsonSerializer());
JsonSerializer<EventBase> eventSerializer = new EventBaseJsonSerializer(
new VenueJsonSerializer(new CourseAreaJsonSerializer()), new LeaderboardGroupBaseJsonSerializer());
JSONObject eventJson = eventSerializer.serialize(event);
String json = eventJson.toJSONString();
@@ -516,11 +575,15 @@ public class EventsResource extends AbstractSailingServerResource {
@GET
@Produces("application/json;charset=UTF-8")
@Path("{eventId}/racestates")
public Response getRaceStates(@PathParam("eventId") String eventId, @QueryParam("filterByLeaderboard") String filterByLeaderboard,
@QueryParam("filterByCourseArea") String filterByCourseArea, @QueryParam("filterByDayOffset") String filterByDayOffset,
public Response getRaceStates(@PathParam("eventId") String eventId,
@QueryParam("filterByLeaderboard") String filterByLeaderboard,
@QueryParam("filterByCourseArea") String filterByCourseArea,
@QueryParam("filterByDayOffset") String filterByDayOffset,
@QueryParam("clientTimeZoneOffsetInMinutes") Integer clientTimeZoneOffsetInMinutes) {
// TODO bug2589, bug3504: the following will require EVENT:READ permission; it requires cross-server links to be authentication aware...
// SecurityUtils.getSubject().checkPermission(Permission.EVENT.getStringPermissionForObjects(Permission.Mode.READ, eventId));
// TODO bug2589, bug3504: the following will require EVENT:READ permission; it requires cross-server links to be
// authentication aware...
// SecurityUtils.getSubject().checkPermission(Permission.EVENT.getStringPermissionForObjects(Permission.Mode.READ,
// eventId));
Response response;
UUID eventUuid;
try {
@@ -535,7 +598,7 @@ public class EventsResource extends AbstractSailingServerResource {
final Duration clientTimeZoneOffset;
if (filterByDayOffset != null) {
if (clientTimeZoneOffsetInMinutes != null) {
clientTimeZoneOffset = new MillisecondsDurationImpl(1000*60*clientTimeZoneOffsetInMinutes);
clientTimeZoneOffset = new MillisecondsDurationImpl(1000 * 60 * clientTimeZoneOffsetInMinutes);
} else {
clientTimeZoneOffset = Duration.NULL;
}
@@ -544,74 +607,77 @@ public class EventsResource extends AbstractSailingServerResource {
}
EventRaceStatesSerializer eventRaceStatesSerializer = new EventRaceStatesSerializer(filterByCourseArea,
filterByLeaderboard, filterByDayOffset, clientTimeZoneOffset, getService());
JSONObject raceStatesJson = eventRaceStatesSerializer.serialize(new Pair<Event, Iterable<Leaderboard>>(event, getService().getLeaderboards().values()));
JSONObject raceStatesJson = eventRaceStatesSerializer
.serialize(new Pair<Event, Iterable<Leaderboard>>(event, getService().getLeaderboards().values()));
String json = raceStatesJson.toJSONString();
response = Response.ok(json).header("Content-Type", MediaType.APPLICATION_JSON + ";charset=UTF-8").build();
}
return response;
}
private Map<Locale, URL> convertToLocalesAndUrls(Map<String, String> sailorsInfoWebsiteURLsByLocaleName) {
Map<Locale, URL> eventURLs = new HashMap<>();
for (Map.Entry<String, String> entry : sailorsInfoWebsiteURLsByLocaleName.entrySet()) {
if (entry.getValue() != null) {
try {
eventURLs.put(toLocale(entry.getKey()), new URL(entry.getValue()));
} catch(Exception e) {
// broken URLs or Locales are not being stored
}
}
}
return eventURLs;
}
private Locale toLocale(String localeName) {
if(localeName == null || localeName.isEmpty()) {
return null;
}
return Locale.forLanguageTag(localeName);
}
private Response getBadEventErrorResponse(String eventId) {
return Response.status(Status.NOT_FOUND).entity("Could not find an event with id '" + StringEscapeUtils.escapeHtml(eventId) + "'.").type(MediaType.TEXT_PLAIN).build();
return Response.status(Status.NOT_FOUND)
.entity("Could not find an event with id '" + StringEscapeUtils.escapeHtml(eventId) + "'.")
.type(MediaType.TEXT_PLAIN).build();
}
private Response couldNotFind(String className, String id) {
return Response.status(Status.NOT_FOUND).entity("Could not find object of type "+className+" with identifier '" + StringEscapeUtils.escapeHtml(id) + "'.").type(MediaType.TEXT_PLAIN).build();
private Response getBadObjectErrorResponse(String className, String id) {
return Response.status(Status.NOT_FOUND).entity("Could not find object of type " + className
+ " with identifier '" + StringEscapeUtils.escapeHtml(id) + "'.").type(MediaType.TEXT_PLAIN).build();
}
private Response alreadyExists(Object object, String id) {
return Response.status(Status.BAD_REQUEST).entity("The object of type "+object.getClass().getSimpleName()+" with id '" + StringEscapeUtils.escapeHtml(id) + "' already exists.").type(MediaType.TEXT_PLAIN).build();
private Response getObjectAlreadyExistsResponse(Object object, String id) {
return Response
.status(Status.BAD_REQUEST).entity("The object of type " + object.getClass().getSimpleName()
+ " with id '" + StringEscapeUtils.escapeHtml(id) + "' already exists.")
.type(MediaType.TEXT_PLAIN).build();
}
private Response isRequired(String parameter) {
return Response.status(Status.BAD_REQUEST).entity("The parameter "+StringEscapeUtils.escapeHtml(parameter)+" is required.").type(MediaType.TEXT_PLAIN).build();
private Response getParameterMissingResponse(String parameter) {
return Response.status(Status.BAD_REQUEST)
.entity("The parameter " + StringEscapeUtils.escapeHtml(parameter) + " is required.")
.type(MediaType.TEXT_PLAIN).build();
}
private Response invalidDateFormat(String date){
return Response.status(Status.BAD_REQUEST).entity("The date "+StringEscapeUtils.escapeHtml(date)+" does not follow the date pattern \"dd-MM-yyyy\".").type(MediaType.TEXT_PLAIN).build();
private Response getInvalidDateFormatResponse(String date) {
return Response.status(Status.BAD_REQUEST).entity(
"The date " + StringEscapeUtils.escapeHtml(date) + " does not follow the date pattern \"dd-MM-yyyy\".")
.type(MediaType.TEXT_PLAIN).build();
}
private Response invalidURLFormat(String url){
return Response.status(Status.BAD_REQUEST).entity("The format of the url "+StringEscapeUtils.escapeHtml(url)+" is incorrect.").type(MediaType.TEXT_PLAIN).build();
private Response getInvalidURLFormatResponse(String url) {
return Response.status(Status.BAD_REQUEST)
.entity("The format of the url " + StringEscapeUtils.escapeHtml(url) + " is incorrect.")
.type(MediaType.TEXT_PLAIN).build();
}
private Response invalidIdFormat(String id){
return Response.status(Status.BAD_REQUEST).entity("The format of the id "+StringEscapeUtils.escapeHtml(id)+" is incorrect.").type(MediaType.TEXT_PLAIN).build();
private Response getInvalidIdFormatResponse(String id) {
return Response.status(Status.BAD_REQUEST)
.entity("The format of the id " + StringEscapeUtils.escapeHtml(id) + " is incorrect.")
.type(MediaType.TEXT_PLAIN).build();
}
private Response valueNotKnown(String value, String correctValues){
return Response.status(Status.BAD_REQUEST).entity("The value \""+StringEscapeUtils.escapeHtml(value)+"\" is not recognized. Correct values are: "+correctValues).type(MediaType.TEXT_PLAIN).build();
private Response getValueUnknownResponse(String value, String correctValues) {
return Response.status(Status.BAD_REQUEST)
.entity("The value \"" + StringEscapeUtils.escapeHtml(value)
+ "\" is not recognized. Correct values are: " + correctValues)
.type(MediaType.TEXT_PLAIN).build();
}
private Response leadeboardNotFound(List<String> leaderboardNames){
return Response.status(Status.BAD_REQUEST).entity("One of the following leaderboards was not found: \""+StringEscapeUtils.escapeHtml(leaderboardNames.toString())+"\" was not found.").type(MediaType.TEXT_PLAIN).build();
private Response getLeaderboardNotFoundResponse(List<String> leaderboardNames) {
return Response.status(Status.BAD_REQUEST)
.entity("One of the following leaderboards was not found: \""
+ StringEscapeUtils.escapeHtml(leaderboardNames.toString()) + "\" was not found.")
.type(MediaType.TEXT_PLAIN).build();
}
private Response atLeastOneParameterRequired(List<String> parameters){
return Response.status(Status.BAD_REQUEST).entity("At least one of the following parameters must be provided: "+parameters.stream().collect(Collectors.joining(", "))+"\" was not found.").type(MediaType.TEXT_PLAIN).build();
private Response getMissingParametersResponse(List<String> parameters) {
return Response.status(Status.BAD_REQUEST)
.entity("At least one of the following parameters must be provided: "
+ parameters.stream().collect(Collectors.joining(", ")) + "\" was not found.")
.type(MediaType.TEXT_PLAIN).build();
}
private Date addOneWeek(Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
@@ -619,4 +685,3 @@ public class EventsResource extends AbstractSailingServerResource {
return c.getTime();
}
}