Search in sources :

Example 1 with Identifier

use of eu.etaxonomy.cdm.model.common.Identifier in project cdmlib by cybertaxonomy.

the class OccurrenceServiceTest method testDnaSampleDesignation.

@Test
@DataSet(loadStrategy = CleanSweepInsertLoadStrategy.class, value = "OccurrenceServiceTest.testDnaSampleDesignation.xml")
public void testDnaSampleDesignation() {
    DefinedTerm sampleDesignationTermType = (DefinedTerm) termService.load(DefinedTerm.uuidSampleDesignation);
    UUID dnaSampleUuid = UUID.fromString("4bee91b9-23d8-438b-8569-6d6aaa5b6587");
    DnaSample dnaSample = HibernateProxyHelper.deproxy(occurrenceService.load(dnaSampleUuid), DnaSample.class);
    assertEquals(1, dnaSample.getIdentifiers().size());
    Identifier identifier = dnaSample.getIdentifiers().iterator().next();
    assertEquals("NK 2088", identifier.getIdentifier());
    assertEquals(sampleDesignationTermType, identifier.getType());
    // change identifier, save and reload
    identifier.setIdentifier("WB10");
    occurrenceService.saveOrUpdate(dnaSample);
    SpecimenOrObservationBase<?> dnaSampleReloaded = occurrenceService.load(dnaSampleUuid);
    assertEquals(1, dnaSample.getIdentifiers().size());
    Identifier identifierReloaded = dnaSample.getIdentifiers().iterator().next();
    assertEquals("WB10", identifierReloaded.getIdentifier());
    assertEquals(sampleDesignationTermType, identifierReloaded.getType());
// DefinedTerm sampleDesignationTermType = (DefinedTerm) termService.load(DefinedTerm.uuidSampleDesignation);
// //UUIDS
// UUID dnaSampleUuid = UUID.fromString("4bee91b9-23d8-438b-8569-6d6aaa5b6587");
// DerivedUnit dnaSample = DnaSample.NewInstance();
// dnaSample.setUuid(dnaSampleUuid);
// Identifier.NewInstance(dnaSample, "NK 2088", sampleDesignationTermType);
// 
// occurrenceService.save(dnaSample);
// 
// commitAndStartNewTransaction(null);
// 
// setComplete();
// endTransaction();
// 
// 
// try {
// writeDbUnitDataSetFile(new String[] {
// "SpecimenOrObservationBase",
// "IDENTIFIER",
// "SpecimenOrObservationBase_Identifier"
// }, "testDnaSampleDesignation");
// } catch (FileNotFoundException e) {
// e.printStackTrace();
// }
}
Also used : Identifier(eu.etaxonomy.cdm.model.common.Identifier) DnaSample(eu.etaxonomy.cdm.model.molecular.DnaSample) UUID(java.util.UUID) DefinedTerm(eu.etaxonomy.cdm.model.term.DefinedTerm) CdmTransactionalIntegrationTest(eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest) Test(org.junit.Test) DataSet(org.unitils.dbunit.annotation.DataSet)

Example 2 with Identifier

use of eu.etaxonomy.cdm.model.common.Identifier in project cdmlib by cybertaxonomy.

the class CdmLightClassificationExport method handleIdentifier.

