package de.kopis.jusenet.utils;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Hibernate;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import de.kopis.jusenet.nntp.Article;
import de.kopis.jusenet.nntp.Group;
public class HibernateUtils {
private static final SessionFactory sessionFactory;
static {
try {
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch(Throwable ex) {
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
public static List<Group> listGroups() {
Session session = getSessionFactory().getCurrentSession();
session.beginTransaction();
Query q = session.createQuery("from Group");
List result = q.list();
session.getTransaction().commit();
return result;
}
public static List listServers() {
Session session = getSessionFactory().getCurrentSession();
session.beginTransaction();
Query q = session.createQuery("from Server");
List result = q.list();
session.getTransaction().commit();
return result;
}
public static List listAllArticles() {
Session session = getSessionFactory().getCurrentSession();
session.beginTransaction();
Query q = session.createQuery("from Article");
List result = q.list();
session.getTransaction().commit();
return result;
}
/**
* Adds a group to the current session.
*
* @param group
*/
public static void addGroup(Group group) {
Session session = getSessionFactory().getCurrentSession();
session.beginTransaction();
session.saveOrUpdate(group);
session.getTransaction().commit();
}
/**
* Adds an article to Group g and updates the links Group - Article.
*
* @param g Group to update with Article
* @param article Article to insert
*/
public static void addArticle(Group g, Article article) {
Session session = getSessionFactory().getCurrentSession();
session.beginTransaction();
g = (Group) session.createQuery("from Group where id=" + g.getId()).uniqueResult();
g.getArticles().add(article);
article.getGroups().add(g);
session.saveOrUpdate(article);
session.saveOrUpdate(g);
session.getTransaction().commit();
}
public static Group getGroup(String groupname) {
Session session = getSessionFactory().getCurrentSession();
session.beginTransaction();
Query q = session.createQuery("from Group where name='" + groupname + "'");
List result = q.list();
session.getTransaction().commit();
//TODO check type of result
return (Group) result.get(0);
}
/**
* Marks all articles in one group read.
*
* @param group
* @param read <code>true</code> if all articles read, else <code>false</code>
*/
public static void markRead(Group group, boolean read) {
Session session = getSessionFactory().getCurrentSession();
session.beginTransaction();
Iterator<Article> it = group.getArticles().iterator();
Article a;
while(it.hasNext()) {
a = it.next();
a.setRead(read);
session.saveOrUpdate(a);
}
session.getTransaction().commit();
}
/**
* Marks a group as subscribed.
*
* @param group
* @param subscribed
*
* @see de.kopis.jusenet.nntp.NNTPUtils#subscribe(Group)
*/
public static void subscribe(Group group, boolean subscribed) {
Session session = getSessionFactory().getCurrentSession();
session.beginTransaction();
group.setSubscribed(subscribed);
session.saveOrUpdate(group);
session.getTransaction().commit();
}
/**
* Removes Article <code>a</code> from database.
*
* @param a Article to remove
*/
public static void remove(Article a) {
Session session = getSessionFactory().getCurrentSession();
session.beginTransaction();
Iterator<Group> it = a.getGroups().iterator();
Group g;
while(it.hasNext()) {
g = it.next();
g.getArticles().remove(a);
session.saveOrUpdate(g);
}
session.delete(a);
session.getTransaction().commit();
}
public static void markRead(Article a, boolean read) {
Session session = getSessionFactory().getCurrentSession();
session.beginTransaction();
a.setRead(read);
session.saveOrUpdate(a);
session.getTransaction().commit();
}
}