Search in sources :

Example 1 with GroupedTaxonDTO

use of eu.etaxonomy.cdm.api.service.dto.GroupedTaxonDTO in project cdmlib by cybertaxonomy.

the class ClassificationServiceImpl method groupTaxaByHigherTaxon.

@Override
public List<GroupedTaxonDTO> groupTaxaByHigherTaxon(List<UUID> originalTaxonUuids, UUID classificationUuid, Rank minRank, Rank maxRank) {
    List<GroupedTaxonDTO> result = new ArrayList<>();
    // get treeindex for each taxonUUID
    Map<UUID, TreeIndex> taxonIdTreeIndexMap = dao.treeIndexForTaxonUuids(classificationUuid, originalTaxonUuids);
    // build treeindex list (or tree)
    // TODO make it work with TreeIndex or move there
    List<String> treeIndexClosureStr = new ArrayList<>();
    for (TreeIndex treeIndex : taxonIdTreeIndexMap.values()) {
        String[] splits = treeIndex.toString().substring(1).split(ITreeNode.separator);
        String currentIndex = ITreeNode.separator;
        for (String split : splits) {
            if (split.equals("")) {
                continue;
            }
            currentIndex += split + ITreeNode.separator;
            if (!treeIndexClosureStr.contains(currentIndex) && !split.startsWith(ITreeNode.treePrefix)) {
                treeIndexClosureStr.add(currentIndex);
            }
        }
    }
    // get rank sortindex for all parent taxa with sortindex <= minRank and sortIndex >= maxRank (if available)
    Integer minRankOrderIndex = minRank == null ? null : minRank.getOrderIndex();
    Integer maxRankOrderIndex = maxRank == null ? null : maxRank.getOrderIndex();
    List<TreeIndex> treeIndexClosure = TreeIndex.NewListInstance(treeIndexClosureStr);
    Map<TreeIndex, Integer> treeIndexSortIndexMapTmp = taxonNodeDao.rankOrderIndexForTreeIndex(treeIndexClosure, minRankOrderIndex, maxRankOrderIndex);
    // remove all treeindex with "exists child in above map(and child.sortindex > xxx)
    List<TreeIndex> treeIndexList = TreeIndex.sort(treeIndexSortIndexMapTmp.keySet());
    Map<TreeIndex, Integer> treeIndexSortIndexMap = new HashMap<>();
    TreeIndex lastTreeIndex = null;
    for (TreeIndex treeIndex : treeIndexList) {
        if (lastTreeIndex != null && lastTreeIndex.hasChild(treeIndex)) {
            treeIndexSortIndexMap.remove(lastTreeIndex);
        }
        treeIndexSortIndexMap.put(treeIndex, treeIndexSortIndexMapTmp.get(treeIndex));
        lastTreeIndex = treeIndex;
    }
    // get taxonID for treeIndexes
    Map<TreeIndex, UuidAndTitleCache<?>> treeIndexTaxonIdMap = taxonNodeDao.taxonUuidsForTreeIndexes(treeIndexSortIndexMap.keySet());
    // fill result list
    for (UUID originalTaxonUuid : originalTaxonUuids) {
        GroupedTaxonDTO item = new GroupedTaxonDTO();
        result.add(item);
        item.setTaxonUuid(originalTaxonUuid);
        TreeIndex groupTreeIndex = taxonIdTreeIndexMap.get(originalTaxonUuid);
        String groupIndexX = TreeIndex.toString(groupTreeIndex);
        while (groupTreeIndex != null) {
            if (treeIndexTaxonIdMap.get(groupTreeIndex) != null) {
                UuidAndTitleCache<?> uuidAndLabel = treeIndexTaxonIdMap.get(groupTreeIndex);
                item.setGroupTaxonUuid(uuidAndLabel.getUuid());
                item.setGroupTaxonName(uuidAndLabel.getTitleCache());
                break;
            } else {
                groupTreeIndex = groupTreeIndex.parent();
            // int index = groupIndex.substring(0, groupIndex.length()-1).lastIndexOf(ITreeNode.separator);
            // groupIndex = index < 0 ? null : groupIndex.substring(0, index+1);
            }
        }
    }
    return result;
}
Also used : HashMap(java.util.HashMap) GroupedTaxonDTO(eu.etaxonomy.cdm.api.service.dto.GroupedTaxonDTO) ArrayList(java.util.ArrayList) UuidAndTitleCache(eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache) TreeIndex(eu.etaxonomy.cdm.model.common.TreeIndex) UUID(java.util.UUID)

