Skip to content

Commit

Permalink
Batch edit / Add mode for better distinction of add/create/replace op…
Browse files Browse the repository at this point in the history
…erations.
  • Loading branch information
fxprunayre committed Oct 10, 2024
1 parent 2ec0025 commit 61d3a93
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 19 deletions.
24 changes: 23 additions & 1 deletion core/src/main/java/org/fao/geonet/kernel/AddElemValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -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("</?gn_(add|replace)>", "");
String finalStringVal = stringValue.replaceAll("</?gn_(add|create|replace)>", "");
if (Xml.isXMLLike(finalStringVal)) {
try {
finalNodeVal = Xml.loadString(stringValue, false);
Expand All @@ -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() {
Expand All @@ -73,4 +76,23 @@ public String getStringValue() {
public Element getNodeValue() {
return nodeValue;
}

private String parseMode(String value) {
if (value.startsWith("<gn_add>")) {
return EditLib.SpecialUpdateTags.ADD;
} else if (value.startsWith("<gn_create>")) {
return EditLib.SpecialUpdateTags.CREATE;
} else if (value.startsWith("<gn_replace_all>")) {
return EditLib.SpecialUpdateTags.REPLACE_ALL;
} else if (value.startsWith("<gn_replace>")) {
return EditLib.SpecialUpdateTags.REPLACE;
} else if (value.startsWith("<gn_delete>")) {
return EditLib.SpecialUpdateTags.DELETE;
}
return EditLib.SpecialUpdateTags.ADD;
}

public String getMode() {
return mode;
}
}
13 changes: 4 additions & 9 deletions core/src/main/java/org/fao/geonet/kernel/EditLib.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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));
}
}
Expand Down Expand Up @@ -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";
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ public void testUpdateRecordElementInsertWithParentXpath() throws Exception {
BatchEditParameter[] listOfupdates = new BatchEditParameter[]{
new BatchEditParameter(
"/mdb:distributionInfo/mrd:MD_Distribution",
"<gn_add><mrd:distributor xmlns:mrd=\"http://standards.iso.org/iso/19115/-3/mrd/1.0\"/></gn_add>"
"<gn_create><mrd:distributor xmlns:mrd=\"http://standards.iso.org/iso/19115/-3/mrd/1.0\"/></gn_create>"
)
};

Expand Down Expand Up @@ -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"
)
};
Expand All @@ -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",
"<gn_replace>anotherNewScope</gn_replace>"
)
};
Expand All @@ -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());
}
Expand Down Expand Up @@ -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",
"<gn_add>value</gn_add>"
"<gn_create>value</gn_create>"
)
};

Expand Down Expand Up @@ -329,7 +329,7 @@ public void testUpdateRecordAddAndDeleteAttributeWithNamespace() throws Exceptio
BatchEditParameter[] listOfupdates = new BatchEditParameter[]{
new BatchEditParameter(
"/mdb:MD_Metadata/mdb:metadataScope/@gco:nilReason",
"<gn_add>withheld</gn_add>"
"<gn_create>withheld</gn_create>"
)
};

Expand Down Expand Up @@ -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]",
"<gn_add><mrd:distributor xmlns:mrd=\"http://standards.iso.org/iso/19115/-3/mrd/1.0\"><mrd:MD_Distributor/></mrd:distributor></gn_add>"
"/mdb:distributionInfo/mrd:MD_Distribution/mrd:distributor",
"<gn_create><mrd:distributor xmlns:mrd=\"http://standards.iso.org/iso/19115/-3/mrd/1.0\"><mrd:MD_Distributor/></mrd:distributor></gn_create>"
)
};

Expand Down

0 comments on commit 61d3a93

Please sign in to comment.