this.courseService = courseService;
}
@Override
public FeedSchools createFeedSchools(String contextPath, Provider provider, MediationService mediationService) throws Exception {
FeedSchools feed = (FeedSchools) this.DAO.createInstance(FeedSchools.class);
// TODO Get configuration object a load different languages
ArrayList<Locale> locales = new ArrayList<Locale>();
// TODO hard-coded languages
locales.add(new Locale("es"));
locales.add(new Locale("eu"));
// Starting xml generation
Element root = new Element(SCHOOL_ROOT);
root.setAttribute(new Attribute("noNamespaceSchemaLocation","http://hirubila.appspot.com/schema/zentroak-1.0.xsd",
Namespace.getNamespace("xsi","http://www.w3.org/1999/XMLSchema-instance")));
// NEW PART Get all schools. To do it, first we've to get all courses by mediationService and then get for each course its school.
HashMap<Long, ExtendedSchool> schools = new HashMap<Long, ExtendedSchool>();
Locale locale = locales.get(0);
//Provider provider = this.providerService.getProviderById(providerId, locale);
//MediationService mediation = this.mediationService.getMediationService(provider.getMediationService(), locale);
Collection<ExtendedCourse> courses = this.courseService.getCoursesByOwner(mediationService.getId(), null, locale);
for(ExtendedCourse course : courses) {
ExtendedSchool school = this.schoolService.getSchool(course.getSchool(), locale);
if(school != null) schools.put(school.getId(), school);
}
for(ExtendedSchool school : schools.values()) {
Element schoolNode = new Element(SCHOOL_ELEMENT);
for(String attr : SCHOOL_ATTRS) {
Element attrNode = new Element(attr);
if(SCHOOL_ATTR_ID.equals(attr)) {
//attrNode.setContent(school.getId() != null ? this.createCData(school.getId().toString()) : "");
attrNode.setContent(mediationService.getId() != null && school.getId() != null ? new CDATA(new StringBuffer(String.valueOf(mediationService.getId())).append("-").append(String.valueOf(school.getId())).toString()) : new CDATA(""));
} else if(SCHOOL_ATTR_NAME.equals(attr)) {
attrNode.setContent(school.getName() != null ? new CDATA(school.getName()) : new CDATA(""));
} else if(SCHOOL_ATTR_PHONE.equals(attr)) {
attrNode.addContent(school.getContactInfo() != null && school.getContactInfo().getTelephone() != null ?
new CDATA(school.getContactInfo().getTelephone()) : new CDATA(""));
} else if(SCHOOL_ATTR_FAX.equals(attr)) {
attrNode.addContent(school.getContactInfo() != null &&school.getContactInfo().getFax() != null ?
new CDATA(school.getContactInfo().getFax()) : new CDATA(""));
} else if(SCHOOL_ATTR_MAIL.equals(attr)) {
attrNode.addContent(school.getContactInfo() != null && school.getContactInfo().getEmail() != null ?
new CDATA(school.getContactInfo().getEmail()) : new CDATA(""));
} else if(SCHOOL_ATTR_ADDR.equals(attr)) {
attrNode.addContent(school.getContactInfo() != null && school.getContactInfo().getStreetAddress() != null ?
new CDATA(school.getContactInfo().getStreetAddress()) : new CDATA(""));
} else if(SCHOOL_ATTR_PK.equals(attr)) {
attrNode.addContent(school.getContactInfo() != null && school.getContactInfo().getZipCode() != null ?
new CDATA(school.getContactInfo().getZipCode()) : new CDATA(""));
} else if(SCHOOL_ATTR_WEB.equals(attr)) {
attrNode.addContent(school.getContactInfo() != null && school.getContactInfo().getWebSite() != null ?
new CDATA(school.getContactInfo().getWebSite()) : new CDATA(""));
} else if(SCHOOL_ATTR_CITY.equals(attr)) {
attrNode.addContent(school.getContactInfo() != null && school.getContactInfo().getCity() != null ?
new CDATA(school.getContactInfo().getCity()) : new CDATA(""));
} else if(SCHOOL_ATTR_FEED.equals(attr)) {
StringBuffer sb = new StringBuffer(contextPath).append("/").append(locale).append(COURSES_FEED).append(provider.getId()).append("/").append(school.getId());
attrNode.addContent(new CDATA(sb.toString()));
}
schoolNode.addContent(attrNode);
}
root.addContent(schoolNode);
}
Document document = new Document(root);
XMLOutputter outputter = new XMLOutputter();
String content = outputter.outputString(document);
feed.setContent(new Blob(content.getBytes("UTF-8")));
feed.setDate(new Date());
feed.setProviderId(provider.getId());
return feed;
}