Search in sources :

Example 1 with UnknownCdmTypeException

use of eu.etaxonomy.cdm.strategy.exceptions.UnknownCdmTypeException in project cdmlib by cybertaxonomy.

the class MarkupNomenclatureImport method handleNameStatus.

/**
 * @param state
 * @param name
 * @param next
 */
private void handleNameStatus(MarkupImportState state, INonViralName name, XMLEvent next) {
    if (isNotBlank(state.getNameStatus())) {
        String nameStatus = state.getNameStatus().trim();
        try {
            NomenclaturalStatusType nomStatusType = NomenclaturalStatusType.getNomenclaturalStatusTypeByAbbreviation(nameStatus, name);
            name.addStatus(NomenclaturalStatus.NewInstance(nomStatusType));
        } catch (UnknownCdmTypeException e) {
            String message = "Status '%s' could not be recognized";
            message = String.format(message, nameStatus);
            fireWarningEvent(message, next, 4);
        }
    }
}
Also used : UnknownCdmTypeException(eu.etaxonomy.cdm.strategy.exceptions.UnknownCdmTypeException) NomenclaturalStatusType(eu.etaxonomy.cdm.model.name.NomenclaturalStatusType)

Example 2 with UnknownCdmTypeException

use of eu.etaxonomy.cdm.strategy.exceptions.UnknownCdmTypeException in project cdmlib by cybertaxonomy.

the class MarkupNomenclatureImport method fillName.

private void fillName(MarkupImportState state, Map<String, String> nameMap, INonViralName name, TaxonRelationship misappliedRel, XMLEvent event) {
    // Ranks: family, subfamily, tribus, genus, subgenus, section,
    // subsection, species, subspecies, variety, subvariety, forma
    // infrank, paraut, author, infrparaut, infraut, status, notes
    String infrank = getAndRemoveMapKey(nameMap, INFRANK);
    String authorStr = getAndRemoveMapKey(nameMap, AUTHOR);
    String paraut = getAndRemoveMapKey(nameMap, PARAUT);
    String infrParAut = getAndRemoveMapKey(nameMap, INFRPARAUT);
    String infrAut = getAndRemoveMapKey(nameMap, INFRAUT);
    String statusStr = getAndRemoveMapKey(nameMap, STATUS);
    String notes = getAndRemoveMapKey(nameMap, NOTES);
    if (misappliedRel != null && authorStr != null && authorStr.startsWith("auct.")) {
        misappliedRel.getFromTaxon().setAppendedPhrase(authorStr);
        authorStr = null;
    }
    if (!name.isProtectedTitleCache()) {
        // otherwise fullName
        makeRankDecision(state, nameMap, name, event, infrank);
        // test consistency of rank and authors
        testRankAuthorConsistency(name, event, authorStr, paraut, infrParAut, infrAut);
        // authors
        makeNomenclaturalAuthors(state, event, name, authorStr, paraut, infrParAut, infrAut);
    }
    // TODO handle pro parte, pro syn. etc.
    if (isNotBlank(statusStr)) {
        String proPartePattern = "(pro parte|p.p.)";
        if (statusStr.matches(proPartePattern)) {
            state.setProParte(true);
        }
        try {
            // TODO handle trim earlier
            statusStr = statusStr.trim();
            NomenclaturalStatusType nomStatusType = NomenclaturalStatusType.getNomenclaturalStatusTypeByAbbreviation(statusStr, name);
            name.addStatus(NomenclaturalStatus.NewInstance(nomStatusType));
        } catch (UnknownCdmTypeException e) {
            String message = "Status '%s' could not be recognized";
            message = String.format(message, statusStr);
            fireWarningEvent(message, event, 4);
        }
    }
    // notes
    if (StringUtils.isNotBlank(notes)) {
        handleNotYetImplementedAttributeValue(event, CLASS, NOTES);
    }
    return;
}
Also used : UnknownCdmTypeException(eu.etaxonomy.cdm.strategy.exceptions.UnknownCdmTypeException) NomenclaturalStatusType(eu.etaxonomy.cdm.model.name.NomenclaturalStatusType)

Example 3 with UnknownCdmTypeException

use of eu.etaxonomy.cdm.strategy.exceptions.UnknownCdmTypeException in project cdmlib by cybertaxonomy.

the class MarkupSpecimenImport method makeFotgSpecimenType.

/**
 * Implemented for Flora of the Guyanas this may include duplicated code from similar places
 * @param state
 * @param collectionAndTypeOrig
 * @param facade
 * @param name
 * @param parentEvent
 * @return
 */
