Search in sources :

Example 1 with NamedArea

use of eu.etaxonomy.cdm.model.location.NamedArea in project cdmlib by cybertaxonomy.

the class CsvTaxExportRedlist method doInvoke.

/**
 * Retrieves data from a CDM DB and serializes them CDM to CSV.
 * Starts with root taxa and traverses the classification to retrieve
 * children taxa, synonyms, relationships, descriptive data, red list
 * status (features).
 * Taxa that are not part of the classification are not found.
 *
 * @param exImpConfig
 * @param dbname
 * @param filename
 */
@Override
protected void doInvoke(CsvTaxExportStateRedlist state) {
    CsvTaxExportConfiguratorRedlist config = state.getConfig();
    TransactionStatus txStatus = startTransaction(true);
    List<NamedArea> selectedAreas = config.getNamedAreas();
    Set<TaxonNode> taxonNodes = assembleTaxonNodeSet(config);
    PrintWriter writer = null;
    ByteArrayOutputStream byteArrayOutputStream;
    try {
        byteArrayOutputStream = config.getByteArrayOutputStream();
        writer = new PrintWriter(byteArrayOutputStream);
        // geographical Filter
        List<TaxonNode> filteredNodes = handleGeographicalFilter(state, selectedAreas, taxonNodes);
        // sorting List
        Collections.sort(filteredNodes, new Comparator<TaxonNode>() {

            @Override
            public int compare(TaxonNode tn1, TaxonNode tn2) {
                Taxon taxon1 = tn1.getTaxon();
                Taxon taxon2 = tn2.getTaxon();
                if (taxon1 != null && taxon2 != null) {
                    return taxon1.getTitleCache().compareTo(taxon2.getTitleCache());
                } else {
                    return 0;
                }
            }
        });
        for (TaxonNode node : filteredNodes) {
            Taxon taxon = CdmBase.deproxy(node.getTaxon(), Taxon.class);
            CsvTaxRecordRedlist record = assembleRecord(state);
            INonViralName name = taxon.getName();
            Classification classification = node.getClassification();
            config.setClassificationTitleCache(classification.getTitleCache());
            if (!this.recordExists(taxon)) {
                handleTaxonBase(record, taxon, name, taxon, classification, null, false, false, config);
                record.write(writer);
                this.addExistingRecord(taxon);
            }
            // misapplied names
            handleMisapplication(taxon, writer, classification, record, config);
            writer.flush();
        }
    } catch (ClassCastException e) {
        e.printStackTrace();
    } finally {
        if (writer != null) {
            writer.close();
        }
        this.clearExistingRecordIds();
    }
    commitTransaction(txStatus);
    return;
}
Also used : INonViralName(eu.etaxonomy.cdm.model.name.INonViralName) TaxonNode(eu.etaxonomy.cdm.model.taxon.TaxonNode) Taxon(eu.etaxonomy.cdm.model.taxon.Taxon) TransactionStatus(org.springframework.transaction.TransactionStatus) NamedArea(eu.etaxonomy.cdm.model.location.NamedArea) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Classification(eu.etaxonomy.cdm.model.taxon.Classification) PrintWriter(java.io.PrintWriter)

Example 2 with NamedArea

use of eu.etaxonomy.cdm.model.location.NamedArea in project cdmlib by cybertaxonomy.

the class CsvTaxExportRedlist method handleDiscriptionData.

private void handleDiscriptionData(CsvTaxRecordRedlist record, Taxon taxon) {
    Set<TaxonDescription> descriptions = taxon.getDescriptions();
    ArrayList<String> distributions = new ArrayList<String>();
    for (TaxonDescription description : descriptions) {
        for (DescriptionElementBase el : description.getElements()) {
            if (el.isInstanceOf(Distribution.class)) {
                Distribution distribution = CdmBase.deproxy(el, Distribution.class);
                NamedArea area = distribution.getArea();
                distributions.add(area.getTitleCache());
            }
        }
    }
    record.setCountryCode(distributions);
}
Also used : Distribution(eu.etaxonomy.cdm.model.description.Distribution) TaxonDescription(eu.etaxonomy.cdm.model.description.TaxonDescription) ArrayList(java.util.ArrayList) NamedArea(eu.etaxonomy.cdm.model.location.NamedArea) DescriptionElementBase(eu.etaxonomy.cdm.model.description.DescriptionElementBase)

Example 3 with NamedArea

use of eu.etaxonomy.cdm.model.location.NamedArea in project cdmlib by cybertaxonomy.

