Skip to content

Commit

Permalink
AKR & OTR(Backend): OPHAKRKEH-505 whenever saving contact info to ONR…
Browse files Browse the repository at this point in the history
… refetch it first [deploy]
  • Loading branch information
markpilkku committed Oct 20, 2023
1 parent 2b46f64 commit 38f3fc7
Show file tree
Hide file tree
Showing 9 changed files with 143 additions and 14 deletions.
24 changes: 24 additions & 0 deletions backend/akr/src/main/java/fi/oph/akr/onr/ContactDetailsUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
import fi.oph.akr.onr.dto.ContactDetailsGroupSource;
import fi.oph.akr.onr.dto.ContactDetailsGroupType;
import fi.oph.akr.onr.dto.ContactDetailsType;
import fi.oph.akr.onr.dto.PersonalDataDTO;
import fi.oph.akr.onr.model.PersonalData;
import fi.oph.akr.util.CustomOrderComparator;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
Expand Down Expand Up @@ -139,4 +141,26 @@ private static ContactDetailsDTO createContactDetailsDTO(final ContactDetailsTyp
contactDetailsDTO.setValue(value);
return contactDetailsDTO;
}

private static List<ContactDetailsGroupDTO> getNonAkrContactDetails(
final List<ContactDetailsGroupDTO> latestContactDetails
) {
return latestContactDetails
.stream()
.filter(cd -> cd.getSource() != ContactDetailsGroupSource.AKR)
.collect(Collectors.toList());
}

static PersonalDataDTO combineContactDetails(
final PersonalDataDTO personalDataDTO,
final List<ContactDetailsGroupDTO> latestContactDetails
) {
final List<ContactDetailsGroupDTO> latestNonOtrContactDetails = getNonAkrContactDetails(latestContactDetails);
final List<ContactDetailsGroupDTO> combinedContactDetails = Stream
.of(latestNonOtrContactDetails, personalDataDTO.getContactDetailsGroups())
.flatMap(Collection::stream)
.collect(Collectors.toList());
personalDataDTO.setContactDetailsGroups(combinedContactDetails);
return personalDataDTO;
}
}
53 changes: 47 additions & 6 deletions backend/akr/src/main/java/fi/oph/akr/onr/OnrOperationApiImpl.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fi.oph.akr.onr;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import fi.oph.akr.config.Constants;
import fi.oph.akr.onr.dto.ContactDetailsGroupDTO;
Expand All @@ -13,7 +14,6 @@
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import lombok.RequiredArgsConstructor;
import net.minidev.json.JSONArray;
import org.asynchttpclient.Request;
import org.asynchttpclient.RequestBuilder;
Expand All @@ -24,7 +24,6 @@
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;

@RequiredArgsConstructor
public class OnrOperationApiImpl implements OnrOperationApi {

private static final Logger LOG = LoggerFactory.getLogger(OnrOperationApiImpl.class);
Expand All @@ -35,6 +34,13 @@ public class OnrOperationApiImpl implements OnrOperationApi {

private final String onrServiceUrl;

public OnrOperationApiImpl(final CasClient onrClient, final String onrServiceUrl) {
this.onrClient = onrClient;
this.onrServiceUrl = onrServiceUrl;

OBJECT_MAPPER.configure(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL, true);
}

@Override
public Map<String, PersonalData> fetchPersonalDatas(final List<String> onrIds) throws Exception {
// /henkilo/masterHenkilosByOidList might be usable as an endpoint for fetching master person data for persons
Expand All @@ -57,7 +63,10 @@ public Map<String, PersonalData> fetchPersonalDatas(final List<String> onrIds) t
personalDataDTOS.forEach(dto -> personalDatas.put(dto.getOnrId(), createPersonalData(dto)));
return personalDatas;
} else {
throw new RuntimeException("ONR service returned unexpected status code: " + response.getStatusCode());
throw new RuntimeException(
"ONR service called with POST /henkilo/henkilotByHenkiloOidList returned unexpected status code: " +
response.getStatusCode()
);
}
}

Expand All @@ -80,7 +89,9 @@ public Optional<PersonalData> findPersonalDataByIdentityNumber(final String iden
} else if (response.getStatusCode() == HttpStatus.NOT_FOUND.value()) {
return Optional.empty();
} else {
throw new RuntimeException("ONR service returned unexpected status code: " + response.getStatusCode());
throw new RuntimeException(
"ONR service called with GET /henkilo/hetu= returned unexpected status code: " + response.getStatusCode()
);
}
}

Expand Down Expand Up @@ -137,6 +148,27 @@ public String insertPersonalData(final PersonalData personalData) throws Excepti
LOG.error("Error code {} from ONR", response.getStatusCode());
LOG.error("Error from ONR with body: {}", response.getResponseBody());
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
throw new RuntimeException(
"ONR service called with POST /henkilo returned unexpected status code: " + response.getStatusCode()
);
}
}

