Adding ability to have default new URI creation behavior in n3 custom forms. NIHVIVO-273
This commit is contained in:
parent
53516cf140
commit
c28519a98e
3 changed files with 39 additions and 18 deletions
|
@ -141,7 +141,7 @@ public interface IndividualDao extends ObjectSourceIface {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Standard way to get a new URI that is not yet used.
|
* Standard way to get a new URI that is not yet used.
|
||||||
* @param individual
|
* @param individual, may be null
|
||||||
* @return new URI that is not found in the subject, predicate or object position of any statement.
|
* @return new URI that is not found in the subject, predicate or object position of any statement.
|
||||||
* @throws InsertException Could not create a URI
|
* @throws InsertException Could not create a URI
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -1113,7 +1113,8 @@ public class IndividualDaoJena extends JenaBaseDao implements IndividualDao {
|
||||||
String uri = null;
|
String uri = null;
|
||||||
boolean uriIsGood = false;
|
boolean uriIsGood = false;
|
||||||
|
|
||||||
if ( (individual.getURI() != null && individual.getURI().startsWith( DEFAULT_NAMESPACE ) )
|
if ( individual == null ||
|
||||||
|
(individual.getURI() != null && individual.getURI().startsWith( DEFAULT_NAMESPACE ) )
|
||||||
|| individual.getNamespace() == null
|
|| individual.getNamespace() == null
|
||||||
|| individual.getNamespace().length() == 0
|
|| individual.getNamespace().length() == 0
|
||||||
|| DEFAULT_NAMESPACE.equals(individual.getNamespace()) ){
|
|| DEFAULT_NAMESPACE.equals(individual.getNamespace()) ){
|
||||||
|
|
|
@ -58,11 +58,16 @@ are well formed.
|
||||||
if (!selfEditing && !LoginFormBean.loggedIn(request, LoginFormBean.NON_EDITOR)) {
|
if (!selfEditing && !LoginFormBean.loggedIn(request, LoginFormBean.NON_EDITOR)) {
|
||||||
%>
|
%>
|
||||||
|
|
||||||
<%@page import="edu.cornell.mannlib.vitro.webapp.dao.jena.DependentResourceDeleteJena"%><c:redirect url="<%= Controllers.LOGIN %>" />
|
<%@page import="edu.cornell.mannlib.vitro.webapp.dao.jena.DependentResourceDeleteJena"%>
|
||||||
|
<%@page import="edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory"%>
|
||||||
|
<%@page import="edu.cornell.mannlib.vitro.webapp.beans.IndividualImpl"%>
|
||||||
|
<%@page import="edu.cornell.mannlib.vitro.webapp.beans.Individual"%>
|
||||||
|
<%@page import="edu.cornell.mannlib.vitro.webapp.dao.InsertException"%><c:redirect url="<%= Controllers.LOGIN %>" />
|
||||||
<%
|
<%
|
||||||
}
|
}
|
||||||
|
|
||||||
VitroRequest vreq = new VitroRequest(request);
|
VitroRequest vreq = new VitroRequest(request);
|
||||||
|
WebappDaoFactory wdf = vreq.getWebappDaoFactory();
|
||||||
|
|
||||||
/* the post parameters seem to get consumed by the parsing so
|
/* the post parameters seem to get consumed by the parsing so
|
||||||
* we have to make a copy. */
|
* we have to make a copy. */
|
||||||
|
@ -133,7 +138,7 @@ are well formed.
|
||||||
entToReturnTo = n3Subber.subInUris(editConfig.getUrisInScope(),entToReturnTo);
|
entToReturnTo = n3Subber.subInUris(editConfig.getUrisInScope(),entToReturnTo);
|
||||||
|
|
||||||
//do edits ever need new resources? (YES)
|
//do edits ever need new resources? (YES)
|
||||||
Map<String,String> varToNewResource = newToUriMap(editConfig.getNewResources(),resourcesModel);
|
Map<String,String> varToNewResource = newToUriMap(editConfig.getNewResources(),wdf);
|
||||||
fieldAssertions = n3Subber.substituteIntoValues(varToNewResource, null, fieldAssertions);
|
fieldAssertions = n3Subber.substituteIntoValues(varToNewResource, null, fieldAssertions);
|
||||||
if(log.isDebugEnabled()) logAddRetract("substituted in URIs for new resources",fieldAssertions,fieldRetractions);
|
if(log.isDebugEnabled()) logAddRetract("substituted in URIs for new resources",fieldAssertions,fieldRetractions);
|
||||||
entToReturnTo = n3Subber.subInUris(varToNewResource,entToReturnTo);
|
entToReturnTo = n3Subber.subInUris(varToNewResource,entToReturnTo);
|
||||||
|
@ -224,7 +229,7 @@ are well formed.
|
||||||
if(log.isDebugEnabled()) logRequiredOpt("substituted in Literals from scope ",n3Required,n3Optional);
|
if(log.isDebugEnabled()) logRequiredOpt("substituted in Literals from scope ",n3Required,n3Optional);
|
||||||
|
|
||||||
/* ****************** New Resources ********************** */
|
/* ****************** New Resources ********************** */
|
||||||
Map<String,String> varToNewResource = newToUriMap(editConfig.getNewResources(),resourcesModel);
|
Map<String,String> varToNewResource = newToUriMap(editConfig.getNewResources(),wdf);
|
||||||
|
|
||||||
//if we are editing an existing prop, no new resources will be substituted since the var will
|
//if we are editing an existing prop, no new resources will be substituted since the var will
|
||||||
//have already been substituted in by urisInScope.
|
//have already been substituted in by urisInScope.
|
||||||
|
@ -376,26 +381,41 @@ are well formed.
|
||||||
|
|
||||||
/* ******************** Utility methods ********************** */
|
/* ******************** Utility methods ********************** */
|
||||||
|
|
||||||
public Map<String,String> newToUriMap(Map<String,String> newResources, Model model){
|
public Map<String,String> newToUriMap(Map<String,String> newResources, WebappDaoFactory wdf){
|
||||||
HashMap<String,String> newUris = new HashMap<String,String>();
|
HashMap<String,String> newUris = new HashMap<String,String>();
|
||||||
for( String key : newResources.keySet()){
|
for( String key : newResources.keySet()){
|
||||||
newUris.put(key,makeNewUri(newResources.get(key), model));
|
newUris.put(key,makeNewUri(newResources.get(key), wdf));
|
||||||
}
|
}
|
||||||
return newUris;
|
return newUris;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public String makeNewUri(String prefix, Model model){
|
public String makeNewUri(String prefix, WebappDaoFactory wdf){
|
||||||
if( prefix == null || prefix.length() == 0 )
|
if( prefix == null || prefix.length() == 0 ){
|
||||||
prefix = defaultUriPrefix;
|
String uri = null;
|
||||||
|
try{
|
||||||
String uri = prefix + Math.abs( random.nextInt() );
|
uri = wdf.getIndividualDao().getUnusedURI(null);
|
||||||
Resource r = ResourceFactory.createResource(uri);
|
}catch(InsertException ex){
|
||||||
while( model.containsResource(r) ){
|
log.error("could not create uri");
|
||||||
uri = prefix + random.nextInt();
|
}
|
||||||
r = ResourceFactory.createResource(uri);
|
return uri;
|
||||||
}
|
}
|
||||||
return uri;
|
|
||||||
|
String goodURI = null;
|
||||||
|
int attempts = 0;
|
||||||
|
while( goodURI == null && attempts < 30 ){
|
||||||
|
Individual ind = new IndividualImpl();
|
||||||
|
ind.setURI( prefix + random.nextInt() );
|
||||||
|
try{
|
||||||
|
goodURI = wdf.getIndividualDao().getUnusedURI(ind);
|
||||||
|
}catch(InsertException ex){
|
||||||
|
log.debug("could not create uri");
|
||||||
|
}
|
||||||
|
attempts++;
|
||||||
|
}
|
||||||
|
if( goodURI == null )
|
||||||
|
log.error("could not create uri for prefix " + prefix);
|
||||||
|
return goodURI;
|
||||||
}
|
}
|
||||||
|
|
||||||
static Random random = new Random();
|
static Random random = new Random();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue