NIHVIVO-413 Csv2Rdf change to create uris for resources.

This commit is contained in:
ass92 2010-12-14 21:15:12 +00:00
parent 2581a146f9
commit 8f1854ddf9

View file

@ -5,6 +5,9 @@ package edu.cornell.mannlib.vitro.webapp.utils;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Random;
import javax.servlet.http.HttpServletRequest;
import org.skife.csv.CSVReader;
import org.skife.csv.SimpleReader;
@ -17,6 +20,11 @@ import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntModelSpec;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.ResourceFactory;
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory;
public class Csv2Rdf {
@ -48,7 +56,7 @@ public class Csv2Rdf {
this.propertyNameBase = individualNameBase+"_";
}
public Model[] convertToRdf(InputStream fis) throws IOException {
public Model[] convertToRdf(InputStream fis,VitroRequest vreq, Model destination) throws IOException {
OntModel ontModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
OntModel tboxOntModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
ontModel.addSubModel(tboxOntModel);
@ -57,7 +65,13 @@ public class Csv2Rdf {
CSVReader cReader = new SimpleReader();
cReader.setSeperator(separatorChar);
cReader.setQuoteCharacters(quoteChars);
WebappDaoFactory wdf = vreq.getFullWebappDaoFactory();
Random random = new Random();
boolean uriIsGood = false;
boolean inDestination = false;
int attempts = 0;
String uri = null;
String errMsg = null;
List<String[]> fileRows = cReader.parse(fis);
String[] columnHeaders = fileRows.get(0);
@ -69,7 +83,21 @@ public class Csv2Rdf {
}
for (int row=1; row<fileRows.size(); row++) {
Individual ind = ontModel.createIndividual(namespace+individualNameBase+row,theClass);
while( uriIsGood == false && attempts < 30 ){
uri = namespace+individualNameBase+random.nextInt( Math.min(Integer.MAX_VALUE,(int)Math.pow(2,attempts + 13)) );
errMsg = wdf.checkURI(uri);
Resource res = ResourceFactory.createResource(uri);
inDestination = destination.contains(res, null);
if( errMsg != null && !inDestination)
uri = null;
else
uriIsGood = true;
attempts++;
}
uriIsGood = false;
attempts =0;
inDestination = false;
Individual ind = ontModel.createIndividual(uri,theClass);
String[] cols = fileRows.get(row);
for (int col=0; col<cols.length; col++) {
String value = cols[col].trim();