private void handleIdentifier(CdmLightExportState state, CdmBase cdmBase) {
    CdmLightExportTable table = CdmLightExportTable.IDENTIFIER;
    String[] csvLine;
    try {
        if (cdmBase instanceof TaxonName) {
            TaxonName name = (TaxonName) cdmBase;
            try {
                List<Identifier> identifiers = name.getIdentifiers();
                // first check which kind of identifiers are available and then sort and create table entries
                Map<DefinedTerm, Set<Identifier>> identifierTypes = new HashMap<>();
                for (Identifier identifier : identifiers) {
                    DefinedTerm type = identifier.getType();
                    if (identifierTypes.containsKey(type)) {
                        identifierTypes.get(type).add(identifier);
                    } else {
                        Set<Identifier> tempList = new HashSet<>();
                        tempList.add(identifier);
                        identifierTypes.put(type, tempList);
                    }
                }
                for (DefinedTerm type : identifierTypes.keySet()) {
                    Set<Identifier> identifiersByType = identifierTypes.get(type);
                    csvLine = new String[table.getSize()];
                    csvLine[table.getIndex(CdmLightExportTable.FK)] = getId(state, name);
                    csvLine[table.getIndex(CdmLightExportTable.REF_TABLE)] = "ScientificName";
                    csvLine[table.getIndex(CdmLightExportTable.IDENTIFIER_TYPE)] = type.getLabel();
                    csvLine[table.getIndex(CdmLightExportTable.EXTERNAL_NAME_IDENTIFIER)] = extractIdentifier(identifiersByType);
                    state.getProcessor().put(table, name.getUuid() + ", " + type.getLabel(), csvLine);
                }
            // Set<String> IPNIidentifiers = name.getIdentifiers(DefinedTerm.IDENTIFIER_NAME_IPNI());
            // Set<String> tropicosIdentifiers = name.getIdentifiers(DefinedTerm.IDENTIFIER_NAME_TROPICOS());
            // Set<String> WFOIdentifiers = name.getIdentifiers(DefinedTerm.uuidWfoNameIdentifier);
            // if (!IPNIidentifiers.isEmpty()) {
            // csvLine = new String[table.getSize()];
            // csvLine[table.getIndex(CdmLightExportTable.FK)] = getId(state, name);
            // csvLine[table.getIndex(CdmLightExportTable.REF_TABLE)] = "ScientificName";
            // csvLine[table.getIndex(CdmLightExportTable.IDENTIFIER_TYPE)] = IPNI_NAME_IDENTIFIER;
            // csvLine[table.getIndex(CdmLightExportTable.EXTERNAL_NAME_IDENTIFIER)] = extractIdentifier(
            // IPNIidentifiers);
            // state.getProcessor().put(table, name.getUuid() + ", " + IPNI_NAME_IDENTIFIER, csvLine);
            // }
            // if (!tropicosIdentifiers.isEmpty()) {
            // csvLine = new String[table.getSize()];
            // csvLine[table.getIndex(CdmLightExportTable.FK)] = getId(state, name);
            // csvLine[table.getIndex(CdmLightExportTable.REF_TABLE)] = "ScientificName";
            // csvLine[table.getIndex(CdmLightExportTable.IDENTIFIER_TYPE)] = TROPICOS_NAME_IDENTIFIER;
            // csvLine[table.getIndex(CdmLightExportTable.EXTERNAL_NAME_IDENTIFIER)] = extractIdentifier(
            // tropicosIdentifiers);
            // state.getProcessor().put(table, name.getUuid() + ", " + IPNI_NAME_IDENTIFIER, csvLine);
            // }
            // if (!WFOIdentifiers.isEmpty()) {
            // csvLine = new String[table.getSize()];
            // csvLine[table.getIndex(CdmLightExportTable.FK)] = getId(state, name);
            // csvLine[table.getIndex(CdmLightExportTable.REF_TABLE)] = "ScientificName";
            // csvLine[table.getIndex(CdmLightExportTable.IDENTIFIER_TYPE)] = WFO_NAME_IDENTIFIER;
            // csvLine[table.getIndex(CdmLightExportTable.EXTERNAL_NAME_IDENTIFIER)] = extractIdentifier(
            // WFOIdentifiers);
            // state.getProcessor().put(table, name.getUuid() + ", " + WFO_NAME_IDENTIFIER, csvLine);
            // }
            } catch (Exception e) {
                state.getResult().addWarning("Please check the identifiers for " + cdmBaseStr(cdmBase) + " maybe there is an empty identifier");
            }
        } else {
            if (cdmBase instanceof IdentifiableEntity) {
                IdentifiableEntity<?> identifiableEntity = (IdentifiableEntity<?>) cdmBase;
                List<Identifier> identifiers = identifiableEntity.getIdentifiers();
                String tableName = null;
                if (cdmBase instanceof Reference) {
                    tableName = "Reference";
                } else if (cdmBase instanceof SpecimenOrObservationBase) {
                    tableName = "Specimen";
                } else if (cdmBase instanceof Taxon) {
                    tableName = "Taxon";
                } else if (cdmBase instanceof Synonym) {
                    tableName = "Synonym";
                } else if (cdmBase instanceof TeamOrPersonBase) {
                    tableName = "PersonOrTeam";
                }
                for (Identifier identifier : identifiers) {
                    if (identifier.getType() == null && identifier.getIdentifier() == null) {
                        state.getResult().addWarning("Please check the identifiers for " + cdmBaseStr(cdmBase) + " there is an empty identifier");
                        continue;
                    }
                    csvLine = new String[table.getSize()];
                    csvLine[table.getIndex(CdmLightExportTable.FK)] = getId(state, cdmBase);
                    if (tableName != null) {
                        csvLine[table.getIndex(CdmLightExportTable.REF_TABLE)] = tableName;
                        csvLine[table.getIndex(CdmLightExportTable.IDENTIFIER_TYPE)] = identifier.getType() != null ? identifier.getType().getLabel() : null;
                        csvLine[table.getIndex(CdmLightExportTable.EXTERNAL_NAME_IDENTIFIER)] = identifier.getIdentifier();
                        state.getProcessor().put(table, cdmBase.getUuid() + (identifier.getType() != null ? identifier.getType().getLabel() : null), csvLine);
                    }
                }
                if (cdmBase instanceof Reference) {
                    Reference ref = (Reference) cdmBase;
                    if (ref.getDoi() != null) {
                        csvLine = new String[table.getSize()];
                        csvLine[table.getIndex(CdmLightExportTable.FK)] = getId(state, cdmBase);
                        csvLine[table.getIndex(CdmLightExportTable.REF_TABLE)] = tableName;
                        csvLine[table.getIndex(CdmLightExportTable.IDENTIFIER_TYPE)] = "DOI";
                        csvLine[table.getIndex(CdmLightExportTable.EXTERNAL_NAME_IDENTIFIER)] = ref.getDoiString();
                        state.getProcessor().put(table, cdmBase.getUuid() + "DOI", csvLine);
                    }
                }
                if (cdmBase instanceof TeamOrPersonBase) {
                    TeamOrPersonBase<?> person = HibernateProxyHelper.deproxy(cdmBase, TeamOrPersonBase.class);
                    if (person instanceof Person && ((Person) person).getOrcid() != null) {
                        csvLine = new String[table.getSize()];
                        csvLine[table.getIndex(CdmLightExportTable.FK)] = getId(state, cdmBase);
                        csvLine[table.getIndex(CdmLightExportTable.REF_TABLE)] = tableName;
                        csvLine[table.getIndex(CdmLightExportTable.IDENTIFIER_TYPE)] = "ORCID";
                        csvLine[table.getIndex(CdmLightExportTable.EXTERNAL_NAME_IDENTIFIER)] = ((Person) person).getOrcid().asURI();
                        state.getProcessor().put(table, cdmBase.getUuid() + "ORCID", csvLine);
                    }
                }
            }
        }
    } catch (Exception e) {
        state.getResult().addException(e, "An unexpected error occurred when handling identifiers for " + cdmBaseStr(cdmBase) + ": " + e.getMessage());
        e.printStackTrace();
    }
}
Also used : IIdentifiableEntity(eu.etaxonomy.cdm.model.common.IIdentifiableEntity) IdentifiableEntity(eu.etaxonomy.cdm.model.common.IdentifiableEntity) TeamOrPersonBase(eu.etaxonomy.cdm.model.agent.TeamOrPersonBase) Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) Reference(eu.etaxonomy.cdm.model.reference.Reference) Taxon(eu.etaxonomy.cdm.model.taxon.Taxon) LanguageString(eu.etaxonomy.cdm.model.common.LanguageString) UnknownCdmTypeException(eu.etaxonomy.cdm.strategy.exceptions.UnknownCdmTypeException) Identifier(eu.etaxonomy.cdm.model.common.Identifier) SpecimenOrObservationBase(eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase) CommonTaxonName(eu.etaxonomy.cdm.model.description.CommonTaxonName) TaxonName(eu.etaxonomy.cdm.model.name.TaxonName) Synonym(eu.etaxonomy.cdm.model.taxon.Synonym) DefinedTerm(eu.etaxonomy.cdm.model.term.DefinedTerm) Person(eu.etaxonomy.cdm.model.agent.Person) HashSet(java.util.HashSet)

