Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package it.unitn.elisco.dao; import it.unitn.elisco.bean.hibernate.Answer; import it.unitn.elisco.bean.hibernate.Context; import it.unitn.elisco.bean.hibernate.Question; import it.unitn.elisco.bean.hibernate.Student; import it.unitn.elisco.utils.HibernateUtil; import java.util.List; import org.hibernate.Criteria; import org.hibernate.Session; import org.hibernate.Transaction; import org.hibernate.criterion.Projections; import org.hibernate.criterion.Restrictions; /** * * @author Andrea Marcolin */ public class AnswerDAO extends DAO<Answer, Long> { public void saveAnswer(Student student, Context context, String answer) { Session s = HibernateUtil.getSession(); Transaction tx = s.beginTransaction(); Answer a = findAnswer(s, student, context); if (a != null && a.getBody() == null) { a.setBody(answer); update(s, a); } tx.commit(); s.close(); } public Answer getAnswerByStudentAndContext(Student student, Context context) { Session s = HibernateUtil.getSession(); Transaction tx = s.beginTransaction(); Answer res = findAnswer(s, student, context); tx.commit(); s.close(); return res; } public Answer getAnswerById(Long id) { Session s = HibernateUtil.getSession(); Transaction tx = s.beginTransaction(); Answer res = getByPrimarykey(s, Answer.class, id); tx.commit(); s.close(); return res; } public boolean completedAnswerTask(Student student, Context context) { Session s = HibernateUtil.getSession(); Transaction tx = s.beginTransaction(); Answer answer = findAnswer(s, student, context); boolean res = (answer != null && answer.getBody() != null && !answer.getBody().equals("")); tx.commit(); s.close(); return res; } // Only visible to other DAOs void assignQuestion(Session s, Student student, Context context, Question question) { insertAnswer(s, student, context, question); } List<Question> getDistinctApprovedQuestionsFromAnswers(Context context) { Session s = HibernateUtil.getSession(); Transaction tx = s.beginTransaction(); List<Question> res = s.createCriteria(Answer.class).add(Restrictions.eq("context", context)) .setProjection(Projections.distinct(Projections.property("question"))).list(); tx.commit(); s.close(); return res; } List<Answer> getAnswersForQuestion(Question question) { Session s = HibernateUtil.getSession(); Transaction tx = s.beginTransaction(); List<Answer> res = s.createCriteria(Answer.class).add(Restrictions.eq("question", question)).list(); tx.commit(); s.close(); return res; } // Auxiliary functions private Answer findAnswer(Session s, Student student, Context context) { Criteria criteria = s.createCriteria(Answer.class); return (Answer) criteria.add(Restrictions.eq("student", student)).add(Restrictions.eq("context", context)) .uniqueResult(); } private void insertAnswer(Session s, Student student, Context context, Question question) { Answer a = new Answer(); a.setStudent(student); a.setContext(context); a.setQuestion(question); insert(s, a); } }