dao.FacultyDAO.java Source code

Java tutorial

Introduction

Here is the source code for dao.FacultyDAO.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 dao;

import service.MailServiceBean;
import java.io.Serializable;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.Stateless;
import model.Course;
import model.Customer;
import model.Faculty;
import model.Section;
;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import utils.HibernateUtil;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import utils.HibernateUtil;

/**
 *
 * @author FrancisAerol
 */


@Stateless
public class FacultyDAO implements IFacultyDAO {

    private SessionFactory sessionFactory = HibernateUtil.getSessionFactory();

    @Override
    public Faculty getFaculty(Integer idFaculty) {
        Faculty obj;
        Session sess = sessionFactory.openSession();
        Transaction tx = sess.getTransaction();
        try {
            //set transaction timeout to 3 seconds
            tx.setTimeout(3);
            tx.begin();
            obj = (Faculty) sess.get(Faculty.class, idFaculty);

            tx.commit();
        } catch (Exception e) {
            tx.rollback();
            throw e; // or display error message
        } finally {
            sess.close();
        }

        return obj;
    }

    @Override
    public Faculty getAdviser() {
        Faculty o;
        Session sess = sessionFactory.openSession();
        Transaction tx = sess.getTransaction();
        try {
            //set transaction timeout to 3 seconds
            tx.setTimeout(3);
            tx.begin();
            
            String sql = "Select f, count(c.idCustomer) as cnt from "
                + "Faculty f left join f.customerList c group by f.idFaculty order by cnt ASC";
            Query sqlQuery = sess.createQuery(sql);
            List<Object[]> fList = sqlQuery.list();
            
            if (fList.size()==0) return null;

            o = (Faculty) fList.get(0)[0];

            tx.commit();
        } catch (Exception e) {
            tx.rollback();
            throw e; // or display error message
        } finally {
            sess.close();
        }

        return o;
    }

    /*
     * Faculty Module 
     */
    @Override
    public List<Customer> getFacultyCustomer(Faculty faculty) {
        List<Customer> obj;
        Session sess = sessionFactory.openSession();
        Transaction tx = sess.getTransaction();
        try {
            //set transaction timeout to 3 seconds
            tx.setTimeout(3);
            tx.begin();
            Query query = sess.createQuery("FROM Customer where advisor = :fa");
            query.setEntity("fa", faculty);
            obj = query.list();
            tx.commit();
        } catch (Exception e) {
            tx.rollback();
            throw e; // or display error message
        } finally {
            sess.close();
        }
        return obj;
    }

    @Override
    public List<Section> getSections(Faculty faculty) {
        List<Section> obj;
        Session sess = sessionFactory.openSession();
        Transaction tx = sess.getTransaction();
        try {
            //set transaction timeout to 3 seconds
            tx.setTimeout(3);
            tx.begin();
            Query query = sess.createQuery("FROM Section where faculty =:fa");
            query.setEntity("fa", faculty);
            obj = query.list();
            tx.commit();
        } catch (Exception e) {
            tx.rollback();
            throw e; // or display error message
        } finally {
            sess.close();
        }
        return obj;
    }
    
    @Override
    public void addFaculty(Faculty faculty) {
        Session sess = sessionFactory.openSession();
        Transaction tx = sess.getTransaction();
        try {
            //set transaction timeout to 3 seconds
            tx.setTimeout(3);
            tx.begin();
            sess.save(faculty);
            tx.commit();
        } catch (Exception e) {
            Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, e);
            tx.rollback();
            throw e; // or display error message
        } finally {
            sess.close();
        }
    }

    @Override
    public void updateFaculty(Faculty faculty) {
        Session sess = sessionFactory.openSession();
        Transaction tx = sess.getTransaction();
        try {
            //set transaction timeout to 3 seconds
            tx.setTimeout(3);
            tx.begin();
            Faculty c = (Faculty) sess.get(Faculty.class,faculty.getIdFaculty());
            c.setFirstName(faculty.getFirstName());
            c.setLastName(faculty.getLastName());
            c.setMiddleName(faculty.getMiddleName());
            c.setUserName(faculty.getUserName());
            c.setPassword(faculty.getPassword());
            c.setGender(faculty.getGender());
            tx.commit();
        } catch (Exception e) {
            tx.rollback();
            throw e; // or display error message
        } finally {
            sess.close();
        }
        
    }

    @Override
    public void deleteFaculty(Integer idFaculty) {
        Session sess = sessionFactory.openSession();
        Transaction tx = sess.getTransaction();
        try {
            //set transaction timeout to 3 seconds
            tx.setTimeout(3);
            tx.begin();
            Faculty c = (Faculty) sess.get(Faculty.class,idFaculty);
            sess.delete(c);
            tx.commit();
        } catch (Exception e) {
            tx.rollback();
            throw e; // or display error message
        } finally {
            sess.close();
        }
    }
    @Override
    public List<Faculty> getAllFaculties(){
        List<Faculty> obj;
        Session sess = sessionFactory.openSession();
        Transaction tx = sess.getTransaction();
        try {
            //set transaction timeout to 3 seconds
            tx.setTimeout(3);
            tx.begin();
            
            Query queryResult = sess.createQuery("from Faculty"); 
            obj = queryResult.list();
            tx.commit();
        } catch (Exception e) {
            tx.rollback();
            throw e; // or display error message
        } finally {
            sess.close();
        }
        return obj;
    }
}