/**
   * Encode the beginning of the given {@link ResourceLineageComponent}.
   *
   * @param facesContext the JSF context for the current request
   * @param component the {@link ResourceLineageComponent} to be encoded
   */
  @Override
  public void encodeBegin(FacesContext facesContext, UIComponent component) throws IOException {
    ResourceLineageComponent resourceLineage = (ResourceLineageComponent) component;
    ResponseWriter writer = facesContext.getResponseWriter();
    long monitorId = HibernatePerformanceMonitor.get().start();
    ResourceManagerLocal resourceManager = LookupUtil.getResourceManager();
    List<Resource> ancestorResources =
        resourceManager.getResourceLineage(resourceLineage.getResourceId());
    HibernatePerformanceMonitor.get().stop(monitorId, "ResourceLineage renderer");
    if (ancestorResources.isEmpty()) {
      throw new IllegalStateException(
          "The list of ancestor resources should always contain at least one resource - the resource whose lineage was requested.");
    }

    Resource parentResource = ancestorResources.get(ancestorResources.size() - 1);
    for (Resource ancestorResource : ancestorResources) {
      writer.startElement("a", resourceLineage);
      writer.writeAttribute("href", buildURL(ancestorResource), null);
      writer.writeText(ancestorResource.getName(), null);
      writer.endElement("a");
      if (ancestorResource.getId()
          != parentResource.getId()) // separator after every item except the last one
      {
        writer.writeText(SEPARATOR, null);
      }
    }
  }
  private void loadTree() {
    int searchId;
    Resource currentResource = EnterpriseFacesContextUtility.getResourceIfExists();
    if (currentResource == null) {
      searchId = Integer.parseInt(FacesContextUtility.getOptionalRequestParameter("parent"));
    } else {
      searchId = currentResource.getId();
    }

    Subject user = EnterpriseFacesContextUtility.getSubject();

    long start = System.currentTimeMillis();
    long monitorId = HibernatePerformanceMonitor.get().start();
    Resource rootResource = resourceManager.getRootResourceForResource(searchId);
    long end = System.currentTimeMillis();
    HibernatePerformanceMonitor.get().stop(monitorId, "ResourceTree root resource");
    log.debug("Found root resource in " + (end - start));

    Agent agent =
        agentManager.getAgentByResourceId(
            LookupUtil.getSubjectManager().getOverlord(), rootResource.getId());

    start = System.currentTimeMillis();
    monitorId = HibernatePerformanceMonitor.get().start();
    List<ResourceFlyweight> resources =
        resourceManager.findResourcesByAgent(
            user, agent.getId(), PageControl.getUnlimitedInstance());
    end = System.currentTimeMillis();
    HibernatePerformanceMonitor.get().stop(monitorId, "ResourceTree agent resource");
    log.debug("Loaded " + resources.size() + " raw resources in " + (end - start));

    start = System.currentTimeMillis();
    monitorId = HibernatePerformanceMonitor.get().start();
    rootNode = load(rootResource.getId(), resources);
    end = System.currentTimeMillis();
    HibernatePerformanceMonitor.get().stop(monitorId, "ResourceTree tree construction");
    log.debug("Constructed tree in " + (end - start));
  }