// TODO remove when new mechanism is in place.
@SuppressWarnings("deprecation")
private CreateOntologyResult _createOntology_oldMethod(CreateOntologyInfo createOntologyInfo) {
CreateOntologyResult createOntologyResult = new CreateOntologyResult();
DataCreationInfo dataCreationInfo = createOntologyInfo.getDataCreationInfo();
if ( dataCreationInfo == null ) {
createOntologyResult.setError("No data creation info provided! (please report this bug)");
return createOntologyResult;
}
createOntologyResult.setCreateOntologyInfo(createOntologyInfo);
// to check if this is going to be a new submission (if ontologyId == null) or, otherwise, a new version.
final String ontologyId = createOntologyInfo.getPriorOntologyInfo().getOntologyId();
//{pons pons: sections related with preserveOriginalBaseNamespace
// this flag will be only true in the case where an external ontology is to be registered
// and the user indicates that the original base namespace be preserved.
createOntologyResult.setPreserveOriginalBaseNamespace(false);
if ( dataCreationInfo instanceof OtherDataCreationInfo ) {
OtherDataCreationInfo odci = (OtherDataCreationInfo) dataCreationInfo;
TempOntologyInfo toi = odci.getTempOntologyInfo();
createOntologyResult.setPreserveOriginalBaseNamespace(toi != null && toi.isPreserveOriginalBaseNamespace());
}
if ( ! createOntologyResult.isPreserveOriginalBaseNamespace() ) {
// Note: if this is the submission of a new version (ontologyId != null) of an "external" (ie, re-hosted)
// ontology, then set this flag to true
if ( ontologyId != null && ! OntServiceUtil.isOntResolvableUri(createOntologyInfo.getUri()) ) {
createOntologyResult.setPreserveOriginalBaseNamespace(true);
// TODO However, note that we're goint to preserve the URI of the given ontology in this submission,
// which may not coincide with the previous one.
}
}
//}pons
Map<String, String> newValues = createOntologyInfo.getMetadataValues();
////////////////////////////////////////////
// check for errors
if ( createOntologyResult.getError() != null ) {
log.info(": error: " +createOntologyResult.getError());
return createOntologyResult;
}
if ( newValues == null ) {
String error = "Unexpected: no new values assigned for review. Please report this bug";
createOntologyResult.setError(error );
log.info(error);
return createOntologyResult;
}
final String namespaceRoot = defaultNamespaceRoot;
final String orgAbbreviation = newValues.get(OmvMmi.origMaintainerCode.getURI());
String shortName = newValues.get(Omv.acronym.getURI());
// TODO: shortName taken NOT from acronym but from a new field explicitly for the shortName piece
if ( ! createOntologyResult.isPreserveOriginalBaseNamespace() ) {
//pons: check the following if regular assignment of namespace
if ( orgAbbreviation == null ) {
log.info("missing origMaintainerCode");
createOntologyResult.setError("missing origMaintainerCode");
return createOntologyResult;
}
if ( shortName == null ) {
if ( ontologyId == null ) {
// This is a new submission.
log.info("missing acronym (to be used as shortName)");
createOntologyResult.setError("missing acronym (to be used as shortName)");
return createOntologyResult;
}
else {
// take from previous information
String originalShortName = createOntologyInfo.getShortName();
shortName = originalShortName;
}
}
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 shortName+orgAbbreviation combination
//
if ( ! Util2.checkNoPreexistingOntology(namespaceRoot, orgAbbreviation, shortName, createOntologyResult) ) {
return createOntologyResult;
}
}
else {
// This is a submission of a *new version* of an existing ontology.
// We need to check the shortName+orgAbbreviation combination as any changes here
// would imply a *new* ontology, not a new version.
//
String originalOrgAbbreviation = createOntologyInfo.getAuthority();
String originalShortName = createOntologyInfo.getShortName();
if ( ! Util2.checkUriKeyCombinationForNewVersion(
originalOrgAbbreviation, originalShortName,
orgAbbreviation, shortName, createOntologyResult) ) {
return createOntologyResult;
}
}
}
// Else: see below, where we obtain the original namespace.
////////////////////////////////////////////////////////////////////////////
// section to create the ontology the base:
if ( dataCreationInfo instanceof VocabularyDataCreationInfo ) {
// vocabulary (voc2rdf) case:
VocabularyDataCreationInfo vocabularyDataCreationInfo = (VocabularyDataCreationInfo) dataCreationInfo;
_createTempVocabularyOntology(createOntologyInfo, vocabularyDataCreationInfo, createOntologyResult);
if ( createOntologyResult.getError() != null ) {
return createOntologyResult;
}
}
else if ( dataCreationInfo instanceof MappingDataCreationInfo ) {
// mapping (vine case):
MappingDataCreationInfo mappingDataCreationInfo = (MappingDataCreationInfo) dataCreationInfo;
_createTempMappingOntology(createOntologyInfo, mappingDataCreationInfo, createOntologyResult);
if ( createOntologyResult.getError() != null ) {
return createOntologyResult;
}
}
else if ( dataCreationInfo instanceof OtherDataCreationInfo) {
// external ontology case: the base ontology is already available, just use it
// by setting the full path in the createOntologyResult:
OtherDataCreationInfo otherDataCreationInfo = (OtherDataCreationInfo) dataCreationInfo;
TempOntologyInfo tempOntologyInfo = otherDataCreationInfo.getTempOntologyInfo();
String full_path;
if ( tempOntologyInfo != null ) {
// new contents were provided. Use that:
full_path = tempOntologyInfo.getFullPath();
}
else {
// No new contents. Only possible way for this to happen is that this is
// a new version of an existing ontology.
if ( ontologyId != null ) {
// Just indicate a null full_path; see below.
full_path = null;
}
else {
// This shouldn't happen!
String error = "Unexpected: Submission of new ontology, but not contents were provided. " +
"This should be detected before submission. Please report this bug";
createOntologyResult.setError(error);
log.info(error);
return createOntologyResult;
}
}
createOntologyResult.setFullPath(full_path);
}
else {
createOntologyResult.setError("Unexpected creation of " +
dataCreationInfo.getClass().getSimpleName()+ ". Please report this bug.");
return createOntologyResult;
}
// current date:
final Date date = new Date(System.currentTimeMillis());
///////////////////////////////////////////////////////////////////
// creation date:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
final String creationDate = sdf.format(date);
///////////////////////////////////////////////////////////////////
// version:
// Note, if the version is given from the client, then use it
String version = newValues.get(Omv.version.getURI());
if ( version != null && version.trim().length() > 0 ) {
// check that the given version is OK:
boolean ok = VERSION_PATTERN.matcher(version).find();
if ( ! ok ) {
String error = "Given version is invalid: " +version;
log.info(error);
createOntologyResult.setError(error);
return createOntologyResult;
}
}
else {
// otherwise: assign it here:
sdf = new SimpleDateFormat("yyyyMMdd'T'HHmmss");
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
version = sdf.format(date);
}
////////////////////////////////////////////
// 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_ = 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() ) {
//
// Only, when we're creating a new model, ie., per the new namespace, do the following removals.
// (If we did this on a model with the same original namespace, we would remove the owl:Ontology
// entry altogether and get an "rdf:Description" instead.
//
log.info("Removing original OWL.Ontology individual");
ontRes.removeProperties();
// TODO the following may be unnecesary but doesn't hurt:
model.remove(ontRes, RDF.type, OWL.Ontology);
}
}
///////////////////////////////////////////////////////
// Update attributes in model:
Map<String, Property> uriPropMap = MdHelper.getUriPropMap();
for ( String uri : newValues.keySet() ) {
String value = newValues.get(uri).trim();
if ( value.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);
}
}
// Set the missing DC attrs that have defined e equivalent MMI attrs:
Util2.setDcAttributes(ont_);
////////////////////////////////////////////////////////////////////////
// Done with the model.
////////////////////////////////////////////////////////////////////////
// Get resulting string:
JenaUtil2.removeUnusedNsPrefixes(model);
String rdf = JenaUtil2.getOntModelAsString(model, "RDF/XML-ABBREV") ; // XXX newOntModel);
if ( log.isDebugEnabled() ) {
if ( createOntologyResult.isPreserveOriginalBaseNamespace() ) {
log.debug(rdf);
}
}
log.debug("createOntology: setting URI: " +base_);
createOntologyResult.setUri(base_);
// write new contents to a new file under previewDir:
File reviewedFile = new File(previewDir, newContentsFileName);
createOntologyResult.setFullPath(reviewedFile.getAbsolutePath());
_writeRdfToFile(rdf, reviewedFile, createOntologyResult);
// Done.