Example 3 with Identifier

use of eu.etaxonomy.cdm.model.common.Identifier in project cdmlib by cybertaxonomy.

the class FullCoverageDataGenerator method handleIdentifiableEntity.

private void handleIdentifiableEntity(IdentifiableEntity<?> identifiableEntity) {
    handleAnnotatableEntity(identifiableEntity);
    // Credits
    Person creditor = Person.NewTitledInstance("Creditor");
    Credit credit = Credit.NewInstance(creditor, "credit");
    identifiableEntity.addCredit(credit);
    // Extension
    Extension.NewInstance(identifiableEntity, "extension", ExtensionType.INFORMAL_CATEGORY());
    // Identifier
    Identifier identifier = identifiableEntity.addIdentifier("ident23", DefinedTerm.SEX_FEMALE());
    handleAnnotatableEntity(identifier);
    // Links
    identifiableEntity.addLinkWebsite(URI.create("http://a.bc.de"), "Description", Language.ENGLISH());
    // Rights
    Rights rights = Rights.NewInstance("right", Language.ENGLISH());
    rights.setUri(URI.create("http://rights.abc.de"));
    rights.setAbbreviatedText("abbrev");
    rights.setType(RightsType.COPYRIGHT());
    Person owner = Person.NewTitledInstance("Owner");
    rights.setAgent(owner);
    identifiableEntity.addRights(rights);
    if (identifiableEntity.isInstanceOf(IdentifiableMediaEntity.class)) {
        Media media = Media.NewInstance(URI.create("http://www.identifiableMedia.de"), 22, "img/jpg", "jpg");
        ((IdentifiableMediaEntity<?>) identifiableEntity).addMedia(media);
    }
    // source
    IdentifiableSource source = identifiableEntity.addSource(OriginalSourceType.Import, "id", "idNamespace", getReference(), "123");
    source.setOriginalNameString("original name");
    // LSID
    try {
        LSID lsid = new LSID("urn:lsid:a.b.de:namespace:1234");
        identifiableEntity.setLsid(lsid);
    } catch (MalformedLSIDException e) {
        e.printStackTrace();
    }
}
Also used : IdentifiableMediaEntity(eu.etaxonomy.cdm.model.media.IdentifiableMediaEntity) LSID(eu.etaxonomy.cdm.model.common.LSID) Rights(eu.etaxonomy.cdm.model.media.Rights) Credit(eu.etaxonomy.cdm.model.common.Credit) Identifier(eu.etaxonomy.cdm.model.common.Identifier) MalformedLSIDException(com.ibm.lsid.MalformedLSIDException) Media(eu.etaxonomy.cdm.model.media.Media) IdentifiableSource(eu.etaxonomy.cdm.model.common.IdentifiableSource) Person(eu.etaxonomy.cdm.model.agent.Person)