the class MarkupFeatureImport method makeVernacular.

private List<DescriptionElementBase> makeVernacular(MarkupImportState state, String subheading, String commonNameString) throws XMLStreamException {
    List<DescriptionElementBase> result = new ArrayList<>();
    Reference sourceReference = state.getConfig().getSourceReference();
    String[] splits = commonNameString.split(",");
    for (String split : splits) {
        split = split.trim();
        if (!split.matches(".*\\(.*\\)\\.?")) {
            fireWarningEvent("Common name string '" + split + "' does not match given pattern", state.getReader().peek(), 4);
        }
        String name = split.replaceAll("\\(.*\\)", "").replace(".", "").trim();
        String languageStr = split.replaceFirst(".*\\(", "").replaceAll("\\)\\.?", "").trim();
        Language language = null;
        if (StringUtils.isNotBlank(languageStr)) {
            language = makeLanguageByLangStr(state, languageStr);
        }
        DescriptionElementBase commonName;
        if (name != null && name.length() < 255) {
            NamedArea area = null;
            commonName = CommonTaxonName.NewInstance(name, language, area);
            commonName.addPrimaryTaxonomicSource(sourceReference);
        } else {
            if (language == null) {
                language = getDefaultLanguage(state);
            }
            commonName = TextData.NewInstance(Feature.COMMON_NAME(), name, language, null);
            commonName.addPrimaryTaxonomicSource(sourceReference);
            String warning = "Vernacular feature is >255 size. Therefore it is handled as TextData, not CommonTaxonName: " + name;
            fireWarningEvent(warning, state.getReader().peek(), 1);
        }
        result.add(commonName);
    }
    return result;
}
Also used : Language(eu.etaxonomy.cdm.model.common.Language) Reference(eu.etaxonomy.cdm.model.reference.Reference) IntextReference(eu.etaxonomy.cdm.model.common.IntextReference) ArrayList(java.util.ArrayList) NamedArea(eu.etaxonomy.cdm.model.location.NamedArea) LanguageString(eu.etaxonomy.cdm.model.common.LanguageString) DescriptionElementBase(eu.etaxonomy.cdm.model.description.DescriptionElementBase)

Example 4 with NamedArea

use of eu.etaxonomy.cdm.model.location.NamedArea in project cdmlib by cybertaxonomy.

the class MarkupImportBase method handleDistributionLocality.

