Java tutorial
package gov.ca.cwds.cals.service.mapper; import gov.ca.cwds.cals.persistence.model.fas.LpaInformation; import gov.ca.cwds.cals.persistence.model.lisfas.LisFacFile; import gov.ca.cwds.cals.service.CwsDictionaryEntriesHolder; import gov.ca.cwds.cals.service.dto.AddressDto; import gov.ca.cwds.cals.service.dto.AssignedWorkerDto; import gov.ca.cwds.cals.service.dto.FacilityAddressDto; import gov.ca.cwds.cals.service.dto.FacilityDto; import gov.ca.cwds.cals.service.dto.PersonPhoneDto; import gov.ca.cwds.cals.util.PlacementHomeUtil; import gov.ca.cwds.cals.util.Utils; import gov.ca.cwds.data.legacy.cms.entity.BaseLicensingVisit; import gov.ca.cwds.data.legacy.cms.entity.CountyLicenseCase; import gov.ca.cwds.data.legacy.cms.entity.PlacementHome; import java.time.LocalDateTime; import java.util.ArrayList; import java.util.List; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; import org.mapstruct.AfterMapping; import org.mapstruct.Mapper; import org.mapstruct.Mapping; import org.mapstruct.MappingTarget; import org.mapstruct.factory.Mappers; /** * @author CWDS CALS API Team */ @Mapper(imports = { LocalDateTime.class, StringUtils.class, PlacementHomeUtil.class }, uses = { FacilityPostMappingProcessor.class, FacilityTypeMapper.class, LicenseStatusTypeMapper.class, CountyMapper.class, DistrictOfficeMapper.class, DictionaryMapper.class, TrailingSpacesRemovalPostMappingProcessor.class }) @SuppressWarnings("squid:S1214") public interface FacilityMapper { FacilityMapper INSTANCE = Mappers.getMapper(FacilityMapper.class); /** * Visit data is converted in {@link FasFacilityMapper} * * @param lisFacFile param to build * @param lpaInformation param to build * @return converted data */ @Mapping(source = "lisFacFile.facNbr", target = "id") @Mapping(source = "lisFacFile.facilityType", target = "type") @Mapping(source = "lisFacFile.facName", target = "name") @Mapping(source = "lisFacFile.facLicenseeName", target = "licenseeName") @Mapping(source = "lisFacFile.facLicenseeType", target = "licenseeType") @Mapping(source = "lpaInformation.lpaCode", target = "assignedWorker.id") @Mapping(source = "lpaInformation.fullLpaName", target = "assignedWorker.fullName") @Mapping(source = "lpaInformation.lpaEmailAddress", target = "assignedWorker.email") @Mapping(source = "lisFacFile.facDoNbr.doNbr", target = "districtOffice.number") @Mapping(source = "lisFacFile.facDoNbr.doName", target = "districtOffice.name") @Mapping(target = "licenseNumber", expression = "java( lisFacFile.getFacNbr() != null ? " + "PlacementHomeUtil.normalizeLicenseNumber(lisFacFile.getFacNbr().toString()) : null )") @Mapping(source = "lisFacFile.licenseStatusType", target = "status", qualifiedByName = "facilityStatus") @Mapping(source = "lisFacFile.facCapacity", target = "capacity") @Mapping(source = "lisFacFile.facLicEffDate", target = "licenseEffectiveDate") @Mapping(source = "lisFacFile.facOrigApplRecDate", target = "originalApplicationRecievedDate") @Mapping(source = "lisFacFile.facEmailAddress", target = "emailAddress") @Mapping(source = "lisFacFile.county", target = "county") @Mapping(target = "facilitySource", constant = "LIS") @Mapping(source = "lisFacFile.facIncCapEffDate", target = "capacityLastChanged") FacilityDto toFacilityDto(LisFacFile lisFacFile, LpaInformation lpaInformation); @AfterMapping default void afterLisFacilityMapping(@MappingTarget FacilityDto facilityDto, LpaInformation lpaInformation) { if (lpaInformation != null) { PersonPhoneDto phone = new PersonPhoneDto(); phone.setRelation("primary"); phone.setNumber(lpaInformation.getLpaTelephone()); facilityDto.getAssignedWorker().addPhone(phone); } } @Mapping(target = "id", source = "placementHome.identifier") @Mapping(target = "adoptionOnly", expression = "java(\"Y\".equalsIgnoreCase(placementHome.getAdhmonly()))") @Mapping(target = "name", source = "placementHome.facltyNm") @Mapping(target = "type", source = "dictionaryEntriesHolder.facilityType") @Mapping(target = "licenseeName", source = "placementHome.licnseeNm") @Mapping(target = "assignedWorker.email", source = "placementHome.countyLicenseCase.staffPerson.emailAddr") @Mapping(target = "assignedWorker.fullName", expression = "java(placementHome.getCountyLicenseCase() == null || " + "placementHome.getCountyLicenseCase().getStaffPerson() == null ?" + " \"\" : StringUtils.trimToEmpty(placementHome.getCountyLicenseCase()." + "getStaffPerson().getFirstName()) + ' '" + " + placementHome.getCountyLicenseCase().getStaffPerson().getLastName())") @Mapping(target = "districtOffice", source = "placementHome.countyLicenseCase.staffPerson.county") @Mapping(target = "licenseNumber", source = "placementHome.licenseNo") @Mapping(target = "status", source = "dictionaryEntriesHolder.licenseStatusType") @Mapping(target = "capacity", source = "placementHome.maxCapNo") @Mapping(target = "licenseEffectiveDate", source = "placementHome.licEfctdt") @Mapping(target = "originalApplicationRecievedDate", source = "placementHome.licAplDt") @Mapping(target = "county", source = "dictionaryEntriesHolder.applicationCounty") @Mapping(target = "facilitySource", constant = "CWS/CMS") FacilityDto toFacilityDto(PlacementHome placementHome, CwsDictionaryEntriesHolder dictionaryEntriesHolder); @Mapping(target = "lastVisitDate", expression = "java(licensingVisit == null || licensingVisit.getVisitDate() == null? " + "null : LocalDateTime.of(licensingVisit.getVisitDate(), java.time.LocalTime.MIN))") @Mapping(target = "lastVisitReason.description", source = "visitType.shortDescription") void toFacilityDto(@MappingTarget FacilityDto facilityDto, BaseLicensingVisit licensingVisit); @AfterMapping default void after(@MappingTarget FacilityDto facilityDto, PlacementHome placementHome, CwsDictionaryEntriesHolder dictionaryEntriesHolder) { mapAssignedWorkerPhones(facilityDto.getAssignedWorker(), placementHome.getCountyLicenseCase()); afterAddresses(facilityDto, placementHome, dictionaryEntriesHolder); afterPhones(facilityDto, placementHome); afterLastVisit(facilityDto, placementHome.getCountyLicenseCase()); } default void afterLastVisit(FacilityDto facilityDto, CountyLicenseCase countyLicenseCase) { if (countyLicenseCase != null && CollectionUtils.isNotEmpty(countyLicenseCase.getLicensingVisits())) { List<? extends BaseLicensingVisit> licensingVisits = countyLicenseCase.getLicensingVisits(); if (licensingVisits != null) { Mappers.getMapper(FacilityMapper.class).toFacilityDto(facilityDto, licensingVisits.get(0)); } } } default void afterAddresses(FacilityDto facilityDto, PlacementHome placementHome, CwsDictionaryEntriesHolder dictionaryEntriesHolder) { List<FacilityAddressDto> facilityAddressDTOs = new ArrayList<>(2); FacilityAddressMapper facilityAddressMapper = Mappers.getMapper(FacilityAddressMapper.class); if (!StringUtils.isAllBlank(placementHome.getStreetNo(), placementHome.getStreetNm(), placementHome.getCityNm(), placementHome.getZipNo(), placementHome.getZipSfxNo())) { FacilityAddressDto residentialAddress = facilityAddressMapper.toResidentialAddress(placementHome, dictionaryEntriesHolder); facilityAddressMapper.afterMapping(residentialAddress, placementHome, dictionaryEntriesHolder); facilityDto.setFullResidentialAddress(AddressDto.buildFullAddressText(residentialAddress.getAddress())); facilityAddressDTOs.add(residentialAddress); } facilityDto.setAddress(facilityAddressDTOs); } default void afterPhones(FacilityDto facilityDto, PlacementHome placementHome) { List<PersonPhoneDto> personPhoneDtos = new ArrayList<>(2); PhoneMapper phoneMapper = Mappers.getMapper(PhoneMapper.class); PersonPhoneDto primaryPhone = phoneMapper.toPrimaryPhoneDto(placementHome); if (Utils.Phone.checkIfPhoneDTOIsValid(primaryPhone)) { personPhoneDtos.add(primaryPhone); } PersonPhoneDto alternativePhone = phoneMapper.toAlternatePhoneDto(placementHome); if (Utils.Phone.checkIfPhoneDTOIsValid(alternativePhone)) { personPhoneDtos.add(alternativePhone); } facilityDto.setPhone(personPhoneDtos); } default void mapAssignedWorkerPhones(AssignedWorkerDto assignedWorkerDto, CountyLicenseCase countyLicenseCase) { if (countyLicenseCase != null && countyLicenseCase.getStaffPerson() != null) { PersonPhoneDto phone = new PersonPhoneDto(); phone.setRelation("primary"); phone.setNumber(String.valueOf(countyLicenseCase.getStaffPerson().getPhoneNo())); assignedWorkerDto.addPhone(phone); } } }