@Test public void testIdExtractionMultipleIdsUnitQualifier() throws IOException { String xml = Resources.toString( Resources.getResource("id_extraction/abcd2_multi.xml"), StandardCharsets.UTF_8); UUID datasetKey = UUID.randomUUID(); byte[] xmlBytes = xml.getBytes(StandardCharsets.UTF_8); Set<IdentifierExtractionResult> extractionResults = XmlFragmentParser.extractIdentifiers( datasetKey, xmlBytes, OccurrenceSchemaType.ABCD_2_0_6, true, true); Triplet triplet1 = new Triplet( datasetKey, "BGBM", "Bridel Herbar", "Bridel-1-428", "Grimmia alpicola Sw. ex Hedw."); Triplet triplet2 = new Triplet( datasetKey, "BGBM", "Bridel Herbar", "Bridel-1-428", "Schistidium agassizii Sull. & Lesq. in Sull."); assertEquals(2, extractionResults.size()); for (IdentifierExtractionResult result : extractionResults) { String uniqueId = result.getUniqueIdentifiers().iterator().next().getUniqueString(); assertTrue( uniqueId.equals(OccurrenceKeyHelper.buildKey(triplet1)) || uniqueId.equals(OccurrenceKeyHelper.buildKey(triplet2))); } }
public static Set<IdentifierExtractionResult> extractIdentifiers( UUID datasetKey, byte[] xml, OccurrenceSchemaType schemaType, boolean useTriplet, boolean useOccurrenceId) { Set<IdentifierExtractionResult> results = Sets.newHashSet(); List<RawOccurrenceRecord> records = parseRecord(xml, schemaType); if (records != null && !records.isEmpty()) { for (RawOccurrenceRecord record : records) { Set<UniqueIdentifier> ids = Sets.newHashSet(); if (useTriplet) { Triplet triplet = null; try { triplet = new Triplet( datasetKey, record.getInstitutionCode(), record.getCollectionCode(), record.getCatalogueNumber(), record.getUnitQualifier()); } catch (IllegalArgumentException e) { log.info( "No holy triplet for an xml snippet in dataset [{}] and schema [{}], got error [{}]", datasetKey.toString(), schemaType.toString(), e.getMessage()); } if (triplet != null) { ids.add(triplet); } } if (useOccurrenceId && record.getIdentifierRecords() != null && !record.getIdentifierRecords().isEmpty()) { for (IdentifierRecord idRecord : record.getIdentifierRecords()) { if ((idRecord.getIdentifierType() == 1 || idRecord.getIdentifierType() == 7) && idRecord.getIdentifier() != null) { ids.add(new PublisherProvidedUniqueIdentifier(datasetKey, idRecord.getIdentifier())); } } } if (!ids.isEmpty()) { results.add(new IdentifierExtractionResult(ids, record.getUnitQualifier())); } } } return results; }
XmlFragmentParser { public static Set<IdentifierExtractionResult> extractIdentifiers( UUID datasetKey, byte[] xml, OccurrenceSchemaType schemaType, boolean useTriplet, boolean useOccurrenceId) { Set<IdentifierExtractionResult> results = Sets.newHashSet(); List<RawOccurrenceRecord> records = parseRecord(xml, schemaType); if (records != null && !records.isEmpty()) { for (RawOccurrenceRecord record : records) { Set<UniqueIdentifier> ids = Sets.newHashSet(); if (useTriplet) { Triplet triplet = null; try { triplet = new Triplet( datasetKey, record.getInstitutionCode(), record.getCollectionCode(), record.getCatalogueNumber(), record.getUnitQualifier()); } catch (IllegalArgumentException e) { log.info( "No holy triplet for an xml snippet in dataset [{}] and schema [{}], got error [{}]", datasetKey.toString(), schemaType.toString(), e.getMessage()); } if (triplet != null) { ids.add(triplet); } } if (useOccurrenceId && record.getIdentifierRecords() != null && !record.getIdentifierRecords().isEmpty()) { for (IdentifierRecord idRecord : record.getIdentifierRecords()) { if ((idRecord.getIdentifierType() == 1 || idRecord.getIdentifierType() == 7) && idRecord.getIdentifier() != null) { ids.add(new PublisherProvidedUniqueIdentifier(datasetKey, idRecord.getIdentifier())); } } } if (!ids.isEmpty()) { results.add(new IdentifierExtractionResult(ids, record.getUnitQualifier())); } } } return results; } }
XmlFragmentParser { public static Set<IdentifierExtractionResult> extractIdentifiers( UUID datasetKey, byte[] xml, OccurrenceSchemaType schemaType, boolean useTriplet, boolean useOccurrenceId) { Set<IdentifierExtractionResult> results = Sets.newHashSet(); List<RawOccurrenceRecord> records = parseRecord(xml, schemaType); if (records != null && !records.isEmpty()) { for (RawOccurrenceRecord record : records) { Set<UniqueIdentifier> ids = Sets.newHashSet(); if (useTriplet) { Triplet triplet = null; try { triplet = new Triplet( datasetKey, record.getInstitutionCode(), record.getCollectionCode(), record.getCatalogueNumber(), record.getUnitQualifier()); } catch (IllegalArgumentException e) { log.info( "No holy triplet for an xml snippet in dataset [{}] and schema [{}], got error [{}]", datasetKey.toString(), schemaType.toString(), e.getMessage()); } if (triplet != null) { ids.add(triplet); } } if (useOccurrenceId && record.getIdentifierRecords() != null && !record.getIdentifierRecords().isEmpty()) { for (IdentifierRecord idRecord : record.getIdentifierRecords()) { if ((idRecord.getIdentifierType() == 1 || idRecord.getIdentifierType() == 7) && idRecord.getIdentifier() != null) { ids.add(new PublisherProvidedUniqueIdentifier(datasetKey, idRecord.getIdentifier())); } } } if (!ids.isEmpty()) { results.add(new IdentifierExtractionResult(ids, record.getUnitQualifier())); } } } return results; } private  XmlFragmentParser(); }
XmlFragmentParser { public static Set<IdentifierExtractionResult> extractIdentifiers( UUID datasetKey, byte[] xml, OccurrenceSchemaType schemaType, boolean useTriplet, boolean useOccurrenceId) { Set<IdentifierExtractionResult> results = Sets.newHashSet(); List<RawOccurrenceRecord> records = parseRecord(xml, schemaType); if (records != null && !records.isEmpty()) { for (RawOccurrenceRecord record : records) { Set<UniqueIdentifier> ids = Sets.newHashSet(); if (useTriplet) { Triplet triplet = null; try { triplet = new Triplet( datasetKey, record.getInstitutionCode(), record.getCollectionCode(), record.getCatalogueNumber(), record.getUnitQualifier()); } catch (IllegalArgumentException e) { log.info( "No holy triplet for an xml snippet in dataset [{}] and schema [{}], got error [{}]", datasetKey.toString(), schemaType.toString(), e.getMessage()); } if (triplet != null) { ids.add(triplet); } } if (useOccurrenceId && record.getIdentifierRecords() != null && !record.getIdentifierRecords().isEmpty()) { for (IdentifierRecord idRecord : record.getIdentifierRecords()) { if ((idRecord.getIdentifierType() == 1 || idRecord.getIdentifierType() == 7) && idRecord.getIdentifier() != null) { ids.add(new PublisherProvidedUniqueIdentifier(datasetKey, idRecord.getIdentifier())); } } } if (!ids.isEmpty()) { results.add(new IdentifierExtractionResult(ids, record.getUnitQualifier())); } } } return results; } private  XmlFragmentParser(); static List<RawOccurrenceRecord> parseRecord(RawXmlOccurrence xmlRecord); static List<RawOccurrenceRecord> parseRecord(String xml, OccurrenceSchemaType schemaType); static List<RawOccurrenceRecord> parseRecord(byte[] xml, OccurrenceSchemaType schemaType); static RawOccurrenceRecord parseRecord(
      byte[] xml, OccurrenceSchemaType schemaType, String unitQualifier); static Set<IdentifierExtractionResult> extractIdentifiers(
      UUID datasetKey,
      byte[] xml,
      OccurrenceSchemaType schemaType,
      boolean useTriplet,
      boolean useOccurrenceId); }
