Search in sources :

Example 1 with VocabularyConcept

use of org.gbif.ipt.model.VocabularyConcept in project ipt by gbif.

the class Eml2Rtf method addNaturalCollections.

/**
 * Add natural collections section.
 *
 * @param doc Document
 * @param eml EML
 * @throws DocumentException if problem occurs during add
 */
private void addNaturalCollections(Document doc, Eml eml) throws DocumentException {
    if (!eml.getCollections().isEmpty() || !eml.getTemporalCoverages().isEmpty() || !eml.getSpecimenPreservationMethods().isEmpty() || !eml.getJgtiCuratorialUnits().isEmpty()) {
        Paragraph p = new Paragraph();
        p.setAlignment(Element.ALIGN_JUSTIFIED);
        p.setFont(font);
        for (org.gbif.metadata.eml.Collection collection : eml.getCollections()) {
            if (exists(collection.getParentCollectionId()) || exists(collection.getCollectionName()) || exists(collection.getCollectionId())) {
                p.add(new Phrase(getText("rtf.collections.description"), fontTitle));
                p.add(Chunk.NEWLINE);
                p.add(Chunk.NEWLINE);
                if (exists(collection.getParentCollectionId())) {
                    p.add(new Phrase(getText("rtf.collections.parent") + ": ", fontTitle));
                    p.add(collection.getParentCollectionId());
                    p.add(Chunk.NEWLINE);
                }
                if (exists(collection.getCollectionName())) {
                    p.add(new Phrase(getText("rtf.collections.name") + ": ", fontTitle));
                    p.add(collection.getCollectionName());
                    p.add(Chunk.NEWLINE);
                }
                if (exists(collection.getCollectionId())) {
                    p.add(new Phrase(getText("rtf.collections.identifier") + ": ", fontTitle));
                    p.add(collection.getCollectionId());
                    p.add(Chunk.NEWLINE);
                }
            }
        }
        for (TemporalCoverage coverage : eml.getTemporalCoverages()) {
            if (coverage.getType() == TemporalCoverageType.FORMATION_PERIOD) {
                p.add(new Phrase(getText("rtf.collections.formatPeriod") + ": ", fontTitle));
                p.add(coverage.getFormationPeriod());
                p.add(Chunk.NEWLINE);
            }
        }
        for (TemporalCoverage coverage : eml.getTemporalCoverages()) {
            if (coverage.getType() == TemporalCoverageType.LIVING_TIME_PERIOD) {
                p.add(new Phrase(getText("rtf.collections.livingPeriod") + ": ", fontTitle));
                p.add(coverage.getLivingTimePeriod());
                p.add(Chunk.NEWLINE);
            }
        }
        for (String preservationMethod : eml.getSpecimenPreservationMethods()) {
            if (exists(preservationMethod)) {
                p.add(new Phrase(getText("rtf.collections.specimen") + ": ", fontTitle));
                VocabularyConcept vocabConcept = vocabManager.get(Constants.VOCAB_URI_PRESERVATION_METHOD).findConcept(preservationMethod);
                // write preservation method in default language as matched from vocabulary or original value
                if (exists(vocabConcept)) {
                    p.add(vocabConcept.getPreferredTerm(DEFAULT_LANGUAGE).getTitle());
                } else {
                    p.add(preservationMethod.replace("\r\n", "\n"));
                }
                p.add(Chunk.NEWLINE);
            }
        }
        for (JGTICuratorialUnit unit : eml.getJgtiCuratorialUnits()) {
            p.add(new Phrase(getText("rtf.collections.curatorial") + ": ", fontTitle));
            if (unit.getType() == JGTICuratorialUnitType.COUNT_RANGE) {
                p.add("Between " + unit.getRangeStart() + " and " + unit.getRangeEnd());
            }
            if (unit.getType() == JGTICuratorialUnitType.COUNT_WITH_UNCERTAINTY) {
                p.add(unit.getRangeMean() + " " + getText("rtf.collections.curatorial.text") + " " + unit.getUncertaintyMeasure());
            }
            p.add(" (" + unit.getUnitType() + ")");
            p.add(Chunk.NEWLINE);
        }
        if (!p.isEmpty()) {
            doc.add(p);
        }
        p.clear();
    }
}
Also used : TemporalCoverage(org.gbif.metadata.eml.TemporalCoverage) VocabularyConcept(org.gbif.ipt.model.VocabularyConcept) Phrase(com.lowagie.text.Phrase) JGTICuratorialUnit(org.gbif.metadata.eml.JGTICuratorialUnit) Paragraph(com.lowagie.text.Paragraph)

