Skip to content

Commit

Permalink
Merge pull request #54 from aodn/features/5311-fix-null-pointer
Browse files Browse the repository at this point in the history
Fix null pointer and add log
  • Loading branch information
vietnguyengit authored Mar 1, 2024
2 parents 75f4f0e + ab572ba commit 1f57022
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 34 deletions.
6 changes: 5 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@

<repositories>
<repository>
<id>mvnrepository.com</id>
<id>osgeo</id>
<url>https://repo.osgeo.org/repository/geonetwork-releases</url>
</repository>
<repository>
<id>geotools</id>
<url>https://repo.osgeo.org/repository/release/</url>
</repository>
</repositories>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public Iterable<String> getAllMetadataRecords() {
// TODO: Can the elastic index not update after insert dataset into GeoNetwork?
final SearchResponse<ObjectNode> response = gn4ElasticClient.search(GEONETWORK_ALL_UUID, ObjectNode.class);

if(Objects.requireNonNull(!response.hits().hits().isEmpty())) {
if(response.hits() != null && response.hits().hits() != null && !response.hits().hits().isEmpty()) {
// Use iterator so that we can get record by record, otherwise we need to store all record
// in memory which use up lots of memory
return () -> new Iterator<>() {
Expand Down Expand Up @@ -198,7 +198,7 @@ public String next() {
};
}
else {
throw new MetadataNotFoundException("Unable to find metadata records in GeoNetwork");
throw new MetadataNotFoundException("Unable to find any metadata records in GeoNetwork");
}
}
catch(IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -411,23 +411,32 @@ List<LinkModel> mapLinks(MDMetadataType source) {
List<ProviderModel> mapProviders(MDMetadataType source) {
List<ProviderModel> results = new ArrayList<>();
source.getContact().forEach(item -> {
if (item.getAbstractResponsibility() != null && item.getAbstractResponsibility().getValue() instanceof CIResponsibilityType2 ciResponsibility) {
ciResponsibility.getParty().forEach(party -> {
try {
ProviderModel providerModel = ProviderModel.builder().build();
providerModel.setRoles(Collections.singletonList(ciResponsibility.getRole().getCIRoleCode().getCodeListValue()));
CIOrganisationType2 organisationType2 = (CIOrganisationType2) party.getAbstractCIParty().getValue();
providerModel.setName(organisationType2.getName().getCharacterString().getValue().toString());
organisationType2.getIndividual().forEach(individual -> individual.getCIIndividual().getContactInfo().forEach(contactInfo -> {
contactInfo.getCIContact().getOnlineResource().forEach(onlineResource -> {
providerModel.setUrl(onlineResource.getCIOnlineResource().getLinkage().getCharacterString().getValue().toString());
});
}));
results.add(providerModel);
} catch (ClassCastException e) {
logger.error("Unable to cast getAbstractCIParty().getValue() to CIOrganisationType2 for metadata record: " + this.mapUUID(source));
}
});
if (item.getAbstractResponsibility() != null) {
if(item.getAbstractResponsibility().getValue() instanceof CIResponsibilityType2 ciResponsibility) {
ciResponsibility.getParty().forEach(party -> {
try {
ProviderModel providerModel = ProviderModel.builder().build();
providerModel.setRoles(Collections.singletonList(ciResponsibility.getRole().getCIRoleCode().getCodeListValue()));
CIOrganisationType2 organisationType2 = (CIOrganisationType2) party.getAbstractCIParty().getValue();
providerModel.setName(organisationType2.getName().getCharacterString().getValue().toString());
organisationType2.getIndividual().forEach(individual -> individual.getCIIndividual().getContactInfo().forEach(contactInfo -> {
contactInfo.getCIContact().getOnlineResource().forEach(onlineResource -> {
providerModel.setUrl(onlineResource.getCIOnlineResource().getLinkage().getCharacterString().getValue().toString());
});
}));
results.add(providerModel);
}
catch (ClassCastException e) {
logger.error("Unable to cast getAbstractCIParty().getValue() to CIOrganisationType2 for metadata record: {}", mapUUID(source));
}
});
}
else {
logger.warn("getContact().getAbstractResponsibility() in mapProviders is not of type CIResponsibilityType2 for UUID {}", mapUUID(source));
}
}
else {
logger.warn("Null value fround for getContact().getAbstractResponsibility() in mapProviders transform for UUID {}", mapUUID(source));
}
});
return results;
Expand Down Expand Up @@ -481,14 +490,16 @@ List<ContactsModel> mapContacts(MDMetadataType source) {
for (MDDataIdentificationType item : items) {
item.getPointOfContact().forEach(poc -> {
if (poc.getAbstractResponsibility() != null) {

AbstractResponsibilityType responsibilityType = poc.getAbstractResponsibility().getValue();
if (responsibilityType instanceof CIResponsibilityType2 ciResponsibility) {
ContactsModel contactsModel = ContactsModel.builder().build();
contactsModel.setRoles(mapContactsRole(ciResponsibility));

if (ciResponsibility.getParty().isEmpty()) {
logger.warn("Unable to find contact info for metadata record: " + this.mapUUID(source));
} else {
}
else {
ciResponsibility.getParty().forEach(party -> {
contactsModel.setOrganization(mapContactsOrganization(party));
try {
Expand All @@ -506,7 +517,8 @@ List<ContactsModel> mapContacts(MDMetadataType source) {

if (organisation.getIndividual().isEmpty()) {
contactInfoList.set(organisation.getContactInfo());
} else {
}
else {
organisation.getIndividual().forEach(individual -> {
name.set(mapContactsName(individual));
position.set(mapContactsPosition(individual));
Expand Down Expand Up @@ -538,14 +550,18 @@ List<ContactsModel> mapContacts(MDMetadataType source) {
contactsModel.setPhones(phones);
contactsModel.setLinks(onlineResources);

} catch (Exception e) {
logger.warn("Unable to find contact info for metadata record: " + this.mapUUID(source));
}
catch (Exception e) {
logger.warn("Unable to find contact info for metadata record: {}", mapUUID(source));
}
});
results.add(contactsModel);
}
}
}
else {
logger.warn("getAbstractResponsibility() is null in mapContact for metadata record: {}", mapUUID(source));
}
});
}
}
Expand All @@ -554,23 +570,27 @@ List<ContactsModel> mapContacts(MDMetadataType source) {

protected String mapContactsRole(CIResponsibilityType2 ciResponsibility) {
CodeListValueType roleCode = ciResponsibility.getRole().getCIRoleCode();
if (roleCode != null) { return roleCode.getCodeListValue(); } else { return ""; }
return roleCode != null ?
roleCode.getCodeListValue() : "";
}

protected String mapContactsOrganization(AbstractCIPartyPropertyType2 party) {
String organisationString = party.getAbstractCIParty().getValue().getName().getCharacterString().getValue().toString();
if (organisationString != null) { return organisationString; } else { return ""; }
return organisationString != null ?
organisationString : "";

}

protected String mapContactsName(CIIndividualPropertyType2 individual) {
CharacterStringPropertyType nameString = individual.getCIIndividual().getName();
if (nameString != null) { return individual.getCIIndividual().getName().getCharacterString().getValue().toString(); } else { return ""; }
return nameString != null ?
individual.getCIIndividual().getName().getCharacterString().getValue().toString() : "";
}

protected String mapContactsPosition(CIIndividualPropertyType2 individual) {
CharacterStringPropertyType positionString = individual.getCIIndividual().getPositionName();
if (positionString != null) { return individual.getCIIndividual().getPositionName().getCharacterString().getValue().toString(); } else { return ""; }
return positionString != null ?
individual.getCIIndividual().getPositionName().getCharacterString().getValue().toString() : "";
}

protected Map<String, Object> mapContactsAddress(CIAddressPropertyType2 address) {
Expand Down Expand Up @@ -599,11 +619,8 @@ protected Map<String, Object> mapContactsAddress(CIAddressPropertyType2 address)
}

protected String mapContactsEmail(CharacterStringPropertyType electronicMailAddress) {
if (electronicMailAddress != null) {
return electronicMailAddress.getCharacterString().getValue().toString();
} else {
return "";
}
return electronicMailAddress != null ?
electronicMailAddress.getCharacterString().getValue().toString() : "";
}

protected Map<String, String> mapContactsPhone(CITelephonePropertyType2 phone) {
Expand Down Expand Up @@ -665,7 +682,8 @@ protected List<LanguageModel> mapLanguages(MDMetadataType source) {
protected String mapLanguagesCode(MDDataIdentificationType i) {
try {
return i.getDefaultLocale().getPTLocale().getValue().getLanguage().getLanguageCode().getCodeListValue();
} catch (NullPointerException e) {
}
catch (NullPointerException e) {
return null;
}
}
Expand Down

0 comments on commit 1f57022

Please sign in to comment.