Node curNode;
String test = "";
while((curNode = ni.nextNode())!= null){
Node node_name = XPathAPI.selectSingleNode(curNode,"name/text()");
if(node_name == null)
throw new ImporterException("Error while parsing importMap: Element 'name' could not be found in at least one 'parameter'");
String value = request.getParameter(node_name.getNodeValue());
Node node_required = XPathAPI.selectSingleNode(curNode,"@required");
boolean required = false;
if(node_required!=null)
required = Boolean.parseBoolean(node_required.getNodeValue());
parameters.add(new ImportParameter(node_name.getNodeValue(),value,required));
}
//Check constraints
String msg = "";
boolean constraintsPassed = true;
Iterator iter = parameters.iterator();
while(iter.hasNext()){
ImportParameter cur = (ImportParameter)iter.next();
if(cur.isRequired() && cur.getValue().compareTo("")==0){
msg += "Field '" + cur.getName() + "' must not be empty\n";
constraintsPassed = false;
}
}
if(!constraintsPassed)
throw new ImporterException(msg);
//Data seemes fine, create statements for import
OntModel ontModel = Util.getOntModel();
Model model = Util.getModel();
//begin transaction
if(model.supportsTransactions())
model.begin();
//If in insert-mode
if(mode == ImportMode.INSERT){
//collect new resources to be created
ni = XPathAPI.selectNodeIterator(doc,"/importMapping/create/createResource");
while((curNode = ni.nextNode())!=null){
String name = XPathAPI.selectSingleNode(curNode,"name/text()").getNodeValue();
String value = Util.getNewResourceID(model);
//Insert newid
Resource res = null;
if(value.contains("@newID@")){
boolean resourceIsUnique = false;
while(!resourceIsUnique){
String tmpValue = value.replace("@newID@",Long.toString(++lastID));
res = model.createResource(tmpValue);
//Check if Namespace is already in use:
ResourceSelector rSel = new ResourceSelector(res);
StmtIterator stmtIt = model.listStatements(rSel);
if(!stmtIt.hasNext())
resourceIsUnique = true;
}
} else{
res = model.createResource(value);
ResourceSelector rSel = new ResourceSelector(res);
StmtIterator stmtIt = model.listStatements(rSel);
if(stmtIt.hasNext())
//Resource exist, cancel operation
throw new ImporterException("Resource '" + value + "' already exists");
}
NamedResource newRes = new NamedResource("@res:" + name + "@",res);
knownResources.add(newRes);
}
} else if (mode == ImportMode.EDIT){
//Find Resources that ought to be edited
ni = XPathAPI.selectNodeIterator(doc,"/importMapping/edit/editResource");
while((curNode = ni.nextNode())!=null){
String name = XPathAPI.selectSingleNode(curNode,"name/text()").getNodeValue();
String value = XPathAPI.selectSingleNode(curNode,"value/text()").getNodeValue();
Resource res = model.getResource(value);
NamedResource newRes = new NamedResource("@res:" + name + "@",res);
knownResources.add(newRes);
//Delete all statements containing the resources that will be edited
for(NamedResource nRes:knownResources)
Util.deleteStatements(model,nRes.getRes());
}
} else
throw new ImporterException("Error while preparing import: Unknown ImportMode");
//Read statement descriptions
ni = XPathAPI.selectNodeIterator(doc,"/importMapping/mappings/mapping");
while((curNode = ni.nextNode())!=null){
boolean isMultiple = false;
String s = null,p = null,o = null;
try {
s = XPathAPI.selectSingleNode(curNode,"s/text()").getNodeValue();
p = XPathAPI.selectSingleNode(curNode, "p/text()").getNodeValue();
o = XPathAPI.selectSingleNode(curNode,"o/text()").getNodeValue();
Node tmp = XPathAPI.selectSingleNode(curNode,"@multiple");
if(tmp!=null)
isMultiple = Boolean.parseBoolean(tmp.getNodeValue());
}catch (Exception ex){
//Statement can not be inserted
String tmp = "Error while retrieving mapping information (s: $s p: $p o: $o)<br/>\n";
if(s!=null)
tmp = tmp.replace("$s",s);
else
tmp = tmp.replace("$s","null");
if(p!=null)
tmp = tmp.replace("$p",p);
else
tmp = tmp.replace("$p","null");
if(o!=null)
tmp = tmp.replace("$o",o);
else
tmp = tmp.replace("$o","null");
msgVerbose += tmp;
}
//Only process this statement if all parts could be retrieved
if(s!=null && p!=null && o!=null){
//SUBJECT PART
Resource res = null;
//Check if the resource has been created in this import session
for(NamedResource nRes:knownResources)
if(nRes.getName().compareTo(s)==0){
//resource found, continue
res = nRes.getRes();
break;
}
//try to obtain resource from model
if(res==null){
res = model.getResource(s);
}
//if resource could not be found, throw exeption
if(res==null){
if(model.supportsTransactions())
model.abort();
throw new ImporterException("Error while constructing statements. Resource '" + s + "' could not be found!");
}
//PROPERTY PART
//Create or get property
//Try to obtain existing property
Property prop = model.getProperty(p);
if(prop==null)
if(p!=null && p.compareTo("") != 0)
prop = model.createProperty(p);
//OBJECT PART
//Determine if object should be treated as resource
String oAsResource = null;
try {
oAsResource = XPathAPI.selectSingleNode(curNode, "o/@asResource").getNodeValue();
} catch (Exception ex) {
//Attribute does not exist.. assume 'false' and continue
}
boolean ObjIsResource = false;
Resource resObject = null;
if(oAsResource!=null)
ObjIsResource = Boolean.parseBoolean(oAsResource);
//If object is a resource, try to obtain existing one
if(ObjIsResource){
//See if it is supposed to be known
if(o.startsWith("@res:")){
for(NamedResource nRes:knownResources){
if(nRes.getName().compareTo(o)==0){
resObject = nRes.getRes();
break;
}
}
if(resObject==null)
throw new ImporterException("Resource '" + o + "' could not be found while preparing the object resource");
} else{
resObject = model.getResource(o);
//If resource could not be obtained, create new
if(resObject==null)
resObject = model.createResource(o);
}
}
Statement statement = null;
//Could all the needed parts for the statementconstruction be collected?
if(res!=null && prop!=null && (resObject!=null || o.compareTo("")!=0)){
//If object is a resource, create a linked statement
//Try to resolve a statement with the same resource and predicate
StatementSelector smtSelector = new StatementSelector(res,prop,null);
StmtIterator stmtIterator = model.listStatements(smtSelector);
//Statement already existing, ->change
if(stmtIterator.hasNext()){
Statement stmt = stmtIterator.nextStatement();
if(ObjIsResource)
stmt.changeObject(resObject);
else
stmt.changeObject(o);
} else {
//Statement could not be found, create new
if(ObjIsResource)
statement = model.createStatement(res,prop,resObject);
//If not, create a regular one
else
statement = model.createStatement(res,prop,o);
if(!isMultiple)
statements.add(statement);
else{
// If the statement contains multiple values, create the according amount of new statements and split the values
// This is necessary for example while assinging one person as a developer to a row of projects
String masterSubject = statement.getSubject().getURI();
String masterObject = null;
if(statement.getObject().isResource())
masterObject = ((Resource)statement.getObject()).getURI();
else
masterObject = ((Literal)statement.getObject()).getString();
String[] subSubjects = null;
String[] subObjects = null;
//minLength is a helps to assure, that in case of a variable subject and object at the same time, it will only loop till the first array is processed
int minLength = 1;
if(masterSubject.contains(ConfigManager.Separator)){
subSubjects = masterSubject.split(ConfigManager.Separator);
minLength = subSubjects.length;
}
if(masterObject.contains(ConfigManager.Separator)){
subObjects = masterObject.split(ConfigManager.Separator);
if(minLength==0)
minLength = subObjects.length;
else
if(minLength>subObjects.length);
minLength = subObjects.length;
}
for(int i = 0;i<minLength;i++){
Resource tmpSubj = null;
RDFNode tmpObj = null;
//If the subject contains multiple values, change it at every iteration
if(subSubjects!=null){
//create new resource
tmpSubj = model.createResource(subSubjects[i]);
}
else
//Use original subject
tmpSubj = statement.getSubject();
//If the object contains multiple values, change it at every iteration
if(subObjects!=null){
//Cancel the loop if the end of the parameters has reached
if(i==subObjects.length)
break;
if(statement.getObject().isResource())
//create new resource
tmpObj = model.createResource(subObjects[i]);
else
//otherwise new literal
tmpObj = model.createLiteral(subObjects[i]);
}
else
//Use original object
tmpObj = statement.getObject();
Statement subStatement = model.createStatement(tmpSubj,statement.getPredicate(),tmpObj);
//Finally add the statement to the collection for later insert into the model
statements.add(subStatement);
}
}
}
} else {
//Some part of the Statement is missng. Most probably a non required parameter is missing.
//Ignore statement
msgVerbose += "Statement s:" + s + " p: " + p + " o:" + o + " has been ignored <br/>";
}
} //end of statement loop
}
Iterator i = statements.iterator();
msg = "";
//Eventualy insert statements into model
while(i.hasNext())
model.add((Statement)i.next());
if(model.supportsTransactions())
model.commit();
if(lastID!=0){
cf.setLastID(lastID);
cf.save();
}
Util.writeModelToFile(model,Locations.URI_rdfFile);
msg = "Import successful!";
if(ConfigManager.getInstance().getVerbose())
msg+= " (" + msgVerbose + ")";
throw new ImporterException(msg);
//throw new ImporterException("last id: " + cm.getLastID() + " displayErrors: " + cm.getSetting_displayErrors() + " basepath: " + cm.getBasePath());
}