Fixing NewIndivdiual form generator and adding unit tests for ProcessRdfForm and EditN3Generator
This commit is contained in:
parent
6b9324119e
commit
ff8c14a725
8 changed files with 135 additions and 71 deletions
|
@ -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<N3ValidatorVTwo> validators;
|
||||
|
|
|
@ -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::< ");
|
||||
|
|
|
@ -42,7 +42,6 @@ public class MultiValueEditSubmission {
|
|||
|
||||
String editKey;
|
||||
|
||||
//TODO: Need to change below to be able to support multiple values
|
||||
private Map<String,List<Literal>> literalsFromForm ;
|
||||
private Map<String,List<String>> urisFromForm ;
|
||||
|
||||
|
@ -52,7 +51,9 @@ public class MultiValueEditSubmission {
|
|||
private Map<String, List<FileItem>> 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<String,String>();
|
||||
|
||||
this.urisFromForm = new HashMap<String,List<String>>();
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<String> entityToReturnTo = new ArrayList<String>();
|
||||
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<String> entityToReturnTo = new ArrayList<String>();
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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<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
|
||||
* from the EditConfiguration. */
|
||||
protected void substituteInSubPredObjURIs(
|
||||
|
@ -199,36 +168,41 @@ public class ProcessRdfForm {
|
|||
EditConfigurationVTwo editConfig, MultiValueEditSubmission submission,
|
||||
List<String> requiredAsserts, List<String> optionalAsserts,
|
||||
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 ********* */
|
||||
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?
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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", "<http://someuri.com/n23", "?newRes");
|
||||
Assert.assertEquals("<http://someuri.com/n23", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullTarget(){
|
||||
List<String> targets = Arrays.asList("?var",null,null,"?var");
|
||||
|
|
|
@ -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<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(){
|
||||
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;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue