com.alogient.cameleon.sdk.community.blog.dao.impl.BlogDaoImpl Maven / Gradle / Ivy
The newest version!
package com.alogient.cameleon.sdk.community.blog.dao.impl;
import com.alogient.cameleon.sdk.common.dao.impl.BaseDao;
import com.alogient.cameleon.sdk.community.blog.dao.BlogDao;
import com.alogient.cameleon.sdk.community.blog.dao.model.Blog;
import com.alogient.cameleon.sdk.community.blog.dao.model.BlogArticle;
import com.alogient.cameleon.sdk.community.blog.dao.model.BlogArticleComment;
import com.alogient.cameleon.sdk.community.blog.dao.model.BlogArticleCulture;
import com.alogient.cameleon.sdk.community.blog.dao.model.BlogTag;
import org.hibernate.Criteria;
import org.hibernate.SQLQuery;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import java.sql.Date;
import java.util.Collection;
public class BlogDaoImpl extends BaseDao implements BlogDao {
/**
* {@inheritDoc}
*/
@Override
public Blog getBlog(Integer blogId) {
return (Blog) getHibernateTemplate().get(Blog.class, blogId);
}
@Override
public int getNumberOfArticles(Integer blogId) {
String query = "SELECT count(*) from article where BlogId = ?";
SQLQuery sqlQuery = getCurrentSession().createSQLQuery(query);
sqlQuery.setInteger(0, blogId);
return ((Number) sqlQuery.uniqueResult()).intValue();
}
@Override
public BlogTag getTagById(Integer tagId) {
return (BlogTag) getHibernateTemplate().get(BlogTag.class, tagId);
}
@SuppressWarnings({"unchecked"})
@Override
public Collection getTags() {
Criteria criteria = getCurrentSession().createCriteria(BlogTag.class);
criteria.addOrder(Order.asc("name"));
return (Collection) criteria.list();
}
@Override
public BlogArticleCulture getArticleCulture(Integer articleId) {
return (BlogArticleCulture) getHibernateTemplate().get(BlogArticleCulture.class, articleId);
}
@SuppressWarnings({"unchecked"})
@Override
public Collection getArticles(Integer blogId, Integer firstResult, Integer maxResult, Long startDate) {
Criteria criteria = getCurrentSession().createCriteria(BlogArticle.class);
// Set the maximum of article to retrieve
if (maxResult != ALL_RESULTS) {
criteria.setMaxResults(maxResult);
}
criteria.setFirstResult(firstResult);
criteria.add(Restrictions.eq("blog.id", blogId));
if (startDate != null) {
criteria.add(Restrictions.gt("creationDate", new Date(startDate)));
}
criteria.addOrder(Order.desc("creationDate"));
return (Collection) criteria.list();
}
@Override
public void saveBlogArticleCulture(BlogArticleCulture blogArticleCulture) {
getHibernateTemplate().saveOrUpdate(blogArticleCulture);
}
@Override
public void saveArticle(BlogArticle blogArticle) {
getHibernateTemplate().saveOrUpdate(blogArticle);
}
@Override
public void deleteArticle(Integer articleId) {
// Retrieve the article
BlogArticleCulture culture = getArticleCulture(articleId);
String query = "DELETE FROM ARTICLE where id = ?";
SQLQuery sqlQuery = getCurrentSession().createSQLQuery(query);
sqlQuery.setInteger(0, culture.getBlogArticle().getId());
sqlQuery.executeUpdate();
}
@Override
public void saveComment(BlogArticleComment blogArticleComment) {
getHibernateTemplate().saveOrUpdate(blogArticleComment);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy