////////////////////////////////////////////
// load model
OntModel model;
Ontology ont;
String uriForEmpty;
String newContentsFileName;
if ( createOntologyResult.getFullPath() != null ) {
//
// new contents to check.
// Get model from the new contents.
//
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;
}
ont = JenaUtil2.getOntology(model);
// get original namespace associated with the ontology, if any:
uriForEmpty = Util2.getDefaultNamespace(model, file, createOntologyResult);
// 2009-12-21: previously returning error if uriForEmpty==null. Not anymore; see below.
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;
}
ont = JenaUtil2.getOntology(model);
if ( ont == null ) {
// Shouldn't happen -- we're reading in an already registered version.
String error = "error while getting Ontology resource a registered version. " +
"Please report this bug.";
log.info(error);
createOntologyResult.setError(error);
return createOntologyResult;
}
uriForEmpty = ont.getURI();
// replace ':', '/', or '\' for '_'
newContentsFileName = uriForEmpty.replaceAll(":|/|\\\\", "_");
}
log.debug("uriForEmpty = '" +uriForEmpty+ "'");
final String original_base_ = uriForEmpty;
// final String original_base_ = JenaUtil2.removeTrailingFragment(uriForEmpty);
// and this is the info for the requested URI:
final String ontUri = createOntologyInfo.getUri();
final String ns_ = JenaUtil2.appendFragment(ontUri);
final String base_ = ontUri;
// final String base_ = JenaUtil2.removeTrailingFragment(ontUri);
log.info("createOntologyReHosted: original namespace: '" +original_base_+ "'");
if ( ! original_base_.equals(base_) ) {
// In this re-hosted case, we force the original URI and the new URI to be the same.
// This may happen only in the case of a submission of a new version.
String error = "The new base URI (" +base_+ ") is not equal to the registered " +
"base URI (" +original_base_+ ") "
;
log.debug(error);
createOntologyResult.setError(error);
return createOntologyResult;
}
/////////////////////////////////////////////////////////////////
// If there is an pre-existing Ontology resource, get the associated statements:
List<Statement> prexistStatements = null;
if ( ont != null ) {
prexistStatements = new ArrayList<Statement>();
if ( log.isDebugEnabled() ) {
log.debug("Getting pre-existing properties from Ontology: " +ont.getURI());
}
StmtIterator iter = ont.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 = OntModelUtil.createOntModel(base_, true, model);
JenaUtil2.setOntologyUriForGetOntology(base_);
final Ontology ont_ = JenaUtil2.getOntology(newOntModel);
if ( log.isDebugEnabled() ) {
log.debug("New ontology created with namespace " + ns_ + " base " + base_);
}
// Set internal attributes, which are updated in the newValues map itself
// so we facilite the processing below:
newValues.put(Omv.version.getURI(), version);
newValues.put(Omv.creationDate.getURI(), creationDate);
//////////////////////////////////////////////////
// transfer any preexisting attributes, and then remove all properties from
// pre-existing ont so just the new OntModel gets added.
if ( prexistStatements != 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 ) {
if ( log.isDebugEnabled() ) {
log.debug(" Transferring: " +st.getSubject()+ " :: " +prd+ " :: " +st.getObject());
}
newOntModel.add(ont_, prd, st.getObject());
}
else {
if ( log.isDebugEnabled() ) {
log.debug(" Removing pre-existing values for predicate: " +prd+ " because of new value " +newValue);
}
newOntModel.removeAll(ont_, prd, null);
}
}
}
///////////////////////////////////////////////////////
// Update attributes in model:
Map<String, Property> uriPropMap = MdHelper.getUriPropMap();
for ( String uri : newValues.keySet() ) {
String value = newValues.get(uri);
if ( value != null && value.trim().length() > 0 ) {
Property prop = uriPropMap.get(uri);
if ( prop == null ) {
log.info("No property found for uri='" +uri+ "'");
continue;
}
log.info(" Assigning: " +uri+ " = " +value);
ont_.addProperty(prop, value);
}
}