private PersonalDataDTO getPersonalData(final String oidNumber) throws Exception {
final Request request = defaultRequestBuilder()
.setUrl(onrServiceUrl + "/henkilo/" + oidNumber)
.setMethod(Methods.GET)
.build();

final Response response = onrClient.executeBlocking(request);

if (response.getStatusCode() == HttpStatus.CREATED.value()) {
final PersonalDataDTO personalDataDTO = OBJECT_MAPPER.readValue(
response.getResponseBody(),
new TypeReference<>() {}
);
return personalDataDTO;
} else {
throw new RuntimeException("ONR service returned unexpected status code: " + response.getStatusCode());
}
}
Expand All @@ -145,10 +177,17 @@ public String insertPersonalData(final PersonalData personalData) throws Excepti
public void updatePersonalData(final PersonalData personalData) throws Exception {
final PersonalDataDTO personalDataDTO = createPersonalDataDTO(personalData);

final List<ContactDetailsGroupDTO> latestContactDetails = getPersonalData(personalData.getOnrId())
.getContactDetailsGroups();
final PersonalDataDTO combinedContactDetailsPersonalDataDTO = ContactDetailsUtil.combineContactDetails(
personalDataDTO,
latestContactDetails
);

final Request request = defaultRequestBuilder()
.setUrl(onrServiceUrl + "/henkilo")
.setMethod(Methods.PUT)
.setBody(OBJECT_MAPPER.writeValueAsString(personalDataDTO))
.setBody(OBJECT_MAPPER.writeValueAsString(combinedContactDetailsPersonalDataDTO))
.build();

final Response response = onrClient.executeBlocking(request);
Expand All @@ -158,7 +197,9 @@ public void updatePersonalData(final PersonalData personalData) throws Exception
LOG.error("Error code {} from ONR", response.getStatusCode());
LOG.error("Error from ONR with body: {}", response.getResponseBody());
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
throw new RuntimeException("ONR service returned unexpected status code: " + response.getStatusCode());
throw new RuntimeException(
"ONR service called with PUT /henkilo returned unexpected status code: " + response.getStatusCode()
);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package fi.oph.akr.onr.dto;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;

@JsonIgnoreProperties(ignoreUnknown = true)
public enum ContactDetailsGroupSource {
@JsonProperty("alkupera1")
VTJ,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package fi.oph.akr.onr.dto;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;

@JsonIgnoreProperties(ignoreUnknown = true)
public enum ContactDetailsGroupType {
@JsonProperty("yhteystietotyyppi1")
KOTIOSOITE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public PublicTranslatorResponseDTO listTranslators() {
// TODO: M.S. after migration is done use:
//final PersonalData personalData = personalDatas.get(t.getOnrId());
final PersonalData personalData = MigrationUtil.get(personalDatas.get(t.getOnrId()), t);
return personalData.getEmail() != null;
return personalData != null && personalData.getEmail() != null;
})
.collect(Collectors.toCollection(ArrayList::new));

Expand All @@ -67,7 +67,7 @@ public PublicTranslatorResponseDTO listTranslators() {
// TODO: M.S. after migration is done use:
//final PersonalData personalData = personalDatas.get(t.getOnrId());
final PersonalData personalData = MigrationUtil.get(personalDatas.get(t.getOnrId()), t);
return personalData.getEmail() == null;
return personalData != null && personalData.getEmail() == null;
})
.collect(Collectors.toCollection(ArrayList::new));

Expand All @@ -82,7 +82,7 @@ public PublicTranslatorResponseDTO listTranslators() {
//final PersonalData personalData = personalDatas.get(translator.getOnrId());
final PersonalData personalData = MigrationUtil.get(personalDatas.get(translator.getOnrId()), translator);

return createPublicTranslatorDTO(translator, personalData, languagePairDTOs);
return toDTO(translator, personalData, languagePairDTOs);
})
.toList();

Expand All @@ -108,7 +108,7 @@ private List<LanguagePairDTO> getLanguagePairDTOs(
.toList();
}

