Bug 5085: Introduced several convenience methods for CourseTemplate and

CourseConfigurationBase to remove code duplications
This commit is contained in:
Steffen Schaefer committed 2019-11-18 11:58:04 +01:00
1 parent ed616f2354
commit c2ecd6b68b
9 files changed
+71 -37

No files matched your search

@@ -2064,10 +2064,9 @@ public class MongoObjectFactoryImpl implements MongoObjectFactory {
// store mark template list including role names for those who have one defined
final BasicDBList markTemplates = new BasicDBList();
courseTemplate.getMarkTemplates().forEach(m -> {
courseTemplate.getMarkTemplatesWithOptionalRoles().forEach((m, role) -> {
final BasicDBObject markTemplateObject = new BasicDBObject(
FieldNames.COURSE_TEMPLATE_MARK_TEMPLATE_ID.name(), m.getId().toString());
final MarkRole role = courseTemplate.getAssociatedRoles().get(m);
if (role != null) {
markTemplateObject.append(FieldNames.COURSE_TEMPLATE_MARK_TEMPLATE_ROLE.name(), role.getId().toString());
}
@@ -53,6 +53,8 @@ public interface CourseConfigurationBase<R extends IsMarkRole> extends WithOptio
*/
Map<MarkConfiguration, R> getAssociatedRoles();
Map<MarkConfiguration, R> getAllMarksWithOptionalRoles();
/**
* A {@link CourseConfigurationBase} having a {@link RepeatablePart} can optionally also specify a number of laps.
* Depending on the use-case this can be omitted (saving a course template) or is required (creating a regatta
@@ -46,6 +46,8 @@ public interface CourseTemplate extends WithOptionalRepeatablePart, NamedWithUUI
*/
Iterable<MarkTemplate> getMarkTemplates();
MarkTemplate getMarkTemplateByIdIfContainedInCourseTemplate(UUID markTemplateId);
Iterable<MarkTemplate> getMarkTemplatesInWaypoints();
/**
@@ -71,6 +73,12 @@ public interface CourseTemplate extends WithOptionalRepeatablePart, NamedWithUUI
Map<MarkTemplate, MarkRole> getAssociatedRoles();
Map<MarkTemplate, MarkRole> getMarkTemplatesWithOptionalRoles();
MarkRole getOptionalAssociatedRole(MarkTemplate markTemplate);
MarkRole getMarkRoleByIdIfContainedInCourseTemplate(UUID markRoleId);
URL getOptionalImageURL();
Integer getDefaultNumberOfLaps();
@@ -3,6 +3,7 @@ package com.sap.sailing.domain.coursetemplate.impl;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import com.sap.sailing.domain.coursetemplate.CourseConfigurationBase;
import com.sap.sailing.domain.coursetemplate.CourseTemplate;
@@ -62,6 +63,11 @@ public abstract class CourseConfigurationBaseImpl<R extends IsMarkRole> implemen
public Map<MarkConfiguration, R> getAssociatedRoles() {
return associatedRoles;
}
@Override
public Map<MarkConfiguration, R> getAllMarksWithOptionalRoles() {
return markConfigurations.stream().collect(Collectors.toMap(mc -> mc, associatedRoles::get));
}
@Override
public Integer getNumberOfLaps() {
@@ -7,6 +7,7 @@ import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;
import com.sap.sailing.domain.coursetemplate.CourseTemplate;
import com.sap.sailing.domain.coursetemplate.MarkRole;
@@ -19,7 +20,7 @@ import com.sap.sse.common.impl.NamedWithUUIDImpl;
public class CourseTemplateImpl extends NamedWithUUIDImpl implements CourseTemplate {
private static final long serialVersionUID = -183875832585632806L;
private final Iterable<MarkTemplate> marks;
private final Set<MarkTemplate> marks;
private final ArrayList<WaypointTemplate> waypoints;
@@ -53,11 +54,10 @@ public class CourseTemplateImpl extends NamedWithUUIDImpl implements CourseTempl
if (optionalRepeatablePart != null) {
optionalRepeatablePart.validateRepeatablePartForSequence(waypoints);
}
final Set<MarkTemplate> theMarks = new HashSet<>();
Util.addAll(marks, theMarks);
this.waypoints = new ArrayList<>();
Util.addAll(waypoints, this.waypoints);
this.marks = theMarks;
this.marks = new HashSet<>();
Util.addAll(marks, this.marks);
this.optionalImageURL = optionalImageURL;
this.optionalRepeatablePart = optionalRepeatablePart;
this.associatedRoles = new HashMap<>(associatedRoles);
@@ -83,6 +83,18 @@ public class CourseTemplateImpl extends NamedWithUUIDImpl implements CourseTempl
public Iterable<MarkTemplate> getMarkTemplates() {
return marks;
}
@Override
public MarkTemplate getMarkTemplateByIdIfContainedInCourseTemplate(UUID markTemplateId) {
MarkTemplate result = null;
for (MarkTemplate markTemplate : marks) {
if (markTemplate.getId().equals(markTemplateId)) {
result = markTemplate;
break;
}
}
return result;
}
@Override
public Iterable<WaypointTemplate> getWaypointTemplates() {
@@ -115,6 +127,28 @@ public class CourseTemplateImpl extends NamedWithUUIDImpl implements CourseTempl
return associatedRoles;
}
@Override
public Map<MarkTemplate, MarkRole> getMarkTemplatesWithOptionalRoles() {
return marks.stream().collect(Collectors.toMap(mc -> mc, associatedRoles::get));
}
@Override
public MarkRole getOptionalAssociatedRole(MarkTemplate markTemplate) {
return associatedRoles.get(markTemplate);
}
@Override
public MarkRole getMarkRoleByIdIfContainedInCourseTemplate(UUID markRoleId) {
MarkRole result = null;
for (MarkRole markRole : associatedRoles.values()) {
if (markRole.getId().equals(markRoleId)) {
result = markRole;
break;
}
}
return result;
}
public void setAssociatedRoles(Map<MarkTemplate, MarkRole> associatedRoles) {
this.associatedRoles.clear();
this.associatedRoles.putAll(associatedRoles);
@@ -124,12 +124,8 @@ public class CourseConfigurationBuilder {
private MarkTemplate resolveMarkTemplateByID(UUID markTemplateID) {
MarkTemplate resolvedMarkTemplate = null;
if (optionalCourseTemplate != null) {
for (MarkTemplate markTemplate : optionalCourseTemplate.getMarkTemplates()) {
if (markTemplate.getId().equals(markTemplateID)) {
resolvedMarkTemplate = markTemplate;
break;
}
}
resolvedMarkTemplate = optionalCourseTemplate
.getMarkTemplateByIdIfContainedInCourseTemplate(markTemplateID);
}
if (resolvedMarkTemplate == null) {
resolvedMarkTemplate = sharedSailingData.getMarkTemplateById(markTemplateID);
@@ -140,12 +136,7 @@ public class CourseConfigurationBuilder {
private MarkRole resolveMarkRoleByID(UUID markRoleID) {
MarkRole resolvedMarkRole = null;
if (optionalCourseTemplate != null) {
for (MarkRole markRole : optionalCourseTemplate.getAssociatedRoles().values()) {
if (markRole.getId().equals(markRoleID)) {
resolvedMarkRole = markRole;
break;
}
}
resolvedMarkRole = optionalCourseTemplate.getMarkRoleByIdIfContainedInCourseTemplate(markRoleID);
}
if (resolvedMarkRole == null) {
resolvedMarkRole = sharedSailingData.getMarkRoleById(markRoleID);
@@ -69,7 +69,8 @@ public class CourseConfigurationJsonSerializer implements JsonSerializer<CourseC
}
final Map<MarkConfiguration, UUID> markConfigurationsToTempIdMap = new HashMap<>();
final JSONArray markConfigurationsJSON = new JSONArray();
for (final MarkConfiguration markConfiguration : courseConfiguration.getAllMarks()) {
for (Map.Entry<MarkConfiguration, IsMarkRole> markWithOptionalRole : courseConfiguration.getAllMarksWithOptionalRoles().entrySet()) {
final MarkConfiguration markConfiguration = markWithOptionalRole.getKey();
JSONObject markConfigurationsEntry = new JSONObject();
final UUID markConfigurationId = UUID.randomUUID();
markConfigurationsEntry.put(FIELD_MARK_CONFIGURATION_ID, markConfigurationId.toString());
@@ -77,7 +78,7 @@ public class CourseConfigurationJsonSerializer implements JsonSerializer<CourseC
markConfigurationsEntry.put(FIELD_MARK_CONFIGURATION_MARK_TEMPLATE_ID,
markConfiguration.getOptionalMarkTemplate().getId().toString());
}
final IsMarkRole associatedRole = courseConfiguration.getAssociatedRoles().get(markConfiguration);
final IsMarkRole associatedRole = markWithOptionalRole.getValue();
if (associatedRole != null) {
markConfigurationsEntry.put(FIELD_MARK_CONFIGURATION_ASSOCIATED_ROLE, associatedRole.getName());
if (associatedRole instanceof MarkRole) {
@@ -4,7 +4,6 @@ import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import com.sap.sailing.domain.coursetemplate.CourseTemplate;
import com.sap.sailing.domain.coursetemplate.MarkRole;
import com.sap.sailing.domain.coursetemplate.MarkTemplate;
import com.sap.sailing.domain.coursetemplate.RepeatablePart;
import com.sap.sailing.server.gateway.serialization.JsonSerializer;
@@ -46,9 +45,8 @@ public class CourseTemplateJsonSerializer implements JsonSerializer<CourseTempla
result.put(FIELD_TAGS, tags);
final JSONArray allMarkTemplates = new JSONArray();
courseTemplate.getMarkTemplates().forEach(mt -> {
courseTemplate.getMarkTemplatesWithOptionalRoles().forEach((mt, associatedRoleOrNull) -> {
final JSONObject markTemplateEntry = markTemplateJsonSerializer.serialize(mt);
final MarkRole associatedRoleOrNull = courseTemplate.getAssociatedRoles().get(mt);
if (associatedRoleOrNull != null) {
markTemplateEntry.put(FIELD_ASSOCIATED_ROLE_ID, associatedRoleOrNull.getId().toString());
}
@@ -221,14 +221,14 @@ public class CourseAndMarkConfigurationFactoryImpl implements CourseAndMarkConfi
if (markRoleId == null) {
return null;
}
MarkRole markRole = null;
if (optionalCourseTemplate != null) {
for (MarkRole markRoleInCourseTemplate : optionalCourseTemplate.getAssociatedRoles().values()) {
if (markRoleInCourseTemplate.getId().equals(markRoleId)) {
return markRoleInCourseTemplate;
}
}
markRole = optionalCourseTemplate.getMarkRoleByIdIfContainedInCourseTemplate(markRoleId);
}
return sharedSailingData.getMarkRoleById(markRoleId);
if (markRole == null) {
markRole = sharedSailingData.getMarkRoleById(markRoleId);
}
return markRole;
}
@Override
@@ -735,7 +735,7 @@ public class CourseAndMarkConfigurationFactoryImpl implements CourseAndMarkConfi
markTemplate, null, /* storeToInventory */ false);
markTemplatesToMarkConfigurations.put(markTemplate, markConfiguration);
resultingRoleMapping.put(markConfiguration,
courseTemplateOrNull.getAssociatedRoles().get(markTemplate));
courseTemplateOrNull.getOptionalAssociatedRole(markTemplate));
}
replaceTemplateBasedConfigurationCandidatesBySuggestedProperties(
markTemplatesToMarkConfigurations, allMarkConfigurations, tagsToFilterMarkProperties,
@@ -842,7 +842,7 @@ public class CourseAndMarkConfigurationFactoryImpl implements CourseAndMarkConfi
while (markTemplateIterator.hasNext()) {
final MarkTemplate markTemplate = markTemplateIterator.next();
final Mark mark = markIterator.next();
final MarkRole roleForMarkTempalte = courseTemplate.getAssociatedRoles().get(markTemplate);
final MarkRole roleForMarkTempalte = courseTemplate.getOptionalAssociatedRole(markTemplate);
if (roleForMarkTempalte == null) {
validCourseTemplateUsage = false;
break;
@@ -956,12 +956,7 @@ public class CourseAndMarkConfigurationFactoryImpl implements CourseAndMarkConfi
private MarkTemplate resolveMarkTemplateByID(CourseTemplate courseTemplate, UUID markTemplateID) {
MarkTemplate resolvedMarkTemplate = null;
if (courseTemplate != null) {
for (MarkTemplate markTemplate : courseTemplate.getMarkTemplates()) {
if (markTemplate.getId().equals(markTemplateID)) {
resolvedMarkTemplate = markTemplate;
break;
}
}
resolvedMarkTemplate = courseTemplate.getMarkTemplateByIdIfContainedInCourseTemplate(markTemplateID);
}
if (resolvedMarkTemplate == null) {
try {