|
| 1 | +package uk.gov.hmcts.reform.iacaseapi.domain.handlers.presubmit; |
| 2 | + |
| 3 | +import org.apache.logging.log4j.util.Strings; |
| 4 | +import org.springframework.stereotype.Component; |
| 5 | +import uk.gov.hmcts.reform.iacaseapi.domain.RequiredFieldMissingException; |
| 6 | +import uk.gov.hmcts.reform.iacaseapi.domain.entities.AsylumCase; |
| 7 | +import uk.gov.hmcts.reform.iacaseapi.domain.entities.AsylumCaseFieldDefinition; |
| 8 | +import uk.gov.hmcts.reform.iacaseapi.domain.entities.ccd.Event; |
| 9 | +import uk.gov.hmcts.reform.iacaseapi.domain.entities.ccd.callback.Callback; |
| 10 | +import uk.gov.hmcts.reform.iacaseapi.domain.entities.ccd.callback.PreSubmitCallbackResponse; |
| 11 | +import uk.gov.hmcts.reform.iacaseapi.domain.entities.ccd.callback.PreSubmitCallbackStage; |
| 12 | +import uk.gov.hmcts.reform.iacaseapi.domain.entities.ccd.field.AddressUk; |
| 13 | +import uk.gov.hmcts.reform.iacaseapi.domain.entities.ccd.field.YesOrNo; |
| 14 | +import uk.gov.hmcts.reform.iacaseapi.domain.handlers.PreSubmitCallbackHandler; |
| 15 | +import uk.gov.hmcts.reform.iacaseapi.domain.service.DetentionFacilityAddressProvider; |
| 16 | +import uk.gov.hmcts.reform.iacaseapi.domain.service.DetentionFacilityAddressProvider.DetentionAddress; |
| 17 | + |
| 18 | +import java.util.EnumSet; |
| 19 | +import java.util.List; |
| 20 | +import java.util.Optional; |
| 21 | +import java.util.Set; |
| 22 | +import java.util.stream.Stream; |
| 23 | + |
| 24 | +import static java.util.Objects.requireNonNull; |
| 25 | +import static java.util.stream.Collectors.toList; |
| 26 | +import static uk.gov.hmcts.reform.iacaseapi.domain.entities.AsylumCaseFieldDefinition.*; |
| 27 | +import static uk.gov.hmcts.reform.iacaseapi.domain.entities.DetentionFacility.*; |
| 28 | +import static uk.gov.hmcts.reform.iacaseapi.domain.entities.ccd.Event.*; |
| 29 | +import static uk.gov.hmcts.reform.iacaseapi.domain.entities.ccd.callback.PreSubmitCallbackStage.MID_EVENT; |
| 30 | +import static uk.gov.hmcts.reform.iacaseapi.domain.entities.ccd.field.YesOrNo.NO; |
| 31 | +import static uk.gov.hmcts.reform.iacaseapi.domain.entities.ccd.field.YesOrNo.YES; |
| 32 | + |
| 33 | +@Component |
| 34 | +public class DetentionLocationAddressPopulator implements PreSubmitCallbackHandler<AsylumCase> { |
| 35 | + |
| 36 | + private static final Set<String> DETENTION_FACILITY_PAGE_IDS = Set.of("ircName", "prisonName", "appellantAddress"); |
| 37 | + private static final EnumSet<Event> SUPPORTED_EVENTS = EnumSet.of( |
| 38 | + START_APPEAL, EDIT_APPEAL, EDIT_APPEAL_AFTER_SUBMIT, UPDATE_DETENTION_LOCATION |
| 39 | + ); |
| 40 | + |
| 41 | + private final DetentionFacilityAddressProvider addressProvider; |
| 42 | + |
| 43 | + public DetentionLocationAddressPopulator(DetentionFacilityAddressProvider addressProvider) { |
| 44 | + this.addressProvider = addressProvider; |
| 45 | + } |
| 46 | + |
| 47 | + public boolean canHandle( |
| 48 | + PreSubmitCallbackStage callbackStage, |
| 49 | + Callback<AsylumCase> callback |
| 50 | + ) { |
| 51 | + requireNonNull(callbackStage, "callbackStage must not be null"); |
| 52 | + requireNonNull(callback, "callback must not be null"); |
| 53 | + |
| 54 | + YesOrNo appellantInDetention = callback.getCaseDetails() |
| 55 | + .getCaseData().read(APPELLANT_IN_DETENTION, YesOrNo.class).orElse(NO); |
| 56 | + |
| 57 | + return callbackStage == MID_EVENT |
| 58 | + && (SUPPORTED_EVENTS.contains(callback.getEvent())) |
| 59 | + && (DETENTION_FACILITY_PAGE_IDS.contains(callback.getPageId())) |
| 60 | + && appellantInDetention.equals(YES); |
| 61 | + } |
| 62 | + |
| 63 | + public PreSubmitCallbackResponse<AsylumCase> handle( |
| 64 | + PreSubmitCallbackStage callbackStage, |
| 65 | + Callback<AsylumCase> callback |
| 66 | + ) { |
| 67 | + if (!canHandle(callbackStage, callback)) { |
| 68 | + throw new IllegalStateException("Cannot handle callback"); |
| 69 | + } |
| 70 | + |
| 71 | + AsylumCase asylumCase = callback |
| 72 | + .getCaseDetails() |
| 73 | + .getCaseData(); |
| 74 | + |
| 75 | + String facilityType = asylumCase.read(DETENTION_FACILITY, String.class) |
| 76 | + .orElseThrow(() -> new IllegalArgumentException("not a valid Detention Facility")); |
| 77 | + |
| 78 | + if (facilityType.equals(IRC.getValue()) || facilityType.equals(PRISON.getValue())) { |
| 79 | + AsylumCaseFieldDefinition nameField = facilityType.equals(IRC.getValue()) ? IRC_NAME : PRISON_NAME; |
| 80 | + |
| 81 | + String facilityName = asylumCase.read(nameField, String.class) |
| 82 | + .orElseThrow(() -> new RequiredFieldMissingException(nameField.value())); |
| 83 | + |
| 84 | + DetentionAddress facilityAddress = addressProvider.getAddressFor(facilityName) |
| 85 | + .orElseThrow(() -> new RuntimeException("Could not find address for facility: " + facilityName)); |
| 86 | + |
| 87 | + asylumCase.write(DETENTION_BUILDING, facilityAddress.building()); |
| 88 | + asylumCase.write(DETENTION_ADDRESS_LINES, facilityAddress.addressLines()); |
| 89 | + asylumCase.write(DETENTION_POSTCODE, facilityAddress.postcode()); |
| 90 | + } |
| 91 | + |
| 92 | + if (facilityType.equals(OTHER.getValue())) { |
| 93 | + buildDetentionAddressFromAppellantAddress(asylumCase); |
| 94 | + } |
| 95 | + |
| 96 | + return new PreSubmitCallbackResponse<>(asylumCase); |
| 97 | + } |
| 98 | + |
| 99 | + private void buildDetentionAddressFromAppellantAddress(AsylumCase asylumCase) { |
| 100 | + AddressUk appellantAddress = asylumCase.read(APPELLANT_ADDRESS, AddressUk.class) |
| 101 | + .orElseThrow(() -> new RequiredFieldMissingException("appellantAddress")); |
| 102 | + |
| 103 | + asylumCase.write(DETENTION_BUILDING, appellantAddress.getAddressLine1().orElse("")); |
| 104 | + asylumCase.write(DETENTION_ADDRESS_LINES, buildAddressLines(appellantAddress)); |
| 105 | + asylumCase.write(DETENTION_POSTCODE, appellantAddress.getPostCode().orElse("")); |
| 106 | + } |
| 107 | + |
| 108 | + private String buildAddressLines(AddressUk appellantAddress) { |
| 109 | + List<String> addressParts = Stream.of( |
| 110 | + appellantAddress.getAddressLine2(), |
| 111 | + appellantAddress.getAddressLine3(), |
| 112 | + appellantAddress.getPostTown(), |
| 113 | + appellantAddress.getCounty(), |
| 114 | + appellantAddress.getCountry() |
| 115 | + ) |
| 116 | + .filter(Optional::isPresent) |
| 117 | + .map(Optional::get) |
| 118 | + .filter(str -> !Strings.isBlank(str)) |
| 119 | + .collect(toList()); |
| 120 | + |
| 121 | + return String.join(", ", addressParts); |
| 122 | + } |
| 123 | +} |
0 commit comments