use of org.gbif.ipt.model.Vocabulary in project ipt by gbif.
the class ExtensionManagerImpl method migrateResourceToNewExtensionVersion.
/**
* Migrate a resource's extension mappings to an extension to a newer version of that extension.
*
* @param r resource whose mappings must be migrated
* @param current extension
* @param newer newer version of extension to migrate mappings to
*/
protected void migrateResourceToNewExtensionVersion(Resource r, Extension current, Extension newer) {
// sanity check that the current and newer extensions share same rowType
if (!current.getRowType().equalsIgnoreCase(newer.getRowType()) || r.getMappings(current.getRowType()).isEmpty()) {
throw new IllegalStateException();
}
LOG.info("Migrating " + r.getShortname() + " mappings to extension " + current.getRowType() + " to latest extension version");
// populate various set to keep track of how many terms were deprecated, how terms' vocabulary was updated, etc
Set<ExtensionProperty> deprecated = new HashSet<>();
Set<ExtensionProperty> vocabulariesRemoved = new HashSet<>();
Set<ExtensionProperty> vocabulariesUnchanged = new HashSet<>();
Set<ExtensionProperty> vocabulariesUpdated = new HashSet<>();
for (ExtensionProperty property : current.getProperties()) {
// newer extension still contain this property?
if (!newer.hasProperty(property.qualifiedName())) {
deprecated.add(property);
} else // if so, check if this property uses a vocabulary, and whether the newer extension uses a newer version of it
{
if (property.getVocabulary() != null) {
Vocabulary v1 = property.getVocabulary();
Vocabulary v2 = newer.getProperty(property.qualifiedName()).getVocabulary();
// case 1: vocabulary removed in newer version
if (v2 == null) {
vocabulariesRemoved.add(property);
} else // case 2: vocabulary versions are unchanged between versions
if (v1.getUriString().equalsIgnoreCase(v2.getUriString())) {
vocabulariesUnchanged.add(property);
} else // case 3: vocabulary has been updated in newer version
if (!v1.getUriString().equalsIgnoreCase(v2.getUriString())) {
vocabulariesUpdated.add(property);
}
}
}
}
LOG.debug(deprecated.size() + " properties have been deprecated in the newer version");
LOG.debug(vocabulariesRemoved.size() + " properties in the newer version of extension no longer use a vocabulary");
LOG.debug(vocabulariesUnchanged.size() + " properties in the newer version of extension use the same vocabulary");
LOG.debug(vocabulariesUpdated.size() + " properties in the newer version of extension use a newer vocabulary");
// set of new terms (terms to add)
Set<ExtensionProperty> added = new HashSet<>();
for (ExtensionProperty property : newer.getProperties()) {
// older extension contain this property?
if (!current.hasProperty(property.qualifiedName())) {
added.add(property);
}
}
LOG.debug("Newer version of extension has " + added.size() + " new properties");
for (ExtensionMapping extensionMapping : r.getMappings(current.getRowType())) {
migrateExtensionMapping(extensionMapping, newer, deprecated);
}
}
use of org.gbif.ipt.model.Vocabulary in project ipt by gbif.
the class VocabulariesManagerImpl method getLatestDefaults.
/**
* Return the latest versions of default vocabularies (that the IPT is configured to use) from the registry.
*
* @return list containing latest versions of default vocabularies
*/
private List<Vocabulary> getLatestDefaults(List<Vocabulary> registered) {
List<Vocabulary> defaults = new ArrayList<>();
for (Vocabulary v : registered) {
if (v.getUriString() != null && DEFAULT_VOCABS.contains(v.getUriString()) && v.isLatest()) {
defaults.add(v);
}
}
// throw exception if not all default vocabularies could not be loaded
if (DEFAULT_VOCABS.size() != defaults.size()) {
String msg = "Not all default vocabularies were loaded!";
LOG.error(msg);
throw new InvalidConfigException(InvalidConfigException.TYPE.INVALID_DATA_DIR, msg);
}
return defaults;
}
use of org.gbif.ipt.model.Vocabulary in project ipt by gbif.
the class VocabulariesManagerImpl method loadFromFile.
/**
* Load the Vocabulary object from the XML definition file.
*
* @param localFile vocabulary XML definition file
*
* @return vocabulary loaded from file
*
* @throws InvalidConfigException if vocabulary could not be loaded successfully
*/
private Vocabulary loadFromFile(File localFile) throws InvalidConfigException {
Objects.requireNonNull(localFile);
if (!localFile.exists()) {
throw new IllegalStateException();
}
try (InputStream fileIn = new FileInputStream(localFile)) {
Vocabulary v = vocabFactory.build(fileIn);
// filesystem date
v.setModified(new Date(localFile.lastModified()));
LOG.info("Successfully loaded vocabulary: " + v.getUriString());
return v;
} catch (IOException e) {
LOG.error("Can't access local vocabulary file (" + localFile.getAbsolutePath() + ")", e);
throw new InvalidConfigException(InvalidConfigException.TYPE.INVALID_VOCABULARY, "Can't access local vocabulary file");
} catch (SAXException e) {
LOG.error("Can't parse local extension file (" + localFile.getAbsolutePath() + ")", e);
throw new InvalidConfigException(InvalidConfigException.TYPE.INVALID_VOCABULARY, "Can't parse local vocabulary file");
} catch (ParserConfigurationException e) {
LOG.error("Can't create sax parser", e);
throw new InvalidConfigException(InvalidConfigException.TYPE.INVALID_VOCABULARY, "Can't create sax parser");
}
}
use of org.gbif.ipt.model.Vocabulary in project ipt by gbif.
the class VocabulariesManagerImpl method uninstall.
/**
* Uninstall vocabulary by its unique identifier.
*
* @param identifier identifier of vocabulary to uninstall
*/
private void uninstall(String identifier) {
if (vocabulariesById.containsKey(identifier)) {
// 1. delete persisted vocab file
Vocabulary toUninstall = vocabulariesById.get(identifier);
File f = getVocabFile(toUninstall.getUriResolvable());
if (f.exists()) {
f.delete();
LOG.debug("Successfully deleted (uninstalled) vocabulary file: " + f.getAbsolutePath());
} else {
LOG.warn("Vocabulary file doesn't exist locally - can't delete: " + f.getAbsolutePath());
}
// 2. delete from local lookup
vocabulariesById.remove(identifier);
} else {
LOG.warn("Vocabulary not installed locally, can't uninstall: " + identifier);
}
}
use of org.gbif.ipt.model.Vocabulary in project ipt by gbif.
the class ThesaurusHandlingRule method begin.
@Override
public void begin(String namespace, String name, Attributes attributes) throws Exception {
for (int i = 0; i < attributes.getLength(); i++) {
if (ThesaurusHandlingRule.ATTRIBUTE_THESAURUS.equals(attributes.getQName(i))) {
Vocabulary tv = null;
try {
URL url = new URL(attributes.getValue(i));
tv = vocabManager.get(url);
// install vocabulary if it's new
if (tv == null) {
LOG.warn("Installing new vocabulary with URL (" + attributes.getValue(i) + ")...");
tv = vocabManager.install(url);
}
} catch (MalformedURLException e) {
LOG.error("Thesaurus URL (" + attributes.getValue(i) + ") is malformed: " + e.getMessage(), e);
}
if (tv == null) {
LOG.error("No Vocabulary object exists for the URL (" + attributes.getValue(i) + ") so cannot be set");
} else {
Object extensionPropertyAsObject = getDigester().peek();
if (extensionPropertyAsObject instanceof ExtensionProperty) {
ExtensionProperty eProperty = (ExtensionProperty) extensionPropertyAsObject;
eProperty.setVocabulary(tv);
LOG.debug("Vocabulary with URI[" + tv.getUriString() + "] added to extension property");
}
}
// since we found the attribute
break;
}
}
}
Aggregations