private boolean makeFotgSpecimenType(MarkupImportState state, final String collectionAndTypeOrig, DerivedUnitFacade facade, INonViralName name, XMLEvent parentEvent) {
    String collectionAndType = collectionAndTypeOrig;
    String notDesignatedRE = "not\\s+designated";
    String designatedByRE = "\\s*\\(((designated\\s+by\\s+|according\\s+to\\s+)[^\\)]+|here\\s+designated)\\)";
    String typesRE = "(holotype|isotypes?|neotype|isoneotype|syntype|lectotype|isolectotypes?|typ\\.\\scons\\.,?)";
    String collectionRE = "[A-Z\\-]{1,5}!?";
    String collectionsRE = String.format("%s(,\\s+%s)*", collectionRE, collectionRE);
    String addInfoRE = "(not\\s+seen|(presumed\\s+)?destroyed)";
    String singleTypeTypeRE = String.format("(%s\\s)?%s(,\\s+%s)*", typesRE, collectionsRE, addInfoRE);
    String allTypesRE = String.format("(\\(not\\s+seen\\)|\\(%s([,;]\\s%s)?\\))", singleTypeTypeRE, singleTypeTypeRE);
    String designatedRE = String.format("%s(%s)?", allTypesRE, designatedByRE);
    if (fotgTypePattern == null) {
        String pattern = String.format("(%s|%s)", notDesignatedRE, designatedRE);
        fotgTypePattern = Pattern.compile(pattern);
    }
    Matcher matcher = fotgTypePattern.matcher(collectionAndType);
    if (matcher.matches()) {
        fireWarningEvent("Try to synchronize type handling (at least creation) with standard type handling. E.g. use TypeInfo and according algorithms", parentEvent, 2);
        if (collectionAndType.matches(notDesignatedRE)) {
            SpecimenTypeDesignation desig = SpecimenTypeDesignation.NewInstance();
            desig.setNotDesignated(true);
            // name.addSpecimenTypeDesignation(typeSpecimen, status, citation, citationMicroReference, originalNameString, isNotDesignated, addToAllHomotypicNames)
            name.addTypeDesignation(desig, true);
        } else if (collectionAndType.matches(designatedRE)) {
            String designatedBy = null;
            Matcher desigMatcher = Pattern.compile(designatedByRE).matcher(collectionAndType);
            boolean hasDesignatedBy = desigMatcher.find();
            if (hasDesignatedBy) {
                designatedBy = desigMatcher.group(0);
                collectionAndType = collectionAndType.replace(designatedBy, "");
            }
            // remove brackets
            collectionAndType = collectionAndType.substring(1, collectionAndType.length() - 1);
            List<String> singleTypes = new ArrayList<String>();
            Pattern singleTypePattern = Pattern.compile("^" + singleTypeTypeRE);
            matcher = singleTypePattern.matcher(collectionAndType);
            while (matcher.find()) {
                String match = matcher.group(0);
                singleTypes.add(match);
                collectionAndType = collectionAndType.substring(match.length());
                if (!collectionAndType.isEmpty()) {
                    collectionAndType = collectionAndType.substring(1).trim();
                } else {
                    break;
                }
                matcher = singleTypePattern.matcher(collectionAndType);
            }
            List<SpecimenTypeDesignation> designations = new ArrayList<SpecimenTypeDesignation>();
            // single types
            for (String singleTypeOrig : singleTypes) {
                String singleType = singleTypeOrig;
                // type
                Pattern typePattern = Pattern.compile("^" + typesRE);
                matcher = typePattern.matcher(singleType);
                SpecimenTypeDesignationStatus typeStatus = null;
                if (matcher.find()) {
                    String typeStr = matcher.group(0);
                    singleType = singleType.substring(typeStr.length()).trim();
                    try {
                        typeStatus = SpecimenTypeParser.parseSpecimenTypeStatus(typeStr);
                    } catch (UnknownCdmTypeException e) {
                        fireWarningEvent("specimen type not recognized. Use generic type instead", parentEvent, 4);
                        typeStatus = SpecimenTypeDesignationStatus.TYPE();
                    // TODO use also type info from state
                    }
                } else {
                    typeStatus = SpecimenTypeDesignationStatus.TYPE();
                // TODO use also type info from state
                }
                // collection
                Pattern collectionPattern = Pattern.compile("^" + collectionsRE);
                matcher = collectionPattern.matcher(singleType);
                String[] collectionStrings = new String[0];
                if (matcher.find()) {
                    String collectionStr = matcher.group(0);
                    singleType = singleType.substring(collectionStr.length());
                    collectionStr = collectionStr.replace("(", "").replace(")", "").replaceAll("\\s", "");
                    collectionStrings = collectionStr.split(",");
                }
                // addInfo
                if (!singleType.isEmpty() && singleType.startsWith(", ")) {
                    singleType = singleType.substring(2);
                }
                boolean notSeen = false;
                if (singleType.equals("not seen")) {
                    singleType = singleType.replace("not seen", "");
                    notSeen = true;
                }
                if (singleType.startsWith("not seen, ")) {
                    singleType = singleType.replace("not seen, ", "");
                    notSeen = true;
                }
                boolean destroyed = false;
                if (singleType.equals("destroyed")) {
                    destroyed = true;
                    singleType = singleType.replace("destroyed", "");
                }
                boolean presumedDestroyed = false;
                if (singleType.equals("presumed destroyed")) {
                    presumedDestroyed = true;
                    singleType = singleType.replace("presumed destroyed", "");
                }
                boolean hasAddInfo = notSeen || destroyed || presumedDestroyed;
                if (!singleType.isEmpty()) {
                    String message = "SingleType was not fully read. Remaining: " + singleType + ". Original singleType was: " + singleTypeOrig;
                    fireWarningEvent(message, parentEvent, 6);
                    System.out.println(message);
                }
                if (collectionStrings.length > 0) {
                    boolean isFirst = true;
                    for (String collStr : collectionStrings) {
                        Collection collection = getCollection(state, collStr);
                        DerivedUnit unit = isFirst ? facade.innerDerivedUnit() : facade.addDuplicate(collection, null, null, null, null);
                        SpecimenTypeDesignation desig = SpecimenTypeDesignation.NewInstance();
                        designations.add(desig);
                        desig.setTypeSpecimen(unit);
                        desig.setTypeStatus(typeStatus);
                        handleSpecimenTypeAddInfo(state, notSeen, destroyed, presumedDestroyed, desig);
                        name.addTypeDesignation(desig, true);
                        isFirst = false;
                    }
                } else if (hasAddInfo) {
                    // handle addInfo if no collection data available
                    SpecimenTypeDesignation desig = SpecimenTypeDesignation.NewInstance();
                    designations.add(desig);
                    desig.setTypeStatus(typeStatus);
                    handleSpecimenTypeAddInfo(state, notSeen, destroyed, presumedDestroyed, desig);
                    name.addTypeDesignation(desig, true);
                } else {
                    fireWarningEvent("No type designation could be created as collection info was not recognized", parentEvent, 4);
                }
            }
            if (designatedBy != null) {
                if (designations.size() != 1) {
                    fireWarningEvent("Size of type designations is not exactly 1, which is expected for 'designated by'", parentEvent, 2);
                }
                designatedBy = designatedBy.trim();
                if (designatedBy.startsWith("(") && designatedBy.endsWith(")")) {
                    designatedBy = designatedBy.substring(1, designatedBy.length() - 1);
                }
                for (SpecimenTypeDesignation desig : designations) {
                    if (designatedBy.startsWith("designated by")) {
                        String titleCache = designatedBy.replace("designated by", "").trim();
                        Reference reference = ReferenceFactory.newGeneric();
                        reference.setTitleCache(titleCache, true);
                        desig.setCitation(reference);
                        // in future we could also try to parse it automatically
                        fireWarningEvent("MANUALLY: Designated by should be parsed manually: " + titleCache, parentEvent, 1);
                    } else if (designatedBy.equals("designated here")) {
                        Reference ref = state.getConfig().getSourceReference();
                        desig.setCitation(ref);
                        fireWarningEvent("MANUALLY: Microcitation should be added to 'designated here", parentEvent, 1);
                    } else if (designatedBy.startsWith("according to")) {
                        String annotationStr = designatedBy.replace("according to", "").trim();
                        Annotation annotation = Annotation.NewInstance(annotationStr, AnnotationType.EDITORIAL(), Language.ENGLISH());
                        desig.addAnnotation(annotation);
                    } else {
                        fireWarningEvent("Designated by does not match known pattern: " + designatedBy, parentEvent, 6);
                    }
                }
            }
        } else {
            fireWarningEvent("CollectionAndType unexpectedly not matching: " + collectionAndTypeOrig, parentEvent, 6);
        }
        return true;
    } else {
        if (state.getConfig().isUseFotGSpecimenTypeCollectionAndTypeOnly()) {
            fireWarningEvent("NO MATCH: " + collectionAndTypeOrig, parentEvent, 4);
        }
        return false;
    }
// // remove brackets
// if (collectionAndType.matches("^\\(.*\\)\\.?$")) {
// collectionAndType = collectionAndType.replaceAll("\\.$", "");
// collectionAndType = collectionAndType.substring(1, collectionAndType.length() - 1);
// }
// 
// String[] split = collectionAndType.split("[;,]");
// for (String str : split) {
// str = str.trim();
// boolean addToAllNamesInGroup = true;
// TypeInfo typeInfo = makeSpecimenTypeTypeInfo(str, parentEvent);
// SpecimenTypeDesignationStatus typeStatus = typeInfo.status;
// Collection collection = this.getCollection(state, typeInfo.collectionString);
// 
// // TODO improve cache strategy handling
// DerivedUnit typeSpecimen = facade.addDuplicate(collection, null, null, null, null);
// typeSpecimen.setCacheStrategy(new DerivedUnitFacadeCacheStrategy());
// name.addSpecimenTypeDesignation(typeSpecimen, typeStatus, null, null, null, false, addToAllNamesInGroup);
// }
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) Reference(eu.etaxonomy.cdm.model.reference.Reference) SpecimenTypeDesignationStatus(eu.etaxonomy.cdm.model.name.SpecimenTypeDesignationStatus) Annotation(eu.etaxonomy.cdm.model.common.Annotation) DerivedUnit(eu.etaxonomy.cdm.model.occurrence.DerivedUnit) UnknownCdmTypeException(eu.etaxonomy.cdm.strategy.exceptions.UnknownCdmTypeException) Collection(eu.etaxonomy.cdm.model.occurrence.Collection) List(java.util.List) ArrayList(java.util.ArrayList) SpecimenTypeDesignation(eu.etaxonomy.cdm.model.name.SpecimenTypeDesignation)

