/**
   * 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);
      }
    }
  }