Search in sources :

Example 1 with NamedAreaLevel

use of eu.etaxonomy.cdm.model.location.NamedAreaLevel 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 2 with NamedAreaLevel

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

the class MarkupImportBase method makeNamedAreaLevel.

/**
 * @param state
 * @param levelString
 * @param next
 * @return
 */
protected NamedAreaLevel makeNamedAreaLevel(MarkupImportState state, String levelString, XMLEvent next) {
    NamedAreaLevel level;
    try {
        level = state.getTransformer().getNamedAreaLevelByKey(levelString);
        if (level == null) {
            UUID levelUuid = state.getTransformer().getNamedAreaLevelUuid(levelString);
            if (levelUuid == null) {
                String message = "Unknown distribution locality class (named area level): %s. Create new level instead.";
                message = String.format(message, levelString);
                fireWarningEvent(message, next, 6);
            }
            level = getNamedAreaLevel(state, levelUuid, levelString, levelString, levelString, null);
        }
    } catch (UndefinedTransformerMethodException e) {
        throw new RuntimeException(e);
    }
    return level;
}
Also used : NamedAreaLevel(eu.etaxonomy.cdm.model.location.NamedAreaLevel) UUID(java.util.UUID) UndefinedTransformerMethodException(eu.etaxonomy.cdm.io.common.mapping.UndefinedTransformerMethodException)

Example 3 with NamedAreaLevel

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

the class MarkupSpecimenImport method handleLocality.

private void handleLocality(MarkupImportState state, XMLEventReader reader, XMLEvent parentEvent, DerivedUnitFacade facade) throws XMLStreamException {
    String classValue = getClassOnlyAttribute(parentEvent);
    boolean isLocality = false;
    NamedAreaLevel areaLevel = null;
    if ("locality".equalsIgnoreCase(classValue) || state.getConfig().isIgnoreLocalityClass()) {
        isLocality = true;
    } else {
        areaLevel = makeNamedAreaLevel(state, classValue, parentEvent);
    }
    String text = "";
    // elements
    while (reader.hasNext()) {
        XMLEvent next = readNoWhitespace(reader);
        if (isMyEndingElement(next, parentEvent)) {
            if (StringUtils.isNotBlank(text)) {
                text = normalize(text);
                text = removeTrailingPunctuation(text);
                if (isLocality) {
                    facade.setLocality(text, getDefaultLanguage(state));
                } else {
                    text = CdmUtils.removeTrailingDots(text);
                    NamedArea area = makeArea(state, text, areaLevel);
                    facade.addCollectingArea(area);
                }
            }
            // TODO
            return;
        } else if (isStartingElement(next, ALTITUDE)) {
            handleNotYetImplementedElement(next);
        // homotypicalGroup = handleNom(state, reader, next, taxon,
        // homotypicalGroup);
        } else if (isStartingElement(next, COORDINATES)) {
            handleNotYetImplementedElement(next);
        } else if (isStartingElement(next, ANNOTATION)) {
            handleNotYetImplementedElement(next);
        } else if (next.isCharacters()) {
            text += next.asCharacters().getData();
        } else {
            handleUnexpectedElement(next);
        }
    }
    throw new IllegalStateException("<SpecimenType> has no closing tag");
}
Also used : XMLEvent(javax.xml.stream.events.XMLEvent) NamedAreaLevel(eu.etaxonomy.cdm.model.location.NamedAreaLevel) NamedArea(eu.etaxonomy.cdm.model.location.NamedArea)

Example 4 with NamedAreaLevel

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

the class SpecimenCdmExcelImport method handleAreas.

private void handleAreas(DerivedUnitFacade facade, SpecimenRow row, SpecimenCdmExcelImportState state) {
    List<PostfixTerm> areas = row.getLeveledAreas();
    for (PostfixTerm lArea : areas) {
        String description = lArea.term;
        String abbrev = lArea.term;
        NamedAreaType type = null;
        String key = lArea.postfix + "_" + lArea.term;
        UUID areaUuid = state.getArea(key);
        NamedAreaLevel level = state.getPostfixLevel(lArea.postfix);
        TermMatchMode matchMode = state.getConfig().getAreaMatchMode();
        NamedArea area = getNamedArea(state, areaUuid, lArea.term, description, abbrev, type, level, null, matchMode);
        facade.addCollectingArea(area);
        if (areaUuid == null) {
            state.putArea(key, area.getUuid());
        }
    }
}
Also used : PostfixTerm(eu.etaxonomy.cdm.io.excel.common.ExcelRowBase.PostfixTerm) NamedAreaLevel(eu.etaxonomy.cdm.model.location.NamedAreaLevel) NamedArea(eu.etaxonomy.cdm.model.location.NamedArea) NamedAreaType(eu.etaxonomy.cdm.model.location.NamedAreaType) UUID(java.util.UUID)

Example 5 with NamedAreaLevel

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

the class NamedAreaLevelExcelImport method firstPass.

@Override
protected void firstPass(SpecimenCdmExcelImportState state) {
    NamedAreaLevellRow row = state.getNamedAreaLevelRow();
    // level
    UUID uuid = row.getUuid();
    String label = row.getAbbreviation();
    String text = row.getDescription();
    String labelAbbrev = row.getAbbreviation();
    NamedAreaLevel level = getNamedAreaLevel(state, uuid, label, text, labelAbbrev, null);
    if (StringUtils.isNotBlank(row.getPostfix())) {
        state.putPostfixLevel(row.getPostfix(), level);
    }
    // save
    getTermService().save(level);
    return;
}
Also used : NamedAreaLevel(eu.etaxonomy.cdm.model.location.NamedAreaLevel) UUID(java.util.UUID)

Aggregations

NamedAreaLevel (eu.etaxonomy.cdm.model.location.NamedAreaLevel)20 NamedArea (eu.etaxonomy.cdm.model.location.NamedArea)10 UUID (java.util.UUID)10 ArrayList (java.util.ArrayList)6 NamedAreaType (eu.etaxonomy.cdm.model.location.NamedAreaType)4 Distribution (eu.etaxonomy.cdm.model.description.Distribution)3 AggregationMode (eu.etaxonomy.cdm.api.service.description.AggregationMode)2 DistributionAggregationConfiguration (eu.etaxonomy.cdm.api.service.description.DistributionAggregationConfiguration)2 TaxonNodeFilter (eu.etaxonomy.cdm.filter.TaxonNodeFilter)2 UndefinedTransformerMethodException (eu.etaxonomy.cdm.io.common.mapping.UndefinedTransformerMethodException)2 Rank (eu.etaxonomy.cdm.model.name.Rank)2 OrderHint (eu.etaxonomy.cdm.persistence.query.OrderHint)2 HashSet (java.util.HashSet)2 List (java.util.List)2 XMLEvent (javax.xml.stream.events.XMLEvent)2 JsonParseException (com.fasterxml.jackson.core.JsonParseException)1 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)1 CdmApplicationController (eu.etaxonomy.cdm.api.application.CdmApplicationController)1 AggregationSourceMode (eu.etaxonomy.cdm.api.service.description.AggregationSourceMode)1 DistributionAggregation (eu.etaxonomy.cdm.api.service.description.DistributionAggregation)1