Example 4 with UnknownCdmTypeException

use of eu.etaxonomy.cdm.strategy.exceptions.UnknownCdmTypeException in project cdmlib by cybertaxonomy.

the class CdmLightClassificationExport method handleName.

private void handleName(CdmLightExportState state, TaxonName name, Taxon acceptedTaxon, boolean acceptedName) {
    if (name == null || state.getNameStore().containsKey(name.getId())) {
        return;
    }
    try {
        Rank rank = name.getRank();
        CdmLightExportTable table = CdmLightExportTable.SCIENTIFIC_NAME;
        name = HibernateProxyHelper.deproxy(name);
        state.getNameStore().put(name.getId(), name.getUuid());
        String[] csvLine = new String[table.getSize()];
        csvLine[table.getIndex(CdmLightExportTable.NAME_ID)] = getId(state, name);
        if (name.getLsid() != null) {
            csvLine[table.getIndex(CdmLightExportTable.LSID)] = name.getLsid().getLsid();
        } else {
            csvLine[table.getIndex(CdmLightExportTable.LSID)] = "";
        }
        handleIdentifier(state, name);
        handleDescriptions(state, name);
        csvLine[table.getIndex(CdmLightExportTable.RANK)] = getTitleCache(rank);
        if (rank != null) {
            csvLine[table.getIndex(CdmLightExportTable.RANK_SEQUENCE)] = String.valueOf(rank.getOrderIndex());
            if (rank.isInfraGeneric()) {
                try {
                    csvLine[table.getIndex(CdmLightExportTable.INFRAGENERIC_RANK)] = name.getRank().getInfraGenericMarker();
                } catch (UnknownCdmTypeException e) {
                    state.getResult().addError("Infrageneric marker expected but not available for rank " + name.getRank().getTitleCache());
                }
            }
            if (rank.isInfraSpecific()) {
                csvLine[table.getIndex(CdmLightExportTable.INFRASPECIFIC_RANK)] = name.getRank().getAbbreviation();
            }
        } else {
            csvLine[table.getIndex(CdmLightExportTable.RANK_SEQUENCE)] = "";
        }
        if (name.isProtectedTitleCache()) {
            csvLine[table.getIndex(CdmLightExportTable.FULL_NAME_WITH_AUTHORS)] = name.getTitleCache();
        } else {
            // TODO: adapt the tropicos titlecache creation
            csvLine[table.getIndex(CdmLightExportTable.FULL_NAME_WITH_AUTHORS)] = name.getTitleCache();
        }
        if (!state.getConfig().isAddHTML()) {
            csvLine[table.getIndex(CdmLightExportTable.FULL_NAME_WITH_REF)] = name.getFullTitleCache();
        } else {
            List<TaggedText> taggedFullTitleCache = name.getTaggedFullTitle();
            List<TaggedText> taggedName = name.getTaggedName();
            String fullTitleWithHtml = createNameWithItalics(taggedFullTitleCache);
            // TODO: adapt the tropicos titlecache creation
            csvLine[table.getIndex(CdmLightExportTable.FULL_NAME_WITH_REF)] = fullTitleWithHtml.trim();
        }
        csvLine[table.getIndex(CdmLightExportTable.FULL_NAME_NO_AUTHORS)] = name.getNameCache();
        csvLine[table.getIndex(CdmLightExportTable.GENUS_UNINOMIAL)] = name.getGenusOrUninomial();
        csvLine[table.getIndex(CdmLightExportTable.INFRAGENERIC_EPITHET)] = name.getInfraGenericEpithet();
        csvLine[table.getIndex(CdmLightExportTable.SPECIFIC_EPITHET)] = name.getSpecificEpithet();
        csvLine[table.getIndex(CdmLightExportTable.INFRASPECIFIC_EPITHET)] = name.getInfraSpecificEpithet();
        csvLine[table.getIndex(CdmLightExportTable.APPENDED_PHRASE)] = name.getAppendedPhrase();
        csvLine[table.getIndex(CdmLightExportTable.BAS_AUTHORTEAM_FK)] = getId(state, name.getBasionymAuthorship());
        if (name.getBasionymAuthorship() != null) {
            if (state.getAuthorFromStore(name.getBasionymAuthorship().getId()) == null) {
                handleAuthor(state, name.getBasionymAuthorship());
            }
        }
        csvLine[table.getIndex(CdmLightExportTable.BAS_EX_AUTHORTEAM_FK)] = getId(state, name.getExBasionymAuthorship());
        if (name.getExBasionymAuthorship() != null) {
            if (state.getAuthorFromStore(name.getExBasionymAuthorship().getId()) == null) {
                handleAuthor(state, name.getExBasionymAuthorship());
            }
        }
        csvLine[table.getIndex(CdmLightExportTable.COMB_AUTHORTEAM_FK)] = getId(state, name.getCombinationAuthorship());
        if (name.getCombinationAuthorship() != null) {
            if (state.getAuthorFromStore(name.getCombinationAuthorship().getId()) == null) {
                handleAuthor(state, name.getCombinationAuthorship());
            }
        }
        csvLine[table.getIndex(CdmLightExportTable.COMB_EX_AUTHORTEAM_FK)] = getId(state, name.getExCombinationAuthorship());
        if (name.getExCombinationAuthorship() != null) {
            if (state.getAuthorFromStore(name.getExCombinationAuthorship().getId()) == null) {
                handleAuthor(state, name.getExCombinationAuthorship());
            }
        }
        csvLine[table.getIndex(CdmLightExportTable.AUTHOR_TEAM_STRING)] = name.getAuthorshipCache();
        Reference nomRef = name.getNomenclaturalReference();
        NomenclaturalSource nomenclaturalSource = name.getNomenclaturalSource();
        if (nomenclaturalSource != null && nomenclaturalSource.getNameUsedInSource() != null) {
            handleName(state, nomenclaturalSource.getNameUsedInSource(), null);
            csvLine[table.getIndex(CdmLightExportTable.NAME_USED_IN_SOURCE)] = getId(state, nomenclaturalSource.getNameUsedInSource());
        }
        if (nomRef != null) {
            if (!state.getReferenceStore().contains(nomRef.getUuid())) {
                handleReference(state, nomRef);
            }
            csvLine[table.getIndex(CdmLightExportTable.REFERENCE_FK)] = getId(state, nomRef);
            csvLine[table.getIndex(CdmLightExportTable.PUBLICATION_TYPE)] = nomRef.getType().name();
            if (nomRef.getVolume() != null) {
                csvLine[table.getIndex(CdmLightExportTable.VOLUME_ISSUE)] = nomRef.getVolume();
                csvLine[table.getIndex(CdmLightExportTable.COLLATION)] = createCollatation(name);
            }
            if (nomRef.getDatePublished() != null) {
                csvLine[table.getIndex(CdmLightExportTable.DATE_PUBLISHED)] = nomRef.getTimePeriodPublishedString();
                csvLine[table.getIndex(CdmLightExportTable.YEAR_PUBLISHED)] = nomRef.getDatePublished().getYear();
                csvLine[table.getIndex(CdmLightExportTable.VERBATIM_DATE)] = nomRef.getDatePublished().getVerbatimDate();
            }
            if (name.getNomenclaturalMicroReference() != null) {
                csvLine[table.getIndex(CdmLightExportTable.DETAIL)] = name.getNomenclaturalMicroReference();
            }
            nomRef = HibernateProxyHelper.deproxy(nomRef);
            if (nomRef.getInReference() != null) {
                Reference inReference = nomRef.getInReference();
                if (inReference.getDatePublished() != null && nomRef.getDatePublished() == null) {
                    csvLine[table.getIndex(CdmLightExportTable.DATE_PUBLISHED)] = inReference.getDatePublishedString();
                    csvLine[table.getIndex(CdmLightExportTable.YEAR_PUBLISHED)] = inReference.getDatePublished().getYear();
                }
                if (nomRef.getVolume() == null && inReference.getVolume() != null) {
                    csvLine[table.getIndex(CdmLightExportTable.VOLUME_ISSUE)] = inReference.getVolume();
                    csvLine[table.getIndex(CdmLightExportTable.COLLATION)] = createCollatation(name);
                }
                if (inReference.getInReference() != null) {
                    inReference = inReference.getInReference();
                }
                if (inReference.getAbbrevTitle() == null) {
                    csvLine[table.getIndex(CdmLightExportTable.ABBREV_TITLE)] = CdmUtils.Nz(inReference.getTitle());
                } else {
                    csvLine[table.getIndex(CdmLightExportTable.ABBREV_TITLE)] = CdmUtils.Nz(inReference.getAbbrevTitle());
                }
                if (inReference.getTitle() == null) {
                    csvLine[table.getIndex(CdmLightExportTable.FULL_TITLE)] = CdmUtils.Nz(inReference.getAbbrevTitle() != null ? inReference.getAbbrevTitle() : inReference.getTitleCache());
                } else {
                    csvLine[table.getIndex(CdmLightExportTable.FULL_TITLE)] = CdmUtils.Nz(inReference.getTitle());
                }
                TeamOrPersonBase<?> author = inReference.getAuthorship();
                if (author != null && (nomRef.isOfType(ReferenceType.BookSection) || nomRef.isOfType(ReferenceType.Section))) {
                    csvLine[table.getIndex(CdmLightExportTable.ABBREV_REF_AUTHOR)] = author.isProtectedTitleCache() ? author.getTitleCache() : CdmUtils.Nz(author.getNomenclaturalTitleCache());
                    csvLine[table.getIndex(CdmLightExportTable.FULL_REF_AUTHOR)] = CdmUtils.Nz(author.getTitleCache());
                } else {
                    csvLine[table.getIndex(CdmLightExportTable.ABBREV_REF_AUTHOR)] = "";
                    csvLine[table.getIndex(CdmLightExportTable.FULL_REF_AUTHOR)] = "";
                }
            } else {
                if (nomRef.getAbbrevTitle() == null) {
                    csvLine[table.getIndex(CdmLightExportTable.ABBREV_TITLE)] = CdmUtils.Nz(nomRef.getTitle() != null ? nomRef.getTitle() : nomRef.getAbbrevTitleCache());
                } else {
                    csvLine[table.getIndex(CdmLightExportTable.ABBREV_TITLE)] = CdmUtils.Nz(nomRef.getAbbrevTitle());
                }
                if (nomRef.getTitle() == null) {
                    csvLine[table.getIndex(CdmLightExportTable.FULL_TITLE)] = CdmUtils.Nz(nomRef.getAbbrevTitle() != null ? nomRef.getAbbrevTitle() : nomRef.getTitleCache());
                } else {
                    csvLine[table.getIndex(CdmLightExportTable.FULL_TITLE)] = CdmUtils.Nz(nomRef.getTitle());
                }
                TeamOrPersonBase<?> author = nomRef.getAuthorship();
                if (author != null) {
                    csvLine[table.getIndex(CdmLightExportTable.ABBREV_REF_AUTHOR)] = author.isProtectedTitleCache() ? author.getTitleCache() : CdmUtils.Nz(author.getNomenclaturalTitleCache());
                    csvLine[table.getIndex(CdmLightExportTable.FULL_REF_AUTHOR)] = CdmUtils.Nz(author.getTitleCache());
                } else {
                    csvLine[table.getIndex(CdmLightExportTable.ABBREV_REF_AUTHOR)] = "";
                    csvLine[table.getIndex(CdmLightExportTable.FULL_REF_AUTHOR)] = "";
                }
            }
        } else {
            csvLine[table.getIndex(CdmLightExportTable.PUBLICATION_TYPE)] = "";
        }
        /*
             * Collation
             *
             * Detail
             *
             * TitlePageYear
             */
        String protologueUriString = extractProtologueURIs(state, name);
        csvLine[table.getIndex(CdmLightExportTable.PROTOLOGUE_URI)] = protologueUriString;
        Collection<TypeDesignationBase> specimenTypeDesignations = new ArrayList<>();
        List<TextualTypeDesignation> textualTypeDesignations = new ArrayList<>();
        for (TypeDesignationBase<?> typeDesignation : name.getTypeDesignations()) {
            if (typeDesignation.isInstanceOf(TextualTypeDesignation.class)) {
                if (((TextualTypeDesignation) typeDesignation).isVerbatim()) {
                    Set<IdentifiableSource> sources = typeDesignation.getSources();
                    boolean isProtologue = false;
                    if (sources != null && !sources.isEmpty()) {
                        IdentifiableSource source = sources.iterator().next();
                        if (name.getNomenclaturalReference() != null) {
                            isProtologue = source.getCitation() != null ? source.getCitation().getUuid().equals(name.getNomenclaturalReference().getUuid()) : false;
                        }
                    }
                    if (isProtologue) {
                        csvLine[table.getIndex(CdmLightExportTable.PROTOLOGUE_TYPE_STATEMENT)] = ((TextualTypeDesignation) typeDesignation).getPreferredText(Language.DEFAULT());
                    } else {
                        textualTypeDesignations.add((TextualTypeDesignation) typeDesignation);
                    }
                } else {
                    textualTypeDesignations.add((TextualTypeDesignation) typeDesignation);
                }
            } else if (typeDesignation.isInstanceOf(SpecimenTypeDesignation.class)) {
                SpecimenTypeDesignation specimenType = HibernateProxyHelper.deproxy(typeDesignation, SpecimenTypeDesignation.class);
                specimenTypeDesignations.add(specimenType);
                handleSpecimenType(state, specimenType);
            } else if (typeDesignation instanceof NameTypeDesignation) {
                specimenTypeDesignations.add(HibernateProxyHelper.deproxy(typeDesignation, NameTypeDesignation.class));
            }
        }
        TypeDesignationSetManager manager = new TypeDesignationSetManager(specimenTypeDesignations, name);
        HTMLTagRules rules = new HTMLTagRules();
        rules.addRule(TagEnum.name, "i");
        String test = manager.print(false, false, false, rules);
        ;
        csvLine[table.getIndex(CdmLightExportTable.TYPE_SPECIMEN)] = manager.print(false, false, false, rules);
        StringBuilder stringbuilder = new StringBuilder();
        int i = 1;
        for (TextualTypeDesignation typeDesignation : textualTypeDesignations) {
            stringbuilder.append(typeDesignation.getPreferredText(Language.DEFAULT()));
            if (typeDesignation.getSources() != null && !typeDesignation.getSources().isEmpty()) {
                stringbuilder.append(" [");
                int index = 1;
                for (IdentifiableSource source : typeDesignation.getSources()) {
                    if (source.getCitation() != null) {
                        stringbuilder.append(OriginalSourceFormatter.INSTANCE.format(source));
                    }
                    if (index < typeDesignation.getSources().size()) {
                        stringbuilder.append(", ");
                    }
                    index++;
                }
                stringbuilder.append("]");
            }
            if (i < textualTypeDesignations.size()) {
                stringbuilder.append("; ");
            } else {
                stringbuilder.append(".");
            }
            i++;
        }
        csvLine[table.getIndex(CdmLightExportTable.TYPE_STATEMENT)] = stringbuilder.toString();
        if (name.getStatus() == null || name.getStatus().isEmpty()) {
            csvLine[table.getIndex(CdmLightExportTable.NOM_STATUS)] = "";
            csvLine[table.getIndex(CdmLightExportTable.NOM_STATUS_ABBREV)] = "";
        } else {
            String statusStringAbbrev = extractStatusString(state, name, true);
            String statusString = extractStatusString(state, name, false);
            csvLine[table.getIndex(CdmLightExportTable.NOM_STATUS)] = statusString.trim();
            csvLine[table.getIndex(CdmLightExportTable.NOM_STATUS_ABBREV)] = statusStringAbbrev.trim();
        }
        HomotypicalGroup group = HibernateProxyHelper.deproxy(name.getHomotypicalGroup(), HomotypicalGroup.class);
        csvLine[table.getIndex(CdmLightExportTable.HOMOTYPIC_GROUP_FK)] = getId(state, group);
        List<TaxonName> typifiedNames = new ArrayList<>();
        if (acceptedTaxon != null) {
            HomotypicGroupTaxonComparator comparator = new HomotypicGroupTaxonComparator(acceptedTaxon);
            List<Synonym> synonymsInGroup = null;
            if (group.equals(acceptedTaxon.getHomotypicGroup())) {
                synonymsInGroup = acceptedTaxon.getHomotypicSynonymsByHomotypicGroup(comparator);
                typifiedNames.add(name);
            } else {
                synonymsInGroup = acceptedTaxon.getSynonymsInGroup(group, comparator);
            }
            synonymsInGroup.stream().forEach(synonym -> typifiedNames.add(HibernateProxyHelper.deproxy(synonym.getName(), TaxonName.class)));
        } else {
            typifiedNames.addAll(group.getTypifiedNames());
        }
        Integer seqNumber = typifiedNames.indexOf(name);
        csvLine[table.getIndex(CdmLightExportTable.HOMOTYPIC_GROUP_SEQ)] = String.valueOf(seqNumber);
        state.getProcessor().put(table, name, csvLine);
        handleNameRelationships(state, name);
    } catch (Exception e) {
        state.getResult().addException(e, "An unexpected error occurred when handling the name " + cdmBaseStr(name) + ": " + name.getTitleCache() + ": " + e.getMessage());
        e.printStackTrace();
    }
}
Also used : HomotypicGroupTaxonComparator(eu.etaxonomy.cdm.compare.taxon.HomotypicGroupTaxonComparator) ArrayList(java.util.ArrayList) LanguageString(eu.etaxonomy.cdm.model.common.LanguageString) HTMLTagRules(eu.etaxonomy.cdm.strategy.cache.HTMLTagRules) UnknownCdmTypeException(eu.etaxonomy.cdm.strategy.exceptions.UnknownCdmTypeException) CommonTaxonName(eu.etaxonomy.cdm.model.description.CommonTaxonName) TaxonName(eu.etaxonomy.cdm.model.name.TaxonName) IdentifiableSource(eu.etaxonomy.cdm.model.common.IdentifiableSource) TaggedText(eu.etaxonomy.cdm.strategy.cache.TaggedText) Reference(eu.etaxonomy.cdm.model.reference.Reference) TypeDesignationBase(eu.etaxonomy.cdm.model.name.TypeDesignationBase) Rank(eu.etaxonomy.cdm.model.name.Rank) UnknownCdmTypeException(eu.etaxonomy.cdm.strategy.exceptions.UnknownCdmTypeException) NameTypeDesignation(eu.etaxonomy.cdm.model.name.NameTypeDesignation) HomotypicalGroup(eu.etaxonomy.cdm.model.name.HomotypicalGroup) NomenclaturalSource(eu.etaxonomy.cdm.model.name.NomenclaturalSource) TypeDesignationSetManager(eu.etaxonomy.cdm.api.service.name.TypeDesignationSetManager) TextualTypeDesignation(eu.etaxonomy.cdm.model.name.TextualTypeDesignation) Synonym(eu.etaxonomy.cdm.model.taxon.Synonym) SpecimenTypeDesignation(eu.etaxonomy.cdm.model.name.SpecimenTypeDesignation)