Example 2 with GroupedTaxonDTO

use of eu.etaxonomy.cdm.api.service.dto.GroupedTaxonDTO in project cdmlib by cybertaxonomy.

the class ClassificationServiceImplTest method testGroupTaxaByHigherTaxon.

@Test
@DataSet
public final void testGroupTaxaByHigherTaxon() {
    Rank minRank = Rank.GENUS();
    Rank maxRank = Rank.KINGDOM();
    List<UUID> taxonUuids = new ArrayList<>();
    taxonUuids.add(acacia_acicularis_uuid);
    taxonUuids.add(acacia_cuspidifolia_uuid);
    taxonUuids.add(acacia_sect_botrycephalae_uuid);
    List<GroupedTaxonDTO> result = this.service.groupTaxaByHigherTaxon(taxonUuids, CLASSIFICATION_UUID, minRank, maxRank);
    System.out.println(result);
    Assert.assertEquals(3, result.size());
    // acacia_acicularis_uuid  //is a root taxon with no parents
    Assert.assertEquals(acacia_acicularis_uuid, result.get(0).getTaxonUuid());
    Assert.assertNull(result.get(0).getGroupTaxonUuid());
    Assert.assertTrue(StringUtils.isBlank(result.get(0).getGroupTaxonName()));
    // acacia_cuspidifolia_uuid
    Assert.assertEquals(acacia_cuspidifolia_uuid, result.get(1).getTaxonUuid());
    Assert.assertNotNull(result.get(1).getGroupTaxonUuid());
    Assert.assertFalse(StringUtils.isBlank(result.get(1).getGroupTaxonName()));
    // acacia_sect_botrycephalae_uuid
    Assert.assertEquals(acacia_sect_botrycephalae_uuid, result.get(2).getTaxonUuid());
    Assert.assertNotNull(result.get(2).getGroupTaxonUuid());
    Assert.assertFalse(StringUtils.isBlank(result.get(2).getGroupTaxonName()));
}
Also used : GroupedTaxonDTO(eu.etaxonomy.cdm.api.service.dto.GroupedTaxonDTO) ArrayList(java.util.ArrayList) Rank(eu.etaxonomy.cdm.model.name.Rank) UUID(java.util.UUID) CdmTransactionalIntegrationTest(eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest) Test(org.junit.Test) DataSet(org.unitils.dbunit.annotation.DataSet)

Example 3 with GroupedTaxonDTO

use of eu.etaxonomy.cdm.api.service.dto.GroupedTaxonDTO in project cdmlib by cybertaxonomy.

the class ClassificationController method getGroupedTaxaByHigherTaxon.

/**
 * @param classificationUuid
 * @param response
 * @return
 * @throws IOException
 */
@RequestMapping(value = { "groupedTaxa" }, method = RequestMethod.GET)
public List<GroupedTaxonDTO> getGroupedTaxaByHigherTaxon(@PathVariable("uuid") UUID classificationUuid, @RequestParam(value = "taxonUuids", required = true) UuidList taxonUuids, @RequestParam(value = "minRankUuid", required = false) UUID minRankUuid, @RequestParam(value = "maxRankUuid", required = false) UUID maxRankUuid, HttpServletRequest request, HttpServletResponse response) throws IOException {
    logger.info("getGroupedTaxaByHigherTaxon() - " + request.getRequestURI());
    Classification classification = service.find(classificationUuid);
    if (classification == null) {
        response.sendError(404, "Classification not found using " + classificationUuid);
        return null;
    }
    Rank minRank = findRank(minRankUuid);
    Rank maxRank = findRank(maxRankUuid);
    // long start = System.currentTimeMillis();
    List<GroupedTaxonDTO> result = service.groupTaxaByHigherTaxon(taxonUuids, classificationUuid, minRank, maxRank);
    return result;
}
Also used : GroupedTaxonDTO(eu.etaxonomy.cdm.api.service.dto.GroupedTaxonDTO) Classification(eu.etaxonomy.cdm.model.taxon.Classification) Rank(eu.etaxonomy.cdm.model.name.Rank) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 4 with GroupedTaxonDTO

