Skip to content

Commit

Permalink
[Backport 4.2.x] Use UI language for metadata selection export to CSV…
Browse files Browse the repository at this point in the history
… / PDF.

Fixes geonetwork#7969.
Backport of geonetwork#8262.
  • Loading branch information
josegar74 authored and juanluisrp committed Jul 23, 2024
1 parent 8d26870 commit ad54e29
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 45 deletions.
104 changes: 68 additions & 36 deletions services/src/main/java/org/fao/geonet/api/records/CatalogApi.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2001-2023 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 @@ -61,13 +61,17 @@
import org.fao.geonet.repository.MetadataRepository;
import org.fao.geonet.util.XslUtil;
import org.fao.geonet.utils.Log;
import org.fao.geonet.web.DefaultLanguage;
import org.jdom.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletContext;
import javax.servlet.ServletOutputStream;
Expand All @@ -81,6 +85,7 @@
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.Map.Entry;
import java.util.stream.Collectors;

import static org.fao.geonet.api.ApiParams.*;
import static org.fao.geonet.kernel.mef.MEFLib.Version.Constants.MEF_V1_ACCEPT_TYPE;
Expand Down Expand Up @@ -114,17 +119,21 @@ public class CatalogApi {
.add("geom")
.add(SOURCE_CATALOGUE)
.add(Geonet.IndexFieldNames.DATABASE_CHANGE_DATE)
.add("resourceTitleObject.default") // TODOES multilingual
.add("resourceAbstractObject.default").build();
.add(Geonet.IndexFieldNames.RESOURCETITLE + "Object")
.add(Geonet.IndexFieldNames.RESOURCEABSTRACT + "Object").build();
}

