diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/IndividualDao.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/IndividualDao.java index f76a8780a..b39edd587 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/IndividualDao.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/IndividualDao.java @@ -141,7 +141,7 @@ public interface IndividualDao extends ObjectSourceIface { /** * 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. * @throws InsertException Could not create a URI */ diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/jena/IndividualDaoJena.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/jena/IndividualDaoJena.java index e2b7d0781..9967452e6 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/jena/IndividualDaoJena.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/jena/IndividualDaoJena.java @@ -1106,14 +1106,15 @@ public class IndividualDaoJena extends JenaBaseDao implements IndividualDao { RDF.type , getOntModel().getResource(vclassURI)); } - + public String getUnusedURI(Individual individual) throws InsertException { String errMsg = null; String namespace = null; String uri = null; 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().length() == 0 || DEFAULT_NAMESPACE.equals(individual.getNamespace()) ){ diff --git a/webapp/web/edit/processRdfForm2.jsp b/webapp/web/edit/processRdfForm2.jsp index 1c8b51d8e..f042f6749 100644 --- a/webapp/web/edit/processRdfForm2.jsp +++ b/webapp/web/edit/processRdfForm2.jsp @@ -58,11 +58,16 @@ are well formed. if (!selfEditing && !LoginFormBean.loggedIn(request, LoginFormBean.NON_EDITOR)) { %> -<%@page import="edu.cornell.mannlib.vitro.webapp.dao.jena.DependentResourceDeleteJena"%> +<%@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"%> <% } VitroRequest vreq = new VitroRequest(request); + WebappDaoFactory wdf = vreq.getWebappDaoFactory(); /* the post parameters seem to get consumed by the parsing so * we have to make a copy. */ @@ -133,7 +138,7 @@ are well formed. entToReturnTo = n3Subber.subInUris(editConfig.getUrisInScope(),entToReturnTo); //do edits ever need new resources? (YES) - Map varToNewResource = newToUriMap(editConfig.getNewResources(),resourcesModel); + Map varToNewResource = newToUriMap(editConfig.getNewResources(),wdf); fieldAssertions = n3Subber.substituteIntoValues(varToNewResource, null, fieldAssertions); if(log.isDebugEnabled()) logAddRetract("substituted in URIs for new resources",fieldAssertions,fieldRetractions); entToReturnTo = n3Subber.subInUris(varToNewResource,entToReturnTo); @@ -224,7 +229,7 @@ are well formed. if(log.isDebugEnabled()) logRequiredOpt("substituted in Literals from scope ",n3Required,n3Optional); /* ****************** New Resources ********************** */ - Map varToNewResource = newToUriMap(editConfig.getNewResources(),resourcesModel); + Map varToNewResource = newToUriMap(editConfig.getNewResources(),wdf); //if we are editing an existing prop, no new resources will be substituted since the var will //have already been substituted in by urisInScope. @@ -376,26 +381,41 @@ are well formed. /* ******************** Utility methods ********************** */ - public Map newToUriMap(Map newResources, Model model){ + public Map newToUriMap(Map newResources, WebappDaoFactory wdf){ HashMap newUris = new HashMap(); for( String key : newResources.keySet()){ - newUris.put(key,makeNewUri(newResources.get(key), model)); + newUris.put(key,makeNewUri(newResources.get(key), wdf)); } return newUris; } - public String makeNewUri(String prefix, Model model){ - if( prefix == null || prefix.length() == 0 ) - prefix = defaultUriPrefix; - - String uri = prefix + Math.abs( random.nextInt() ); - Resource r = ResourceFactory.createResource(uri); - while( model.containsResource(r) ){ - uri = prefix + random.nextInt(); - r = ResourceFactory.createResource(uri); + public String makeNewUri(String prefix, WebappDaoFactory wdf){ + if( prefix == null || prefix.length() == 0 ){ + String uri = null; + try{ + uri = wdf.getIndividualDao().getUnusedURI(null); + }catch(InsertException ex){ + log.error("could not create 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();