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 ae70dcd5e62..873b9c3bcdf 100644 --- a/core/src/main/java/org/fao/geonet/kernel/EditLib.java +++ b/core/src/main/java/org/fao/geonet/kernel/EditLib.java @@ -33,18 +33,10 @@ import java.io.IOException; import java.io.StringReader; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.BitSet; -import java.util.HashMap; -import java.util.Iterator; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.UUID; -import java.util.Vector; +import java.util.*; import java.util.concurrent.ConcurrentHashMap; +import java.util.stream.Collectors; +import java.util.stream.Stream; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.jxpath.ri.parser.Token; @@ -272,7 +264,7 @@ private void addChildToParent(MetadataSchema mdSchema, Element targetElement, El // remove everything and then, depending on removeExisting // readd all children to the element and assure a correct position for the new one: at the end of the others // or just add the new one - List existingAllType = new ArrayList(targetElement.getChildren()); + List existingAllType = new ArrayList(targetElement.getChildren()); targetElement.removeContent(); for (String singleType: type.getAlElements()) { List existingForThisType = filterOnQname(existingAllType, singleType); @@ -283,9 +275,22 @@ private void addChildToParent(MetadataSchema mdSchema, Element targetElement, El LOGGER_ADD_ELEMENT.debug("#### - add child {}", existingChild.toString()); } } - if (qname.equals(singleType)) + if (qname.equals(singleType)) { targetElement.addContent(childToAdd); + } + + filterOnQname(existingAllType, "geonet:child") + .stream() + .filter(gnChild -> (gnChild.getAttributeValue("prefix") + ":" + gnChild.getAttributeValue("name")).equals(singleType)) + .findFirst() + .ifPresent(targetElement::addContent); } + + Stream.concat( + filterOnQname(existingAllType, "geonet:element").stream(), + filterOnQname(existingAllType, "geonet:attribute").stream() + ).forEach(targetElement::addContent); + } public void addXMLFragments(String schema, Element md, Map xmlInputs) throws Exception { diff --git a/services/src/main/java/org/fao/geonet/api/records/editing/AjaxEditUtils.java b/services/src/main/java/org/fao/geonet/api/records/editing/AjaxEditUtils.java index 25253fb95f0..cf06d7c9e34 100644 --- a/services/src/main/java/org/fao/geonet/api/records/editing/AjaxEditUtils.java +++ b/services/src/main/java/org/fao/geonet/api/records/editing/AjaxEditUtils.java @@ -355,7 +355,6 @@ public synchronized Element addElementEmbedded(UserSession session, String id, S //--- locate the geonet:element and geonet:info elements and clone for //--- later re-use - Element refEl = (Element) (el.getChild(Edit.RootChild.ELEMENT, Edit.NAMESPACE)).clone(); Element info = null; if (md.getChild(Edit.RootChild.INFO, Edit.NAMESPACE) != null) { @@ -376,6 +375,8 @@ public synchronized Element addElementEmbedded(UserSession session, String id, S if (defaultChild != null) { defaultValue = defaultChild.getAttributeValue(Edit.Attribute.Attr.VALUE); } + attributeDef.removeAttribute(Edit.Attribute.Attr.ADD); + attributeDef.setAttribute(new Attribute(Edit.Attribute.Attr.DEL, "true")); } } @@ -383,7 +384,6 @@ public synchronized Element addElementEmbedded(UserSession session, String id, S //--- Add new attribute with default value el.setAttribute(new Attribute(attInfo.two(), defaultValue, attInfo.one())); - // TODO : add attribute should be false and del true after adding an attribute child = el; } else { //--- normal element @@ -409,19 +409,13 @@ public synchronized Element addElementEmbedded(UserSession session, String id, S } //--- now enumerate the new child (if not a simple attribute) if (childName == null || !childName.equals("geonet:attribute")) { - //--- now add the geonet:element back again to keep ref number - el.addContent(refEl); - int iRef = editLib.findMaximumRef(md); - editLib.expandElements(schema, child); editLib.enumerateTreeStartingAt(child, iRef + 1, Integer.parseInt(ref)); - - //--- add editing info to everything from the parent down - editLib.expandTree(mds, el); - + editLib.expandTree(mds, child); } if (info != null) { - //--- attach the info element to the child + //--- remove and re-attach the info element to the child + child.removeChild(Edit.RootChild.INFO, Edit.NAMESPACE); child.addContent(info); }