From daff48471a14da5fa2ae7833fda5abdb1564f2ed Mon Sep 17 00:00:00 2001 From: Georgy Litvinov Date: Tue, 5 Nov 2024 15:59:51 +0100 Subject: [PATCH] Fixes for N3Optionals --- .../n3editing/VTwo/EditConfigurationVTwo.java | 31 ++++++------- .../edit/n3editing/VTwo/ProcessRdfForm.java | 46 ++++++++++++++----- 2 files changed, 49 insertions(+), 28 deletions(-) diff --git a/api/src/main/java/edu/cornell/mannlib/vitro/webapp/edit/n3editing/VTwo/EditConfigurationVTwo.java b/api/src/main/java/edu/cornell/mannlib/vitro/webapp/edit/n3editing/VTwo/EditConfigurationVTwo.java index 8380a4b37..f3dffb2c6 100644 --- a/api/src/main/java/edu/cornell/mannlib/vitro/webapp/edit/n3editing/VTwo/EditConfigurationVTwo.java +++ b/api/src/main/java/edu/cornell/mannlib/vitro/webapp/edit/n3editing/VTwo/EditConfigurationVTwo.java @@ -56,6 +56,8 @@ public class EditConfigurationVTwo { List n3Optional = new ArrayList(); //String representing optional N3 for RDF retracts List n3OptionalRetracts = new ArrayList(); + //String representing optional N3 for RDF retracts + List n3OptionalAssertions = new ArrayList(); //Names of variables of 'objects' i.e. URIs on form List urisOnform = new ArrayList(); //Names of variables corresponding to data values i.e. literals on form @@ -493,25 +495,20 @@ public void addN3Optional(String ... n3Strs){ this.n3Optional.addAll(Arrays.asList( n3Strs )); } - public List getN3OptionalRetracts() { - return EditConfigurationUtils.copy(n3OptionalRetracts); - } - - public void setN3OptionalRetracts(List n3OptionalRetracts) { - this.n3OptionalRetracts = n3OptionalRetracts; - } - - public void setN3OptionalRetracts(String... n3Strs) { - this.n3OptionalRetracts = new ArrayList(Arrays.asList(n3Strs)); - } + public List getN3OptionalAssertions() { + return EditConfigurationUtils.copy(n3OptionalAssertions); + } - public void addN3OptionalRetracts(List n3OptionalRetracts) { - this.n3OptionalRetracts.addAll(n3OptionalRetracts); - } + public void addN3OptionalAssertions(List assertions) { + this.n3OptionalAssertions.addAll(assertions); + } + public List getN3OptionalRetractions() { + return EditConfigurationUtils.copy(n3OptionalRetracts); + } - public void addN3OptionalRetracts(String... n3Strs) { - this.n3OptionalRetracts.addAll(Arrays.asList(n3Strs)); - } + public void addN3OptionalRetracts(List n3OptionalRetracts) { + this.n3OptionalRetracts.addAll(n3OptionalRetracts); + } public Map getNewResources() { return newResources; diff --git a/api/src/main/java/edu/cornell/mannlib/vitro/webapp/edit/n3editing/VTwo/ProcessRdfForm.java b/api/src/main/java/edu/cornell/mannlib/vitro/webapp/edit/n3editing/VTwo/ProcessRdfForm.java index 551ae2c05..c98f6c6ad 100644 --- a/api/src/main/java/edu/cornell/mannlib/vitro/webapp/edit/n3editing/VTwo/ProcessRdfForm.java +++ b/api/src/main/java/edu/cornell/mannlib/vitro/webapp/edit/n3editing/VTwo/ProcessRdfForm.java @@ -130,6 +130,10 @@ private AdditionsAndRetractions createNewStatements( List requiredN3 = configuration.getN3Required(); List optionalN3 = configuration.getN3Optional(); + if (isCustomizedOptional(configuration)) { + optionalN3 = configuration.getN3OptionalAssertions(); + } + /* substitute in the form values and existing values */ subInValuesToN3( configuration, submission, requiredN3, optionalN3, null , null, vreq); @@ -172,8 +176,12 @@ protected AdditionsAndRetractions editExistingStatements( List N3RequiredAssert = editConfig.getN3Required(); List N3OptionalAssert = editConfig.getN3Optional(); List N3RequiredRetract = editConfig.getN3Required(); - List N3OptionalRetract = editConfig.getN3OptionalRetracts(); - + List N3OptionalRetract = editConfig.getN3Optional(); + + if (isCustomizedOptional(editConfig)) { + N3OptionalAssert = editConfig.getN3OptionalAssertions(); + N3OptionalRetract = editConfig.getN3OptionalRetractions(); + } subInValuesToN3(editConfig, submission, N3RequiredAssert, N3OptionalAssert, N3RequiredRetract, N3OptionalRetract, vreq); @@ -183,6 +191,13 @@ protected AdditionsAndRetractions editExistingStatements( N3RequiredRetract, N3OptionalRetract, vreq, editConfig); } + private boolean isCustomizedOptional(EditConfigurationVTwo editConfig){ + return + editConfig.getN3Optional().isEmpty() && ( + !editConfig.getN3OptionalAssertions().isEmpty() || + !editConfig.getN3OptionalRetractions().isEmpty() ); + } + @SuppressWarnings("unchecked") protected void subInValuesToN3( EditConfigurationVTwo editConfig, MultiValueEditSubmission submission, @@ -199,11 +214,16 @@ protected void subInValuesToN3( //At this point deprecated optional retracts n3 are the same as optional asserts n3 List deprecatedRetracts = new ArrayList(); - if (optionalAsserts != null && optionalRetracts != null) { - deprecatedRetracts.addAll(optionalAsserts); - } /* ********** Form submission URIs ********* */ - substituteInMultiURIs(submission.getUrisFromForm(), requiredAsserts, optionalAsserts, optionalRetracts, URLToReturnTo); + if (isCustomizedOptional(editConfig)) { + if (optionalAsserts != null) { + deprecatedRetracts.addAll(optionalAsserts); + } + substituteInMultiURIs(submission.getUrisFromForm(), requiredAsserts, optionalAsserts, optionalRetracts, URLToReturnTo); + } else { + substituteInMultiURIs(submission.getUrisFromForm(), requiredAsserts, optionalAsserts, URLToReturnTo); + } + logSubstitue( "Added form URIs", requiredAsserts, optionalAsserts, requiredRetracts, optionalRetracts); //Retractions does NOT get values from form. @@ -242,12 +262,16 @@ else if ( RDF.dtLangString.getURI().equals(aLiteratDT)) { literalsFromForm.replace(aKey, newLiteralFromForm); } - substituteInMultiLiterals( literalsFromForm, requiredAsserts, optionalAsserts, optionalRetracts, URLToReturnTo); - logSubstitue( "Added form Literals", requiredAsserts, optionalAsserts, requiredRetracts, optionalRetracts); - //Retractions does NOT get values from form. - if (optionalAsserts != null && optionalRetracts != null) { - optionalRetracts.addAll(deprecatedRetracts); + if (isCustomizedOptional(editConfig)) { + substituteInMultiLiterals( literalsFromForm, requiredAsserts, optionalAsserts, optionalRetracts, URLToReturnTo); + //Retractions does NOT get values from form. + if (optionalRetracts != null) { + optionalRetracts.addAll(deprecatedRetracts); + } + } else { + substituteInMultiLiterals( literalsFromForm, requiredAsserts, optionalAsserts, URLToReturnTo); } + logSubstitue( "Added form Literals", requiredAsserts, optionalAsserts, requiredRetracts, optionalRetracts); /* *********** Add subject, object and predicate ******** */ substituteInSubPredObjURIs(editConfig, requiredAsserts, optionalAsserts, requiredRetracts, optionalRetracts, URLToReturnTo);