@Autowired
DefaultLanguage defaultLanguage;
@Autowired
ThesaurusManager thesaurusManager;
@Autowired
MetadataRepository metadataRepository;
@Autowired
IMetadataUtils metadataUtils;
@Autowired
SchemaManager schemaManager;
@Autowired
DataManager dataManager;
@Autowired
GeonetworkDataDirectory dataDirectory;
Expand All @@ -150,7 +159,7 @@ private static String paramsAsString(Map<String, String> requestParams) {
StringBuilder paramNonPaging = new StringBuilder();
for (Entry<String, String> pair : requestParams.entrySet()) {
if (!pair.getKey().equals("from") && !pair.getKey().equals("to")) {
paramNonPaging.append(paramNonPaging.toString().equals("") ? "" : "&").append(pair.getKey()).append("=").append(pair.getValue());
paramNonPaging.append(paramNonPaging.toString().isEmpty() ? "" : "&").append(pair.getKey()).append("=").append(pair.getValue());
}
}
return paramNonPaging.toString();
Expand Down Expand Up @@ -251,7 +260,7 @@ public void exportAsMef(
if (version == MEFLib.Version.V1) {
throw new IllegalArgumentException("MEF version 1 only support one record. Use the /records/{uuid}/formatters/zip to retrieve that format");
} else {
Set<String> allowedUuid = new HashSet<String>();
Set<String> allowedUuid = new HashSet<>();
for (String uuid : uuidList) {
try {
ApiUtils.canViewRecord(uuid, request);
Expand All @@ -271,7 +280,7 @@ public void exportAsMef(
if (withRelated) {
int maxhits = Integer.parseInt(settingInfo.getSelectionMaxRecords());

Set<String> tmpUuid = new HashSet<String>();
Set<String> tmpUuid = new HashSet<>();
for (String uuid : allowedUuid) {
Map<RelatedItemType, List<AssociatedRecord>> associated =
MetadataUtils.getAssociated(context,
Expand Down Expand Up @@ -351,6 +360,11 @@ public void exportAsPdf(
required = false
)
String bucket,
@RequestParam(
required = false,
defaultValue = "eng"
)
String language,
@Parameter(hidden = true)
@RequestParam
Map<String, String> allRequestParams,
Expand Down Expand Up @@ -379,81 +393,86 @@ public void exportAsPdf(

Map<String, Object> params = new HashMap<>();
Element request = new Element("request");
allRequestParams.entrySet().forEach(e -> {
Element n = new Element(e.getKey());
n.setText(e.getValue());
allRequestParams.forEach((key, value) -> {
Element n = new Element(key);
n.setText(value);
request.addContent(n);
});

if (!languageUtils.getUiLanguages().contains(language)) {
language = languageUtils.getDefaultUiLanguage();
}

String langCode = "lang" + language;

Element response = new Element("response");
Arrays.asList(searchResponse.getHits().getHits()).forEach(h -> {
Element r = new Element("metadata");
final Map<String, Object> source = h.getSourceAsMap();
source.entrySet().forEach(e -> {
Object v = e.getValue();
source.forEach((key, v) -> {
if (v instanceof String) {
Element t = new Element(e.getKey());
Element t = new Element(key);
t.setText((String) v);
r.addContent(t);
} else if (v instanceof HashMap && e.getKey().endsWith("Object")) {
Element t = new Element(e.getKey());
Map<String, String> textFields = (HashMap) e.getValue();
t.setText(textFields.get("default"));
} else if (v instanceof HashMap && key.endsWith("Object")) {
Element t = new Element(key);
Map<String, String> textFields = (HashMap) v;
String textValue = textFields.get(langCode) != null ? textFields.get(langCode) : textFields.get("default");
t.setText(textValue);
r.addContent(t);
} else if (v instanceof ArrayList && e.getKey().equals("link")) {
} else if (v instanceof ArrayList && key.equals("link")) {
//landform|Physiography of North and Central Eurasia Landform|http://geonetwork3.fao.org/ows/7386_landf|OGC:WMS-1.1.1-http-get-map|application/vnd.ogc.wms_xml
((ArrayList) v).forEach(i -> {
Element t = new Element(e.getKey());
Element t = new Element(key);
Map<String, String> linkProperties = (HashMap) i;
t.setText(linkProperties.get("description") + "|" + linkProperties.get("name") + "|" + linkProperties.get("url") + "|" + linkProperties.get("protocol"));
r.addContent(t);
});
} else if (v instanceof HashMap && e.getKey().equals("overview")) {
Element t = new Element(e.getKey());
} else if (v instanceof HashMap && key.equals("overview")) {
Element t = new Element(key);
Map<String, String> overviewProperties = (HashMap) v;
t.setText(overviewProperties.get("url") + "|" + overviewProperties.get("name"));
r.addContent(t);
} else if (v instanceof ArrayList) {
((ArrayList) v).forEach(i -> {
if (i instanceof HashMap && e.getKey().equals("overview")) {
Element t = new Element(e.getKey());
if (i instanceof HashMap && key.equals("overview")) {
Element t = new Element(key);
Map<String, String> overviewProperties = (HashMap) i;
t.setText(overviewProperties.get("url") + "|" + overviewProperties.get("name"));
r.addContent(t);
} else if (i instanceof HashMap) {
Element t = new Element(e.getKey());
Element t = new Element(key);
Map<String, String> tags = (HashMap) i;
t.setText(tags.get("default")); // TODOES: Multilingual support
r.addContent(t);
} else {
Element t = new Element(e.getKey());
Element t = new Element(key);
t.setText((String) i);
r.addContent(t);
}
});
} else if (v instanceof HashMap && e.getKey().equals("geom")) {
Element t = new Element(e.getKey());
} else if (v instanceof HashMap && key.equals("geom")) {
Element t = new Element(key);
t.setText(((HashMap) v).get("coordinates").toString());
r.addContent(t);
} else if (v instanceof HashMap) {
// Skip.
} else {
Element t = new Element(e.getKey());
Element t = new Element(key);
t.setText(v.toString());
r.addContent(t);
}
});
response.addContent(r);
});

Locale locale = languageUtils.parseAcceptLanguage(httpRequest.getLocales());
String language = IsoLanguagesMapper.iso639_2T_to_iso639_2B(locale.getISO3Language());
language = XslUtil.twoCharLangCode(language, "eng").toLowerCase();

new XsltResponseWriter("env", "search")
.withJson(String.format("catalog/locales/%s-v4.json", language))
.withJson(String.format("catalog/locales/%s-core.json", language))
.withJson(String.format("catalog/locales/%s-search.json", language))
String language2Code = XslUtil.twoCharLangCode(language, "eng").toLowerCase();

new XsltResponseWriter("env", "search", language)
.withJson(String.format("catalog/locales/%s-v4.json", language2Code))
.withJson(String.format("catalog/locales/%s-core.json", language2Code))
.withJson(String.format("catalog/locales/%s-search.json", language2Code))
.withXml(response)
.withParams(params)
.withXsl("xslt/services/pdf/portal-present-fop.xsl")
Expand Down Expand Up @@ -490,6 +509,11 @@ public void exportAsCsv(
required = false
)
String bucket,
@RequestParam(
required = false,
defaultValue = "eng"
)
String language,
@Parameter(hidden = true)
@RequestParam
Map<String, String> allRequestParams,
Expand Down Expand Up @@ -524,7 +548,15 @@ public void exportAsCsv(
}
});

Element r = new XsltResponseWriter(null, "search")
if (!languageUtils.getUiLanguages().contains(language)) {
language = languageUtils.getDefaultUiLanguage();
}

Element r = new XsltResponseWriter(null, "search", language)
.withParams(allRequestParams.entrySet().stream()
.collect(Collectors.toMap(
Entry::getKey,
Entry::getValue)))
.withXml(response)
.withXsl("xslt/services/csv/csv-search.xsl")
.asElement();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* =============================================================================
* === Copyright (C) 2001-2023 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 @@ -60,6 +60,10 @@ public class XsltResponseWriter {
Map<String, Object> xslParams = new HashMap<>();

public XsltResponseWriter(String envTagName, String serviceName) {
this(envTagName, serviceName, "eng");
}

public XsltResponseWriter(String envTagName, String serviceName, String lang) {
SettingManager settingManager = ApplicationContextHolder.get().getBean(SettingManager.class);
String url = settingManager.getBaseURL();
Element gui = new Element("gui");
Expand All @@ -69,8 +73,7 @@ public XsltResponseWriter(String envTagName, String serviceName) {
gui.addContent(new Element("nodeUrl").setText(settingManager.getNodeURL()));
gui.addContent(new Element("baseUrl").setText(settingManager.getBaseURL()));
gui.addContent(new Element("serverUrl").setText(settingManager.getServerURL()));
// TODO: set language based on header
gui.addContent(new Element("language").setText("eng"));
gui.addContent(new Element("language").setText(lang));


Element settings = settingManager.getAllAsXML(true);
Expand All @@ -93,8 +96,7 @@ public XsltResponseWriter withXml(Element xml) {
public XsltResponseWriter withXsl(String xsl) {
ApplicationContext applicationContext = ApplicationContextHolder.get();
GeonetworkDataDirectory dataDirectory = applicationContext.getBean(GeonetworkDataDirectory.class);
Path xslt = dataDirectory.getWebappDir().resolve(xsl);
this.xsl = xslt;
this.xsl = dataDirectory.getWebappDir().resolve(xsl);
return this;
}

Expand Down Expand Up @@ -152,7 +154,7 @@ public XsltResponseWriter withJson(String json) {
});
} catch (IOException e) {
Log.warning(Geonet.GEONETWORK, String.format(
"Can't find JSON file '%s'.", jsonPath.toString()
"Can't find JSON file '%s'.", jsonPath
));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"$q",
"$http",
"gnConfig",
"gnLangs",
function (
$rootScope,
$timeout,
Expand All @@ -67,7 +68,8 @@
$translate,
$q,
$http,
gnConfig
gnConfig,
gnLangs
) {
var windowName = "geonetwork";
var windowOption = "";
Expand Down Expand Up @@ -154,7 +156,7 @@
if (params.sortOrder) {
url += "&sortOrder=" + params.sortOrder;
}
url += "&bucket=" + bucket;
url += "&bucket=" + bucket + "&language=" + gnLangs.current;
location.replace(url);
} else if (angular.isString(params)) {
gnMdFormatter.getFormatterUrl(null, null, params).then(function (url) {
Expand Down Expand Up @@ -194,7 +196,11 @@
};

this.exportCSV = function (bucket) {
window.open("../api/records/csv" + "?bucket=" + bucket, windowName, windowOption);
window.open(
"../api/records/csv" + "?bucket=" + bucket + "&language=" + gnLangs.current,
windowName,
windowOption
);
};
this.validateMdLinks = function (bucket) {
$rootScope.$broadcast("operationOnSelectionStart");
Expand Down

0 comments on commit ad54e29

Please sign in to comment.