Skip to content

Commit

Permalink
feat(FSADT1-1390): First Nation, Government, Ministry of Forests, and…
Browse files Browse the repository at this point in the history
… Unregistered Business client types (#1070)
  • Loading branch information
mamartinezmejia committed Aug 11, 2024
1 parent e639417 commit 4c08eff
Show file tree
Hide file tree
Showing 28 changed files with 930 additions and 126 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ left join nrfc.province_code pc on (pc.province_code = sl.province_code and pc.c
public static final String ROLE_EDITOR = "CLIENT_EDITOR";
public static final String ROLE_ADMIN = "CLIENT_ADMIN";

public static final String OPENDATA_FILTER = "<Filter><PropertyIsLike wildCard=\"*\" singleChar=\".\" escape=\"!\"><PropertyName>%s</PropertyName><Literal>*%s*</Literal></PropertyIsLike></Filter>";
public static final String OPENDATA_FILTER = "<Filter><Or><PropertyIsLike wildCard=\"*\" singleChar=\".\" escape=\"!\"><PropertyName>%s</PropertyName><Literal>*%s*</Literal></PropertyIsLike><PropertyIsLike wildCard=\"*\" singleChar=\".\" escape=\"!\"><PropertyName>%s</PropertyName><Literal>*%s*</Literal></PropertyIsLike><PropertyIsLike wildCard=\"*\" singleChar=\".\" escape=\"!\"><PropertyName>%s</PropertyName><Literal>*%s*</Literal></PropertyIsLike><PropertyIsLike wildCard=\"*\" singleChar=\".\" escape=\"!\"><PropertyName>%s</PropertyName><Literal>*%s*</Literal></PropertyIsLike></Or></Filter>";

public static final String MDC_USERID = "X-USER";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,10 @@ public String getJwksUrl() {
@AllArgsConstructor
public static class OpenDataConfiguration {

private String sacUrl;
private String bcMapsUrl;
private String sacBandUrl;
private String sacTribeUrl;
private String openMapsBandUrl;
private String openMapsTribeUrl;
}

public record NameSecretDto(String name, String secret) {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,12 +217,21 @@ public WebClient addressCompleteApi(
* @return A WebClient instance configured for the Open Data SAC API.
*/
@Bean
public WebClient openDataSacApi(
public WebClient openDataSacBandApi(
ForestClientConfiguration configuration,
WebClient.Builder webClientBuilder
) {
return webClientBuilder
.baseUrl(configuration.getOpenData().getSacUrl()).build();
.baseUrl(configuration.getOpenData().getSacBandUrl()).build();
}

@Bean
public WebClient openDataSacTribeApi(
ForestClientConfiguration configuration,
WebClient.Builder webClientBuilder
) {
return webClientBuilder
.baseUrl(configuration.getOpenData().getSacTribeUrl()).build();
}

/**
Expand All @@ -236,11 +245,19 @@ public WebClient openDataSacApi(
* @return A WebClient instance configured for the Open Data BC Maps API.
*/
@Bean
public WebClient openDataBcMapsApi(
public WebClient openDataBcMapsBandApi(
ForestClientConfiguration configuration,
WebClient.Builder webClientBuilder
) {
return webClientBuilder.baseUrl(configuration.getOpenData().getOpenMapsBandUrl()).build();
}

@Bean
public WebClient openDataBcMapsTribeApi(
ForestClientConfiguration configuration,
WebClient.Builder webClientBuilder
) {
return webClientBuilder.baseUrl(configuration.getOpenData().getBcMapsUrl()).build();
return webClientBuilder.baseUrl(configuration.getOpenData().getOpenMapsTribeUrl()).build();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public record ClientDetailsDto(
String name,
String id,
Boolean goodStanding,
String clientType,
List<ClientAddressDto> addresses,
List<ClientContactDto> contacts
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*/
public enum ClientTypeEnum {
// Enum constants representing client types
C, S, A, I, P, L, RSP, USP;
C, S, A, I, P, L, RSP, USP, B;

// A map for reverse lookup of enum constants by their name
private static final Map<String, ClientTypeEnum> CONSTANTS = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
package ca.bc.gov.app.dto.opendata;

import ca.bc.gov.app.dto.legacy.ClientTypeCodeEnum;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.Objects;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import org.apache.commons.lang3.StringUtils;

@JsonIgnoreProperties(ignoreUnknown = true)
public record FeatureProperties(
@JsonProperty("GmlID") String gmlId,
@JsonProperty("OBJECTID") int objectId,
@JsonProperty("Numéro_de_bande___Band_Number") int bandNumber,
@JsonProperty("Numéro_du_conseil_tribal___Tribal_Council_Number") int tribeNumber,
@JsonProperty("Nom_de_bande___Band_Name") String bandName,
@JsonProperty("Nom_du_conseil_tribal___Tribal_Council_Name") String tribeName,
@JsonProperty("COMMUNITY_LOCATION_ID") int communityLocationId,
@JsonProperty("FIRST_NATION_BC_NAME") String firstNationBCName,
@JsonProperty("FIRST_NATION_FEDERAL_NAME") String firstNationFederalName,
Expand All @@ -35,20 +39,31 @@ public record FeatureProperties(
@JsonProperty("SITE_NAME") String siteName,
@JsonProperty("SITE_NUMBER") String siteNumber,
@JsonProperty("COMMENTS") String comments,
@JsonProperty("SE_ANNO_CAD_DATA") Object seAnnoCadData // Keeping this as Object since it's null in the provided JSON
@JsonProperty("SE_ANNO_CAD_DATA") Object seAnnoCadData,
@JsonProperty("CPC_CODE") String cpcCode
// Keeping this as Object since it's null in the provided JSON
) {

public String getNationName() {
return StringUtils.defaultIfBlank(firstNationBCName(), bandName());
return Stream
.of(firstNationBCName(), bandName(), tribeName())
.filter(StringUtils::isNotBlank)
.findFirst()
.orElse(StringUtils.EMPTY);
}

public int getNationId() {
return IntStream
.of(firstNationFederalId(), bandNumber())
.of(firstNationFederalId(), bandNumber(), tribeNumber())
.filter(id -> id > 0)
.filter(Objects::nonNull)
.findFirst()
.orElse(0);
}

public ClientTypeCodeEnum getFirstNationType() {
return
StringUtils.isNotBlank(tribeName()) ? ClientTypeCodeEnum.T : ClientTypeCodeEnum.B;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,7 @@ private ClientDetailsDto buildSimpleClientDetails(
"",
"",
false,
"",
List.of(),
List.of()
);
Expand All @@ -408,6 +409,7 @@ private ClientDetailsDto buildSimpleClientDetails(
businessDto.legalName(),
businessDto.identifier(),
businessDto.goodStanding(),
businessDto.legalType(),
List.of(),
List.of()
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import ca.bc.gov.app.ApplicationConstant;
import ca.bc.gov.app.dto.opendata.OpenData;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

Expand All @@ -17,6 +18,10 @@ public abstract class AbstractOpenDataService {


public Mono<OpenData> getFeature(String nationName) {

if(StringUtils.isBlank(nationName))
return Mono.empty();

return getWebClient()
.get()
.uri(pathBuilder ->
Expand All @@ -29,7 +34,16 @@ public Mono<OpenData> getFeature(String nationName) {
.queryParam("filter",
String.format(ApplicationConstant.OPENDATA_FILTER,
getSearchField(),
nationName
nationName,

getSearchField(),
nationName.toLowerCase(),

getSearchField(),
nationName.toUpperCase(),

getSearchField(),
StringUtils.capitalize(nationName)
)
)
.build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class BcMapsService extends AbstractOpenDataService {
private final WebClient webClient;

public BcMapsService(
@Qualifier("openDataBcMapsApi") WebClient webClient) {
@Qualifier("openDataBcMapsBandApi") WebClient webClient) {
this.webClient = webClient;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,64 +5,84 @@
import ca.bc.gov.app.dto.client.ClientValueTextDto;
import ca.bc.gov.app.dto.opendata.Feature;
import ca.bc.gov.app.dto.opendata.OpenData;
import ca.bc.gov.app.repository.client.ProvinceCodeRepository;
import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

@RequiredArgsConstructor
@Slf4j
@Service
public class OpenDataService {

private final BcMapsService bcMapsService;
private final SacService sacService;
private final SacFirstNationService sacFirstNationService;
private final SacFirstNationTribeService sacFirstNationTribeService;
private final ProvinceCodeRepository provinceCodeRepository;

public Flux<ClientDetailsDto> getFirstNationData(String nationName) {
return bcMapsService
.getFirstNationData(nationName)
.switchIfEmpty(sacService.getFirstNationData(nationName))
.switchIfEmpty(sacFirstNationService.getFirstNationData(nationName))
.switchIfEmpty(sacFirstNationTribeService.getFirstNationData(nationName))
.flatMapIterable(OpenData::features)
.doOnNext(openDataFeature ->
log.info("Returning first nation data for nationName: {} {}", nationName,
openDataFeature)
)
.map(this::convertToDto);
.flatMap(this::convertToDto);
}

private ClientDetailsDto convertToDto(Feature openDataFeature) {
private Mono<ClientDetailsDto> convertToDto(Feature openDataFeature) {
return loadProvince("CA", openDataFeature.properties().officeProvince())
.map(provinceDto -> {
ClientAddressDto addressDto = new ClientAddressDto(
StringUtils.defaultIfBlank(
openDataFeature.properties().addressLine2(),
openDataFeature.properties().addressLine1()
),
StringUtils.isNotBlank(openDataFeature.properties().addressLine2()) ?
openDataFeature.properties().addressLine1() : StringUtils.EMPTY,
null,
new ClientValueTextDto("CA", "Canada"),
provinceDto,
openDataFeature.properties().officeCity(),
StringUtils.defaultIfBlank(
openDataFeature.properties().officePostalCode(),
StringUtils.EMPTY)
.replace(" ", StringUtils.EMPTY),
StringUtils.EMPTY,
StringUtils.EMPTY,
StringUtils.EMPTY,
StringUtils.EMPTY,
StringUtils.EMPTY,
0,
"Mailing Address"
);

ClientAddressDto addressDto = new ClientAddressDto(
StringUtils.defaultIfBlank(
openDataFeature.properties().addressLine2(),
openDataFeature.properties().addressLine1()
),
StringUtils.isNotBlank(openDataFeature.properties().addressLine2())
? openDataFeature.properties().addressLine1() : StringUtils.EMPTY,
StringUtils.EMPTY,
new ClientValueTextDto("CA", "Canada"),
new ClientValueTextDto(openDataFeature.properties().officeProvince(), null),
openDataFeature.properties().officeCity(),
StringUtils.defaultIfBlank(openDataFeature.properties().officePostalCode(),
StringUtils.EMPTY).replace(" ", StringUtils.EMPTY),
StringUtils.EMPTY,
StringUtils.EMPTY,
StringUtils.EMPTY,
StringUtils.EMPTY,
StringUtils.EMPTY,
0,
"Mailing Address"
);

return new ClientDetailsDto(
openDataFeature.properties().getNationName(),
openDataFeature.properties().getNationId() + "",
true,
addressDto.isValid() ? List.of(addressDto) : List.of(),
List.of()
);
return new ClientDetailsDto(
openDataFeature.properties().getNationName(),
openDataFeature.properties().getNationId() + "",
true,
openDataFeature.properties().getFirstNationType().name(),
addressDto.isValid() ? List.of(addressDto) : List.of(),
List.of()
);
});
}

private Mono<ClientValueTextDto> loadProvince(String countryCode, String province) {
return
provinceCodeRepository
.findByCountryCodeAndProvinceCode(countryCode, province)
.map(
entity -> new ClientValueTextDto(entity.getProvinceCode(), entity.getDescription())
)
.defaultIfEmpty(new ClientValueTextDto(province, province));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

@Slf4j
@Service
public class SacService extends AbstractOpenDataService {
public class SacFirstNationService extends AbstractOpenDataService {

public Mono<OpenData> getFirstNationData(String nationName) {
return getFeature(nationName)
Expand All @@ -18,8 +18,8 @@ public Mono<OpenData> getFirstNationData(String nationName) {

private final WebClient webClient;

public SacService(
@Qualifier("openDataSacApi") WebClient webClient) {
public SacFirstNationService(
@Qualifier("openDataSacBandApi") WebClient webClient) {
this.webClient = webClient;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package ca.bc.gov.app.service.opendata;

import ca.bc.gov.app.dto.opendata.OpenData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

@Slf4j
@Service
public class SacFirstNationTribeService extends AbstractOpenDataService {

public Mono<OpenData> getFirstNationData(String nationName) {
return getFeature(nationName)
.doOnNext(openData -> log.info("SAC: {}", openData));
}

private final WebClient webClient;

public SacFirstNationTribeService(
@Qualifier("openDataSacTribeApi") WebClient webClient) {
this.webClient = webClient;
}

@Override
WebClient getWebClient() {
return webClient;
}

@Override
String getTypeName() {
return "Donnees_Ouvertes-Open_Data_Conseil_Tribal_Tribal_Council:Conseil_tribal___Tribal_Council";
}

@Override
String getVersion() {
return "2.0.0";
}

@Override
String getOutputFormat() {
return "GEOJSON";
}

@Override
String getSearchField() {
return "Donnees_Ouvertes-Open_Data_Conseil_Tribal_Tribal_Council:Nom_du_conseil_tribal___Tribal_Council_Name";
}
}
Loading

0 comments on commit 4c08eff

Please sign in to comment.