Example 4 with Identifier

use of eu.etaxonomy.cdm.model.common.Identifier in project cdmlib by cybertaxonomy.

the class IdentifierImport method handleSingleLine.

/**
 * @param config configurator
 * @param strs text array for given line
 * @param i line counter
 * @param entityUuidsHandled
 * @return
 */
private IdentifiableEntity<?> handleSingleLine(IdentifierImportConfigurator config, String[] strs, DefinedTerm idType, int i, Set<UUID> entityUuidsHandled) {
    // no data
    if (strs.length < 1) {
        String message = String.format("No data available in line %d. Skipped", i);
        logger.warn(message);
        return null;
    }
    // entity uuid
    String uuidStr = strs[0];
    UUID uuid;
    try {
        uuid = UUID.fromString(uuidStr);
    } catch (Exception e) {
        String message = String.format("Entity identifier not recognized as UUID in line %d. Skipped. Value was: %s", i, uuidStr);
        logger.warn(message);
        return null;
    }
    TransactionStatus tx = this.startTransaction();
    IdentifiableEntity<?> entity = getEntityFromRepository(config, uuid);
    if (entity == null) {
        String message = String.format("Entity for uuid %s could not be found in line %d. Skipped", uuid.toString(), i);
        logger.warn(message);
        return null;
    }
    // identifier value
    if (strs.length < 2) {
        String message = String.format("Record in line %d has no identifier value information. Skipped.", i);
        logger.warn(message);
        this.commitTransaction(tx);
        return null;
    }
    // titleCache
    if (strs.length > 2) {
        String entityCache = entity.getTitleCache();
        String titleCache = strs[2];
        if (!CdmUtils.nullSafeEqual(entityCache, titleCache)) {
            String message = String.format("Record in line %d has different titleCache: " + entityCache + " <-> " + titleCache, i);
            logger.warn(message);
        }
    }
    String value = null;
    if (isNotBlank(strs[1])) {
        value = strs[1];
    } else if (config.isIgnoreEmptyIdentifier()) {
        String message = String.format("Record in line %d has empty identifier value information. Skipped.", i);
        logger.debug(message);
        this.commitTransaction(tx);
        return null;
    }
    Identifier identifier = null;
    // TODO clean redundant code
    if (config.isWarnAndDoNotOverrideIfExists()) {
        boolean wasAlreadyImported = entityUuidsHandled.contains(uuid);
        if (wasAlreadyImported) {
            String message = String.format("More than 1 instance for uuid '%s' (" + entity.getTitleCache() + ") found in line %d. Updating not possible without deleting previous value as 'update existing' was selected. Record in line was neglected.", uuidStr, i);
            logger.warn(message);
            this.commitTransaction(tx);
            return null;
        }
        Set<Identifier> existingIdentifiers = entity.getIdentifiers(idType.getUuid());
        if (!existingIdentifiers.isEmpty()) {
            identifier = existingIdentifiers.iterator().next();
            if (!CdmUtils.nullSafeEqual(identifier.getIdentifier(), value)) {
                String message = String.format("Existing identifier in line %d differs: " + identifier.getIdentifier() + "(existing)<->" + value + "(import). Line not imported", i);
                logger.warn(message);
                this.commitTransaction(tx);
                return null;
            }
        } else {
            addNewIdentifier(idType, entity, value, identifier);
        }
    } else if (config.isUpdateExisting()) {
        boolean wasAlreadyImported = entityUuidsHandled.contains(uuid);
        if (wasAlreadyImported) {
            String message = String.format("More than 1 instance for uuid '%s' (" + entity.getTitleCache() + ") found in line %d. Updating not possible without deleting previous value as 'update existing' was selected. Record in line was neglected.", uuidStr, i);
            logger.warn(message);
            this.commitTransaction(tx);
            return null;
        } else {
            Set<Identifier> existingIdentifiers = entity.getIdentifiers(idType.getUuid());
            if (existingIdentifiers.size() == 1) {
                identifier = existingIdentifiers.iterator().next();
                if (!CdmUtils.nullSafeEqual(identifier.getIdentifier(), value)) {
                    String message = String.format("Existing identifier in line %d differs: " + value, i);
                    logger.warn(message);
                    identifier.setIdentifier(value);
                }
            } else if (existingIdentifiers.size() > 1) {
                String message = String.format("Taxon name in line %d has more than a single entry for the given identifier type. I can't update the value but added a new record", i);
                logger.warn(message);
                addNewIdentifier(idType, entity, value, identifier);
            } else {
                addNewIdentifier(idType, entity, value, identifier);
            }
        }
    } else {
        addNewIdentifier(idType, entity, value, identifier);
    }
    entityUuidsHandled.add(uuid);
    this.commitTransaction(tx);
    return entity;
}
Also used : Identifier(eu.etaxonomy.cdm.model.common.Identifier) Set(java.util.Set) HashSet(java.util.HashSet) TransactionStatus(org.springframework.transaction.TransactionStatus) UUID(java.util.UUID) IOException(java.io.IOException)

