Search in sources :

Example 1 with TaxonomyLevelRow

use of org.olat.modules.taxonomy.ui.TaxonomyLevelRow in project OpenOLAT by OpenOLAT.

the class TaxonomyAllTreesBuilder method toTree.

public List<TaxonomyLevelRow> toTree(List<TaxonomyLevelRow> taxonomyLevels) {
    GenericTreeNode rootNode = new GenericTreeNode();
    Map<Long, GenericTreeNode> fieldKeyToNode = new HashMap<Long, GenericTreeNode>();
    for (TaxonomyLevelRow taxonomyLevel : taxonomyLevels) {
        Long key = taxonomyLevel.getKey();
        GenericTreeNode node = fieldKeyToNode.get(key);
        if (node == null) {
            node = new GenericTreeNode(nodeKey(taxonomyLevel));
            node.setUserObject(taxonomyLevel);
            fieldKeyToNode.put(key, node);
        }
        TaxonomyLevelRow parentLevel = taxonomyLevel.getParent();
        if (parentLevel == null) {
            // this is a root
            rootNode.addChild(node);
        } else {
            Long parentKey = parentLevel.getKey();
            GenericTreeNode parentNode = fieldKeyToNode.get(parentKey);
            if (parentNode == null) {
                parentNode = new GenericTreeNode("level-" + parentLevel.getKey());
                parentNode.setUserObject(parentLevel);
                fieldKeyToNode.put(parentKey, parentNode);
            }
            parentNode.addChild(node);
        }
    }
    List<TreeNode> nodeList = new ArrayList<>();
    TreeHelper.makeTreeFlat(rootNode, nodeList);
    List<TaxonomyLevelRow> sortedRows = new ArrayList<>();
    for (TreeNode node : nodeList) {
        if (node.getUserObject() != null) {
            sortedRows.add((TaxonomyLevelRow) node.getUserObject());
        }
    }
    return sortedRows;
}
Also used : GenericTreeNode(org.olat.core.gui.components.tree.GenericTreeNode) HashMap(java.util.HashMap) GenericTreeNode(org.olat.core.gui.components.tree.GenericTreeNode) TreeNode(org.olat.core.gui.components.tree.TreeNode) ArrayList(java.util.ArrayList) TaxonomyLevelRow(org.olat.modules.taxonomy.ui.TaxonomyLevelRow)

Example 2 with TaxonomyLevelRow

use of org.olat.modules.taxonomy.ui.TaxonomyLevelRow in project openolat by klemens.

the class TaxonomyAllTreesBuilder method toTree.

public List<TaxonomyLevelRow> toTree(List<TaxonomyLevelRow> taxonomyLevels) {
    GenericTreeNode rootNode = new GenericTreeNode();
    Map<Long, GenericTreeNode> fieldKeyToNode = new HashMap<Long, GenericTreeNode>();
    for (TaxonomyLevelRow taxonomyLevel : taxonomyLevels) {
        Long key = taxonomyLevel.getKey();
        GenericTreeNode node = fieldKeyToNode.get(key);
        if (node == null) {
            node = new GenericTreeNode(nodeKey(taxonomyLevel));
            node.setUserObject(taxonomyLevel);
            fieldKeyToNode.put(key, node);
        }
        TaxonomyLevelRow parentLevel = taxonomyLevel.getParent();
        if (parentLevel == null) {
            // this is a root
            rootNode.addChild(node);
        } else {
            Long parentKey = parentLevel.getKey();
            GenericTreeNode parentNode = fieldKeyToNode.get(parentKey);
            if (parentNode == null) {
                parentNode = new GenericTreeNode("level-" + parentLevel.getKey());
                parentNode.setUserObject(parentLevel);
                fieldKeyToNode.put(parentKey, parentNode);
            }
            parentNode.addChild(node);
        }
    }
    List<TreeNode> nodeList = new ArrayList<>();
    TreeHelper.makeTreeFlat(rootNode, nodeList);
    List<TaxonomyLevelRow> sortedRows = new ArrayList<>();
    for (TreeNode node : nodeList) {
        if (node.getUserObject() != null) {
            sortedRows.add((TaxonomyLevelRow) node.getUserObject());
        }
    }
    return sortedRows;
}
Also used : GenericTreeNode(org.olat.core.gui.components.tree.GenericTreeNode) HashMap(java.util.HashMap) GenericTreeNode(org.olat.core.gui.components.tree.GenericTreeNode) TreeNode(org.olat.core.gui.components.tree.TreeNode) ArrayList(java.util.ArrayList) TaxonomyLevelRow(org.olat.modules.taxonomy.ui.TaxonomyLevelRow)

Aggregations

ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 GenericTreeNode (org.olat.core.gui.components.tree.GenericTreeNode)2 TreeNode (org.olat.core.gui.components.tree.TreeNode)2 TaxonomyLevelRow (org.olat.modules.taxonomy.ui.TaxonomyLevelRow)2