gov.ca.cwds.cals.service.FacilityInspectionService.java Source code

Java tutorial

Introduction

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

Source

package gov.ca.cwds.cals.service;

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

import com.google.inject.Inject;
import gov.ca.cwds.cals.persistence.dao.fas.InspectionDao;
import gov.ca.cwds.cals.persistence.model.fas.Rr809Dn;
import gov.ca.cwds.cals.service.mapper.FacilityInspectionMapper;
import gov.ca.cwds.cals.web.rest.parameter.FacilityInspectionParameterObject;
import gov.ca.cwds.cms.data.access.service.impl.PlacementHomeCoreService;
import gov.ca.cwds.data.legacy.cms.entity.PlacementHome;
import gov.ca.cwds.rest.api.Response;
import io.dropwizard.hibernate.UnitOfWork;
import java.io.Serializable;
import java.util.Optional;
import org.apache.commons.lang3.StringUtils;

/**
 * @author CWDS CALS API Team
 */
public class FacilityInspectionService extends CrudServiceAdapter {

    @Inject
    private FacilityInspectionMapper facilityInspectionMapper;
    @Inject
    private InspectionDao inspectionDao;
    @Inject
    private PlacementHomeCoreService placementHomeService;

    @Override
    public Response find(Serializable params) {
        FacilityInspectionParameterObject paramsObj = (FacilityInspectionParameterObject) params;
        String facilityNumber = getFacilityNumber(paramsObj.getFacilityId());
        Rr809Dn rr809Dn = getRr809Dn(paramsObj, facilityNumber);
        return facilityInspectionMapper.toFacilityInspectionDto(rr809Dn);
    }

    @UnitOfWork(value = FAS)
    protected Rr809Dn getRr809Dn(FacilityInspectionParameterObject paramsObj, String facilityNumber) {
        return inspectionDao.getDeficiencyByFacilityNumberAndId(facilityNumber, paramsObj.getInspectionId());
    }

    /**
     * Numeric IDS are considered for FAS facility,
     * Other IDS are considered for CWS facility
     *
     * @param facilityId gets facility number for inspections search in FAS
     * @return Facility ID for FAS facility and License number for CWS facility
     */
    public String getFacilityNumber(String facilityId) {
        if (StringUtils.isNumeric(facilityId)) {
            return facilityId;
        } else {
            return getFacilityLicenseNo(facilityId);
        }
    }

    @UnitOfWork(value = CMS)
    protected String getFacilityLicenseNo(String facilityId) {
        return Optional.ofNullable(placementHomeService.find(facilityId)).map(PlacementHome::getLicenseNo)
                .orElse(null);
    }
}