private String handleDistributionLocality(MarkupImportState state, XMLEventReader reader, XMLEvent parentEvent) throws XMLStreamException {
    Map<String, Attribute> attributes = getAttributes(parentEvent);
    String classValue = getAndRemoveRequiredAttributeValue(parentEvent, attributes, CLASS);
    String statusValue = getAndRemoveAttributeValue(attributes, STATUS);
    String frequencyValue = getAndRemoveAttributeValue(attributes, FREQUENCY);
    Taxon taxon = state.getCurrentTaxon();
    // TODO which ref to take?
    Reference sourceReference = state.getConfig().getSourceReference();
    String text = "";
    while (reader.hasNext()) {
        XMLEvent next = readNoWhitespace(reader);
        if (isMyEndingElement(next, parentEvent)) {
            if (StringUtils.isNotBlank(text)) {
                String label = CdmUtils.removeTrailingDots(normalize(text));
                TaxonDescription description = getExtractedMarkupMarkedDescription(state, taxon, sourceReference);
                NamedAreaLevel level = makeNamedAreaLevel(state, classValue, next);
                // status
                PresenceAbsenceTerm status = null;
                if (isNotBlank(statusValue)) {
                    try {
                        status = state.getTransformer().getPresenceTermByKey(statusValue);
                        if (status == null) {
                            UUID uuid = state.getTransformer().getPresenceTermUuid(statusValue);
                            if (uuid != null) {
                                status = this.getPresenceAbsenceTerm(state, uuid, statusValue, statusValue, statusValue, false, null);
                            }
                        }
                        if (status == null) {
                            // TODO
                            String message = "The presence/absence status '%s' could not be transformed to an CDM status";
                            fireWarningEvent(String.format(message, statusValue), next, 4);
                        }
                    } catch (UndefinedTransformerMethodException e) {
                        throw new RuntimeException(e);
                    }
                } else {
                    status = PresenceAbsenceTerm.PRESENT();
                }
                // frequency
                if (isNotBlank(frequencyValue)) {
                    if (frequencyValue.equalsIgnoreCase("absent") && PresenceAbsenceTerm.PRESENT().equals(status)) {
                        // to be on the safe side that not real status has been defined yet.
                        status = PresenceAbsenceTerm.ABSENT();
                    } else {
                        String message = "The frequency attribute is currently not yet available in CDM";
                        fireWarningEvent(message, parentEvent, 6);
                    }
                }
                NamedArea higherArea = null;
                List<NamedArea> areas = new ArrayList<>();
                String patSingleArea = "([^,\\(]{3,})";
                String patSeparator = "(,|\\sand\\s)";
                String hierarchiePattern = String.format("%s\\((%s(%s%s)*)\\)", patSingleArea, patSingleArea, patSeparator, patSingleArea);
                Pattern patHierarchie = Pattern.compile(hierarchiePattern, Pattern.CASE_INSENSITIVE);
                Matcher matcher = patHierarchie.matcher(label);
                if (matcher.matches()) {
                    String higherAreaStr = matcher.group(1).trim();
                    higherArea = makeArea(state, higherAreaStr, level);
                    String[] innerAreas = matcher.group(2).split(patSeparator);
                    for (String innerArea : innerAreas) {
                        if (isNotBlank(innerArea)) {
                            NamedArea singleArea = makeArea(state, innerArea.trim(), level);
                            areas.add(singleArea);
                            NamedArea partOf = singleArea.getPartOf();
                        // if (partOf == null){
                        // singleArea.setPartOf(higherArea);
                        // }
                        }
                    }
                } else {
                    NamedArea singleArea = makeArea(state, label, level);
                    areas.add(singleArea);
                }
                for (NamedArea area : areas) {
                    // create distribution
                    Distribution distribution = Distribution.NewInstance(area, status);
                    distribution.addPrimaryTaxonomicSource(sourceReference);
                    description.addElement(distribution);
                }
            } else {
                String message = "Empty distribution locality";
                fireWarningEvent(message, next, 4);
            }
            return text;
        } else if (isStartingElement(next, COORDINATES)) {
            // TODO
            handleNotYetImplementedElement(next);
        } else if (isEndingElement(next, COORDINATES)) {
            // TODO
            popUnimplemented(next.asEndElement());
        } else if (next.isCharacters()) {
            text += next.asCharacters().getData();
        } else {
            handleUnexpectedElement(next);
        }
    }
    throw new IllegalStateException("<DistributionLocality> has no closing tag");
}
Also used : Pattern(java.util.regex.Pattern) Attribute(javax.xml.stream.events.Attribute) Matcher(java.util.regex.Matcher) IntextReference(eu.etaxonomy.cdm.model.common.IntextReference) Reference(eu.etaxonomy.cdm.model.reference.Reference) Taxon(eu.etaxonomy.cdm.model.taxon.Taxon) TaxonDescription(eu.etaxonomy.cdm.model.description.TaxonDescription) ArrayList(java.util.ArrayList) NamedArea(eu.etaxonomy.cdm.model.location.NamedArea) Distribution(eu.etaxonomy.cdm.model.description.Distribution) XMLEvent(javax.xml.stream.events.XMLEvent) NamedAreaLevel(eu.etaxonomy.cdm.model.location.NamedAreaLevel) PresenceAbsenceTerm(eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm) UUID(java.util.UUID) UndefinedTransformerMethodException(eu.etaxonomy.cdm.io.common.mapping.UndefinedTransformerMethodException)

Example 5 with NamedArea

use of eu.etaxonomy.cdm.model.location.NamedArea in project cdmlib by cybertaxonomy.

the class MarkupImportBase method makeArea.

/**
 * @param state
 * @param areaName
 * @param level
 * @return
 */
