com.muslim.family.dao.impl.AnswerDAOImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.muslim.family.dao.impl.AnswerDAOImpl.java

Source

/*
 * 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 com.muslim.family.dao.impl;

import com.muslim.family.dao.inft.AnswerDAO;
import com.muslim.family.hib.entity.Answer_tbl;
import java.io.IOException;
import java.sql.Blob;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.hibernate.Criteria;
import org.hibernate.Hibernate;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

/**
 *
 * @author Tariq
 */
@Repository
@Transactional
public class AnswerDAOImpl implements AnswerDAO {

    private SessionFactory sessionFactory;

    public AnswerDAOImpl() {
    }

    @Autowired
    public AnswerDAOImpl(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    @Override
    public void submitAnswerDao(Answer_tbl ans) {
        sessionFactory.getCurrentSession().save(ans);
    }

    public boolean isAnswerAvailableDao(long id) {

        Query qry = sessionFactory.getCurrentSession()
                .createQuery("Select ans.id from Answer_tbl ans where ans.question.id" + id);
        int size = qry.getFetchSize();
        if (size > 0)
            return true;
        else
            return false;

    }

    public Answer_tbl getAnswerByQuestionIdDao(long questionId) {

        Query qry = sessionFactory.getCurrentSession()
                .createQuery("from Answer_tbl ans where ans.question.id = :questionId");
        qry.setLong("questionId", questionId);
        List<Answer_tbl> answerList = (List<Answer_tbl>) qry.list();
        Answer_tbl answer = null;

        if (answerList.isEmpty())
            return null;
        else
            return answerList.get(0);
    }

    public void saveOrUpdateAudioDao(MultipartFile audioFile, Answer_tbl answer) {

        try {

            Session session = sessionFactory.getCurrentSession();
            Blob audioMessage = Hibernate.getLobCreator(session).createBlob(audioFile.getBytes());
            answer.setAudioMessage(audioMessage);
            session.saveOrUpdate(answer);

        } catch (IOException ex) {
            Logger.getLogger(AnswerDAOImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void saveOrUpdateShaikhTextDao(Answer_tbl answer) {

        Session session = sessionFactory.getCurrentSession();
        session.saveOrUpdate(answer);

    }

    @Override
    public Answer_tbl getAnswerByIdDao(long id) {
        Criteria cr = sessionFactory.getCurrentSession().createCriteria(Answer_tbl.class);
        cr.createAlias("question", "qs").add(Restrictions.eq("qs.id", id)).list();

        if (cr.list().isEmpty())
            return null;
        else
            return (Answer_tbl) cr.list().get(0);

        //        Object result = cr.uniqueResult();
        //            if (result != null) {
        //                return (Answer_tbl) result;
        //            }
        //        return null;
    }

}