int currentMetadataId = 1;
HashMap<Integer, Stage> stageMetadata = new HashMap<Integer, Stage>();
HashMap<Integer, Show> showMetadata = new HashMap<Integer, Show>();
HashMap<Show, Integer> changeMapping = new HashMap<Show, Integer>();
XmlSerializer ser = new MXSerializer();
ser.setOutput(os, "UTF-8");
ser.setProperty(
"http://xmlpull.org/v1/doc/properties.html#serializer-indentation",
"\t");
ser.startDocument("UTF-8", true);
ser.startTag(NS, "event");
attribute(ser, "xmlns", "http://michal.linhard.sk/openair/event");
attribute(ser, "uri", e.getUri());
attribute(ser, "name", e.getName());
attribute(ser, "shortName", e.getShortName());
attribute(ser, "version", e.getVersion());
attribute(ser, "versionTime",
Util.formatDateTime(e.getVersionTime()));
ser.startTag(NS, "program");
for (Stage eachStage : e) {
ser.startTag(NS, "stage");
attribute(ser, "name", eachStage.getName());
attribute(ser, "shortName", eachStage.getShortName());
if (eachStage.getMetadata() != null) {
attribute(ser, "id", "st" + currentMetadataId);
stageMetadata.put(currentMetadataId, eachStage);
currentMetadataId++;
}
for (DayProgram eachDay : eachStage) {
ser.startTag(NS, "day");
attribute(ser, "date",
Util.formatDate(eachDay.getDayStart()));
for (Show eachShow : eachDay) {
ser.startTag(NS, "show");
attribute(ser, "start",
Util.formatDateTime(eachShow.getStart()));
attribute(ser, "duration",
Util.formatDuration(eachShow.getDuration()));
attribute(ser, "name", eachShow.getName());
attribute(ser, "shortName", eachShow.getShortName());
if (eachShow.isCancelled()) {
attribute(ser, "cancelled", Boolean.TRUE);
}
Integer showID = changeMapping.get(eachShow);
if (showID == null
&& (eachShow.getMetadata() != null || eachShow
.isMoved())) {
showID = currentMetadataId;
currentMetadataId++;
}
if (showID != null) {
attribute(ser, "id", "sh" + showID);
if (eachShow.getMetadata() != null) {
showMetadata.put(showID, eachShow);
}
if (eachShow.isMoved()) {
if (eachShow.isOldVersion()) {
changeMapping.put(eachShow.getNewVersion(),
showID);
attribute(ser, "oldVersion", true);
} else {
changeMapping.put(eachShow.getOldVersion(),
showID);
}
}
}
ser.endTag(NS, "show");
}
ser.endTag(NS, "day");
}
ser.endTag(NS, "stage");
}
ser.endTag(NS, "program");
ser.startTag(NS, "metadata");
if (e.getMetadata() != null) {
ser.startTag(NS, "event");
attribute(ser, "url", e.getUrl());
attribute(ser, "description", e.getDescription());
ser.endTag(NS, "event");
}
for (Entry<Integer, Stage> stageEntry : stageMetadata.entrySet()) {
ser.startTag(NS, "stage");
attribute(ser, "id", "st" + stageEntry.getKey());
attribute(ser, "url", stageEntry.getValue().getUrl());
attribute(ser, "description", stageEntry.getValue()
.getDescription());
ser.endTag(NS, "stage");
}
for (Entry<Integer, Show> showEntry : showMetadata.entrySet()) {
ser.startTag(NS, "show");
attribute(ser, "id", "sh" + showEntry.getKey());
attribute(ser, "url", showEntry.getValue().getUrl());
attribute(ser, "description", showEntry.getValue()
.getDescription());
ser.endTag(NS, "show");
}
ser.endTag(NS, "metadata");
ser.endTag(NS, "event");
ser.endDocument();
ser.flush();
} catch (IllegalArgumentException e1) {
throw new RuntimeException(e1);
} catch (IllegalStateException e1) {
throw new RuntimeException(e1);
} catch (IOException e1) {