protected NamedArea makeArea(MarkupImportState state, String areaName, NamedAreaLevel level) {
    // TODO FM vocabulary
    TermVocabulary<NamedArea> voc = null;
    NamedAreaType areaType = null;
    NamedArea area = null;
    try {
        area = state.getTransformer().getNamedAreaByKey(areaName);
    } catch (UndefinedTransformerMethodException e) {
        throw new RuntimeException(e);
    }
    if (area == null) {
        boolean isNewInState = false;
        UUID uuid = state.getAreaUuid(areaName);
        if (uuid == null) {
            isNewInState = true;
            try {
                uuid = state.getTransformer().getNamedAreaUuid(areaName);
                if (uuid == null) {
                    uuid = UUID.randomUUID();
                    state.putAreaUuid(areaName, uuid);
                }
            } catch (UndefinedTransformerMethodException e) {
                throw new RuntimeException(e);
            }
        }
        CdmImportBase.TermMatchMode matchMode = CdmImportBase.TermMatchMode.UUID_LABEL;
        area = getNamedArea(state, uuid, areaName, areaName, areaName, areaType, level, voc, matchMode);
        if (isNewInState) {
            state.putAreaUuid(areaName, area.getUuid());
            // TODO just for testing -> make generic and move to better place
            String geoServiceLayer = "vmap0_as_bnd_political_boundary_a";
            String layerFieldName = "nam";
            if ("Bangka".equals(areaName)) {
                String areaValue = "PULAU BANGKA#SUMATERA SELATAN";
                GeoServiceArea geoServiceArea = new GeoServiceArea();
                geoServiceArea.add(geoServiceLayer, layerFieldName, areaValue);
                this.editGeoService.setMapping(area, geoServiceArea);
            // save(area, state);
            }
            if ("Luzon".equals(areaName)) {
                GeoServiceArea geoServiceArea = new GeoServiceArea();
                List<String> list = Arrays.asList("HERMANA MAYOR ISLAND#CENTRAL LUZON", "HERMANA MENOR ISLAND#CENTRAL LUZON", "CENTRAL LUZON");
                for (String areaValue : list) {
                    geoServiceArea.add(geoServiceLayer, layerFieldName, areaValue);
                }
                this.editGeoService.setMapping(area, geoServiceArea);
            // save(area, state);
            }
            if ("Mindanao".equals(areaName)) {
                GeoServiceArea geoServiceArea = new GeoServiceArea();
                List<String> list = Arrays.asList("NORTHERN MINDANAO", "SOUTHERN MINDANAO", "WESTERN MINDANAO");
                // TODO to be continued
                for (String areaValue : list) {
                    geoServiceArea.add(geoServiceLayer, layerFieldName, areaValue);
                }
                this.editGeoService.setMapping(area, geoServiceArea);
            // save(area, state);
            }
            if ("Palawan".equals(areaName)) {
                GeoServiceArea geoServiceArea = new GeoServiceArea();
                List<String> list = Arrays.asList("PALAWAN#SOUTHERN TAGALOG");
                for (String areaValue : list) {
                    geoServiceArea.add(geoServiceLayer, layerFieldName, areaValue);
                }
                this.editGeoService.setMapping(area, geoServiceArea);
            // save(area, state);
            }
        }
    }
    return area;
}
Also used : CdmImportBase(eu.etaxonomy.cdm.io.common.CdmImportBase) NamedArea(eu.etaxonomy.cdm.model.location.NamedArea) GeoServiceArea(eu.etaxonomy.cdm.ext.geo.GeoServiceArea) NamedAreaType(eu.etaxonomy.cdm.model.location.NamedAreaType) TermMatchMode(eu.etaxonomy.cdm.io.common.CdmImportBase.TermMatchMode) UUID(java.util.UUID) UndefinedTransformerMethodException(eu.etaxonomy.cdm.io.common.mapping.UndefinedTransformerMethodException)

Aggregations

NamedArea (eu.etaxonomy.cdm.model.location.NamedArea)141 ArrayList (java.util.ArrayList)35 Distribution (eu.etaxonomy.cdm.model.description.Distribution)34 Test (org.junit.Test)34 Taxon (eu.etaxonomy.cdm.model.taxon.Taxon)29 HashSet (java.util.HashSet)29 UUID (java.util.UUID)25 PresenceAbsenceTerm (eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm)23 TaxonDescription (eu.etaxonomy.cdm.model.description.TaxonDescription)23 CdmTransactionalIntegrationTest (eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest)22 LanguageString (eu.etaxonomy.cdm.model.common.LanguageString)16 DescriptionElementBase (eu.etaxonomy.cdm.model.description.DescriptionElementBase)12 TaxonNode (eu.etaxonomy.cdm.model.taxon.TaxonNode)12 HashMap (java.util.HashMap)12 NamedAreaLevel (eu.etaxonomy.cdm.model.location.NamedAreaLevel)10 DefinedTerm (eu.etaxonomy.cdm.model.term.DefinedTerm)10 Reference (eu.etaxonomy.cdm.model.reference.Reference)8 Classification (eu.etaxonomy.cdm.model.taxon.Classification)8 Language (eu.etaxonomy.cdm.model.common.Language)7 DataSet (org.unitils.dbunit.annotation.DataSet)7