private PublicTranslatorDTO createPublicTranslatorDTO(
private PublicTranslatorDTO toDTO(
final Translator translator,
final PersonalData personalData,
final List<LanguagePairDTO> languagePairDTOS
Expand Down
26 changes: 25 additions & 1 deletion backend/otr/src/main/java/fi/oph/otr/onr/ContactDetailsUtil.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package fi.oph.otr.onr;

import static fi.oph.otr.onr.dto.ContactDetailsGroupType.AKR_OSOITE;
import static fi.oph.otr.onr.dto.ContactDetailsGroupType.KOTIMAINEN_POSTIOSOITE;
import static fi.oph.otr.onr.dto.ContactDetailsGroupType.KOTIOSOITE;
import static fi.oph.otr.onr.dto.ContactDetailsGroupType.OTR_OSOITE;
import static fi.oph.otr.onr.dto.ContactDetailsGroupType.AKR_OSOITE;
import static fi.oph.otr.onr.dto.ContactDetailsGroupType.SAHKOINEN_OSOITE;
import static fi.oph.otr.onr.dto.ContactDetailsGroupType.TILAPAINEN_KOTIMAAN_OSOITE;
import static fi.oph.otr.onr.dto.ContactDetailsGroupType.TILAPAINEN_ULKOMAAN_OSOITE;
Expand All @@ -19,8 +19,10 @@
import fi.oph.otr.onr.dto.ContactDetailsGroupSource;
import fi.oph.otr.onr.dto.ContactDetailsGroupType;
import fi.oph.otr.onr.dto.ContactDetailsType;
import fi.oph.otr.onr.dto.PersonalDataDTO;
import fi.oph.otr.onr.model.PersonalData;
import fi.oph.otr.util.CustomOrderComparator;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
Expand Down Expand Up @@ -139,4 +141,26 @@ private static ContactDetailsDTO createContactDetailsDTO(final ContactDetailsTyp
contactDetailsDTO.setValue(value);
return contactDetailsDTO;
}

private static List<ContactDetailsGroupDTO> getNonOtrContactDetails(
final List<ContactDetailsGroupDTO> latestContactDetails
) {
return latestContactDetails
.stream()
.filter(cd -> cd.getSource() != ContactDetailsGroupSource.OTR)
.collect(Collectors.toList());
}

static PersonalDataDTO combineContactDetails(
final PersonalDataDTO personalDataDTO,
final List<ContactDetailsGroupDTO> latestContactDetails
) {
final List<ContactDetailsGroupDTO> latestNonOtrContactDetails = getNonOtrContactDetails(latestContactDetails);
final List<ContactDetailsGroupDTO> combinedContactDetails = Stream
.of(latestNonOtrContactDetails, personalDataDTO.getContactDetailsGroups())
.flatMap(Collection::stream)
.collect(Collectors.toList());
personalDataDTO.setContactDetailsGroups(combinedContactDetails);
return personalDataDTO;
}
}
38 changes: 35 additions & 3 deletions backend/otr/src/main/java/fi/oph/otr/onr/OnrOperationApiImpl.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fi.oph.otr.onr;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import fi.oph.otr.config.Constants;
import fi.oph.otr.onr.dto.ContactDetailsGroupDTO;
Expand All @@ -12,7 +13,6 @@
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import lombok.RequiredArgsConstructor;
import net.minidev.json.JSONArray;
import org.asynchttpclient.Request;
import org.asynchttpclient.RequestBuilder;
Expand All @@ -21,7 +21,6 @@
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;

@RequiredArgsConstructor
public class OnrOperationApiImpl implements OnrOperationApi {

private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
Expand All @@ -30,6 +29,13 @@ public class OnrOperationApiImpl implements OnrOperationApi {

private final String onrServiceUrl;

public OnrOperationApiImpl(final CasClient onrClient, final String onrServiceUrl) {
this.onrClient = onrClient;
this.onrServiceUrl = onrServiceUrl;

OBJECT_MAPPER.configure(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL, true);
}

@Override
public Map<String, PersonalData> fetchPersonalDatas(final List<String> onrIds) throws Exception {
// /henkilo/masterHenkilosByOidList might be usable as an endpoint for fetching master person data for persons
Expand Down Expand Up @@ -132,14 +138,40 @@ public String insertPersonalData(final PersonalData personalData) throws Excepti
}
}

private PersonalDataDTO getPersonalData(final String oidNumber) throws Exception {
final Request request = defaultRequestBuilder()
.setUrl(onrServiceUrl + "/henkilo/" + oidNumber)
.setMethod(Methods.GET)
.build();

final Response response = onrClient.executeBlocking(request);

if (response.getStatusCode() == HttpStatus.CREATED.value()) {
final PersonalDataDTO personalDataDTO = OBJECT_MAPPER.readValue(
response.getResponseBody(),
new TypeReference<>() {}
);
return personalDataDTO;
} else {
throw new RuntimeException("ONR service returned unexpected status code: " + response.getStatusCode());
}
}

@Override
public void updatePersonalData(final PersonalData personalData) throws Exception {
final PersonalDataDTO personalDataDTO = createPersonalDataDTO(personalData);

final List<ContactDetailsGroupDTO> latestContactDetails = getPersonalData(personalData.getOnrId())
.getContactDetailsGroups();
final PersonalDataDTO combinedContactDetailsPersonalDataDTO = ContactDetailsUtil.combineContactDetails(
personalDataDTO,
latestContactDetails
);

final Request request = defaultRequestBuilder()
.setUrl(onrServiceUrl + "/henkilo")
.setMethod(Methods.PUT)
.setBody(OBJECT_MAPPER.writeValueAsString(personalDataDTO))
.setBody(OBJECT_MAPPER.writeValueAsString(combinedContactDetailsPersonalDataDTO))
.build();

final Response response = onrClient.executeBlocking(request);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package fi.oph.otr.onr.dto;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;

@JsonIgnoreProperties(ignoreUnknown = true)
public enum ContactDetailsGroupSource {
@JsonProperty("alkupera1")
VTJ,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package fi.oph.otr.onr.dto;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;

@JsonIgnoreProperties(ignoreUnknown = true)
public enum ContactDetailsGroupType {
@JsonProperty("yhteystietotyyppi1")
KOTIOSOITE,
Expand Down

0 comments on commit 38f3fc7

Please sign in to comment.