com.registryKit.hierarchy.hierarchyDAO.java Source code

Java tutorial

Introduction

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

    @Autowired
    private SessionFactory sessionFactory;

    /**
     * The 'getProgramOrgHierarchy' function will return a list of organization hierarchy entries for the selected
     * program.
     * 
     * @param programId The id of the selected program.
     * @return This function will return a list of organization hierarchy objects.
     * @throws Exception 
     */
    public List<programOrgHierarchy> getProgramOrgHierarchy(Integer programId) {
        Query query = sessionFactory.getCurrentSession()
                .createQuery("from programOrgHierarchy where programId = :programId order by dspPos asc");
        query.setParameter("programId", programId);

        List<programOrgHierarchy> orgList = query.list();
        return orgList;
    }

    /**
     * The 'getOrgHierarchyById' function will return the details of the selected organization hierarchy.
     * 
     * @param id The id of the selected organization hierarchy.
     * @return  This function will return a single organization hierarchy object.
     * @throws Exception 
     */
    public programOrgHierarchy getOrgHierarchyById(Integer id) throws Exception {
        return (programOrgHierarchy) sessionFactory.getCurrentSession().get(programOrgHierarchy.class, id);
    }

    /**
    * The 'saveOrgHierarchyItem' function will save/update the passed in organization hierarchy object.
    * 
    * @param hierarchyDetails  The programOrgHierarchy object containing the form fields.
    * @throws Exception 
    */
    public void saveOrgHierarchyItem(programHierarchyDetails hierarchyDetails) throws Exception {
        sessionFactory.getCurrentSession().saveOrUpdate(hierarchyDetails);
    }

    /**
     * The 'saveOrgHierarchyAssocWith' function will updated the associated with value for the passed in 
     * hierarchy item
     * @param hierarchyDetails
     * @throws Exception 
     */
    public void saveOrgHierarchyAssocwith(programHierarchyDetails hierarchyDetails) throws Exception {

        /* First delete the existing associated with */
        String deleteSQL = "Delete from programOrgHierarchy_assoc where programHierarchyId = :programHierarchyId";

        Query deleteQuery = sessionFactory.getCurrentSession().createSQLQuery(deleteSQL)
                .setParameter("programHierarchyId", hierarchyDetails.getId());

        deleteQuery.executeUpdate();

        /* Second save the new associated with value */
        String insertSQL = "insert into programOrgHierarchy_assoc (programHierarchyId, associatedWith, dspPos) values (:programHierarchyId, :assocWith, 1)";

        Query insertQuery = sessionFactory.getCurrentSession().createSQLQuery(insertSQL)
                .setParameter("programHierarchyId", hierarchyDetails.getId())
                .setParameter("assocWith", hierarchyDetails.getAssociatedWith());

        insertQuery.executeUpdate();
    }

    /**
     * The 'saveOrgHierarchy' function will save/update the passed in organization hierarchy object.
     * 
     * @param hierarchyDetails  The programOrgHierarchy object containing the form fields.
     * @throws Exception 
     */
    public void saveOrgHierarchy(programOrgHierarchy hierarchyDetails) throws Exception {
        sessionFactory.getCurrentSession().saveOrUpdate(hierarchyDetails);
    }

    /**
     * The 'getProgramOrgHierarchyBydspPos' function will return the organization hierarchy that currently
     * has the dspPos set to the dspPos passed in.
     * 
     * @param dspPos    The display position that we need to find
     * @param programId The id of the program the organization hierarchy being updated belongs to.
     * @return 
     */
    public programOrgHierarchy getProgramOrgHierarchyBydspPos(Integer dspPos, Integer programId) {
        Query query = sessionFactory.getCurrentSession()
                .createQuery("from programOrgHierarchy where programId = :programId and dspPos = :dspPos");
        query.setParameter("programId", programId);
        query.setParameter("dspPos", dspPos);

        return (programOrgHierarchy) query.uniqueResult();
    }

    /**
     * The 'getProgramOrgHierarchyById' function will return the organization hierarchy for the selected hierarchy
     * 
     * @param id    The id of the selected hierarchy
     * @return 
     */
    public programOrgHierarchy getProgramOrgHierarchyById(Integer id) throws Exception {
        Query query = sessionFactory.getCurrentSession().createQuery("from programOrgHierarchy where id = :id");
        query.setParameter("id", id);

        return (programOrgHierarchy) query.uniqueResult();
    }

    /**
     * The 'getProgramHierarchyItems' function will return a list of items associated to the selected hierarchy.
     * 
     * @param hierarchyId The id of the selected program hierarchy.
     * @return This function will return a list of hierarchy items.
     * @throws Exception 
     */
    public List<programHierarchyDetails> getProgramHierarchyItems(Integer hierarchyId) throws Exception {
        Query query = sessionFactory.getCurrentSession().createQuery(
                "from programHierarchyDetails where programHierarchyId = :hierarchyId order by id asc");
        query.setParameter("hierarchyId", hierarchyId);

        List<programHierarchyDetails> itemList = query.list();
        return itemList;
    }

    /**
     * The 'getProgramHierarchyItemDetails' function will return the details of the selected organization hierarchy item.
     * 
     * @param id The id of the selected organization hierarchy item.
     * @return  This function will return a single organization hierarchy item object.
     * @throws Exception 
     */
    public programHierarchyDetails getProgramHierarchyItemDetails(Integer id) throws Exception {
        return (programHierarchyDetails) sessionFactory.getCurrentSession().get(programHierarchyDetails.class, id);
    }

    /**
     * The 'getAssociatedHierarchy' function will return the associated hierarchy item for the passed in selected item id.
     * 
     * @param id    The id of the selected hierarchy item
     * @return  This function will return the id of the associated hierarchy item
     * @throws Exception 
     */
    public Integer getAssociatedHierarchy(Integer id) throws Exception {

        String sqlQuery = "SELECT associatedWith from programOrgHierarchy_assoc where programHierarchyId = :id";

        Query query = sessionFactory.getCurrentSession().createSQLQuery(sqlQuery).setParameter("id", id);

        return (Integer) query.uniqueResult();

    }

    /**
     * The 'getProgramOrgHierarchyItems' function will return the organization hierarchy entries for the passed in programId, level and assocId.
     * 
     * @param programId The id of the selected program
     * @param level     The hierarchy level to return.
     * @param assocId   0 or the selected hierarchy to find associated sub levels.
     * @return
     * @throws Exception 
     */
    public List getProgramOrgHierarchyItems(Integer programId, Integer level, Integer assocId) throws Exception {

        String sqlQuery = "SELECT a.id, a.name from programOrgHierarchy_details a inner join programOrgHierarchy b on a.programHierarchyId = b.id";

        if (assocId > 0) {
            sqlQuery += " inner join programOrgHierarchy_assoc c on c.programHierarchyId = a.id and c.associatedWith = "
                    + assocId;
        }

        sqlQuery += " where b.programId = :programId and b.dspPos = :level";

        Query query = sessionFactory.getCurrentSession().createSQLQuery(sqlQuery)
                .setParameter("programId", programId).setParameter("level", level);

        return query.list();
    }

    /**
     * The 'saveUserProgramHierarchy' function will save the authorized program organizational 
     * hierarchies for the user.
     * 
     * @param hierarchy The userProgramHierarchy object to save
     * @throws Exception 
     */
    public void saveUserProgramHierarchy(userProgramHierarchy hierarchy) throws Exception {
        sessionFactory.getCurrentSession().saveOrUpdate(hierarchy);
    }

    /**
     * The 'getUserProgramHierarchy' function will return a list of authorized hierarchy for the selected user and 
     * program.
     * 
     * @param programId The id of the selected program
     * @param userId    The id of the selected user
     * @return  This function will return a list of userProgramHierarchy objects
     * @throws Exception 
     */
    public List<userProgramHierarchy> getUserProgramHierarchy(Integer programId, Integer userId) throws Exception {

        String sqlQuery = "select a.id, a.systemUserId, a.programId, a.programHierarchyId, a.orgHierarchyDetailId, a.dateCreated, b.name as hierarchyName from user_authorizedOrgHierarchy a inner join programOrgHierarchy_details b on b.id = a.orgHierarchyDetailId where a.programId = "
                + programId + " and a.systemUserId = " + userId;

        Query query = sessionFactory.getCurrentSession().createSQLQuery(sqlQuery)
                .setResultTransformer(Transformers.aliasToBean(userProgramHierarchy.class));

        return query.list();
    }

    /**
     * The 'removeUserProgramHierarchy' function will remove the selected hierarchy for the user and program.
     * 
     * @param Id
     * @throws Exception 
     */
    public void removeUserProgramHierarchy(Integer Id) throws Exception {

        Query removeProgram = sessionFactory.getCurrentSession()
                .createQuery("delete from userProgramHierarchy where id = :Id");
        removeProgram.setParameter("Id", Id);
        removeProgram.executeUpdate();

    }

    /**
    * The 'getProviders' function will return a list of providers associated with the program.
    * 
    * @param programId The id of the selected program.
    * @return This function will return a list of providers.
    * @throws Exception 
    */
    public List<provider> getProviders(Integer programId) throws Exception {
        Query query = sessionFactory.getCurrentSession()
                .createQuery("from provider where programId = :programId order by id asc");
        query.setParameter("programId", programId);

        List<provider> providerList = query.list();
        return providerList;
    }

    /**
     * The 'getProviderDetails' function will return the details of the selected provider.
     * 
     * @param providerId The id of the selected provider.
     * @return This function will return the provider details.
     * @throws Exception 
     */
    public provider getProviderDetails(Integer providerId) throws Exception {
        Query query = sessionFactory.getCurrentSession().createQuery("from provider where id = :providerId");
        query.setParameter("providerId", providerId);

        return (provider) query.uniqueResult();
    }

    /**
     * The 'createProvider' function will save the new provider
     * 
     * @param providerDetails The object holding the new provider details
     * @return
     * @throws Exception 
     */
    public Integer createProvider(provider providerDetails) throws Exception {
        Integer lastId = null;

        lastId = (Integer) sessionFactory.getCurrentSession().save(providerDetails);

        return lastId;
    }

    /**
     * The 'updateProvider' function will update the selected provider
     * 
     * @param providerDetails   The object holding the provider details
     * @throws Exception 
     */
    public void updateProvider(provider providerDetails) throws Exception {
        sessionFactory.getCurrentSession().update(providerDetails);
    }

    /**
     * The 'getProviderAssociatedHierarchy' list will return a list of associated hierarchy items for the 
     * passed in provider.
     * 
     * @param providerId    The selected provider
     * @return  This function will return a list of providerHierarchyAssoc items
     * @throws Exception 
     */
    public List<providerHierarchyAssoc> getProviderAssociatedHierarchy(Integer providerId) throws Exception {

        Query query = sessionFactory.getCurrentSession().createQuery(
                "from providerHierarchyAssoc where providerId = :providerId order by dateCreated desc");
        query.setParameter("providerId", providerId);

        List<providerHierarchyAssoc> associatedList = query.list();
        return associatedList;

    }

    /**
     * The 'saveProviderHierarchyAssoc' will save the new provider hierarchy item association.
     * 
     * @param newAssoc  The providerHierarchyAssoc object
     * @throws Exception 
     */
    public void saveProviderHierarchyAssoc(providerHierarchyAssoc newAssoc) throws Exception {
        sessionFactory.getCurrentSession().save(newAssoc);
    }

    /**
     * The 'removeProviderHierarchyAssoc' function will remove the selected item id for the provider
     * 
     * @param providerId The selected providerId
     * @param itemId    The selected item Id
     * @throws Exception 
     */
    public void removeProviderHierarchyAssoc(Integer itemId) throws Exception {

        /* First delete the existing associated with */
        String deleteSQL = "Delete from providerHierarchyAssoc where id = :itemId";

        Query deleteQuery = sessionFactory.getCurrentSession().createSQLQuery(deleteSQL).setParameter("itemId",
                itemId);

        deleteQuery.executeUpdate();

    }
}