Example 5 with UnknownCdmTypeException

use of eu.etaxonomy.cdm.strategy.exceptions.UnknownCdmTypeException in project cdmlib by cybertaxonomy.

the class TaxonXDescriptionImport method doInvoke.

@Override
public void doInvoke(TaxonXImportState state) {
    logger.debug("not yet fully implemented");
    TaxonXImportConfigurator txConfig = state.getConfig();
    Element root = txConfig.getSourceRoot();
    Namespace nsTaxonx = root.getNamespace();
    // Object source = config.getSource();
    logger.info("start make Descriptions ...");
    // for testing only
    Taxon taxon = getTaxon(txConfig);
    if (taxon == null) {
        logger.warn("Taxon could not be found");
        state.setUnsuccessfull();
    }
    Reference modsReference = state.getModsReference();
    if (modsReference == null) {
        modsReference = state.getConfig().getSourceReference();
    }
    // unlazyDescription(txConfig, taxon);
    TaxonDescription description = TaxonDescription.NewInstance();
    description.setTitleCache(getDescriptionTitle(state), true);
    if (modsReference != null) {
        description.addSource(OriginalSourceType.PrimaryTaxonomicSource, null, null, modsReference, null);
    }
    Element elTaxonBody = root.getChild("taxonxBody", nsTaxonx);
    Element elTreatment = elTaxonBody.getChild("treatment", nsTaxonx);
    List<Element> elDivs = elTreatment.getChildren("div", nsTaxonx);
    for (Element div : elDivs) {
        Attribute attrType = div.getAttribute("type", nsTaxonx);
        String strType = attrType.getValue();
        Feature feature = null;
        try {
            feature = TaxonXTransformer.descriptionType2feature(strType);
        } catch (UnknownCdmTypeException e) {
            feature = handleFeatureException(strType, e, txConfig);
        }
        String text = getText(div);
        if (!"".equals(CdmUtils.Nz(text).trim())) {
            // for now we truncate any description text to 65500 characters.
            if (text.length() > 65500) {
                text = text.substring(0, 65500) + "... [text truncated]";
                logger.warn("Truncation of text: description for taxon " + taxon.getTitleCache() + " was longer than 65500 characters.");
            }
            DescriptionElementBase descriptionElement = TextData.NewInstance(text, Language.ENGLISH(), null);
            descriptionElement.setFeature(feature);
            description.addElement(descriptionElement);
            // add reference
            if (modsReference != null) {
                descriptionElement.addSource(OriginalSourceType.PrimaryTaxonomicSource, null, null, modsReference, null, null, null);
            }
        }
    }
    if (description.size() > 0) {
        taxon.addDescription(description);
        getTaxonService().save(taxon);
    }
    return;
}
Also used : Attribute(org.jdom.Attribute) Reference(eu.etaxonomy.cdm.model.reference.Reference) UnknownCdmTypeException(eu.etaxonomy.cdm.strategy.exceptions.UnknownCdmTypeException) Element(org.jdom.Element) Taxon(eu.etaxonomy.cdm.model.taxon.Taxon) TaxonDescription(eu.etaxonomy.cdm.model.description.TaxonDescription) Feature(eu.etaxonomy.cdm.model.description.Feature) Namespace(org.jdom.Namespace) DescriptionElementBase(eu.etaxonomy.cdm.model.description.DescriptionElementBase)

