Fixing NewIndivdiual form generator and adding unit tests for ProcessRdfForm and EditN3Generator

This commit is contained in:
briancaruso 2011-11-14 19:47:09 +00:00
parent 6b9324119e
commit ff8c14a725
8 changed files with 135 additions and 71 deletions

View file

@ -101,9 +101,23 @@ public class EditConfigurationVTwo {
String datapropKey; String datapropKey;
String datapropValue; 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 ; 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; 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 formUrl;
String editKey; String editKey;
List<N3ValidatorVTwo> validators; List<N3ValidatorVTwo> validators;

View file

@ -237,8 +237,8 @@ public class EditN3GeneratorVTwo {
//Already includes "<> for URIs so no need to add those here //Already includes "<> for URIs so no need to add those here
protected String subInNonBracketedURIS(String var, String value, String target) { protected String subInNonBracketedURIS(String var, String value, String target) {
/* var followed by dot some whitespace or var followed by whitespace*/ /* var followed by dot some whitespace OR var followed by whitespace OR at end of line*/
String varRegex = "\\?" + var + "(?=\\p{Punct}|\\p{Space})"; String varRegex = "\\?" + var + "(?=\\p{Punct}|\\p{Space}|$)";
String out = null; String out = null;
if("".equals(value)) if("".equals(value))
return target.replaceAll(varRegex,">::" + var + " was BLANK::< "); return target.replaceAll(varRegex,">::" + var + " was BLANK::< ");

View file

@ -42,7 +42,6 @@ public class MultiValueEditSubmission {
String editKey; String editKey;
//TODO: Need to change below to be able to support multiple values
private Map<String,List<Literal>> literalsFromForm ; private Map<String,List<Literal>> literalsFromForm ;
private Map<String,List<String>> urisFromForm ; private Map<String,List<String>> urisFromForm ;
@ -53,6 +52,8 @@ public class MultiValueEditSubmission {
private static Model literalCreationModel; private static Model literalCreationModel;
private String entityToReturnTo;
static{ static{
literalCreationModel = ModelFactory.createDefaultModel(); literalCreationModel = ModelFactory.createDefaultModel();
} }
@ -64,6 +65,8 @@ public class MultiValueEditSubmission {
if( this.editKey == null || this.editKey.trim().length() == 0) if( this.editKey == null || this.editKey.trim().length() == 0)
throw new Error("EditSubmission needs an 'editKey' parameter from the EditConfiguration"); throw new Error("EditSubmission needs an 'editKey' parameter from the EditConfiguration");
entityToReturnTo = editConfig.getEntityToReturnTo();
validationErrors = new HashMap<String,String>(); validationErrors = new HashMap<String,String>();
this.urisFromForm = new HashMap<String,List<String>>(); this.urisFromForm = new HashMap<String,List<String>>();
@ -277,4 +280,12 @@ public class MultiValueEditSubmission {
} }
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;
}
} }

View file

@ -35,33 +35,45 @@ public class N3EditUtils {
/** Process Entity to Return to - substituting uris etc. */ /**
//TODO: move this to utils or contorller * Process Entity to Return to - substituting uris etc.
public static String processEntityToReturnTo(EditConfigurationVTwo configuration, * May return null. */
MultiValueEditSubmission submission, VitroRequest vreq) { public static String processEntityToReturnTo(
List<String> entityToReturnTo = new ArrayList<String>(); EditConfigurationVTwo configuration,
String entity = configuration.getEntityToReturnTo(); MultiValueEditSubmission submission,
entityToReturnTo.add(entity); VitroRequest vreq) {
//Substitute uris and literals on form //TODO: move this method to utils or contorller?
//Substitute uris and literals in scope
//Substite var to new resource
EditN3GeneratorVTwo n3Subber = configuration.getN3Generator();
//Substitute URIs and literals from form String returnTo = null;
n3Subber.subInMultiUris(submission.getUrisFromForm(), entityToReturnTo);
n3Subber.subInMultiLiterals(submission.getLiteralsFromForm(), entityToReturnTo);
//TODO: this won't work, must the same new resources as in ProcessRdfForm.process //usually the submission should have a returnTo that is
//setVarToNewResource(configuration, vreq); // already substituted in with values during ProcessRdfForm.process()
//entityToReturnTo = n3Subber.subInMultiUris(varToNewResource, entityToReturnTo); 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<String> entityToReturnTo = new ArrayList<String>();
String entity = configuration.getEntityToReturnTo();
entityToReturnTo.add(entity);
EditN3GeneratorVTwo n3Subber = configuration.getN3Generator();
String processedEntity = entityToReturnTo.get(0); //Substitute URIs and literals from form
if(processedEntity != null) { n3Subber.subInMultiUris(submission.getUrisFromForm(), entityToReturnTo);
n3Subber.subInMultiLiterals(submission.getLiteralsFromForm(), entityToReturnTo);
processedEntity = processedEntity.trim().replaceAll("<","").replaceAll(">",""); //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;
} }
/** /**

View file

@ -59,6 +59,9 @@ public class ProcessRdfForm {
* This will handle data property editing, object property editing * This will handle data property editing, object property editing
* and general 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 * @throws Exception May throw an exception if Required N3 does not
* parse correctly. * parse correctly.
*/ */
@ -116,40 +119,6 @@ public class ProcessRdfForm {
return parseN3ToChange(requiredN3, optionalN3, null, null); return parseN3ToChange(requiredN3, optionalN3, null, null);
} }
// @SuppressWarnings("unchecked")
// protected void substituteInValuesForCreateNew(
// EditConfigurationVTwo configuration,
// MultiValueEditSubmission submission, List<String> requiredN3,
// List<String> 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 /* for a list of N3 strings, substitute in the subject, predicate and object URIs
* from the EditConfiguration. */ * from the EditConfiguration. */
protected void substituteInSubPredObjURIs( protected void substituteInSubPredObjURIs(
@ -200,35 +169,40 @@ public class ProcessRdfForm {
List<String> requiredAsserts, List<String> optionalAsserts, List<String> requiredAsserts, List<String> optionalAsserts,
List<String> requiredRetracts, List<String> optionalRetracts ) throws InsertException{ List<String> requiredRetracts, List<String> optionalRetracts ) throws InsertException{
//need to substitute into the return to URL becase it may need new resource URIs
List<String> URLToReturnTo = Arrays.asList(submission.getEntityToReturnTo());
/* ********** Form submission URIs ********* */ /* ********** Form submission URIs ********* */
substituteInMultiURIs(submission.getUrisFromForm(), requiredAsserts, optionalAsserts); substituteInMultiURIs(submission.getUrisFromForm(), requiredAsserts, optionalAsserts, URLToReturnTo);
logSubstitue( "Added form URIs", requiredAsserts, optionalAsserts, requiredRetracts, optionalRetracts); logSubstitue( "Added form URIs", requiredAsserts, optionalAsserts, requiredRetracts, optionalRetracts);
//Retractions does NOT get values from form. //Retractions does NOT get values from form.
/* ******** Form submission Literals *********** */ /* ******** Form submission Literals *********** */
substituteInMultiLiterals( submission.getLiteralsFromForm(), requiredAsserts, optionalAsserts); substituteInMultiLiterals( submission.getLiteralsFromForm(), requiredAsserts, optionalAsserts, URLToReturnTo);
logSubstitue( "Added form Literals", requiredAsserts, optionalAsserts, requiredRetracts, optionalRetracts); logSubstitue( "Added form Literals", requiredAsserts, optionalAsserts, requiredRetracts, optionalRetracts);
//Retractions does NOT get values from form. //Retractions does NOT get values from form.
/* *********** Add subject, object and predicate ******** */ /* *********** 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); logSubstitue( "Added sub, pred and obj URIs", requiredAsserts, optionalAsserts, requiredRetracts, optionalRetracts);
/* ********* Existing URIs and Literals ********** */ /* ********* Existing URIs and Literals ********** */
substituteInMultiURIs(editConfig.getUrisInScope(), substituteInMultiURIs(editConfig.getUrisInScope(),
requiredAsserts, optionalAsserts, requiredRetracts, optionalRetracts); requiredAsserts, optionalAsserts, requiredRetracts, optionalRetracts, URLToReturnTo);
logSubstitue( "Added existing URIs", requiredAsserts, optionalAsserts, requiredRetracts, optionalRetracts); logSubstitue( "Added existing URIs", requiredAsserts, optionalAsserts, requiredRetracts, optionalRetracts);
substituteInMultiLiterals(editConfig.getLiteralsInScope(), substituteInMultiLiterals(editConfig.getLiteralsInScope(),
requiredAsserts, optionalAsserts, requiredRetracts, optionalRetracts); requiredAsserts, optionalAsserts, requiredRetracts, optionalRetracts, URLToReturnTo);
logSubstitue( "Added existing Literals", requiredAsserts, optionalAsserts, requiredRetracts, optionalRetracts); logSubstitue( "Added existing Literals", requiredAsserts, optionalAsserts, requiredRetracts, optionalRetracts);
//Both Assertions and Retractions get existing values. //Both Assertions and Retractions get existing values.
/* ************ Edits may need new resources *********** */ /* ************ Edits may need new resources *********** */
urisForNewResources = URIsForNewRsources(editConfig, newURIMaker); urisForNewResources = URIsForNewRsources(editConfig, newURIMaker);
substituteInURIs( urisForNewResources, requiredAsserts, optionalAsserts); substituteInURIs( urisForNewResources, requiredAsserts, optionalAsserts, URLToReturnTo);
logSubstitue( "Added URIs for new Resources", requiredAsserts, optionalAsserts, requiredRetracts, optionalRetracts); logSubstitue( "Added URIs for new Resources", requiredAsserts, optionalAsserts, requiredRetracts, optionalRetracts);
// Only Assertions get new resources. // Only Assertions get new resources.
submission.setEntityToReturnTo(URLToReturnTo.get(0));
} }
//TODO: maybe move this to utils or contorller? //TODO: maybe move this to utils or contorller?

View file

@ -102,8 +102,6 @@ public class ProcessRdfFormController extends FreemarkerHttpServlet{
//Here we are trying to get the entity to return to URL, //Here we are trying to get the entity to return to URL,
//More involved processing for data property apparently //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); String entityToReturnTo = N3EditUtils.processEntityToReturnTo(configuration, submission, vreq);
//For data property processing, need to update edit configuration for back button //For data property processing, need to update edit configuration for back button

View file

@ -21,6 +21,12 @@ import com.hp.hpl.jena.rdf.model.ResourceFactory;
public class EditN3GeneratorVTwoTest { public class EditN3GeneratorVTwoTest {
static EditN3GeneratorVTwo gen = new EditN3GeneratorVTwo(); static EditN3GeneratorVTwo gen = new EditN3GeneratorVTwo();
@Test
public void testVarAtEndOfString(){
String result = gen.subInNonBracketedURIS("newRes", "<http://someuri.com/n23", "?newRes");
Assert.assertEquals("<http://someuri.com/n23", result);
}
@Test @Test
public void testNullTarget(){ public void testNullTarget(){
List<String> targets = Arrays.asList("?var",null,null,"?var"); List<String> targets = Arrays.asList("?var",null,null,"?var");

View file

@ -197,6 +197,55 @@ public class ProcessRdfFormTest extends AbstractTestClass{
ResourceFactory.createResource(test3))); 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<String,String[]> values = new HashMap<String, String[]>();
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 */
List<String>req = config.getN3Required();
List<String>opt = config.getN3Optional();
processor.subInValuesToN3( config , submission, req, opt, null , null);
assertNotNull(req);
assertTrue( req.size() > 0);
assertNotNull(req.get(0));
assertEquals("<"+NEWURI_STRING + "0> <http://test.com/uri2> <http://test.com/uri3> .", 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(){ public NewURIMaker getMockNewURIMaker(){
return new NewURIMaker() { return new NewURIMaker() {
int count = 0; int count = 0;
@ -205,7 +254,7 @@ public class ProcessRdfFormTest extends AbstractTestClass{
if( prefixURI != null ) if( prefixURI != null )
return prefixURI + count; return prefixURI + count;
else else
return "http://newURI/n" + count; return NEWURI_STRING + count;
} }
}; };
} }