Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
cmangeat committed Feb 16, 2024
1 parent 13c4a0a commit 5e33a96
Showing 1 changed file with 20 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@

package org.fao.geonet.kernel.datamanager.base;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.base.Predicate;
Expand Down Expand Up @@ -747,31 +745,8 @@ public synchronized AbstractMetadata updateMetadata(final ServiceContext context
getSearchManager().delete(String.format("+uuid:\"%s\"", uuidBeforeUfo));
}
metadataIndexer.indexMetadata(metadataId, true, indexingMode);

if (metadata.getDataInfo().getType() == MetadataType.SUB_TEMPLATE) {
ObjectMapper objectMapper = new ObjectMapper();

JsonNode query = objectMapper.createObjectNode()
.set("bool", objectMapper.createObjectNode().set("must", objectMapper.createArrayNode()
.add(objectMapper.createObjectNode().set("query_string",
objectMapper.createObjectNode().put("query", String.format("xlink:*%s*", metadata.getUuid()))))
)
);

SearchResponse response = this.searchManager.query(query, Collections.singleton("_id"), 0, 10000, null);
ArrayList<String> toIndex = new ArrayList<>();
response.getHits().forEach(consumer -> {
String consumerUuid = consumer.getId();
try {
String consumerId = this.metadataUtils.getMetadataId(consumerUuid);
if (consumerId != null) {
toIndex.add(consumerId);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
});
metadataIndexer.batchIndexInThreadPool(context, toIndex);
indexMdsReferencingSubTemplate(context, metadata);
}
}
}
Expand Down Expand Up @@ -1340,4 +1315,23 @@ boolean hasReferencingMetadata(ServiceContext context, AbstractMetadata metadata
return this.searchManager.query(query.toString(), null, 0, 0).getHits().getTotalHits().value > 0;
}

private void indexMdsReferencingSubTemplate(ServiceContext context, AbstractMetadata subTemplate) throws Exception {
StringBuilder query = new StringBuilder(String.format("xlink:*%s*", subTemplate.getUuid()));
SearchResponse response = this.searchManager.query(query.toString(), null, 0, 10000);
ArrayList<String> toIndex = new ArrayList<>();
response.getHits().forEach(consumer -> {
String consumerUuid = consumer.getId();
try {
String consumerId = this.metadataUtils.getMetadataId(consumerUuid);
if (consumerId != null) {
toIndex.add(consumerId);
}
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new RuntimeException(e);
}
});
metadataIndexer.batchIndexInThreadPool(context, toIndex);
}
}

0 comments on commit 5e33a96

Please sign in to comment.