com.registryKit.program.programDAO.java Source code

Java tutorial

Introduction

Here is the source code for com.registryKit.program.programDAO.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.registryKit.program;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.springframework.stereotype.Repository;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;

/**
 *
 * @author chadmccue
 */
@Repository
public class programDAO {

    @Autowired
    private SessionFactory sessionFactory;

    /**
     * The 'getProgramById' function will return a programManager based on the id passed in
     * 
     * @param programId        The id to search for
     * 
     * @return This function will return a programManager object
     */
    public program getProgramById(Integer programId) throws Exception {
        return (program) sessionFactory.getCurrentSession().get(program.class, programId);
    }

    /**
     * The 'getProgramModuleById' function will return a module object based on the id passed in
     * 
     * @param moduleId        The id to search for
     * 
     * @return This function will return a modules object
     */
    public modules getProgramModuleById(Integer moduleId) throws Exception {
        return (modules) sessionFactory.getCurrentSession().get(modules.class, moduleId);
    }

    /**
     * The 'getProgramsByAdministratory' function will return a list of programs associated to the program admin
     * logging in.
     * 
     * @param userId    The id of the program admin.
     * @return
     * @throws Exception 
     */
    public List<program> getProgramsByAdminisrator(Integer userId) throws Exception {

        Query query = sessionFactory.getCurrentSession()
                .createQuery("from programAdmin where systemUserId = :systemUserId");
        query.setParameter("systemUserId", userId);

        List<programAdmin> adminProgramList = query.list();
        List<Integer> programIds = null;

        if (!adminProgramList.isEmpty()) {
            programIds = new CopyOnWriteArrayList<Integer>();

            for (programAdmin admin : adminProgramList) {
                programIds.add(admin.getProgramId());
            }
        }

        List<program> programs = null;

        if (!programIds.isEmpty()) {

            Criteria criteria = sessionFactory.getCurrentSession().createCriteria(program.class);
            criteria.add(Restrictions.in("id", programIds));

            programs = criteria.list();

        }

        return programs;

    }

}