XmlFragmentParser { public static Set<IdentifierExtractionResult> extractIdentifiers( UUID datasetKey, byte[] xml, OccurrenceSchemaType schemaType, boolean useTriplet, boolean useOccurrenceId) { Set<IdentifierExtractionResult> results = Sets.newHashSet(); List<RawOccurrenceRecord> records = parseRecord(xml, schemaType); if (records != null && !records.isEmpty()) { for (RawOccurrenceRecord record : records) { Set<UniqueIdentifier> ids = Sets.newHashSet(); if (useTriplet) { Triplet triplet = null; try { triplet = new Triplet( datasetKey, record.getInstitutionCode(), record.getCollectionCode(), record.getCatalogueNumber(), record.getUnitQualifier()); } catch (IllegalArgumentException e) { log.info( "No holy triplet for an xml snippet in dataset [{}] and schema [{}], got error [{}]", datasetKey.toString(), schemaType.toString(), e.getMessage()); } if (triplet != null) { ids.add(triplet); } } if (useOccurrenceId && record.getIdentifierRecords() != null && !record.getIdentifierRecords().isEmpty()) { for (IdentifierRecord idRecord : record.getIdentifierRecords()) { if ((idRecord.getIdentifierType() == 1 || idRecord.getIdentifierType() == 7) && idRecord.getIdentifier() != null) { ids.add(new PublisherProvidedUniqueIdentifier(datasetKey, idRecord.getIdentifier())); } } } if (!ids.isEmpty()) { results.add(new IdentifierExtractionResult(ids, record.getUnitQualifier())); } } } return results; } private  XmlFragmentParser(); static List<RawOccurrenceRecord> parseRecord(RawXmlOccurrence xmlRecord); static List<RawOccurrenceRecord> parseRecord(String xml, OccurrenceSchemaType schemaType); static List<RawOccurrenceRecord> parseRecord(byte[] xml, OccurrenceSchemaType schemaType); static RawOccurrenceRecord parseRecord(
      byte[] xml, OccurrenceSchemaType schemaType, String unitQualifier); static Set<IdentifierExtractionResult> extractIdentifiers(
      UUID datasetKey,
      byte[] xml,
      OccurrenceSchemaType schemaType,
      boolean useTriplet,
      boolean useOccurrenceId);  }