Example 5 with Identifier

use of eu.etaxonomy.cdm.model.common.Identifier in project cdmlib by cybertaxonomy.

the class DeduplicationHelper method copyIdentifiableExtensions.

/**
 * Clones all extensions of cdmBase2 and
 * attaches the clones to cdmBase1.
 * Finally removes all annotations and markers from cdmBase2.
 * @param cdmBase1
 * @param cdmBase2
 * @param session
 */
// TODO Why do we not handle credits, rights and sources here
private <T> void copyIdentifiableExtensions(T cdmBase1, T cdmBase2, Session session) {
    IdentifiableEntity<?> identifiableEntity1 = (IdentifiableEntity<?>) cdmBase1;
    IdentifiableEntity<?> identifiableEntity2 = (IdentifiableEntity<?>) cdmBase2;
    // extensions
    List<Extension> removeListExtension = new ArrayList<>();
    for (Extension changeObject : identifiableEntity2.getExtensions()) {
        try {
            Extension clone = changeObject.clone();
            identifiableEntity1.addExtension(clone);
            removeListExtension.add(changeObject);
        } catch (CloneNotSupportedException e) {
            throw new RuntimeException("Clone is not yet supported for class " + changeObject.getClass().getName() + " but should.");
        }
    }
    for (Extension removeObject : removeListExtension) {
        identifiableEntity2.removeExtension(removeObject);
        session.delete(removeObject);
    }
    // identifiers
    List<Identifier> removeListIdentifier = new ArrayList<>();
    for (Identifier changeObject : identifiableEntity2.getIdentifiers()) {
        try {
            Identifier clone = changeObject.clone();
            identifiableEntity1.addIdentifier(clone);
            removeListIdentifier.add(changeObject);
        } catch (CloneNotSupportedException e) {
            throw new RuntimeException(e);
        }
    }
    for (Identifier removeObject : removeListIdentifier) {
        identifiableEntity2.removeIdentifier(removeObject);
        session.delete(removeObject);
    }
}
Also used : Extension(eu.etaxonomy.cdm.model.common.Extension) IdentifiableEntity(eu.etaxonomy.cdm.model.common.IdentifiableEntity) Identifier(eu.etaxonomy.cdm.model.common.Identifier) ArrayList(java.util.ArrayList)