use of eu.etaxonomy.cdm.api.service.dto.GroupedTaxonDTO in project cdmlib by cybertaxonomy.

the class TestScriptService method testGroupedTaxa.

private void testGroupedTaxa(CdmApplicationController appCtr) {
    UUID classificationUuid = UUID.fromString("91231ebf-1c7a-47b9-a56c-b45b33137244");
    UUID taxonUuid1 = UUID.fromString("3bae1c86-1235-4e2e-be63-c7f8c4410527");
    UUID taxonUuid2 = UUID.fromString("235d3872-defe-4b92-bf2f-75a7c91510de");
    List<UUID> taxonUuids = Arrays.asList(new UUID[] { taxonUuid1, taxonUuid2 });
    Rank maxRank = DefinedTermBase.getTermByClassAndUUID(Rank.class, UUID.fromString("af5f2481-3192-403f-ae65-7c957a0f02b6"));
    Rank minRank = DefinedTermBase.getTermByClassAndUUID(Rank.class, UUID.fromString("78786e16-2a70-48af-a608-494023b91904"));
    List<GroupedTaxonDTO> groupedTaxa = appCtr.getClassificationService().groupTaxaByHigherTaxon(taxonUuids, classificationUuid, minRank, maxRank);
    System.out.println(groupedTaxa);
}
Also used : GroupedTaxonDTO(eu.etaxonomy.cdm.api.service.dto.GroupedTaxonDTO) Rank(eu.etaxonomy.cdm.model.name.Rank) UUID(java.util.UUID)

Example 5 with GroupedTaxonDTO

use of eu.etaxonomy.cdm.api.service.dto.GroupedTaxonDTO in project cdmlib by cybertaxonomy.

the class ClassificationController method getGroupedTaxaByMarkedParents.

/**
 * @param classificationUuid
 * @param response
 * @return
 * @throws IOException
 */
@RequestMapping(value = { "groupedTaxaByMarker" }, method = RequestMethod.GET)
public List<GroupedTaxonDTO> getGroupedTaxaByMarkedParents(@PathVariable("uuid") UUID classificationUuid, @RequestParam(value = "taxonUuids", required = true) UuidList taxonUuids, @RequestParam(value = "markerTypeUuid", required = false) UUID markerTypeUuid, @RequestParam(value = "flag", required = false) Boolean flag, HttpServletRequest request, HttpServletResponse response) throws IOException {
    logger.info("getGroupedTaxaByHigherTaxon() - " + request.getRequestURI());
    Classification classification = service.find(classificationUuid);
    if (classification == null) {
        response.sendError(404, "Classification not found using " + classificationUuid);
        return null;
    }
    MarkerType markerType = findMarkerType(markerTypeUuid);
    // long start = System.currentTimeMillis();
    List<GroupedTaxonDTO> result = service.groupTaxaByMarkedParents(taxonUuids, classificationUuid, markerType, flag);
    return result;
}
Also used : GroupedTaxonDTO(eu.etaxonomy.cdm.api.service.dto.GroupedTaxonDTO) Classification(eu.etaxonomy.cdm.model.taxon.Classification) MarkerType(eu.etaxonomy.cdm.model.common.MarkerType) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

GroupedTaxonDTO (eu.etaxonomy.cdm.api.service.dto.GroupedTaxonDTO)6 UUID (java.util.UUID)4 Rank (eu.etaxonomy.cdm.model.name.Rank)3 ArrayList (java.util.ArrayList)3 TreeIndex (eu.etaxonomy.cdm.model.common.TreeIndex)2 Classification (eu.etaxonomy.cdm.model.taxon.Classification)2 UuidAndTitleCache (eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 MarkerType (eu.etaxonomy.cdm.model.common.MarkerType)1 CdmTransactionalIntegrationTest (eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Test (org.junit.Test)1 DataSet (org.unitils.dbunit.annotation.DataSet)1