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;
}
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()));
}
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;
}
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);
}
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;
}
Aggregations