com.registryKit.hierarchy.serviceDAO.java Source code

Java tutorial

Introduction

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

import java.util.List;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.transform.Transformers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

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

    @Autowired
    private SessionFactory sessionFactory;

    /**
     * The 'getProgramServiceCategories' function will return a list of services for the passed in programId.
     * 
     * @param programId The selected program id.
     * @return
     * @throws Exception 
     */
    public List<programServices> getProgramServices(Integer programId, Integer providerId) throws Exception {

        String sql = "select id, programId, status, serviceName, dateCreated from program_services where programId = "
                + programId
                + " and status = 1 and id not in (select serviceId from providerServices where programId = "
                + programId + " and providerId = " + providerId + ") order by serviceName asc";

        Query query = sessionFactory.getCurrentSession().createSQLQuery(sql)
                .setResultTransformer(Transformers.aliasToBean(programServices.class));

        List<programServices> serviceList = query.list();

        return serviceList;

    }

    /**
     * The 'getProviderServices' function will return a list of services associated to the provider.
     * 
     * @param programId The selected program id.
     * @return
     * @throws Exception 
     */
    public List<programServices> getProviderServices(Integer programId, Integer providerId) throws Exception {

        String sql = "select id, programId, status, serviceName, dateCreated from program_services where programId = "
                + programId + " and status = 1 and id in (select serviceId from providerServices where programId = "
                + programId + " and providerId = " + providerId + ") order by serviceName asc";

        Query query = sessionFactory.getCurrentSession().createSQLQuery(sql)
                .setResultTransformer(Transformers.aliasToBean(programServices.class));

        List<programServices> serviceList = query.list();

        return serviceList;

    }

    /**
     * The 'saveProviderService' function will save the service to the provider.
     * 
     * @param service   The object holding the providerService
     * @throws Exception 
     */
    public void saveProviderService(providerServices service) throws Exception {
        sessionFactory.getCurrentSession().save(service);
    }

    /**
     * The 'removeProviderService' function will remove the passed in service for the passed in provider.
     * 
     * @param programId The selected program
     * @param serviceId The selected service
     * @param providerId    The selected provider
     * @throws Exception 
     */
    public void removeProviderService(Integer programId, Integer serviceId, Integer providerId) throws Exception {
        //Remove the user provider service
        Query removeService = sessionFactory.getCurrentSession().createQuery(
                "delete from providerServices where programId = :programId and serviceId = :serviceId and providerId = :providerId");
        removeService.setParameter("programId", programId);
        removeService.setParameter("serviceId", serviceId);
        removeService.setParameter("providerId", providerId);
        removeService.executeUpdate();
    }

}