* @param blog the owning Blog instance
* @return a Collection of Category instances
* @throws PersistenceException if categories cannot be loaded
*/
public Category getCategories(Blog blog) throws PersistenceException {
CategoryBuilder categoryBuilder = new CategoryBuilder(blog);
File source = new File(blog.getRoot(), CATEGORIES_FILE_NAME);
if (source.exists()) {
try {
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
JAXBElement<CategoriesType> controller = (JAXBElement)unmarshaller.unmarshal(source);
CategoriesType categoriesType = controller.getValue();
for (CategoryType categoryType : categoriesType.getCategory()) {
Category category = new Category(categoryType.getId(), categoryType.getName());
category.setBlog(blog);
category.setTags(categoryType.getTags());
categoryBuilder.addCategory(category);
}
} catch (Exception e) {
log.error(e.getMessage(), e);
e.printStackTrace();
throw new PersistenceException(e.getMessage());
}
}
return categoryBuilder.getRootCategory();
}