Search in sources :

Example 1 with TreeIndex

use of eu.etaxonomy.cdm.model.common.TreeIndex 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 TreeIndex

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

the class TaxonNodeDaoHibernateImpl method rankOrderIndexForTreeIndex.

@Override
public Map<TreeIndex, Integer> rankOrderIndexForTreeIndex(List<TreeIndex> treeIndexes, Integer minRankOrderIndex, Integer maxRankOrderIndex) {
    Map<TreeIndex, Integer> result = new HashMap<>();
    if (treeIndexes == null || treeIndexes.isEmpty()) {
        return result;
    }
    String hql = " SELECT tn.treeIndex, r.orderIndex " + " FROM TaxonNode tn " + "     JOIN tn.taxon t " + "     JOIN t.name n " + "      JOIN n.rank r " + " WHERE tn.treeIndex IN (:treeIndexes) ";
    if (minRankOrderIndex != null) {
        hql += " AND r.orderIndex <= :minOrderIndex";
    }
    if (maxRankOrderIndex != null) {
        hql += " AND r.orderIndex >= :maxOrderIndex";
    }
    Query query = getSession().createQuery(hql);
    query.setParameterList("treeIndexes", TreeIndex.toString(treeIndexes));
    if (minRankOrderIndex != null) {
        query.setParameter("minOrderIndex", minRankOrderIndex);
    }
    if (maxRankOrderIndex != null) {
        query.setParameter("maxOrderIndex", maxRankOrderIndex);
    }
    @SuppressWarnings("unchecked") List<Object[]> list = query.list();
    for (Object[] o : list) {
        result.put(TreeIndex.NewInstance((String) o[0]), (Integer) o[1]);
    }
    return result;
}
Also used : BigInteger(java.math.BigInteger) Query(org.hibernate.Query) TreeIndex(eu.etaxonomy.cdm.model.common.TreeIndex) HashMap(java.util.HashMap)

Example 3 with TreeIndex

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

the class ClassificationDaoHibernateImpl method prepareRankSpecificRootNodes.

/**
 * See <a href="https://dev.e-taxonomy.eu/redmine/projects/edit/wiki/CdmClassificationRankSpecificRootnodes">
 * https://dev.e-taxonomy.eu/redmine/projects/edit/wiki/CdmClassificationRankSpecificRootnodes</a>
 *
 * @param classification
 * @param rank
 * @return
 *      one or two Queries as array, depending on the <code>rank</code> parameter:
 *      <code>rank == null</code>: array with one item, <code>rank != null</code>: array with two items.
 */
private Query[] prepareRankSpecificRootNodes(Classification classification, TaxonNode subtree, Rank rank, boolean includeUnpublished, boolean doCount) {
    Query query1;
    Query query2 = null;
    String whereClassification = classification != null ? " AND tn.classification = :classification " : "";
    String whereUnpublished = includeUnpublished ? "" : " AND tn.taxon.publish = :publish ";
    String whereSubtree = subtree != null ? " AND tn.treeIndex like :treeIndexLike " : "";
    TreeIndex treeIndex = TreeIndex.NewInstance(subtree);
    String whereHighest = treeIndex == null ? " tn.parent.parent = null " : treeIndex.isTreeRoot() ? " tn.parent.treeIndex = :treeIndex " : " tn.treeIndex = :treeIndex ";
    String selectWhat = doCount ? "COUNT(distinct tn)" : "DISTINCT tn";
    String joinFetch = doCount ? "" : " JOIN FETCH tn.taxon t JOIN FETCH t.name n LEFT JOIN FETCH n.rank LEFT JOIN FETCH t.secSource ss LEFT JOIN FETCH ss.citation ";
    if (rank == null) {
        String hql = "SELECT " + selectWhat + " FROM TaxonNode tn" + joinFetch + " WHERE " + whereHighest + whereClassification + whereUnpublished;
        query1 = getSession().createQuery(hql);
    } else {
        // this is for the cases
        // - exact match of the ranks
        // - rank of root node is lower but it has no parents
        String hql1 = "SELECT " + selectWhat + " FROM TaxonNode tn " + joinFetch + " WHERE " + " (tn.taxon.name.rank = :rank" + "   OR ((tn.taxon.name.rank.orderIndex > :rankOrderIndex) AND (" + whereHighest + "))" + " )" + whereClassification + whereSubtree + whereUnpublished;
        // this is for the case
        // - rank of root node is lower and it has a parent with higher rank
        String whereParentSubtree = subtree != null ? " AND parent.treeIndex like :treeIndexLike " : "";
        String hql2 = "SELECT " + selectWhat + " FROM TaxonNode tn JOIN tn.parent as parent" + joinFetch + " WHERE " + " (tn.taxon.name.rank.orderIndex > :rankOrderIndex " + "     AND parent.taxon.name.rank.orderIndex < :rankOrderIndex )" + whereClassification + whereSubtree + whereParentSubtree + whereUnpublished;
        query1 = getSession().createQuery(hql1);
        query2 = getSession().createQuery(hql2);
        query1.setParameter("rank", rank);
        query1.setParameter("rankOrderIndex", rank.getOrderIndex());
        query2.setParameter("rankOrderIndex", rank.getOrderIndex());
    }
    // parameters
    if (classification != null) {
        query1.setParameter("classification", classification);
        if (query2 != null) {
            query2.setParameter("classification", classification);
        }
    }
    if (subtree != null) {
        query1.setParameter("treeIndex", subtree.treeIndex());
        if (rank != null) {
            query1.setParameter("treeIndexLike", subtree.treeIndex() + "%");
        }
        if (query2 != null) {
            query2.setParameter("treeIndexLike", subtree.treeIndex() + "%");
        }
    }
    if (!includeUnpublished) {
        query1.setBoolean("publish", true);
        if (query2 != null) {
            query2.setBoolean("publish", true);
        }
    }
    if (query2 != null) {
        return new Query[] { query1, query2 };
    } else {
        return new Query[] { query1 };
    }
}
Also used : Query(org.hibernate.Query) TreeIndex(eu.etaxonomy.cdm.model.common.TreeIndex)