Example 2 with VocabularyConcept

use of org.gbif.ipt.model.VocabularyConcept in project ipt by gbif.

the class VocabularyFactoryTest method testBuild.

@Test
public void testBuild() {
    try {
        Vocabulary tv = getFactory().build(VocabularyFactoryTest.class.getResourceAsStream("/thesauri/type-vocabulary.xml"));
        assertEquals("Dublin Core Type Vocabulary", tv.getTitle());
        assertEquals("http://dublincore.org/documents/dcmi-type-vocabulary/", tv.getUriString());
        assertEquals("The DCMI Type Vocabulary provides a general, cross-domain list of approved terms that may be used as values for the Resource Type element to identify the genre of a resource. The terms documented here are also included in the more comprehensive document \"DCMI Metadata Terms\" at http://dublincore.org/documents/dcmi-terms/.", tv.getDescription());
        assertEquals("http://dublincore.org/documents/dcmi-type-vocabulary/", tv.getLink().toString());
        assertNotNull(tv.getConcepts());
        assertEquals(12, tv.getConcepts().size());
        VocabularyConcept tc = tv.getConcepts().get(0);
        assertEquals("Collection", tc.getIdentifier());
        assertNull(tc.getLink());
        assertEquals("http://purl.org/dc/dcmitype/Collection", tc.getUri());
        assertEquals(tv, tc.getVocabulary());
        assertEquals("Collection", tc.getPreferredTerm("en").getTitle());
        assertEquals("Sammlung", tc.getPreferredTerm("de").getTitle());
        assertNotNull(tc.getTerms());
        assertNotNull(tc.getPreferredTerms());
        assertEquals(2, tc.getPreferredTerms().size());
        assertEquals(0, tc.getAlternativeTerms().size());
        assertEquals(2, tc.getTerms().size());
        // previously there was an assertion that caused IPT to fail when built with Java 5
        // Java 5 - term that comes off iterator 1st is de
        // Java 6 - term that comes off iterator 1st is en
        VocabularyTerm tt = tc.getTerms().iterator().next();
        if (tt.getLang().equals("en")) {
            assertEquals("Collection", tt.getTitle());
        } else {
            assertEquals("de", tt.getLang());
            assertEquals("Sammlung", tt.getTitle());
        }
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
Also used : Vocabulary(org.gbif.ipt.model.Vocabulary) VocabularyTerm(org.gbif.ipt.model.VocabularyTerm) VocabularyConcept(org.gbif.ipt.model.VocabularyConcept) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) Test(org.junit.jupiter.api.Test)

Example 3 with VocabularyConcept

use of org.gbif.ipt.model.VocabularyConcept in project ipt by gbif.

the class TranslationAction method automap.

/**
 * Automatically map source values to the terms in a vocabulary. A match occurs when the source value matches
 * against one of the vocabulary's term's name, preferred name, or alternate name.
 *
 * @return SUCCESS result, staying on translation page
 */
public String automap() {
    if (property == null || property.getVocabulary() == null) {
        addActionError(getText("manage.translation.cantfind.vocabulary"));
    } else {
        Vocabulary vocab = property.getVocabulary();
        int count = 0;
        for (Entry<String, String> sourceValueEntry : getSourceValuesMap().entrySet()) {
            // only if not yet mapped
            if (!getTmap().containsValue(sourceValueEntry.getValue())) {
                VocabularyConcept vc = vocab.findConcept(sourceValueEntry.getValue());
                if (vc != null) {
                    getTmap().put(sourceValueEntry.getKey(), vc.getIdentifier());
                    count++;
                }
            }
        }
        addActionMessage(getText("manage.translation.mapped.terms", new String[] { String.valueOf(count) }));
    }
    return SUCCESS;
}
Also used : Vocabulary(org.gbif.ipt.model.Vocabulary) VocabularyConcept(org.gbif.ipt.model.VocabularyConcept)

Example 4 with VocabularyConcept

use of org.gbif.ipt.model.VocabularyConcept in project ipt by gbif.

the class VocabularyFactoryTest method testBuildVersionedVocabulary.

/**
 * Test building Quantity Type vocabulary that has issued date (used to version the vocabulary).
 */
@Test
public void testBuildVersionedVocabulary() {
    try {
        Vocabulary v = getFactory().build(VocabularyFactoryTest.class.getResourceAsStream("/thesauri/quantity_type-2015-05-04.xml"));
        assertEquals("Quantity Type Vocabulary", v.getTitle());
        assertEquals("http://rs.gbif.org/vocabulary/gbif/quantityType", v.getUriString());
        assertNull(v.getLink());
        assertEquals("The quantityType Vocabulary is a recommended set of values to use for the Darwin Core quantityType property.", v.getDescription());
        // issued date parsed correctly?
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String issued = "2015-05-04";
        Date result = df.parse(issued);
        assertEquals(result.toString(), v.getIssued().toString());
        assertNotNull(v.getConcepts());
        assertEquals(12, v.getConcepts().size());
        VocabularyConcept tc = v.getConcepts().get(0);
        assertEquals("percentageOfSpecies", tc.getIdentifier());
        assertNull(tc.getLink());
        assertEquals("http://rs.gbif.org/vocabulary/gbif/quantityType/percentageOfSpecies", tc.getUri());
        assertEquals(v, tc.getVocabulary());
        assertEquals("percentage of species", tc.getPreferredTerm("en").getTitle());
        assertNotNull(tc.getTerms());
        assertEquals(1, tc.getTerms().size());
        assertNotNull(tc.getPreferredTerms());
        assertEquals(1, tc.getPreferredTerms().size());
        assertEquals(0, tc.getAlternativeTerms().size());
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
Also used : Vocabulary(org.gbif.ipt.model.Vocabulary) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) VocabularyConcept(org.gbif.ipt.model.VocabularyConcept) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) Test(org.junit.jupiter.api.Test)

Example 5 with VocabularyConcept

use of org.gbif.ipt.model.VocabularyConcept in project ipt by gbif.

the class TranslationActionTest method setup.

@BeforeEach
public void setup() throws Exception {
    // mock needed managers
    SimpleTextProvider mockTextProvider = mock(SimpleTextProvider.class);
    LocaleProviderFactory localeProviderFactory = new DefaultLocaleProviderFactory();
    AppConfig mockCfg = mock(AppConfig.class);
    ResourceManager mockResourceManager = mock(ResourceManager.class);
    SourceManager mockSourceManager = mock(SourceManager.class);
    VocabulariesManager mockVocabManager = mock(VocabulariesManager.class);
    TranslationAction.Translation translation = new TranslationAction.Translation();
    RegistrationManager mockRegistrationManager = mock(RegistrationManager.class);
    Container container = mock(Container.class);
    // mock getting list of values back for BasisOfRecord field/column in source
    Set<String> values = new LinkedHashSet<>();
    values.add("spe");
    values.add("obs");
    values.add("fos");
    when(mockSourceManager.inspectColumn(any(SourceBase.class), anyInt(), anyInt(), anyInt())).thenReturn(values);
    // mock getI18nVocab - only called in prepare()
    Map<String, String> mockVocab = new HashMap<>();
    mockVocab.put("NomenclaturalChecklist", "Nomenclatural Checklist");
    mockVocab.put("MachineObservation", "Machine Observation");
    when(mockVocabManager.getI18nVocab(anyString(), anyString(), anyBoolean())).thenReturn(mockVocab);
    // initialize new Resource
    Resource resource = new Resource();
    String resourceShortName = "TestResource";
    resource.setShortname(resourceShortName);
    // initialize new ExtensionMapping
    ExtensionMapping mapping = new ExtensionMapping();
    // add source to mapping
    mapping.setSource(new TextFileSource());
    ExtensionFactory factory = ExtensionFactoryTest.getFactory();
    Extension e = factory.build(ExtensionFactoryTest.class.getResourceAsStream("/extensions/dwc_occurrence.xml"));
    // ensure rowType for Extension is set
    if (e.getRowType() == null) {
        e.setRowType(Constants.DWC_ROWTYPE_TAXON);
    }
    // add extension to ExtensionMapping
    mapping.setExtension(e);
    // create map of source value
    TreeMap<String, String> sourceValues = new TreeMap<>();
    sourceValues.put("k1", "spe");
    sourceValues.put("k2", "obs");
    // create map of translation values
    TreeMap<String, String> translatedValues = new TreeMap<>();
    translatedValues.put("k1", "Preserved Specimen");
    translatedValues.put("k2", "observation");
    // create map of translations that get persisted
    Map<String, String> persistedTranslations = new HashMap<>();
    persistedTranslations.put("spe", "Preserved Specimen");
    persistedTranslations.put("obs", "observation");
    // initialize PropertyMapping for BasisOfRecord term
    PropertyMapping field = new PropertyMapping();
    // set ConceptTerm
    field.setTerm(DwcTerm.basisOfRecord);
    // set index
    field.setIndex(1);
    // add translations to field
    field.setTranslation(persistedTranslations);
    // add set of PropertyMapping, including field, to ExtensionMapping
    Set<PropertyMapping> fields = new TreeSet<>();
    fields.add(field);
    mapping.setFields(fields);
    // add ExtensionMapping to resource, with mapping ID 0
    List<ExtensionMapping> mappings = new LinkedList<>();
    mappings.add(mapping);
    resource.setMappings(mappings);
    // mock resourceManager.get - called only in ManagerBaseAction.prepare()
    when(mockResourceManager.get(anyString())).thenReturn(resource);
    // mock a locale provider
    when(container.getInstance(LocaleProviderFactory.class)).thenReturn(localeProviderFactory);
    // create mock Action
    action = new TranslationAction(mockTextProvider, mockCfg, mockRegistrationManager, mockResourceManager, mockSourceManager, mockVocabManager, translation);
    action.setContainer(container);
    // initialize ExtensionProperty representing BasisOfRecord field on Occurrence core Extension
    ExtensionProperty property = mapping.getExtension().getProperty(field.getTerm());
    // set a vocabulary for the BasisOfRecord field
    // mock creation of BasisOfRecord vocabulary
    VocabularyConcept concept = new VocabularyConcept();
    concept.setIdentifier("PreservedSpecimen");
    concept.setUri("http://rs.tdwg.org/dwc/dwctype/PreservedSpecimen");
    // preferred titles
    Set<VocabularyTerm> preferredTerms = new HashSet<>();
    VocabularyTerm term = new VocabularyTerm();
    term.setLang("en");
    term.setTitle("Preserved Specimen");
    preferredTerms.add(term);
    concept.setPreferredTerms(preferredTerms);
    // alternative titles
    Set<VocabularyTerm> alternateTerms = new HashSet<>();
    term = new VocabularyTerm();
    term.setLang("en");
    term.setTitle("Conserved Specimen");
    alternateTerms.add(term);
    concept.setAlternativeTerms(alternateTerms);
    Vocabulary vocab = new Vocabulary();
    List<VocabularyConcept> concepts = new ArrayList<>();
    concepts.add(concept);
    vocab.setConcepts(concepts);
    vocab.setUriString("http://rs.gbif.org/vocabulary/dwc/basis_of_record");
    property.setVocabulary(vocab);
    // create sessionScoped Translation
    // populate sessionScoped Translation with translations
    action.getTrans().setTmap(mapping.getExtension().getRowType(), property, sourceValues, translatedValues);
    // set various properties on Action
    action.setField(field);
    action.setExtensionMapping(mapping);
    action.setProperty(property);
    // mock servlet request
    HttpServletRequest mockRequest = mock(HttpServletRequest.class);
    // the mapping id is 0 - relates to resource's List<ExtensionMapping> mappings
    when(mockRequest.getParameter(TranslationAction.REQ_PARAM_MAPPINGID)).thenReturn("0");
    when(mockRequest.getParameter(TranslationAction.REQ_PARAM_ROWTYPE)).thenReturn(Constants.DWC_ROWTYPE_OCCURRENCE);
    when(mockRequest.getParameter(TranslationAction.REQ_PARAM_TERM)).thenReturn(DwcTerm.basisOfRecord.qualifiedName());
    when(mockRequest.getParameter(Constants.REQ_PARAM_RESOURCE)).thenReturn(resourceShortName);
    action.setServletRequest(mockRequest);
    // ensure the resource is set
    action.setResource(resource);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Vocabulary(org.gbif.ipt.model.Vocabulary) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) HttpServletRequest(javax.servlet.http.HttpServletRequest) Container(com.opensymphony.xwork2.inject.Container) TreeSet(java.util.TreeSet) LocaleProviderFactory(com.opensymphony.xwork2.LocaleProviderFactory) DefaultLocaleProviderFactory(com.opensymphony.xwork2.DefaultLocaleProviderFactory) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) AppConfig(org.gbif.ipt.config.AppConfig) RegistrationManager(org.gbif.ipt.service.admin.RegistrationManager) ExtensionFactoryTest(org.gbif.ipt.model.factory.ExtensionFactoryTest) SourceBase(org.gbif.ipt.model.SourceBase) Resource(org.gbif.ipt.model.Resource) VocabularyConcept(org.gbif.ipt.model.VocabularyConcept) TextFileSource(org.gbif.ipt.model.TextFileSource) ResourceManager(org.gbif.ipt.service.manage.ResourceManager) VocabulariesManager(org.gbif.ipt.service.admin.VocabulariesManager) TreeMap(java.util.TreeMap) LinkedList(java.util.LinkedList) Extension(org.gbif.ipt.model.Extension) ExtensionProperty(org.gbif.ipt.model.ExtensionProperty) ExtensionMapping(org.gbif.ipt.model.ExtensionMapping) ExtensionFactory(org.gbif.ipt.model.factory.ExtensionFactory) VocabularyTerm(org.gbif.ipt.model.VocabularyTerm) SimpleTextProvider(org.gbif.ipt.struts2.SimpleTextProvider) PropertyMapping(org.gbif.ipt.model.PropertyMapping) SourceManager(org.gbif.ipt.service.manage.SourceManager) DefaultLocaleProviderFactory(com.opensymphony.xwork2.DefaultLocaleProviderFactory) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

VocabularyConcept (org.gbif.ipt.model.VocabularyConcept)10 Vocabulary (org.gbif.ipt.model.Vocabulary)7 Paragraph (com.lowagie.text.Paragraph)4 Phrase (com.lowagie.text.Phrase)4 VocabularyTerm (org.gbif.ipt.model.VocabularyTerm)4 SimpleDateFormat (java.text.SimpleDateFormat)3 ArrayList (java.util.ArrayList)3 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)3 SAXException (org.xml.sax.SAXException)3 Date (java.util.Date)2 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 LinkedHashSet (java.util.LinkedHashSet)2 Map (java.util.Map)2 AppConfig (org.gbif.ipt.config.AppConfig)2 RegistrationManager (org.gbif.ipt.service.admin.RegistrationManager)2 VocabulariesManager (org.gbif.ipt.service.admin.VocabulariesManager)2 SimpleTextProvider (org.gbif.ipt.struts2.SimpleTextProvider)2 Inject (com.google.inject.Inject)1 Singleton (com.google.inject.Singleton)1