XMLDocument newdoc = new XMLDocument(createDocument(),"");
//if there are more than one schedule nodes
Node previousSchedule = null;
// HashMap<String,Node> schedules = new HashMap<String,Node>();
for(int i = 0; i < telecasts.size(); i++) {
//check size of references
ArrayList<String> inforefs = telecasts.get(i).getInformationDocumentReferences();
ArrayList<String> locrefs = telecasts.get(i).getLocationDocumentReferences();
//just if both lists aren't empty we can extract data out of locationtable
//and extract the programinformation out of the informationtable
if(inforefs.size() != 0 && locrefs.size() != 0) {
//initialize root and parent nodes
if(newdoc.getRoot() == null) {
newdoc.initializeNodes(false, true, false);
}
//for all location references - normally 1
for(int j = 0; j < locrefs.size(); j++) {
//get location document
XMLDocument doc = memory.getDocument(locrefs.get(j));
//get the scheduleevent for this telecast and the parent schedule for that node
Node scheduleEvent = doc.getXPathExpression("//ScheduleEvent[Program/@crid=\""+telecasts.get(i).getCrid()+"\"]").item(0);
Node schedule = doc.getXPathExpression("//ScheduleEvent[Program/@crid=\""+telecasts.get(i).getCrid()+"\"]/..").item(0);
//import it and create a new schedule (otherwise it stores scheduleevents two times)
Node cloneScheduleEvent = newdoc.getDocument().importNode(scheduleEvent, true);
Element cloneSchedule = newdoc.getDocument().createElement("Schedule");
//add attributes
// String key ="";
for(int x = 0; x < schedule.getAttributes().getLength(); x++) {
cloneSchedule.setAttribute(schedule.getAttributes().item(x).getNodeName(), schedule.getAttributes().item(x).getTextContent());
// key += schedule.getAttributes().item(x).getTextContent();
}
// schedules.put(key, cloneSchedule);
//set programurl to scheduleEvent
for(int k = 0; k < cloneScheduleEvent.getChildNodes().getLength(); k++) {
if(cloneScheduleEvent.getChildNodes().item(k).getNodeName().equals("ProgramURL")) {
cloneScheduleEvent.getChildNodes().item(k).setTextContent(telecasts.get(i).getProgramURL_to_telecast());
break;
}
}
//initialize
if(previousSchedule == null)
previousSchedule = cloneSchedule;
//if a new schedule node occurs, append the old one to the table
//set the new one
if(! ( ((Element)cloneSchedule).getAttribute("serviceIDRef").equals(((Element)previousSchedule).getAttribute("serviceIDRef")) ) ) {
newdoc.appendChild("loc",previousSchedule);
previousSchedule = cloneSchedule;
}
previousSchedule.appendChild(cloneScheduleEvent);
}
}
//else don't add it to list
}
if(newdoc.getRoot() != null) {
newdoc.appendChild("loc",previousSchedule);
newdoc.appendChild("root",null);
String ret = newdoc.getXMLContent();
return Response.status(200).entity(ret).build();
}
else
return new ResponsePage("Sorry maybe there is no programinformation available, or no dates were found.",true).buildResponse(404);
}