@Override
public FeedCourses createFeedCourses(String contextPath, Provider provider,
MediationService mediationService, ExtendedSchool school, Collection<Locale> locales) throws Exception {
FeedCourses feed = (FeedCourses) this.DAO.createInstance(FeedCourses.class);
// Starting xml generation
Element root = new Element(COURSE_ROOT);
root.setAttribute(new Attribute("noNamespaceSchemaLocation","http://hirubila.appspot.com/schema/kurtsoak-1.0.xsd",
Namespace.getNamespace("xsi","http://www.w3.org/1999/XMLSchema-instance")));
Collection<ExtendedCourse> courses = this.courseService.getCoursesBySchoolByMediation(school.getId(),
mediationService.getId(), "title", null);
// DATE format
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
for(ExtendedCourse course : courses) {
// Make a map with languages and course with this language
HashMap<String, ExtendedCourse> i18nCourse = new HashMap<String, ExtendedCourse>();
for(Locale locale : locales) {
i18nCourse.put(locale.getLanguage(), this.courseService.getCourse(course.getId(), locale));
}
// Make course node element with i18n properties
Element courseNode = new Element(COURSE_ELEMENT);
for(String attr : COURSE_ATTRS) {
if(COURSE_ATTR_ID.equals(attr)) {
courseNode.setContent(new Element(attr).addContent(mediationService.getId() != null && course.getId() != null ? new CDATA(new StringBuffer(String.valueOf(mediationService.getId())).append("-").append(String.valueOf(course.getId())).toString()) : new CDATA("")));
} else if(COURSE_ATTR_NAME.equals(attr)) { // I18nProperty
Iterator<String> it = i18nCourse.keySet().iterator();
while(it.hasNext()) {
String language = it.next();
String i18nAttr = new StringBuffer(attr).append(language).toString();
ExtendedCourse c = i18nCourse.get(language);
courseNode.addContent(new Element(i18nAttr).setContent(c.getTitle() != null ? new CDATA(c.getTitle()) : new CDATA("")));
}
} else if(COURSE_ATTR_URL.equals(attr)) { // I18nProperty // TODO may be better a function...
Iterator<String> it = i18nCourse.keySet().iterator();
while(it.hasNext()) {
String language = it.next();
String i18nAttr = new StringBuffer(attr).append(language).toString();
ExtendedCourse c = i18nCourse.get(language);
String url = new StringBuffer(contextPath).append("/").append(language).append(COURSE_DETAIL_URL).append(course.getId()).toString();
courseNode.addContent(new Element(i18nAttr).setContent(new CDATA(url)));
}
} else if(COURSE_ATTR_START.equals(attr)) {
courseNode.addContent(new Element(attr).setContent(course.getStart() != null ? new CDATA(dateFormat.format(course.getStart())) : new CDATA("")));
} else if(COURSE_ATTR_END.equals(attr)) {
courseNode.addContent(new Element(attr).setContent(course.getEnd() != null ? new CDATA(dateFormat.format(course.getEnd())) : new CDATA("")));
} else if(COURSE_ATTR_INFO.equals(attr)) { // I18nProperty // TODO may be better a function...
Iterator<String> it = i18nCourse.keySet().iterator();
while(it.hasNext()) {
String language = it.next();
String i18nAttr = new StringBuffer(attr).append(language).toString();
ExtendedCourse c = i18nCourse.get(language);
courseNode.addContent(new Element(i18nAttr).setContent(c.getInformation() != null ? new CDATA(c.getInformation().getValue()) : new CDATA("")));
}
} else if(COURSE_ATTR_GAIAK.equals(attr)) {
Element tagsNode = new Element(attr);
for(Category category : course.getTags()) {
Element tagNode = new Element(COURSE_ATTR_GAIA);
for(String gaiaAttr : GAIA_ATTRS) {
Element tagAttrNode = new Element(gaiaAttr);
if(GAIA_ATTR_ID.equals(gaiaAttr)) {
tagAttrNode.setText("");
} else if(GAIA_ATTR_NAME.equals(gaiaAttr)) {
tagAttrNode.setContent(new CDATA(category.getCategory()));
}
tagNode.addContent(tagAttrNode);
}
tagsNode.addContent(tagNode);
}
courseNode.addContent(tagsNode);
}
}
root.addContent(courseNode);
}
Document document = new Document(root);
XMLOutputter outputter = new XMLOutputter();
String content = outputter.outputString(document);
feed.setContent(new Blob(content.getBytes("UTF-8")));
feed.setSchool(school.getId());
feed.setDate(new Date());
feed.setProviderId(provider.getId());
return feed;
}