Aggregations

UnknownCdmTypeException (eu.etaxonomy.cdm.strategy.exceptions.UnknownCdmTypeException)43 Rank (eu.etaxonomy.cdm.model.name.Rank)17 TaxonName (eu.etaxonomy.cdm.model.name.TaxonName)14 Reference (eu.etaxonomy.cdm.model.reference.Reference)11 NomenclaturalStatusType (eu.etaxonomy.cdm.model.name.NomenclaturalStatusType)9 NomenclaturalCode (eu.etaxonomy.cdm.model.name.NomenclaturalCode)7 ArrayList (java.util.ArrayList)7 INonViralName (eu.etaxonomy.cdm.model.name.INonViralName)5 Taxon (eu.etaxonomy.cdm.model.taxon.Taxon)5 UUID (java.util.UUID)5 NomenclaturalStatus (eu.etaxonomy.cdm.model.name.NomenclaturalStatus)4 Person (eu.etaxonomy.cdm.model.agent.Person)3 IdentifiableSource (eu.etaxonomy.cdm.model.common.IdentifiableSource)3 URISyntaxException (java.net.URISyntaxException)3 DerivedUnitFacade (eu.etaxonomy.cdm.api.facade.DerivedUnitFacade)2 URI (eu.etaxonomy.cdm.common.URI)2 MapWrapper (eu.etaxonomy.cdm.io.common.MapWrapper)2 Team (eu.etaxonomy.cdm.model.agent.Team)2 Annotation (eu.etaxonomy.cdm.model.common.Annotation)2 TimePeriod (eu.etaxonomy.cdm.model.common.TimePeriod)2