示例#1
0
 private static Set<Node> collectParents(List<Node> nodeList, Node end) {
   Set<Node> result = new HashSet<Node>();
   for (Node node : nodeList) {
     if (node.getParent() == null || node.getParent().equals(end)) {
       continue;
     }
     result.add(node.getParent());
   }
   return result;
 }
示例#2
0
  public static List<Node> findNodesByParent(Node parent, long regionId) {
    if (parent == null) {
      throw new IllegalArgumentException("The parent argument is required");
    }
    List<Node> resultList = new ArrayList<Node>();
    Set<Node> availableList = new HashSet<Node>();
    Set<Node> currentList;

    EntityManager em = Node.entityManager();

    TypedQuery<Node> q;
    if (NO_REGION == regionId) {
      q = em.createQuery("SELECT o FROM Node AS o WHERE o.active = :active", Node.class);
    } else {
      q =
          em.createQuery(
              "SELECT o FROM Node AS o WHERE o.region = :region AND o.active = :active",
              Node.class);
      q.setParameter("region", Region.findRegion(regionId));
    }
    q.setParameter("active", true);

    availableList.addAll(q.getResultList());
    currentList = collectParents(new ArrayList<Node>(availableList), parent);
    availableList.addAll(currentList);

    q =
        em.createQuery(
            "SELECT o FROM Node AS o WHERE o IN :childs AND o.active = :active", Node.class);
    while (currentList.size() > 0) {

      q.setParameter("childs", currentList);
      q.setParameter("active", true);
      currentList = collectParents(q.getResultList(), parent);
      availableList.addAll(currentList);
    }

    q =
        em.createQuery(
            "SELECT o FROM Node AS o WHERE o IN :avaliable AND o.parent = :parent ORDER BY o.rate DESC, o.name ASC",
            Node.class);
    q.setParameter("parent", parent);
    q.setParameter("avaliable", availableList);

    for (Node node : q.getResultList()) {
      if (node instanceof Entity) {
        Entity entity = (Entity) node;
        Set<Resource> resList = new HashSet<Resource>();

        for (Resource res : entity.getResources()) {
          resList.add(Resource.findResource(res.getId()));
        }
        entity.setResources(resList);
      }

      resultList.add(node);
    }

    return resultList;
  }
示例#3
0
  public static List<Node> findRootNodes(long regionId) {
    EntityManager em = Node.entityManager();
    TypedQuery<Node> q =
        em.createQuery(
            "SELECT o FROM Node AS o WHERE o.parent IS NULL AND dtype = 'Node'", Node.class);
    Node root = q.getSingleResult();

    return findNodesByParent(root, regionId);
  }