From 89869984e854c64b454dedce145d9a846ef5c416 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jose=20Garc=C3=ADa?=
Date: Tue, 26 Nov 2024 11:34:22 +0100
Subject: [PATCH 1/4] Metadata editor / Support add embedded multilingual
elements for metadata schemas that require to duplicate the element for each
language
---
.../java/org/fao/geonet/kernel/EditLib.java | 64 ++++++++++++++----
.../ISO19115_3_2018SchemaPlugin.java | 36 ++++++++++
.../schema/iso19139/ISO19139SchemaPlugin.java | 27 ++++++++
.../schema/MultilingualSchemaPlugin.java | 59 +++++++++++++++++
.../api/records/editing/AjaxEditUtils.java | 66 ++++++++++++++-----
.../records/editing/MetadataEditingApi.java | 28 ++++++--
6 files changed, 244 insertions(+), 36 deletions(-)
diff --git a/core/src/main/java/org/fao/geonet/kernel/EditLib.java b/core/src/main/java/org/fao/geonet/kernel/EditLib.java
index 873b9c3bcdf..516185a3fe8 100644
--- a/core/src/main/java/org/fao/geonet/kernel/EditLib.java
+++ b/core/src/main/java/org/fao/geonet/kernel/EditLib.java
@@ -3,7 +3,7 @@
//=== EditLib
//===
//=============================================================================
-//=== 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)
//===
@@ -35,7 +35,6 @@
import java.io.StringReader;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
-import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.apache.commons.collections.CollectionUtils;
@@ -50,6 +49,7 @@
import org.fao.geonet.kernel.schema.MetadataAttribute;
import org.fao.geonet.kernel.schema.MetadataSchema;
import org.fao.geonet.kernel.schema.MetadataType;
+import org.fao.geonet.kernel.schema.MultilingualSchemaPlugin;
import org.fao.geonet.kernel.schema.SchemaPlugin;
import org.fao.geonet.utils.Xml;
import org.jaxen.JaxenException;
@@ -223,6 +223,45 @@ public Element addElement(MetadataSchema mdSchema, Element el, String qname) thr
return child;
}
+ /**
+ * Creates an element with a name (qname) as child of an element (el).
+ *
+ * If the element to create is multilingual and the related metadata schemas requires to duplicate it,
+ * one child per metadata language is added.
+ *
+ * See {@link org.fao.geonet.kernel.schema.MultilingualSchemaPlugin#duplicateElementsForMultilingual()}
+ *
+ * @param mdSchema Metadata schema
+ * @param el Parent element to add the new element.
+ * @param qname Name of the new element to add.
+ * @param languages Languages to add to the new elements.
+ * @return List of child elements added. For non-multilingual, contains 1 element.
+ * @throws Exception
+ */
+ public List addElements(MetadataSchema mdSchema, Element el,
+ String qname, List languages) throws Exception {
+
+ List result = new ArrayList<>();
+
+ MultilingualSchemaPlugin multilingualSchemaPlugin = (MultilingualSchemaPlugin) mdSchema.getSchemaPlugin();
+
+ if (!languages.isEmpty() &&
+ multilingualSchemaPlugin.duplicateElementsForMultilingual() &&
+ multilingualSchemaPlugin.isMultilingualElementType(mdSchema.getElementType(qname, el.getName()))) {
+ for(String language : languages) {
+ Element child = addElement(mdSchema, el, qname);
+ ((MultilingualSchemaPlugin) mdSchema.getSchemaPlugin()).addTranslationToElement(child, language, "");
+ result.add(child);
+ }
+ return result;
+ } else {
+ // If no multilingual management is required, process the single element.
+ result.add(addElement(mdSchema, el, qname));
+ }
+
+ return result;
+ }
+
/**
* Adds XML fragment to the metadata record in the last element of the type of the element in
* its parent.
@@ -968,7 +1007,7 @@ public void clearVersion(String id) {
//--------------------------------------------------------------------------
private List filterOnQname(List children, String qname) {
- Vector result = new Vector();
+ Vector result = new Vector<>();
for (Element child : children) {
if (child.getQualifiedName().equals(qname)) {
result.add(child);
@@ -1166,7 +1205,7 @@ public List searchChildren(String chName, Element md, String schema) th
//
boolean hasContent = false;
- Vector holder = new Vector();
+ Vector holder = new Vector<>();
MetadataSchema mdSchema = scm.getSchema(schema);
String chUQname = getUnqualifiedName(chName);
@@ -1222,12 +1261,12 @@ public void expandElements(String schema, Element md) throws Exception {
MetadataType thisType = mdSchema.getTypeInfo(typeName);
if (thisType.hasContainers) {
- Vector holder = new Vector();
+ Vector holder = new Vector<>();
for (String chName: thisType.getAlElements()) {
if (edit_CHOICE_GROUP_SEQUENCE_in(chName)) {
List elems = searchChildren(chName, md, schema);
- if (elems.size() > 0) {
+ if (!elems.isEmpty()) {
holder.addAll(elems);
}
} else {
@@ -1246,7 +1285,7 @@ public void expandElements(String schema, Element md) throws Exception {
* For each container element - descend and collect children.
*/
private Vector
+ */
+ private int getGroupSwapIndex(List list, int swapIndex, List languages) {
+ String elementLanguage = list.get(swapIndex).getAttributeValue("lang", Namespace.XML_NAMESPACE);
+ if (list.size() == 1 || languages.size() == 1 || elementLanguage == null) {
+ return -1;
+ }
+
+ // Consider element are always in order (see update-fixed-info.xsl)
+ // River index = 3, group index = 3 - (en lang index)
+ int indexInGroup = languages.indexOf(elementLanguage);
+ if (indexInGroup == -1) {
+ return swapIndex; // Language not declared?
+ }
+ return swapIndex - indexInGroup;
+ }
+ private static int getElementSwapIndex(List list, Element elSwap) {
+ int i = -1;
+ for (Element element : list) {
+ i++;
+ if (element == elSwap) {
+ return i;
+ }
+ }
+ return i;
}
/**
From 3d7a65cb515f090e04d4e169a43a4e0180af7f9d Mon Sep 17 00:00:00 2001
From: Francois Prunayre
Date: Mon, 9 Dec 2024 09:11:17 +0100
Subject: [PATCH 3/4] Metadata editor / Support add embedded multilingual /
Simplify XPath and sonarlint.
---
.../ISO19115_3_2018SchemaPlugin.java | 18 ++++--------------
.../schema/iso19139/ISO19139SchemaPlugin.java | 6 ++----
2 files changed, 6 insertions(+), 18 deletions(-)
diff --git a/schemas/iso19115-3.2018/src/main/java/org/fao/geonet/schema/iso19115_3_2018/ISO19115_3_2018SchemaPlugin.java b/schemas/iso19115-3.2018/src/main/java/org/fao/geonet/schema/iso19115_3_2018/ISO19115_3_2018SchemaPlugin.java
index d765a91abc5..261c2b1ae78 100644
--- a/schemas/iso19115-3.2018/src/main/java/org/fao/geonet/schema/iso19115_3_2018/ISO19115_3_2018SchemaPlugin.java
+++ b/schemas/iso19115-3.2018/src/main/java/org/fao/geonet/schema/iso19115_3_2018/ISO19115_3_2018SchemaPlugin.java
@@ -588,23 +588,13 @@ public boolean duplicateElementsForMultilingual() {
@Override
public List getMetadataLanguages(Element metadata) {
try {
- Xml.selectNodes(metadata, ".//mdb:defaultLocale/lan:PT_Locale/@id", allNamespaces.asList())
+ return Xml.selectNodes(metadata, ".//*[name() = 'mdb:defaultLocale' or name() = 'mdb:otherLocale']/lan:PT_Locale/@id", allNamespaces.asList())
.stream()
- .filter(node -> node instanceof Attribute)
+ .filter(Attribute.class::isInstance)
.map(node -> ((Attribute)node).getValue())
.filter(s -> s != null && !s.isBlank())
- .filter(Objects::nonNull)
- .collect(Collectors.toList()).addAll(
- Xml.selectNodes(metadata, ".//mdb:otherLocale/lan:PT_Locale/@id", allNamespaces.asList())
- .stream()
- .filter(node -> node instanceof Attribute)
- .map(node -> ((Attribute)node).getValue())
- .filter(s -> s != null && !s.isBlank())
- .filter(Objects::nonNull)
- .collect(Collectors.toList())
- );
- } catch (JDOMException e) {
- e.printStackTrace();
+ .collect(Collectors.toList());
+ } catch (JDOMException ignored) {
}
return Collections.emptyList();
}
diff --git a/schemas/iso19139/src/main/java/org/fao/geonet/schema/iso19139/ISO19139SchemaPlugin.java b/schemas/iso19139/src/main/java/org/fao/geonet/schema/iso19139/ISO19139SchemaPlugin.java
index f7345c8a1ae..e3a48470065 100644
--- a/schemas/iso19139/src/main/java/org/fao/geonet/schema/iso19139/ISO19139SchemaPlugin.java
+++ b/schemas/iso19139/src/main/java/org/fao/geonet/schema/iso19139/ISO19139SchemaPlugin.java
@@ -617,13 +617,11 @@ public List getMetadataLanguages(Element metadata) {
try {
return Xml.selectNodes(metadata, ".//gmd:locale/gmd:PT_Locale/@id", allNamespaces.asList())
.stream()
- .filter(node -> node instanceof Attribute)
+ .filter(Attribute.class::isInstance)
.map(node -> ((Attribute)node).getValue())
.filter(s -> s != null && !s.isBlank())
- .filter(Objects::nonNull)
.collect(Collectors.toList());
- } catch (JDOMException e) {
- e.printStackTrace();
+ } catch (JDOMException ignored) {
}
return Collections.emptyList();
}
From 1e51f4d0c345f6509a5431f5d48fb5e175ca1056 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jose=20Garc=C3=ADa?=
Date: Wed, 11 Dec 2024 12:07:46 +0100
Subject: [PATCH 4/4] Metadata editor / Support add embedded multilingual /
Check if a metadata schema is multilingual before doing a cast to
MultilingualSchemaPlugin
---
.../java/org/fao/geonet/kernel/EditLib.java | 28 ++++++++++---------
1 file changed, 15 insertions(+), 13 deletions(-)
diff --git a/core/src/main/java/org/fao/geonet/kernel/EditLib.java b/core/src/main/java/org/fao/geonet/kernel/EditLib.java
index 516185a3fe8..ce3733fdc85 100644
--- a/core/src/main/java/org/fao/geonet/kernel/EditLib.java
+++ b/core/src/main/java/org/fao/geonet/kernel/EditLib.java
@@ -243,22 +243,24 @@ public List addElements(MetadataSchema mdSchema, Element el,
List result = new ArrayList<>();
- MultilingualSchemaPlugin multilingualSchemaPlugin = (MultilingualSchemaPlugin) mdSchema.getSchemaPlugin();
-
- if (!languages.isEmpty() &&
- multilingualSchemaPlugin.duplicateElementsForMultilingual() &&
- multilingualSchemaPlugin.isMultilingualElementType(mdSchema.getElementType(qname, el.getName()))) {
- for(String language : languages) {
- Element child = addElement(mdSchema, el, qname);
- ((MultilingualSchemaPlugin) mdSchema.getSchemaPlugin()).addTranslationToElement(child, language, "");
- result.add(child);
+ if (mdSchema.getSchemaPlugin() instanceof MultilingualSchemaPlugin) {
+ MultilingualSchemaPlugin multilingualSchemaPlugin = (MultilingualSchemaPlugin) mdSchema.getSchemaPlugin();
+
+ if (!languages.isEmpty() &&
+ multilingualSchemaPlugin.duplicateElementsForMultilingual() &&
+ multilingualSchemaPlugin.isMultilingualElementType(mdSchema.getElementType(qname, el.getName()))) {
+ for(String language : languages) {
+ Element child = addElement(mdSchema, el, qname);
+ ((MultilingualSchemaPlugin) mdSchema.getSchemaPlugin()).addTranslationToElement(child, language, "");
+ result.add(child);
+ }
+ return result;
}
- return result;
- } else {
- // If no multilingual management is required, process the single element.
- result.add(addElement(mdSchema, el, qname));
}
+ // If no multilingual management is required, process the single element.
+ result.add(addElement(mdSchema, el, qname));
+
return result;
}