gov.ca.cwds.cals.util.PlacementHomeUtil.java Source code

Java tutorial

Introduction

Here is the source code for gov.ca.cwds.cals.util.PlacementHomeUtil.java

Source

package gov.ca.cwds.cals.util;

import static gov.ca.cwds.cals.Constants.UnitOfWork.CMS;
import static gov.ca.cwds.cals.Constants.UnitOfWork.LIS;

import gov.ca.cwds.cals.persistence.model.calsns.rfa.RFA1aApplicant;
import gov.ca.cwds.cals.service.dto.rfa.ApplicantDTO;
import gov.ca.cwds.cals.service.dto.rfa.ResidenceDTO;
import gov.ca.cwds.cals.service.mapper.RFA1aFormMapper;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;

/**
 * Created by Alexander Serbin on 8/9/2018.
 */
public final class PlacementHomeUtil {

    public static final String WATER_BODY = "water body";
    public static final String WEAPON_IN_HOME_BODY = "weapon in home";

    private PlacementHomeUtil() {
    }

    /**
     * Get Hazards description.
     */
    public static String getHazardsDescription(ResidenceDTO residence) {
        if (residence.getWeaponInHome() && residence.getBodyOfWaterExist()) {
            return WATER_BODY + ", " + WEAPON_IN_HOME_BODY;
        }
        if (residence.getWeaponInHome() && !residence.getBodyOfWaterExist()) {
            return WEAPON_IN_HOME_BODY;
        }
        if (!residence.getWeaponInHome() && residence.getBodyOfWaterExist()) {
            return WATER_BODY;
        }
        return " ";
    }

    /**
     * Overloaded method for composing facility name according to applicants list.
     *
     * @param applicantsList applicants list
     * @return facility name
     */
    public static String composeFacilityNameByApplicantsList(List<RFA1aApplicant> applicantsList) {
        return composeFacilityName(
                applicantsList.stream().map(RFA1aFormMapper.INSTANCE::toApplicantDTO).collect(Collectors.toList()));
    }

    /**
     * Composes facility name according to applicantDTOs list.
     *
     * <p> <b>Rules for Facility / Family name</b>
     *
     * Reads Applicant Name(s) as Last Name, First Name of Applicant #1 and Last Name, First Name of
     * Applicant #2. (ex. Smith, John & Jones, Jane) Last Name is separated from First Name by a
     * comma. Applicant Names are separated from each other by "and" or "&". When the Last Name is
     * the same for Applicant #1 and Applicant #2, name reads "Common Last Name, First Name
     * Applicant #1 & First Name Applicant #2" (ex. Smith, John & Jane). </p>
     *
     * @param applicantsList applicantsDTOs list
     * @return facility name
     */
    public static String composeFacilityName(List<ApplicantDTO> applicantsList) {
        // Assume that Facility/Family name composed from the first 2 applicants
        return Optional.ofNullable(applicantsList).map(applicants -> {
            Optional<ApplicantDTO> firstApplicant = getApplicantBuIndex(applicants, 0);
            Optional<ApplicantDTO> secondApplicant = getApplicantBuIndex(applicants, 1);

            StringBuilder firstPartSb = firstApplicant.map(PlacementHomeUtil::composeFirstPartOfFacilityName)
                    .orElse(new StringBuilder());

            StringBuilder secondPartSb = secondApplicant
                    .map(applicantDTO -> composeSecondPartOfFacilityName(firstApplicant, applicantDTO))
                    .orElse(new StringBuilder());

            if (firstPartSb.length() > 0 && secondPartSb.length() > 0) {
                firstPartSb.append(" & ");
            }
            firstPartSb.append(secondPartSb);

            return firstPartSb.toString();
        }).orElse(null);
    }

    /**
     * Adds leading zeroes to make sure licence number is of 9 digits.
     */
    public static String normalizeLicenseNumber(String facilityNumber) {
        return StringUtils.leftPad(facilityNumber.trim(), 9, '0');
    }

    private static StringBuilder composeSecondPartOfFacilityName(Optional<ApplicantDTO> firstApplicant,
            ApplicantDTO secondApplicant) {
        StringBuilder sbForSecondApplicant = new StringBuilder();
        Optional<String> secondLastName = Optional.ofNullable(secondApplicant.getLastName());
        Optional<String> secondFirstName = Optional.ofNullable(secondApplicant.getFirstName());
        if (firstApplicant.isPresent() && secondLastName.isPresent()
                && !secondLastName.get().equals(firstApplicant.get().getLastName())) {
            sbForSecondApplicant.append(secondLastName.get());
            if (secondFirstName.isPresent()) {
                sbForSecondApplicant.append(", ");
            }
        }
        secondFirstName.ifPresent(sbForSecondApplicant::append);
        return sbForSecondApplicant;
    }

    private static StringBuilder composeFirstPartOfFacilityName(ApplicantDTO applicant) {
        StringBuilder sb = new StringBuilder();
        Optional<String> lastName = Optional.ofNullable(applicant.getLastName());
        Optional<String> firstName = Optional.ofNullable(applicant.getFirstName());
        lastName.ifPresent(ln -> {
            sb.append(ln);
            if (firstName.isPresent()) {
                sb.append(", ");
            }
        });
        firstName.ifPresent(sb::append);
        return sb;
    }

    private static Optional<ApplicantDTO> getApplicantBuIndex(List<ApplicantDTO> applicants, int index) {
        return applicants.size() > index ? Optional.ofNullable(applicants.get(index)) : Optional.empty();
    }

    /**
     * Returns the number of available beds i.e. (capacity - number of children).
     * @param capacity total number of beds in facility.
     * @param numberOfChildren total number of children in facility.
     * @return the total number of available beds.
     */
    public static int getNumberOfAvailableBeds(int capacity, int numberOfChildren) {
        return (capacity - numberOfChildren < 0) ? 0 : (capacity - numberOfChildren);
    }

    /**
     * Returns the unit of work i.e. FAS, CWS, LIS from UnitOfWork Constant.
     *
     * @param facilityNumber Id of the facility.
     * @return the unit of work.
     */
    public static String getUnitOfWork(String facilityNumber) {
        if (NumberUtils.isCreatable(facilityNumber)) {
            return LIS;
        } else {
            return CMS;
        }
    }

}