Refactored RaceData class
#story[778]
This commit is contained in:
@@ -30,18 +30,10 @@ public class RaceData {
|
|||||||
Element raceElement;
|
Element raceElement;
|
||||||
|
|
||||||
private OffsetDateTime creationTimeDate;
|
private OffsetDateTime creationTimeDate;
|
||||||
private OffsetDateTime raceStartTime;
|
|
||||||
private List<BoatInRace> participants;
|
|
||||||
private List<Marker> course;
|
|
||||||
private List<GPSCoordinate> courseLimit;
|
|
||||||
|
|
||||||
public RaceData(RaceDataSource dataSource) {
|
public RaceData(RaceDataSource dataSource) {
|
||||||
this.dataSource = dataSource;
|
this.dataSource = dataSource;
|
||||||
creationTimeDate = OffsetDateTime.now();
|
creationTimeDate = OffsetDateTime.now();
|
||||||
raceStartTime = OffsetDateTime.now().plusMinutes(3);
|
|
||||||
participants = dataSource.getBoats();
|
|
||||||
course = dataSource.getMarkers();
|
|
||||||
courseLimit = dataSource.getBoundary();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public String createXML() {
|
public String createXML() {
|
||||||
@@ -56,86 +48,7 @@ public class RaceData {
|
|||||||
raceElement = doc.createElement("Race");
|
raceElement = doc.createElement("Race");
|
||||||
doc.appendChild(raceElement);
|
doc.appendChild(raceElement);
|
||||||
|
|
||||||
appendRaceId();
|
appendChildElements();
|
||||||
appendRaceType();
|
|
||||||
|
|
||||||
Element creationTimeElement = doc.createElement("CreationTimeDate");
|
|
||||||
creationTimeElement.appendChild(doc.createTextNode(creationTimeDate.toString()));
|
|
||||||
raceElement.appendChild(creationTimeElement);
|
|
||||||
|
|
||||||
Element startTimeElement = doc.createElement("RaceStartTime");
|
|
||||||
startTimeElement.setAttribute("Time", raceStartTime.toString());
|
|
||||||
startTimeElement.setAttribute("Postpone", "false");
|
|
||||||
raceElement.appendChild(startTimeElement);
|
|
||||||
|
|
||||||
|
|
||||||
Element participantsElement = doc.createElement("Participants");
|
|
||||||
|
|
||||||
for (BoatInRace boat : participants) {
|
|
||||||
Element yachtElement = doc.createElement("Yacht");
|
|
||||||
yachtElement.setAttribute("SourceID", boat.getSourceID() + "");
|
|
||||||
participantsElement.appendChild(yachtElement);
|
|
||||||
}
|
|
||||||
|
|
||||||
raceElement.appendChild(participantsElement);
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Element courseElement = doc.createElement("Course");
|
|
||||||
Element compoundMarkSeqElement = doc.createElement("CompoundMarkSequence");
|
|
||||||
|
|
||||||
int i = 1;
|
|
||||||
for (Marker marker : course) {
|
|
||||||
Element compoundMarkElement = doc.createElement("CompoundMark");
|
|
||||||
compoundMarkElement.setAttribute("CompoundMarkId", i + "");
|
|
||||||
compoundMarkElement.setAttribute("Name", marker.getName());
|
|
||||||
|
|
||||||
Element mark1 = doc.createElement("Mark");
|
|
||||||
mark1.setAttribute("TargetLat", marker.getMark1().getLatitude() + "");
|
|
||||||
mark1.setAttribute("TargetLng", marker.getMark1().getLongitude() + "");
|
|
||||||
|
|
||||||
compoundMarkElement.appendChild(mark1);
|
|
||||||
|
|
||||||
if (!(marker.getMark1().equals(marker.getMark2()))) {
|
|
||||||
Element mark2 = doc.createElement("Mark");
|
|
||||||
mark2.setAttribute("TargetLat", marker.getMark2().getLatitude() + "");
|
|
||||||
mark2.setAttribute("TargetLng", marker.getMark2().getLongitude() + "");
|
|
||||||
compoundMarkElement.appendChild(mark2);
|
|
||||||
}
|
|
||||||
|
|
||||||
courseElement.appendChild(compoundMarkElement);
|
|
||||||
|
|
||||||
|
|
||||||
Element cornerElement = doc.createElement("Corner");
|
|
||||||
cornerElement.setAttribute("SeqID", i + "");
|
|
||||||
cornerElement.setAttribute("CompoundMarkID", i + "");
|
|
||||||
|
|
||||||
compoundMarkSeqElement.appendChild(cornerElement);
|
|
||||||
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
|
|
||||||
raceElement.appendChild(compoundMarkSeqElement);
|
|
||||||
raceElement.appendChild(courseElement);
|
|
||||||
|
|
||||||
|
|
||||||
int j = 1;
|
|
||||||
|
|
||||||
Element courseLimitElement = doc.createElement("CourseLimit");
|
|
||||||
for (GPSCoordinate coordinate : courseLimit) {
|
|
||||||
Element limitElement = doc.createElement("Limit");
|
|
||||||
limitElement.setAttribute("SeqID", j + "");
|
|
||||||
|
|
||||||
limitElement.setAttribute("Lat", coordinate.getLatitude() + "");
|
|
||||||
limitElement.setAttribute("Lon", coordinate.getLongitude() + "");
|
|
||||||
|
|
||||||
courseLimitElement.appendChild(limitElement);
|
|
||||||
|
|
||||||
j++;
|
|
||||||
}
|
|
||||||
|
|
||||||
raceElement.appendChild(courseLimitElement);
|
|
||||||
|
|
||||||
|
|
||||||
// write the content into xml file
|
// write the content into xml file
|
||||||
TransformerFactory transformerFactory = TransformerFactory.newInstance();
|
TransformerFactory transformerFactory = TransformerFactory.newInstance();
|
||||||
@@ -149,7 +62,6 @@ public class RaceData {
|
|||||||
|
|
||||||
return stringWriter.toString();
|
return stringWriter.toString();
|
||||||
|
|
||||||
|
|
||||||
} catch (ParserConfigurationException pce) {
|
} catch (ParserConfigurationException pce) {
|
||||||
throw new InvalidRaceDataException();
|
throw new InvalidRaceDataException();
|
||||||
} catch (TransformerException tfe) {
|
} catch (TransformerException tfe) {
|
||||||
@@ -157,6 +69,16 @@ public class RaceData {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void appendChildElements() {
|
||||||
|
appendRaceId();
|
||||||
|
appendRaceType();
|
||||||
|
appendCreationTimeDate();
|
||||||
|
appendRaceStartTime();
|
||||||
|
appendParticipants();
|
||||||
|
appendCourse();
|
||||||
|
appendCourseLimit();
|
||||||
|
}
|
||||||
|
|
||||||
private void appendRaceId() {
|
private void appendRaceId() {
|
||||||
Element raceIdElement = doc.createElement("RaceID");
|
Element raceIdElement = doc.createElement("RaceID");
|
||||||
raceIdElement.appendChild(doc.createTextNode(dataSource.getRaceId()));
|
raceIdElement.appendChild(doc.createTextNode(dataSource.getRaceId()));
|
||||||
@@ -169,6 +91,94 @@ public class RaceData {
|
|||||||
raceElement.appendChild(raceTypeElement);
|
raceElement.appendChild(raceTypeElement);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void appendCreationTimeDate() {
|
||||||
|
Element creationTimeElement = doc.createElement("CreationTimeDate");
|
||||||
|
creationTimeElement.appendChild(doc.createTextNode(creationTimeDate.toString()));
|
||||||
|
raceElement.appendChild(creationTimeElement);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void appendRaceStartTime() {
|
||||||
|
Element startTimeElement = doc.createElement("RaceStartTime");
|
||||||
|
startTimeElement.setAttribute("Time", (creationTimeDate.plusMinutes(3)).toString());
|
||||||
|
startTimeElement.setAttribute("Postpone", "false");
|
||||||
|
raceElement.appendChild(startTimeElement);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void appendParticipants() {
|
||||||
|
Element participantsElement = doc.createElement("Participants");
|
||||||
|
|
||||||
|
for (BoatInRace boat : dataSource.getBoats()) {
|
||||||
|
Element yachtElement = doc.createElement("Yacht");
|
||||||
|
yachtElement.setAttribute("SourceID", boat.getSourceID() + "");
|
||||||
|
participantsElement.appendChild(yachtElement);
|
||||||
|
}
|
||||||
|
raceElement.appendChild(participantsElement);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void appendCourse() {
|
||||||
|
Element courseElement = doc.createElement("Course");
|
||||||
|
Element compoundMarkSeqElement = doc.createElement("CompoundMarkSequence");
|
||||||
|
|
||||||
|
int i = 1;
|
||||||
|
for (Marker marker : dataSource.getMarkers()) {
|
||||||
|
|
||||||
|
courseElement.appendChild(createCompoundMarker(marker, i));
|
||||||
|
compoundMarkSeqElement.appendChild(createCornerElement(i));
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
|
||||||
|
raceElement.appendChild(compoundMarkSeqElement);
|
||||||
|
raceElement.appendChild(courseElement);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private Element createMark(GPSCoordinate marker) {
|
||||||
|
Element mark = doc.createElement("Mark");
|
||||||
|
mark.setAttribute("TargetLat", marker.getLatitude() + "");
|
||||||
|
mark.setAttribute("TargetLng", marker.getLongitude() + "");
|
||||||
|
return mark;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Element createCompoundMarker(Marker marker, int i) {
|
||||||
|
Element compoundMarkElement = doc.createElement("CompoundMark");
|
||||||
|
compoundMarkElement.setAttribute("CompoundMarkId", i + "");
|
||||||
|
compoundMarkElement.setAttribute("Name", marker.getName());
|
||||||
|
|
||||||
|
compoundMarkElement.appendChild(createMark(marker.getMark1()));
|
||||||
|
|
||||||
|
if (!(marker.getMark1().equals(marker.getMark2()))) {
|
||||||
|
compoundMarkElement.appendChild(createMark(marker.getMark2()));
|
||||||
|
}
|
||||||
|
|
||||||
|
return compoundMarkElement;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Element createCornerElement(int i) {
|
||||||
|
Element cornerElement = doc.createElement("Corner");
|
||||||
|
cornerElement.setAttribute("SeqID", i + "");
|
||||||
|
cornerElement.setAttribute("CompoundMarkID", i + "");
|
||||||
|
|
||||||
|
return cornerElement;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void appendCourseLimit() {
|
||||||
|
int j = 1;
|
||||||
|
|
||||||
|
Element courseLimitElement = doc.createElement("CourseLimit");
|
||||||
|
for (GPSCoordinate coordinate : dataSource.getBoundary()) {
|
||||||
|
Element limitElement = doc.createElement("Limit");
|
||||||
|
limitElement.setAttribute("SeqID", j + "");
|
||||||
|
|
||||||
|
limitElement.setAttribute("Lat", coordinate.getLatitude() + "");
|
||||||
|
limitElement.setAttribute("Lon", coordinate.getLongitude() + "");
|
||||||
|
|
||||||
|
courseLimitElement.appendChild(limitElement);
|
||||||
|
|
||||||
|
j++;
|
||||||
|
}
|
||||||
|
|
||||||
|
raceElement.appendChild(courseLimitElement);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -81,6 +81,4 @@ public class RaceDataTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user