////////////////////////////////////////////
// load model
OntModel model;
String uriForEmpty;
String newContentsFileName;
if ( createOntologyResult.getFullPath() != null ) {
//
// new contents to check.
// Get model from the new contents.
//
String full_path = createOntologyResult.getFullPath();
log.info("Loading model: " +full_path);
File file = new File(full_path);
try {
model = Util2.loadModelWithCheckingUtf8(file, null);
}
catch ( Throwable ex ) {
String error = "Unexpected error: " +ex.getClass().getName()+ " : " +ex.getMessage();
log.info(error);
createOntologyResult.setError(error);
return createOntologyResult;
}
uriForEmpty = Util2.getDefaultNamespace(model, file, createOntologyResult);
if ( uriForEmpty == null ) {
String error = "Cannot get base URI for the ontology";
log.info(error);
createOntologyResult.setError(error);
return createOntologyResult;
}
newContentsFileName = file.getName();
}
else {
// NO new contents.
// Use contents from prior version.
PriorOntologyInfo priorVersionInfo = createOntologyInfo.getPriorOntologyInfo();
try {
model = OntServiceUtil.retrieveModel(createOntologyInfo.getUri(), priorVersionInfo.getVersionNumber());
}
catch (Exception e) {
String error = "error while retrieving registered ontology: " +e.getMessage();
log.info(error, e);
createOntologyResult.setError(error);
return createOntologyResult;
}
uriForEmpty = model.getNsPrefixURI("");
if ( uriForEmpty == null ) {
// Shouldn't happen -- we're reading in an already registered version.
String error = "error while getting URI for empty prefix for a registered version. " +
"Please report this bug.";
log.info(error);
createOntologyResult.setError(error);
return createOntologyResult;
}
// replace ':', '/', or '\' for '_'
newContentsFileName = uriForEmpty.replaceAll(":|/|\\\\", "_");
}
log.info("createOntology: using '" +uriForEmpty+ "' as base URI");
final String original_ns_ = uriForEmpty;
log.info("original namespace: " +original_ns_);
String ns_;
String base_;
if ( createOntologyResult.isPreserveOriginalBaseNamespace() ) {
//pons: just use original namespace
ns_ = original_ns_;
base_ = ns_;
// base_ = JenaUtil2.removeTrailingFragment(ns_);
///////////////////////////////////////////////////////
if ( ontologyId == null ) {
// This is a new submission. We need to check for any conflict with a preexisting
// ontology in the repository with the same URI, base_
//
if ( ! Util2.checkNoPreexistingOntology(base_, createOntologyResult) ) {
return createOntologyResult;
}
}
else {
// This is a submission of a *new version* of an existing ontology.
// NO check needed--the given URI (base_) is respected.
}
///////////////////////////////////////////////////////
}
else {
final String finalUri = namespaceRoot + "/" +
orgAbbreviation + "/" +
version + "/" +
shortName;
ns_ = JenaUtil2.appendFragment(finalUri);
base_ = JenaUtil2.removeTrailingFragment(finalUri);
log.info("Setting prefix \"\" for URI " + ns_);
model.setNsPrefix("", ns_);
// Update statements according to the new namespace:
Util2.replaceNameSpace(model, original_ns_, ns_);
}
/////////////////////////////////////////////////////////////////
// Is there an existing OWL.Ontology individual?
// TODO Note that ONLY the first OWL.Ontology individual is considered.
Resource ontRes = JenaUtil2.getFirstIndividual(model, OWL.Ontology);
List<Statement> prexistStatements = null;
if ( ontRes != null ) {
prexistStatements = new ArrayList<Statement>();
log.info("Getting pre-existing properties for OWL.Ontology individual: " +ontRes.getURI());
StmtIterator iter = ontRes.listProperties();
while ( iter.hasNext() ) {
Statement st = iter.nextStatement();
prexistStatements.add(st);
}
}
// The new OntModel that will contain the pre-existing attributes (if any),
// plus the new and updated attributes:
final OntModel newOntModel = _createOntModel(model);
final Ontology ont_ = newOntModel.createOntology(base_);
log.info("New ontology created with namespace " + ns_ + " base " + base_);
newOntModel.setNsPrefix("", ns_);
// set preferred prefixes:
Map<String, String> preferredPrefixMap = MdHelper.getPreferredPrefixMap();
for ( String uri : preferredPrefixMap.keySet() ) {
String prefix = preferredPrefixMap.get(uri);
newOntModel.setNsPrefix(prefix, uri);
}
// Set internal attributes, which are updated in the newValues map itself
// so we facilite the processing below:
newValues.put(Omv.uri.getURI(), base_);
newValues.put(Omv.version.getURI(), version);
newValues.put(Omv.creationDate.getURI(), creationDate);
//////////////////////////////////////////////////
// transfer any preexisting attributes, and then remove all properties from
// pre-existing ontRes so just the new OntModel gets added.
if ( ontRes != null ) {
for ( Statement st : prexistStatements ) {
Property prd = st.getPredicate();
//
// Do not tranfer pre-existing/pre-assigned-above attributes
//
String newValue = newValues.get(prd.getURI());
if ( newValue == null || newValue.trim().length() == 0 ) {
log.info(" Transferring: " +st.getSubject()+ " :: " +prd+ " :: " +st.getObject());
newOntModel.add(ont_, prd, st.getObject());
}
else {
log.info(" Removing pre-existing values for predicate: " +prd+ " because of new value " +newValue);
newOntModel.removeAll(ont_, prd, null);
}
}
if ( ! createOntologyResult.isPreserveOriginalBaseNamespace() ) {