Example 4 with TreeIndex

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

the class TreeIndexComparatorTest method test.

@Test
public void test() {
    TreeIndexComparator comparator = new TreeIndexComparator();
    TreeIndex ti10 = TreeIndex.NewInstance("#t10#10#");
    TreeIndex ti10_20 = TreeIndex.NewInstance("#t10#10#20#");
    TreeIndex ti10_30 = TreeIndex.NewInstance("#t10#10#30#");
    TreeIndex ti10_30_11 = TreeIndex.NewInstance("#t10#10#30#11#");
    // both null
    Assert.assertTrue(0 == comparator.compare(null, null));
    // one null
    Assert.assertTrue(0 > comparator.compare(null, ti10));
    Assert.assertTrue(0 < comparator.compare(ti10, null));
    // equal
    Assert.assertTrue(0 == comparator.compare(ti10, ti10));
    // same start
    Assert.assertTrue(0 > comparator.compare(ti10, ti10_20));
    Assert.assertTrue(0 < comparator.compare(ti10_20, ti10));
    // different ends
    Assert.assertTrue(0 > comparator.compare(ti10_20, ti10_30));
    Assert.assertTrue(0 < comparator.compare(ti10_30, ti10_20));
    // different ends
    Assert.assertTrue(0 > comparator.compare(ti10_20, ti10_30));
    Assert.assertTrue(0 > comparator.compare(ti10_20, ti10_30_11));
    Assert.assertTrue(0 < comparator.compare(ti10_30, ti10_20));
    Assert.assertTrue(0 < comparator.compare(ti10_30_11, ti10_20));
}
Also used : TreeIndexComparator(eu.etaxonomy.cdm.compare.common.TreeIndexComparator) TreeIndex(eu.etaxonomy.cdm.model.common.TreeIndex) Test(org.junit.Test)

Example 5 with TreeIndex

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

the class TaxonNodeDaoHibernateImpl method taxonUuidsForTreeIndexes.

@Override
public Map<TreeIndex, UuidAndTitleCache<?>> taxonUuidsForTreeIndexes(Collection<TreeIndex> treeIndexes) {
    Map<TreeIndex, UuidAndTitleCache<?>> result = new HashMap<>();
    if (treeIndexes == null || treeIndexes.isEmpty()) {
        return result;
    }
    String hql = " SELECT tn.treeIndex, t.uuid, tnb.titleCache " + " FROM TaxonNode tn JOIN tn.taxon t Join t.name tnb " + " WHERE tn.treeIndex IN (:treeIndexes) ";
    Query query = getSession().createQuery(hql);
    query.setParameterList("treeIndexes", TreeIndex.toString(treeIndexes));
    @SuppressWarnings("unchecked") List<Object[]> list = query.list();
    for (Object[] o : list) {
        result.put(TreeIndex.NewInstance((String) o[0]), new UuidAndTitleCache<>((UUID) o[1], null, (String) o[2]));
    }
    return result;
}
Also used : Query(org.hibernate.Query) TreeIndex(eu.etaxonomy.cdm.model.common.TreeIndex) HashMap(java.util.HashMap) UuidAndTitleCache(eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache) UUID(java.util.UUID)

Aggregations

TreeIndex (eu.etaxonomy.cdm.model.common.TreeIndex)10 UUID (java.util.UUID)6 Query (org.hibernate.Query)5 HashMap (java.util.HashMap)4 UuidAndTitleCache (eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache)3 GroupedTaxonDTO (eu.etaxonomy.cdm.api.service.dto.GroupedTaxonDTO)2 IProgressMonitor (eu.etaxonomy.cdm.common.monitor.IProgressMonitor)2 SubProgressMonitor (eu.etaxonomy.cdm.common.monitor.SubProgressMonitor)2 TaxonNode (eu.etaxonomy.cdm.model.taxon.TaxonNode)2 OrderHint (eu.etaxonomy.cdm.persistence.query.OrderHint)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 Transactional (org.springframework.transaction.annotation.Transactional)2 TreeIndexComparator (eu.etaxonomy.cdm.compare.common.TreeIndexComparator)1 CdmBase (eu.etaxonomy.cdm.model.common.CdmBase)1 Reference (eu.etaxonomy.cdm.model.reference.Reference)1 TaxonBase (eu.etaxonomy.cdm.model.taxon.TaxonBase)1 BigInteger (java.math.BigInteger)1 Test (org.junit.Test)1