@Override
public TemplateModel get(final String key) throws TemplateModelException {
if ("published_posts".equals(key)) {
List<ODocument> query = db.query(new OSQLSynchQuery<ODocument>("select * from post where status='published' order by date desc"));
return new SimpleSequence(DocumentList.wrap(query.iterator()));
}
if ("published_pages".equals(key)) {
List<ODocument> query = db.query(new OSQLSynchQuery<ODocument>("select * from page where status='published' order by date desc"));
return new SimpleSequence(DocumentList.wrap(query.iterator()));
}
if ("published_content".equals(key)) {
List<ODocument> publishedContent = new ArrayList<ODocument>();
String[] documentTypes = DocumentTypes.getDocumentTypes();
for (String docType : documentTypes) {
List<ODocument> query = db.query(new OSQLSynchQuery<ODocument>("select * from "+docType+" where status='published' order by date desc"));
publishedContent.addAll(query);
}
return new SimpleSequence(DocumentList.wrap(publishedContent.iterator()));
}
if ("all_content".equals(key)) {
List<ODocument> allContent = new ArrayList<ODocument>();
String[] documentTypes = DocumentTypes.getDocumentTypes();
for (String docType : documentTypes) {
List<ODocument> query = db.query(new OSQLSynchQuery<ODocument>("select * from "+docType+" order by date desc"));
allContent.addAll(query);
}
return new SimpleSequence(DocumentList.wrap(allContent.iterator()));
}
if ("alltags".equals(key)) {
List<ODocument> query = db.query(new OSQLSynchQuery<ODocument>("select tags from post where status='published'"));
Set<String> result = new HashSet<String>();
for (ODocument document : query) {
String[] tags = DBUtil.toStringArray(document.field("tags"));
Collections.addAll(result, tags);
}
return new SimpleCollection(result);
}
String[] documentTypes = DocumentTypes.getDocumentTypes();
for (String docType : documentTypes) {
if ((docType+"s").equals(key)) {
return new SimpleSequence(DocumentList.wrap(DBUtil.query(db, "select * from "+docType+" order by date desc").iterator()));
}
}
if ("tag_posts".equals(key)) {
String tag = eagerModel.get("tag").toString();
// fetch the tag posts from db
List<ODocument> query = DBUtil.query(db, "select * from post where status='published' where ? in tags order by date desc", tag);
return new SimpleSequence(DocumentList.wrap(query.iterator()));
}
if ("published_date".equals(key)) {
return new SimpleDate(new Date(), TemplateDateModel.UNKNOWN);
}
return eagerModel.get(key);