Skip to content

Commit e606996

Browse files
committed
XSL / Utility / Add multilingual support to getIndexField
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.
1 parent a0aadf5 commit e606996

File tree

6 files changed

+22
-61
lines changed

6 files changed

+22
-61
lines changed

core/src/main/java/org/fao/geonet/kernel/search/EsSearchManager.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -811,8 +811,8 @@ public SearchResponse query(JsonNode jsonRequest, Set<String> includedFields,
811811
return client.query(defaultIndex, jsonRequest, null, includedFields, from, size);
812812
}
813813

814-
public Map<String, String> getFieldsValues(String id, Set<String> fields) throws IOException {
815-
return client.getFieldsValues(defaultIndex, id, fields);
814+
public Map<String, String> getFieldsValues(String id, Set<String> fields, String language) throws Exception {
815+
return client.getFieldsValues(defaultIndex, id, fields, language);
816816
}
817817

818818

core/src/main/java/org/fao/geonet/util/XslUtil.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -843,11 +843,9 @@ public static String getIndexField(Object appName, Object uuid, Object field, Ob
843843
try {
844844
Set<String> fields = new HashSet<>();
845845
fields.add(fieldname);
846-
// TODO: Multilingual fields
847-
final Map<String, String> values = searchManager.getFieldsValues(id, fields);
846+
final Map<String, String> values = searchManager.getFieldsValues(id, fields, language);
848847
return values.get(fieldname);
849848
} catch (Exception e) {
850-
e.printStackTrace();
851849
Log.error(Geonet.GEONETWORK, "Failed to get index field '" + fieldname + "' value on '" + id + "', caused by " + e.getMessage());
852850
}
853851
return "";

index/src/main/java/org/fao/geonet/index/es/EsRestClient.java

+12-36
Original file line numberDiff line numberDiff line change
@@ -413,47 +413,23 @@ public Map<String, Object> getDocument(String index, String id) throws Exception
413413
/**
414414
* Query the index for a specific record and return values for a set of fields.
415415
*/
416-
public Map<String, String> getFieldsValues(String index, String id, Set<String> fields) throws IOException {
416+
public Map<String, String> getFieldsValues(String index, String id, Set<String> fields, String language) throws Exception {
417417
if (!activated) {
418418
return Collections.emptyMap();
419419
}
420420

421-
Map<String, String> fieldValues = new HashMap<>(fields.size());
422-
try {
423-
String query = String.format("_id:\"%s\"", id);
424-
// TODO: Check maxRecords
425-
// TODO: Use _doc API?
426-
427-
428-
final SearchResponse searchResponse = this.query(index, query, null, fields, new HashMap<>(), 0, 1, null);
429-
430-
List<Hit> totalHits = searchResponse.hits().hits();
431-
long matches = totalHits.size();
432-
if (matches == 0) {
433-
return fieldValues;
434-
} else if (matches == 1) {
435-
final Hit hit = totalHits.get(0);
436-
437-
fields.forEach(f -> {
438-
final Object o = hit.fields().get(f);
439-
if (o instanceof String) {
440-
fieldValues.put(f, (String) o);
441-
} else if (o instanceof HashMap && f.endsWith("Object")) {
442-
fieldValues.put(f, (String) ((HashMap) o).get("default"));
443-
}
444-
});
445-
} else {
446-
throw new IOException(String.format(
447-
"Your query '%s' returned more than one record, %d in fact. Can't retrieve field values for more than one record.",
448-
query,
449-
matches
450-
));
421+
Map<String, String> fieldValues = new HashMap<>();
422+
Map<String, Object> sources = getDocument(index, id);
423+
424+
for (String field : fields) {
425+
Object value = sources.get(field);
426+
if (value instanceof String) {
427+
fieldValues.put(field, (String) value);
428+
} else if (value instanceof Map && field.endsWith("Object")) {
429+
Map valueMap = (Map) value;
430+
String languageValue = (String) valueMap.get("lang" + language);
431+
fieldValues.put(field, languageValue != null ? languageValue : (String) valueMap.get("default"));
451432
}
452-
453-
} catch (Exception e) {
454-
throw new IOException(String.format(
455-
"Error during fields value retrieval. Errors is '%s'.", e.getMessage()
456-
));
457433
}
458434
return fieldValues;
459435
}

services/src/main/java/org/fao/geonet/api/records/MetadataWorkflowApi.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1024,7 +1024,7 @@ private List<MetadataStatusResponse> buildMetadataStatusResponses(List<MetadataS
10241024
fields.add(titleField);
10251025
Optional<Metadata> metadata = metadataRepository.findById(s.getMetadataId());
10261026
final Map<String, String> values =
1027-
searchManager.getFieldsValues(metadata.get().getUuid(), fields);
1027+
searchManager.getFieldsValues(metadata.get().getUuid(), fields, language);
10281028
title = values.get(titleField);
10291029
titles.put(s.getMetadataId(), title);
10301030
} catch (Exception e1) {

web/src/main/webapp/WEB-INF/data/data/formatter/xslt/render-functions.xsl

+2-15
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@
162162
<xsl:function name="gn-fn-render:getMetadataTitle">
163163
<xsl:param name="uuid" as="xs:string"/>
164164
<xsl:param name="language" as="xs:string"/>
165-
<!-- TODOES: Fallback to default language -->
165+
166166
<xsl:variable name="metadataTitle"
167167
select="util:getIndexField(
168168
$language,
@@ -171,20 +171,7 @@
171171
$language)"/>
172172
<xsl:choose>
173173
<xsl:when test="$metadataTitle=''">
174-
<xsl:variable name="metadataDefaultTitle"
175-
select="util:getIndexField(
176-
$language,
177-
$uuid,
178-
'resourceTitleObject',
179-
$language)"/>
180-
<xsl:choose>
181-
<xsl:when test="$metadataDefaultTitle=''">
182-
<xsl:value-of select="$uuid"/>
183-
</xsl:when>
184-
<xsl:otherwise>
185-
<xsl:value-of select="$metadataDefaultTitle"/>
186-
</xsl:otherwise>
187-
</xsl:choose>
174+
<xsl:value-of select="$uuid"/>
188175
</xsl:when>
189176
<xsl:otherwise>
190177
<xsl:value-of select="$metadataTitle"/>

web/src/main/webapp/xslt/base-layout.xsl

+4-4
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@
4949
<xsl:variable name="htmlHeadTitle"
5050
select="if ($discoveryServiceRecordUuid != '')
5151
then util:getIndexField(
52-
$lang,
52+
string($lang),
5353
$discoveryServiceRecordUuid,
5454
'resourceTitleObject',
55-
$lang)
55+
string($lang))
5656
else if (contains($nodeName, '|'))
5757
then substring-before($nodeName, '|')
5858
else $nodeName"/>
@@ -61,10 +61,10 @@
6161
<xsl:variable name="htmlHeadDescription"
6262
select="if ($discoveryServiceRecordUuid != '')
6363
then util:getIndexField(
64-
$lang,
64+
string($lang),
6565
$discoveryServiceRecordUuid,
6666
'resourceAbstractObject',
67-
$lang)
67+
string($lang))
6868
else if (contains($nodeName, '|'))
6969
then substring-after($nodeName, '|')
7070
else $nodeName"/>

0 commit comments

Comments
 (0)