gov.ca.cwds.cals.service.dao.FacilityChildDao.java Source code

Java tutorial

Introduction

Here is the source code for gov.ca.cwds.cals.service.dao.FacilityChildDao.java

Source

package gov.ca.cwds.cals.service.dao;

import static gov.ca.cwds.cals.persistence.model.cwscms.ChildAssignedWorker.RETRIEVE_CASE_ASSIGNED_WORKER_QUERY;
import static gov.ca.cwds.cals.persistence.model.cwscms.ChildAssignedWorker.RETRIEVE_REFERRAL_ASSIGNED_WORKER_QUERY;
import static gov.ca.cwds.cals.persistence.model.cwscms.ChildPlacementInformation.RETRIEVE_CHILDREN_PLACEMENT_INFORMATION_QUERY;
import static gov.ca.cwds.cals.persistence.model.cwscms.FacilityChildrenCountInformation.RETRIEVE_CWS_FACILITY_CHILDREN_COUNT_QUERY_NAME;
import static gov.ca.cwds.cals.persistence.model.cwscms.FacilityChildrenCountInformation.RETRIEVE_LIS_FACILITY_CHILDREN_COUNT_QUERY_NAME;

import com.google.inject.Inject;
import gov.ca.cwds.cals.persistence.model.cwscms.ChildAssignedWorker;
import gov.ca.cwds.cals.persistence.model.cwscms.ChildPlacementInformation;
import gov.ca.cwds.inject.CmsSessionFactory;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.apache.commons.lang3.ArrayUtils;
import org.hibernate.SessionFactory;
import org.hibernate.query.Query;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Custom Dao for implementing dao logic for facility children.
 *
 * @author CWDS CALS API Team
 */
public class FacilityChildDao extends CustomDao {

    private static final Logger LOG = LoggerFactory.getLogger(FacilityChildDao.class);

    public static final String CHILDREN_IDS_PARAMETER_NAME = "children_ids";
    public static final String CWS_FACILITY_ID_PARAMETER_NAME = "facilityId";
    public static final String LIS_FACILITY_ID_PARAMETER_NAME = "licenseNumber";

    @Inject
    public FacilityChildDao(@CmsSessionFactory SessionFactory sessionFactory) {
        super(sessionFactory);
    }

    /** Get Placement Information for children. */
    public List<ChildPlacementInformation> retrieveChildPlacementInformationList(String[] clientIds) {
        if (ArrayUtils.isEmpty(clientIds)) {
            return Collections.emptyList();
        }
        List<ChildPlacementInformation> childPlacementInformationList = prepareChildPlacementInformationQuery(
                clientIds).list();
        if (LOG.isWarnEnabled() && childPlacementInformationList.size() > clientIds.length) {
            LOG.warn("One or more than one child {} in the home has more than one open placement",
                    Arrays.toString(clientIds));
        }
        return childPlacementInformationList;
    }

    /** Get Assigned Worker Information for children. */
    public List<ChildAssignedWorker> retrieveChildAssignedWorkerList(String[] clientIds) {
        if (ArrayUtils.isEmpty(clientIds)) {
            return Collections.emptyList();
        }
        List<ChildAssignedWorker> childAssignedWorkerList = prepareCaseAssignedWorkerQuery(clientIds).list();
        Set<String> clientIdSet = new HashSet<>(Arrays.asList(clientIds));
        Set<String> availableIdSet = new HashSet<>();
        childAssignedWorkerList.stream()
                .forEach(childAssignedWorker -> availableIdSet.add(childAssignedWorker.getChildIdentifier()));
        clientIdSet.removeAll(availableIdSet);

        if (clientIdSet.isEmpty()) {
            return childAssignedWorkerList;
        }

        childAssignedWorkerList.addAll(
                prepareReferralAssignedWorkerQuery(clientIdSet.toArray(new String[clientIdSet.size()])).list());

        return childAssignedWorkerList;
    }

    public ChildPlacementInformation retrieveChildPlacementInformation(String clientId) {
        List<ChildPlacementInformation> childPlacementInformation = prepareChildPlacementInformationQuery(
                new String[] { clientId }).list();
        if (childPlacementInformation.size() > 1) {
            LOG.warn("Child with identifier {} has more than one open placement", clientId);
        }
        return childPlacementInformation.get(0);
    }

    /**
     * Gets the total count of the CWS Facility children.
     *
     * @param facilityId Id of the CWS facility.
     * @return the total count of the CWS Facility children.
     */
    public int getCountOfCwsFacilityChildren(String facilityId) {
        return getCountOfFacilityChildren(RETRIEVE_CWS_FACILITY_CHILDREN_COUNT_QUERY_NAME,
                CWS_FACILITY_ID_PARAMETER_NAME, facilityId);
    }

    /**
     * Gets the total count of the LIS Facility children.
     *
     * @param licenseNumber License of the LIS facility.
     * @return the total count of the LIS Facility children.
     */
    public int getCountOfLisFacilityChildren(String licenseNumber) {
        return getCountOfFacilityChildren(RETRIEVE_LIS_FACILITY_CHILDREN_COUNT_QUERY_NAME,
                LIS_FACILITY_ID_PARAMETER_NAME, licenseNumber);
    }

    private int getCountOfFacilityChildren(String query, String facilityIdParameterName, String facilityId) {
        return currentSession().createNamedQuery(query, Long.class)
                .setParameter(facilityIdParameterName, facilityId).uniqueResult().intValue();
    }

    private Query<ChildPlacementInformation> prepareChildPlacementInformationQuery(String[] clientIds) {
        return currentSession()
                .createNamedQuery(RETRIEVE_CHILDREN_PLACEMENT_INFORMATION_QUERY, ChildPlacementInformation.class)
                .setParameterList(CHILDREN_IDS_PARAMETER_NAME, clientIds);
    }

    private Query<ChildAssignedWorker> prepareCaseAssignedWorkerQuery(String[] clientIds) {
        return currentSession().createNamedQuery(RETRIEVE_CASE_ASSIGNED_WORKER_QUERY, ChildAssignedWorker.class)
                .setParameterList(CHILDREN_IDS_PARAMETER_NAME, clientIds);
    }

    private Query<ChildAssignedWorker> prepareReferralAssignedWorkerQuery(String[] clientIds) {
        return currentSession().createNamedQuery(RETRIEVE_REFERRAL_ASSIGNED_WORKER_QUERY, ChildAssignedWorker.class)
                .setParameterList(CHILDREN_IDS_PARAMETER_NAME, clientIds);
    }
}