From ff8c14a725ce4f8deb662e428caa883f67ce7097 Mon Sep 17 00:00:00 2001 From: briancaruso Date: Mon, 14 Nov 2011 19:47:09 +0000 Subject: [PATCH] Fixing NewIndivdiual form generator and adding unit tests for ProcessRdfForm and EditN3Generator --- .../n3editing/VTwo/EditConfigurationVTwo.java | 14 +++++ .../n3editing/VTwo/EditN3GeneratorVTwo.java | 4 +- .../VTwo/MultiValueEditSubmission.java | 19 +++++-- .../edit/n3editing/VTwo/N3EditUtils.java | 56 +++++++++++-------- .../edit/n3editing/VTwo/ProcessRdfForm.java | 54 +++++------------- .../controller/ProcessRdfFormController.java | 2 - .../VTwo/EditN3GeneratorVTwoTest.java | 6 ++ .../n3editing/VTwo/ProcessRdfFormTest.java | 51 ++++++++++++++++- 8 files changed, 135 insertions(+), 71 deletions(-) diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/edit/n3editing/VTwo/EditConfigurationVTwo.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/edit/n3editing/VTwo/EditConfigurationVTwo.java index 3f36bcd30..2e08d0bb5 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/edit/n3editing/VTwo/EditConfigurationVTwo.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/edit/n3editing/VTwo/EditConfigurationVTwo.java @@ -101,9 +101,23 @@ public class EditConfigurationVTwo { String datapropKey; String datapropValue; + + /** urlPatternToReturnTo is the URL to use as the servlet to return to. + * Usually it is "/individual" and entityToReturnTo will be added as a + * "uri" parameter. */ String urlPatternToReturnTo = INDIVIDUAL_CONTROLLER ; + + /** If this is non-null it should be the URI of an Individual to return to after + * the edit. */ String entityToReturnTo; + + /** + * formUrl saves the URL that was used to request the form so that it can be + * reissued if a form validation fails and the client can be redirected back + * to the original form. */ String formUrl; + + String editKey; List validators; diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/edit/n3editing/VTwo/EditN3GeneratorVTwo.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/edit/n3editing/VTwo/EditN3GeneratorVTwo.java index 10a108eea..1a5704e71 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/edit/n3editing/VTwo/EditN3GeneratorVTwo.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/edit/n3editing/VTwo/EditN3GeneratorVTwo.java @@ -237,8 +237,8 @@ public class EditN3GeneratorVTwo { //Already includes "<> for URIs so no need to add those here protected String subInNonBracketedURIS(String var, String value, String target) { - /* var followed by dot some whitespace or var followed by whitespace*/ - String varRegex = "\\?" + var + "(?=\\p{Punct}|\\p{Space})"; + /* var followed by dot some whitespace OR var followed by whitespace OR at end of line*/ + String varRegex = "\\?" + var + "(?=\\p{Punct}|\\p{Space}|$)"; String out = null; if("".equals(value)) return target.replaceAll(varRegex,">::" + var + " was BLANK::< "); diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/edit/n3editing/VTwo/MultiValueEditSubmission.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/edit/n3editing/VTwo/MultiValueEditSubmission.java index 575eb3784..8075b6e0d 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/edit/n3editing/VTwo/MultiValueEditSubmission.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/edit/n3editing/VTwo/MultiValueEditSubmission.java @@ -42,7 +42,6 @@ public class MultiValueEditSubmission { String editKey; - //TODO: Need to change below to be able to support multiple values private Map> literalsFromForm ; private Map> urisFromForm ; @@ -52,7 +51,9 @@ public class MultiValueEditSubmission { private Map> filesFromForm; private static Model literalCreationModel; - + + private String entityToReturnTo; + static{ literalCreationModel = ModelFactory.createDefaultModel(); } @@ -64,6 +65,8 @@ public class MultiValueEditSubmission { if( this.editKey == null || this.editKey.trim().length() == 0) throw new Error("EditSubmission needs an 'editKey' parameter from the EditConfiguration"); + entityToReturnTo = editConfig.getEntityToReturnTo(); + validationErrors = new HashMap(); this.urisFromForm = new HashMap>(); @@ -141,7 +144,7 @@ public class MultiValueEditSubmission { } } } - + if( log.isDebugEnabled() ) log.debug( this.toString() ); } @@ -276,5 +279,13 @@ public class MultiValueEditSubmission { return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE); } - private Log log = LogFactory.getLog(MultiValueEditSubmission.class); + private Log log = LogFactory.getLog(MultiValueEditSubmission.class); + + public String getEntityToReturnTo() { + return entityToReturnTo; + } + + public void setEntityToReturnTo(String string) { + entityToReturnTo = string; + } } diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/edit/n3editing/VTwo/N3EditUtils.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/edit/n3editing/VTwo/N3EditUtils.java index e17491f79..36f8687e9 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/edit/n3editing/VTwo/N3EditUtils.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/edit/n3editing/VTwo/N3EditUtils.java @@ -35,33 +35,45 @@ public class N3EditUtils { - /** Process Entity to Return to - substituting uris etc. */ - //TODO: move this to utils or contorller - public static String processEntityToReturnTo(EditConfigurationVTwo configuration, - MultiValueEditSubmission submission, VitroRequest vreq) { - List entityToReturnTo = new ArrayList(); - String entity = configuration.getEntityToReturnTo(); - entityToReturnTo.add(entity); - //Substitute uris and literals on form - //Substitute uris and literals in scope - //Substite var to new resource - EditN3GeneratorVTwo n3Subber = configuration.getN3Generator(); + /** + * Process Entity to Return to - substituting uris etc. + * May return null. */ + public static String processEntityToReturnTo( + EditConfigurationVTwo configuration, + MultiValueEditSubmission submission, + VitroRequest vreq) { + //TODO: move this method to utils or contorller? - //Substitute URIs and literals from form - n3Subber.subInMultiUris(submission.getUrisFromForm(), entityToReturnTo); - n3Subber.subInMultiLiterals(submission.getLiteralsFromForm(), entityToReturnTo); + String returnTo = null; - //TODO: this won't work, must the same new resources as in ProcessRdfForm.process - //setVarToNewResource(configuration, vreq); - //entityToReturnTo = n3Subber.subInMultiUris(varToNewResource, entityToReturnTo); + //usually the submission should have a returnTo that is + // already substituted in with values during ProcessRdfForm.process() + if( submission != null && submission.getEntityToReturnTo() != null + && !submission.getEntityToReturnTo().trim().isEmpty()){ + returnTo = submission.getEntityToReturnTo(); + }else{ + //If submission doesn't have it, do the best that we can do. + //this will not have the new resource URIs. + List entityToReturnTo = new ArrayList(); + String entity = configuration.getEntityToReturnTo(); + entityToReturnTo.add(entity); + EditN3GeneratorVTwo n3Subber = configuration.getN3Generator(); - String processedEntity = entityToReturnTo.get(0); - if(processedEntity != null) { - - processedEntity = processedEntity.trim().replaceAll("<","").replaceAll(">",""); + //Substitute URIs and literals from form + n3Subber.subInMultiUris(submission.getUrisFromForm(), entityToReturnTo); + n3Subber.subInMultiLiterals(submission.getLiteralsFromForm(), entityToReturnTo); + + //TODO: this won't work, must the same new resources as in ProcessRdfForm.process + //setVarToNewResource(configuration, vreq); + //entityToReturnTo = n3Subber.subInMultiUris(varToNewResource, entityToReturnTo); + returnTo = entityToReturnTo.get(0); } - return processedEntity; + //TODO: what is this about? + if(returnTo != null) { + returnTo = returnTo.trim().replaceAll("<","").replaceAll(">",""); + } + return returnTo; } /** diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/edit/n3editing/VTwo/ProcessRdfForm.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/edit/n3editing/VTwo/ProcessRdfForm.java index c506b44a0..c0beb70a5 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/edit/n3editing/VTwo/ProcessRdfForm.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/edit/n3editing/VTwo/ProcessRdfForm.java @@ -59,6 +59,9 @@ public class ProcessRdfForm { * This will handle data property editing, object property editing * and general editing. * + * The submission object will be modified to have its entityToReturnTo string + * substituted with the values from the processing. + * * @throws Exception May throw an exception if Required N3 does not * parse correctly. */ @@ -116,40 +119,6 @@ public class ProcessRdfForm { return parseN3ToChange(requiredN3, optionalN3, null, null); } -// @SuppressWarnings("unchecked") -// protected void substituteInValuesForCreateNew( -// EditConfigurationVTwo configuration, -// MultiValueEditSubmission submission, List requiredN3, -// List optionalN3) throws InsertException { -// logRequiredOpt("Original valus for required and optional", requiredN3, optionalN3); -// -// /* add subject from configuration */ -// substituteInSubPredObjURIs( configuration, requiredN3, optionalN3); -// logRequiredOpt("attempted to substitute in subject, predicate and object from configuration", requiredN3, optionalN3); -// -// /* add URIs from the form/EditSubmission */ -// substituteInMultiURIs( submission.getUrisFromForm(), requiredN3, optionalN3); -// logRequiredOpt("substitued in URIs from submission", requiredN3, optionalN3); -// -// /* add Literals from the form/EditSubmission */ -// substituteInMultiLiterals( submission.getLiteralsFromForm(), requiredN3, optionalN3); -// logRequiredOpt("substitued in Literals from form", requiredN3, optionalN3); -// -// /* Add URIs in scope */ -// substituteInMultiURIs( configuration.getUrisInScope(), requiredN3, optionalN3); -// logRequiredOpt("substitued in URIs from configuration scope", requiredN3, optionalN3); -// -// /* Add Literals in scope */ -// substituteInMultiLiterals(configuration.getLiteralsInScope(), requiredN3, optionalN3); -// logRequiredOpt("substitued in Literals from scope", requiredN3, optionalN3); -// -// /* add URIs for new resources */ -// urisForNewResources = URIsForNewRsources(configuration, newURIMaker); -// substituteInURIs( urisForNewResources, requiredN3, optionalN3); -// logRequiredOpt("substitued in new resource URIs", requiredN3, optionalN3); -// } - - /* for a list of N3 strings, substitute in the subject, predicate and object URIs * from the EditConfiguration. */ protected void substituteInSubPredObjURIs( @@ -199,36 +168,41 @@ public class ProcessRdfForm { EditConfigurationVTwo editConfig, MultiValueEditSubmission submission, List requiredAsserts, List optionalAsserts, List requiredRetracts, List optionalRetracts ) throws InsertException{ + + //need to substitute into the return to URL becase it may need new resource URIs + List URLToReturnTo = Arrays.asList(submission.getEntityToReturnTo()); /* ********** Form submission URIs ********* */ - substituteInMultiURIs(submission.getUrisFromForm(), requiredAsserts, optionalAsserts); + substituteInMultiURIs(submission.getUrisFromForm(), requiredAsserts, optionalAsserts, URLToReturnTo); logSubstitue( "Added form URIs", requiredAsserts, optionalAsserts, requiredRetracts, optionalRetracts); //Retractions does NOT get values from form. /* ******** Form submission Literals *********** */ - substituteInMultiLiterals( submission.getLiteralsFromForm(), requiredAsserts, optionalAsserts); + substituteInMultiLiterals( submission.getLiteralsFromForm(), requiredAsserts, optionalAsserts, URLToReturnTo); logSubstitue( "Added form Literals", requiredAsserts, optionalAsserts, requiredRetracts, optionalRetracts); //Retractions does NOT get values from form. /* *********** Add subject, object and predicate ******** */ - substituteInSubPredObjURIs(editConfig, requiredAsserts, optionalAsserts, requiredRetracts, optionalRetracts); + substituteInSubPredObjURIs(editConfig, requiredAsserts, optionalAsserts, requiredRetracts, optionalRetracts, URLToReturnTo); logSubstitue( "Added sub, pred and obj URIs", requiredAsserts, optionalAsserts, requiredRetracts, optionalRetracts); /* ********* Existing URIs and Literals ********** */ substituteInMultiURIs(editConfig.getUrisInScope(), - requiredAsserts, optionalAsserts, requiredRetracts, optionalRetracts); + requiredAsserts, optionalAsserts, requiredRetracts, optionalRetracts, URLToReturnTo); logSubstitue( "Added existing URIs", requiredAsserts, optionalAsserts, requiredRetracts, optionalRetracts); substituteInMultiLiterals(editConfig.getLiteralsInScope(), - requiredAsserts, optionalAsserts, requiredRetracts, optionalRetracts); + requiredAsserts, optionalAsserts, requiredRetracts, optionalRetracts, URLToReturnTo); logSubstitue( "Added existing Literals", requiredAsserts, optionalAsserts, requiredRetracts, optionalRetracts); //Both Assertions and Retractions get existing values. /* ************ Edits may need new resources *********** */ urisForNewResources = URIsForNewRsources(editConfig, newURIMaker); - substituteInURIs( urisForNewResources, requiredAsserts, optionalAsserts); + substituteInURIs( urisForNewResources, requiredAsserts, optionalAsserts, URLToReturnTo); logSubstitue( "Added URIs for new Resources", requiredAsserts, optionalAsserts, requiredRetracts, optionalRetracts); // Only Assertions get new resources. + + submission.setEntityToReturnTo(URLToReturnTo.get(0)); } //TODO: maybe move this to utils or contorller? diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/edit/n3editing/controller/ProcessRdfFormController.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/edit/n3editing/controller/ProcessRdfFormController.java index 6153580b2..c5a8eb398 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/edit/n3editing/controller/ProcessRdfFormController.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/edit/n3editing/controller/ProcessRdfFormController.java @@ -102,8 +102,6 @@ public class ProcessRdfFormController extends FreemarkerHttpServlet{ //Here we are trying to get the entity to return to URL, //More involved processing for data property apparently - //Also what do we actually DO with the vreq attribute: Answer: Use it for redirection - //And no where else so we could technically calculate and send that here String entityToReturnTo = N3EditUtils.processEntityToReturnTo(configuration, submission, vreq); //For data property processing, need to update edit configuration for back button diff --git a/webapp/test/edu/cornell/mannlib/vitro/webapp/edit/n3editing/VTwo/EditN3GeneratorVTwoTest.java b/webapp/test/edu/cornell/mannlib/vitro/webapp/edit/n3editing/VTwo/EditN3GeneratorVTwoTest.java index 3c6146668..c13e9ede2 100644 --- a/webapp/test/edu/cornell/mannlib/vitro/webapp/edit/n3editing/VTwo/EditN3GeneratorVTwoTest.java +++ b/webapp/test/edu/cornell/mannlib/vitro/webapp/edit/n3editing/VTwo/EditN3GeneratorVTwoTest.java @@ -21,6 +21,12 @@ import com.hp.hpl.jena.rdf.model.ResourceFactory; public class EditN3GeneratorVTwoTest { static EditN3GeneratorVTwo gen = new EditN3GeneratorVTwo(); + @Test + public void testVarAtEndOfString(){ + String result = gen.subInNonBracketedURIS("newRes", " targets = Arrays.asList("?var",null,null,"?var"); diff --git a/webapp/test/edu/cornell/mannlib/vitro/webapp/edit/n3editing/VTwo/ProcessRdfFormTest.java b/webapp/test/edu/cornell/mannlib/vitro/webapp/edit/n3editing/VTwo/ProcessRdfFormTest.java index 5f568a96d..7d992e620 100644 --- a/webapp/test/edu/cornell/mannlib/vitro/webapp/edit/n3editing/VTwo/ProcessRdfFormTest.java +++ b/webapp/test/edu/cornell/mannlib/vitro/webapp/edit/n3editing/VTwo/ProcessRdfFormTest.java @@ -197,6 +197,55 @@ public class ProcessRdfFormTest extends AbstractTestClass{ ResourceFactory.createResource(test3))); } + @Test + public void basicNewResourceTest() throws Exception{ + /* A very basic new statement edit. */ + EditConfigurationVTwo config = new EditConfigurationVTwo(); + config.setEditKey("mockEditKey"); + config.setN3Required(Arrays.asList("?newRes ?test2 ?test3 ." )); + config.setUrisOnform(Arrays.asList( "test2", "test3")); + config.addNewResource("newRes", null); + config.setEntityToReturnTo("?newRes"); + + Map values = new HashMap(); + + values.put("test2", (new String[] {"http://test.com/uri2"})); + values.put("test3", (new String[] {"http://test.com/uri3"})); + values.put("editKey", (new String[] {"mockEditKey"})); + + MultiValueEditSubmission submission = new MultiValueEditSubmission(values, config); + + ProcessRdfForm processor = new ProcessRdfForm(config,getMockNewURIMaker()); + + /* test just the N3 substitution part */ + Listreq = config.getN3Required(); + Listopt = config.getN3Optional(); + processor.subInValuesToN3( config , submission, req, opt, null , null); + assertNotNull(req); + assertTrue( req.size() > 0); + assertNotNull(req.get(0)); + assertEquals("<"+NEWURI_STRING + "0> .", req.get(0)); + + assertEquals("<" + NEWURI_STRING + "0>", submission.getEntityToReturnTo()); + + /* test the N3 and parse RDF parts */ + AdditionsAndRetractions changes = processor.process( config, submission ); + + assertNotNull( changes ); + assertNotNull( changes.getAdditions() ); + assertNotNull( changes.getRetractions()); + assertTrue( changes.getAdditions().size() == 1 ); + assertTrue( changes.getRetractions().size() == 0 ); + + assertTrue( changes.getAdditions().contains( + ResourceFactory.createResource(NEWURI_STRING + "0"), + ResourceFactory.createProperty("http://test.com/uri2"), + ResourceFactory.createResource("http://test.com/uri3"))); + } + + + String NEWURI_STRING= "http://newURI/n"; + public NewURIMaker getMockNewURIMaker(){ return new NewURIMaker() { int count = 0; @@ -205,7 +254,7 @@ public class ProcessRdfFormTest extends AbstractTestClass{ if( prefixURI != null ) return prefixURI + count; else - return "http://newURI/n" + count; + return NEWURI_STRING + count; } }; }