Search in sources :

Example 1 with Vocabulary

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);
    }
}
Also used : ExtensionProperty(org.gbif.ipt.model.ExtensionProperty) Vocabulary(org.gbif.ipt.model.Vocabulary) ExtensionMapping(org.gbif.ipt.model.ExtensionMapping) HashSet(java.util.HashSet)

Example 2 with Vocabulary

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;
}
Also used : Vocabulary(org.gbif.ipt.model.Vocabulary) ArrayList(java.util.ArrayList) InvalidConfigException(org.gbif.ipt.service.InvalidConfigException)

Example 3 with Vocabulary

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");
    }
}
Also used : Vocabulary(org.gbif.ipt.model.Vocabulary) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) InvalidConfigException(org.gbif.ipt.service.InvalidConfigException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) FileInputStream(java.io.FileInputStream) Date(java.util.Date) SAXException(org.xml.sax.SAXException)

Example 4 with Vocabulary

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);
    }
}
Also used : Vocabulary(org.gbif.ipt.model.Vocabulary) File(java.io.File)

Example 5 with Vocabulary

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;
        }
    }
}
Also used : ExtensionProperty(org.gbif.ipt.model.ExtensionProperty) Vocabulary(org.gbif.ipt.model.Vocabulary) MalformedURLException(java.net.MalformedURLException) URL(java.net.URL)

Aggregations

Vocabulary (org.gbif.ipt.model.Vocabulary)25 File (java.io.File)7 VocabularyConcept (org.gbif.ipt.model.VocabularyConcept)7 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)6 InvalidConfigException (org.gbif.ipt.service.InvalidConfigException)6 Test (org.junit.jupiter.api.Test)6 SAXException (org.xml.sax.SAXException)6 IOException (java.io.IOException)5 ArrayList (java.util.ArrayList)5 MalformedURLException (java.net.MalformedURLException)4 URL (java.net.URL)4 Date (java.util.Date)4 VocabularyTerm (org.gbif.ipt.model.VocabularyTerm)4 RegistryException (org.gbif.ipt.service.RegistryException)4 URISyntaxException (java.net.URISyntaxException)3 HashMap (java.util.HashMap)3 LinkedHashMap (java.util.LinkedHashMap)3 VocabulariesManager (org.gbif.ipt.service.admin.VocabulariesManager)3 RegistryManager (org.gbif.ipt.service.registry.RegistryManager)3 FileInputStream (java.io.FileInputStream)2