public Supplemental parseSupplemental (Storage storage, Calendar calendar, XMLSupplemental xmlSupp) {
String suppId = calendar.getOid() + "-supp-" + xmlSupp.getId();
// Create a new supplemental or get the existing one from the calendar
Supplemental supplemental = new Supplemental();
supplemental.setId(suppId);
int index = -1;
if(calendar != null && calendar.getSupplementals() != null &&
(index = calendar.getSupplementals().indexOf(supplemental)) != -1) {
supplemental = calendar.getSupplementals().get(index);
} else {
supplemental.setSupplementalId(xmlSupp.getId());
}
// Set parent reference
supplemental.setCalendar(calendar);
// Set this as the current object in case of removal
setRemoveObject(supplemental, supplemental.getId());
// Set the supplemental calendar date from the parent calendar
if (xmlSupp.getCaldate()!=null) {
try {
Date calDate = OpenLegConstants.LRS_DATE_ONLY_FORMAT.parse(xmlSupp.getCaldate().getContent());
supplemental.setCalendarDate(calDate);
}
catch (ParseException e) {
logger.error("Unable to parse calDate for supplement=" + xmlSupp.getId(),e);
}
}
// Set the supplemental release date-time
if (xmlSupp.getReleasedate()!=null && xmlSupp.getReleasetime()!=null) {
try {
String dateString = xmlSupp.getReleasedate().getContent() + xmlSupp.getReleasetime().getContent();
Date releaseDateTime = OpenLegConstants.LRS_DATETIME_FORMAT.parse(dateString);
supplemental.setReleaseDateTime(releaseDateTime);
} catch (ParseException e) {
logger.error("Unable to parse relDate for supplement=" + xmlSupp.getId(),e);
}
}
if (xmlSupp.getSections()!=null) {
Section section = null;
// Get the existing set of sections if available.
if (supplemental.getSections() == null)
supplemental.setSections(new ArrayList<Section>());
List<Section> sections = supplemental.getSections();
// Parse the various sections from the supplied XML
// TODO: Since whole sections might be sent each time, are whole supplementals resent also?
for(XMLSection xmlSection:xmlSupp.getSections().getSection()) {
section = parseSection(storage, supplemental, xmlSection);
// Only add new sections
if(!sections.contains(section))
sections.add(section);
}
supplemental.setSections(sections);
}
// Handle sequences when available
XMLSequence xmlSequence = xmlSupp.getSequence();
if (xmlSequence != null) {
Sequence sequence = parseSequence(storage, supplemental, xmlSequence);
supplemental.addSequence(sequence);
// TODO: new removal objects? how/why?
setRemoveObject(sequence, sequence.getId());
}