Skip to content

Commit

Permalink
XSL / Utility / Add multilingual support to getIndexField (#8065)
Browse files Browse the repository at this point in the history
The function is not much used but can be tested by configuring a
multilingual metadata as CSW service.

Then the document title is the title of the metadata record and should
match UI language or use default if not found.

Co-authored-by: Francois Prunayre <fx.prunayre@gmail.com>
  • Loading branch information
ianwallen and fxprunayre committed May 31, 2024
1 parent 73b0db7 commit ec1b58b
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -792,8 +792,8 @@ public SearchResponse query(JsonNode jsonRequest, Set<String> includedFields,
return client.query(defaultIndex, jsonRequest, null, includedFields, from, size);
}

public Map<String, String> getFieldsValues(String id, Set<String> fields) throws IOException {
return client.getFieldsValues(defaultIndex, id, fields);
public Map<String, String> getFieldsValues(String id, Set<String> fields, String language) throws Exception {
return client.getFieldsValues(defaultIndex, id, fields, language);
}


Expand Down
4 changes: 1 addition & 3 deletions core/src/main/java/org/fao/geonet/util/XslUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -859,11 +859,9 @@ public static String getIndexField(Object appName, Object uuid, Object field, Ob
try {
Set<String> fields = new HashSet<>();
fields.add(fieldname);
// TODO: Multilingual fields
final Map<String, String> values = searchManager.getFieldsValues(id, fields);
final Map<String, String> values = searchManager.getFieldsValues(id, fields, language);
return values.get(fieldname);
} catch (Exception e) {
e.printStackTrace();
Log.error(Geonet.GEONETWORK, "Failed to get index field '" + fieldname + "' value on '" + id + "', caused by " + e.getMessage());
}
return "";
Expand Down
50 changes: 12 additions & 38 deletions index/src/main/java/org/fao/geonet/index/es/EsRestClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -378,49 +378,23 @@ public Map<String, Object> getDocument(String index, String id) throws Exception
/**
* Query the index for a specific record and return values for a set of fields.
*/
public Map<String, String> getFieldsValues(String index, String id, Set<String> fields) throws IOException {
public Map<String, String> getFieldsValues(String index, String id, Set<String> fields, String language) throws Exception {
if (!activated) {
return Collections.emptyMap();
}

Map<String, String> fieldValues = new HashMap<>(fields.size());
try {
String query = String.format("_id:\"%s\"", id);
// TODO: Check maxRecords
// TODO: Use _doc API?
final SearchResponse searchResponse = this.query(index, query, null, fields, new HashMap<>(), 0, 1, null);
if (searchResponse.status().getStatus() == 200) {
TotalHits totalHits = searchResponse.getHits().getTotalHits();
long matches = totalHits == null ? -1 : totalHits.value;
if (matches == 0) {
return fieldValues;
} else if (matches == 1) {
final SearchHit[] hits = searchResponse.getHits().getHits();

fields.forEach(f -> {
final Object o = hits[0].getSourceAsMap().get(f);
if (o instanceof String) {
fieldValues.put(f, (String) o);
} else if (o instanceof HashMap && f.endsWith("Object")) {
fieldValues.put(f, (String) ((HashMap) o).get("default"));
}
});
} else {
throw new IOException(String.format(
"Your query '%s' returned more than one record, %d in fact. Can't retrieve field values for more than one record.",
query,
matches
));
}
} else {
throw new IOException(String.format(
"Error during fields value retrieval. Status is '%s'.", searchResponse.status().getStatus()
));
Map<String, String> fieldValues = new HashMap<>();
Map<String, Object> sources = getDocument(index, id);

for (String field : fields) {
Object value = sources.get(field);
if (value instanceof String) {
fieldValues.put(field, (String) value);
} else if (value instanceof Map && field.endsWith("Object")) {
Map valueMap = (Map) value;
String languageValue = (String) valueMap.get("lang" + language);
fieldValues.put(field, languageValue != null ? languageValue : (String) valueMap.get("default"));
}
} catch (Exception e) {
throw new IOException(String.format(
"Error during fields value retrieval. Errors is '%s'.", e.getMessage()
));
}
return fieldValues;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1024,7 +1024,7 @@ private List<MetadataStatusResponse> buildMetadataStatusResponses(List<MetadataS
fields.add(titleField);
Optional<Metadata> metadata = metadataRepository.findById(s.getMetadataId());
final Map<String, String> values =
searchManager.getFieldsValues(metadata.get().getUuid(), fields);
searchManager.getFieldsValues(metadata.get().getUuid(), fields, language);
title = values.get(titleField);
titles.put(s.getMetadataId(), title);
} catch (Exception e1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@
<xsl:function name="gn-fn-render:getMetadataTitle">
<xsl:param name="uuid" as="xs:string"/>
<xsl:param name="language" as="xs:string"/>
<!-- TODOES: Fallback to default language -->

<xsl:variable name="metadataTitle"
select="util:getIndexField(
$language,
Expand All @@ -131,20 +131,7 @@
$language)"/>
<xsl:choose>
<xsl:when test="$metadataTitle=''">
<xsl:variable name="metadataDefaultTitle"
select="util:getIndexField(
$language,
$uuid,
'resourceTitleObject',
$language)"/>
<xsl:choose>
<xsl:when test="$metadataDefaultTitle=''">
<xsl:value-of select="$uuid"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$metadataDefaultTitle"/>
</xsl:otherwise>
</xsl:choose>
<xsl:value-of select="$uuid"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$metadataTitle"/>
Expand Down
8 changes: 4 additions & 4 deletions web/src/main/webapp/xslt/base-layout.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@
<xsl:variable name="htmlHeadTitle"
select="if ($discoveryServiceRecordUuid != '')
then util:getIndexField(
$lang,
string($lang),
$discoveryServiceRecordUuid,
'resourceTitleObject',
$lang)
string($lang))
else if (contains($nodeName, '|'))
then substring-before($nodeName, '|')
else $nodeName"/>
Expand All @@ -61,10 +61,10 @@
<xsl:variable name="htmlHeadDescription"
select="if ($discoveryServiceRecordUuid != '')
then util:getIndexField(
$lang,
string($lang),
$discoveryServiceRecordUuid,
'resourceAbstractObject',
$lang)
string($lang))
else if (contains($nodeName, '|'))
then substring-after($nodeName, '|')
else $nodeName"/>
Expand Down

0 comments on commit ec1b58b

Please sign in to comment.