diff --git a/core/src/main/java/org/fao/geonet/kernel/AddElemValue.java b/core/src/main/java/org/fao/geonet/kernel/AddElemValue.java index dd0992e6e40..1d3252d93ab 100644 --- a/core/src/main/java/org/fao/geonet/kernel/AddElemValue.java +++ b/core/src/main/java/org/fao/geonet/kernel/AddElemValue.java @@ -37,10 +37,11 @@ public class AddElemValue { private final String stringValue; private final Element nodeValue; + private String mode = null; public AddElemValue(String stringValue) throws JDOMException, IOException { Element finalNodeVal = null; - String finalStringVal = stringValue.replaceAll("", ""); + String finalStringVal = stringValue.replaceAll("", ""); if (Xml.isXMLLike(finalStringVal)) { try { finalNodeVal = Xml.loadString(stringValue, false); @@ -55,11 +56,13 @@ public AddElemValue(String stringValue) throws JDOMException, IOException { } this.nodeValue = finalNodeVal; this.stringValue = finalStringVal; + this.mode = parseMode(stringValue); } public AddElemValue(Element nodeValue) { this.nodeValue = nodeValue; this.stringValue = null; + this.mode = parseMode(Xml.getString(nodeValue)); } public boolean isXml() { @@ -73,4 +76,23 @@ public String getStringValue() { public Element getNodeValue() { return nodeValue; } + + private String parseMode(String value) { + if (value.startsWith("")) { + return EditLib.SpecialUpdateTags.ADD; + } else if (value.startsWith("")) { + return EditLib.SpecialUpdateTags.CREATE; + } else if (value.startsWith("")) { + return EditLib.SpecialUpdateTags.REPLACE_ALL; + } else if (value.startsWith("")) { + return EditLib.SpecialUpdateTags.REPLACE; + } else if (value.startsWith("")) { + return EditLib.SpecialUpdateTags.DELETE; + } + return EditLib.SpecialUpdateTags.ADD; + } + + public String getMode() { + return mode; + } } 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..04dc2f340d7 100644 --- a/core/src/main/java/org/fao/geonet/kernel/EditLib.java +++ b/core/src/main/java/org/fao/geonet/kernel/EditLib.java @@ -499,12 +499,8 @@ public boolean addElementOrFragmentFromXpath(Element metadataRecord, boolean isUpdated = false; try { final boolean isValueXml = value.isXml(); - final boolean isDeleteMode = value.getNodeValue() != null && - value.getNodeValue().getName() - .startsWith(SpecialUpdateTags.DELETE); - final boolean isCreateMode = value.getNodeValue() != null && - value.getNodeValue().getName() - .equals(SpecialUpdateTags.CREATE); + final boolean isDeleteMode = SpecialUpdateTags.DELETE.equals(value.getMode()); + final boolean isCreateMode = SpecialUpdateTags.CREATE.equals(value.getMode()); LOGGER_ADD_ELEMENT.debug("Inserting at location {} the snippet or value {}", xpathProperty, value); @@ -582,8 +578,7 @@ public boolean addElementOrFragmentFromXpath(Element metadataRecord, if (parent != null) { Element matchingNode = ((Element) propNode); // Remove only matching node - if (value.getNodeValue().getName() - .equals(SpecialUpdateTags.DELETE)) { + if (SpecialUpdateTags.DELETE.equals(value.getMode())) { parent.removeContent(parent.indexOf(matchingNode)); } } @@ -1819,7 +1814,7 @@ public interface SpecialUpdateTags { */ String REPLACE = "gn_replace"; /** - * Add to the target. + * Add to the target (or set value for an attribute). */ String ADD = "gn_add"; /** diff --git a/services/src/test/java/org/fao/geonet/services/metadata/BatchEditsServiceTest.java b/services/src/test/java/org/fao/geonet/services/metadata/BatchEditsServiceTest.java index 67ca12d6efb..075d0695fb0 100644 --- a/services/src/test/java/org/fao/geonet/services/metadata/BatchEditsServiceTest.java +++ b/services/src/test/java/org/fao/geonet/services/metadata/BatchEditsServiceTest.java @@ -171,7 +171,7 @@ public void testUpdateRecordElementInsertWithParentXpath() throws Exception { BatchEditParameter[] listOfupdates = new BatchEditParameter[]{ new BatchEditParameter( "/mdb:distributionInfo/mrd:MD_Distribution", - "" + "" ) }; @@ -202,7 +202,7 @@ public void testUpdateRecordUpdateAttribute() throws Exception { final String uuid = "db07463b-6769-401e-944b-f22e2e3bcc26"; BatchEditParameter[] listOfupdates = new BatchEditParameter[]{ new BatchEditParameter( - "/mdb:MD_Metadata/mdb:metadataScope/mdb:MD_MetadataScope/mdb:resourceScope/@codeListValue", + "/mdb:MD_Metadata/mdb:metadataScope/*/mdb:resourceScope/*/@codeListValue", "newScope" ) }; @@ -225,14 +225,14 @@ public void testUpdateRecordUpdateAttribute() throws Exception { Element xml = Xml.loadString(updatedRecord.getData(), false); List attr = org.fao.geonet.utils.Xml.selectNodes(xml, - "./mdb:metadataScope/mdb:MD_MetadataScope/mdb:resourceScope/@codeListValue", + "./mdb:metadataScope/*/mdb:resourceScope/*/@codeListValue", xml.getAdditionalNamespaces()); Assert.assertEquals("newScope", ((Attribute) attr.get(0)).getValue()); listOfupdates = new BatchEditParameter[]{ new BatchEditParameter( - "/mdb:MD_Metadata/mdb:metadataScope/mdb:MD_MetadataScope/mdb:resourceScope/@codeListValue", + "/mdb:MD_Metadata/mdb:metadataScope/*/mdb:resourceScope/*/@codeListValue", "anotherNewScope" ) }; @@ -250,7 +250,7 @@ public void testUpdateRecordUpdateAttribute() throws Exception { xml = Xml.loadString(updatedRecord.getData(), false); attr = org.fao.geonet.utils.Xml.selectNodes(xml, - "./mdb:metadataScope/mdb:MD_MetadataScope/mdb:resourceScope/@codeListValue", + "./mdb:metadataScope/*/mdb:resourceScope/*/@codeListValue", xml.getAdditionalNamespaces()); Assert.assertEquals("anotherNewScope", ((Attribute) attr.get(0)).getValue()); } @@ -296,7 +296,7 @@ public void testUpdateRecordAddAttribute() throws Exception { BatchEditParameter[] listOfupdates = new BatchEditParameter[]{ new BatchEditParameter( "/mdb:MD_Metadata/mdb:metadataScope/mdb:MD_MetadataScope/mdb:resourceScope/@newAttribute", - "value" + "value" ) }; @@ -329,7 +329,7 @@ public void testUpdateRecordAddAndDeleteAttributeWithNamespace() throws Exceptio BatchEditParameter[] listOfupdates = new BatchEditParameter[]{ new BatchEditParameter( "/mdb:MD_Metadata/mdb:metadataScope/@gco:nilReason", - "withheld" + "withheld" ) }; @@ -389,8 +389,8 @@ public void testUpdateRecordElement() throws Exception { // XPath has no match and same type as fragment, element is created BatchEditParameter[] listOfupdates = new BatchEditParameter[]{ new BatchEditParameter( - "/mdb:distributionInfo/mrd:MD_Distribution/mrd:distributor[1]", - "" + "/mdb:distributionInfo/mrd:MD_Distribution/mrd:distributor", + "" ) };