Skip to content

Commit

Permalink
CSW Harvester / Avoid increment 2 metrics for a single metadata in ce…
Browse files Browse the repository at this point in the history
…rtain conditions.

Retrieve metadata method increments metrics under certain conditions and returns null. Methods calling the retrieve metadata method increments additionally the unretrievable metric if null is returned. Sonarlint fixeas are already applied. Fixes geonetwork#8039
  • Loading branch information
josegar74 committed May 19, 2024
1 parent ca92682 commit 86a62b1
Showing 1 changed file with 15 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//=============================================================================
//=== Copyright (C) 2001-2007 Food and Agriculture Organization of the
//=== Copyright (C) 2001-2024 Food and Agriculture Organization of the
//=== United Nations (FAO-UN), United Nations World Food Programme (WFP)
//=== and United Nations Environment Programme (UNEP)
//===
Expand Down Expand Up @@ -232,7 +232,7 @@ private void insertOrUpdate(Collection<RecordInfo> records, Collection<HarvestEr
}

result.totalMetadata++;
} catch (Throwable t) {
} catch (Exception t) {
errors.add(new HarvestError(this.context, t));
log.error("Unable to process record from csw (" + this.params.getName() + ")");
log.error(" Record failed: " + ri.uuid + ". Error is: " + t.getMessage());
Expand Down Expand Up @@ -285,7 +285,6 @@ private void addMetadata(RecordInfo ri, String uuidToAssign) throws Exception {
Element md = retrieveMetadata(ri.uuid);

if (md == null) {
result.unretrievable++;
return;
}

Expand Down Expand Up @@ -399,7 +398,7 @@ public static void applyBatchEdits(
if (StringUtils.isNotEmpty(batchEditParameter.getCondition())) {
applyEdit = false;
final Object node = Xml.selectSingle(md, batchEditParameter.getCondition(), metadataSchema.getNamespaces());
if (node != null && node instanceof Boolean && (Boolean)node == true) {
if (node instanceof Boolean && Boolean.TRUE.equals(node)) {
applyEdit = true;
}
}
Expand All @@ -425,7 +424,7 @@ private void updateMetadata(RecordInfo ri, String id, Boolean force) throws Exce
if (date == null && !force) {
log.debug(" - Skipped metadata managed by another harvesting node. uuid:" + ri.uuid + ", name:" + params.getName());
} else {
if (!force && !ri.isMoreRecentThan(date)) {
if (Boolean.TRUE.equals(!force) && !ri.isMoreRecentThan(date)) {
log.debug(" - Metadata XML not changed for uuid:" + ri.uuid);
result.unchangedMetadata++;
} else {
Expand All @@ -442,7 +441,6 @@ boolean updatingLocalMetadata(RecordInfo ri, String id, Boolean force) throws Ex
Element md = retrieveMetadata(ri.uuid);

if (md == null) {
result.unchangedMetadata++;
return false;
}

Expand Down Expand Up @@ -474,8 +472,8 @@ boolean updatingLocalMetadata(RecordInfo ri, String id, Boolean force) throws Ex
String language = context.getLanguage();
final AbstractMetadata metadata = metadataManager.updateMetadata(context, id, md, validate, ufo, language, ri.changeDate, true, IndexingMode.none);

if (force || updateSchema) {
if (force) {
if (Boolean.TRUE.equals(force) || updateSchema) {
if (Boolean.TRUE.equals(force)) {
//change ownership of metadata to new harvester
metadata.getHarvestInfo().setUuid(params.getUuid());
metadata.getSourceInfo().setSourceId(params.getUuid());
Expand All @@ -495,8 +493,11 @@ boolean updatingLocalMetadata(RecordInfo ri, String id, Boolean force) throws Ex
}

/**
* Does CSW GetRecordById request. If validation is requested and the metadata does not
* validate, null is returned.
* Does CSW GetRecordById request. Returns null on error conditions:
* - If validation is requested and the metadata does not validate.
* - No metadata is retrieved.
* - If metadata resource is duplicated.
* - An exception occurs retrieving the metadata.
*
* @param uuid uuid of metadata to request
* @return metadata the metadata
Expand All @@ -519,6 +520,7 @@ private Element retrieveMetadata(String uuid) {
//--- maybe the metadata has been removed

if (list.isEmpty()) {
result.unretrievable++;
return null;
}

Expand All @@ -539,11 +541,10 @@ private Element retrieveMetadata(String uuid) {
return null;
}

if (params.rejectDuplicateResource) {
if (foundDuplicateForResource(uuid, response)) {
if (params.rejectDuplicateResource && (foundDuplicateForResource(uuid, response))) {
result.unchangedMetadata++;
return null;
}

}

return response;
Expand Down Expand Up @@ -607,7 +608,7 @@ private boolean foundDuplicateForResource(String uuid, Element response) {
}
}
}
} catch (Throwable e) {
} catch (Exception e) {
log.warning(" - Error when searching for resource duplicate " + uuid + ". Error is: " + e.getMessage());
}
}
Expand Down

0 comments on commit 86a62b1

Please sign in to comment.