Aggregations

Identifier (eu.etaxonomy.cdm.model.common.Identifier)10 DefinedTerm (eu.etaxonomy.cdm.model.term.DefinedTerm)4 UUID (java.util.UUID)4 Test (org.junit.Test)4 Person (eu.etaxonomy.cdm.model.agent.Person)3 IdentifiableEntity (eu.etaxonomy.cdm.model.common.IdentifiableEntity)3 DnaSample (eu.etaxonomy.cdm.model.molecular.DnaSample)3 Taxon (eu.etaxonomy.cdm.model.taxon.Taxon)3 CdmTransactionalIntegrationTest (eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest)3 DataSet (org.unitils.dbunit.annotation.DataSet)3 IdentifiableSource (eu.etaxonomy.cdm.model.common.IdentifiableSource)2 LanguageString (eu.etaxonomy.cdm.model.common.LanguageString)2 TaxonName (eu.etaxonomy.cdm.model.name.TaxonName)2 HashSet (java.util.HashSet)2 Set (java.util.Set)2 MalformedLSIDException (com.ibm.lsid.MalformedLSIDException)1 MetadataEntry (de.digitalcollections.iiif.model.MetadataEntry)1 PropertyValue (de.digitalcollections.iiif.model.PropertyValue)1 FindOccurrencesConfigurator (eu.etaxonomy.cdm.api.service.config.FindOccurrencesConfigurator)1 SpecimenDeleteConfigurator (eu.etaxonomy.cdm.api.service.config.SpecimenDeleteConfigurator)1