@SuppressWarnings("unchecked")
  @Override
  protected Object buildModel(RenderingContext context) {
    // The templateNode can be null.
    NodeRef companyHome = repository.getCompanyHome();
    NodeRef templateNode = getTemplateNode(context);
    Map<String, Serializable> paramMap = context.getCheckedParam(PARAM_MODEL, Map.class);
    TemplateImageResolver imgResolver =
        context.getCheckedParam(PARAM_IMAGE_RESOLVER, TemplateImageResolver.class);

    // The fully authenticated user below is the username of the person who logged in and
    // who requested the execution of the current rendition. This will not be the
    // same person as the current user as renditions are executed by the system user.
    String fullyAuthenticatedUser = AuthenticationUtil.getFullyAuthenticatedUser();
    NodeRef person = serviceRegistry.getPersonService().getPerson(fullyAuthenticatedUser);

    NodeRef userHome = repository.getUserHome(person);
    Map<String, Object> model =
        getTemplateService()
            .buildDefaultModel(person, companyHome, userHome, templateNode, imgResolver);

    TemplateNode sourceTemplateNode =
        new TemplateNode(context.getSourceNode(), serviceRegistry, imgResolver);
    // TODO Add xml dom here.
    // model.put("xml", NodeModel.wrap(null));
    model.put(KEY_NODE, sourceTemplateNode);
    if (paramMap != null) model.putAll(paramMap);
    return model;
  }
 private void processTemplate(
     RenderingContext context, NodeRef templateNode, Object model, Writer out) {
   String templateType = getTemplateType();
   String template = context.getCheckedParam(PARAM_TEMPLATE, String.class);
   if (template != null) {
     templateService.processTemplateString(templateType, template, model, out);
   } else if (templateNode != null) {
     templateService.processTemplate(templateType, templateNode.toString(), model, out);
   } else {
     throwTemplateParamsNotFoundException();
   }
 }
  protected NodeRef getTemplateNode(RenderingContext context) {
    NodeRef node = context.getCheckedParam(PARAM_TEMPLATE_NODE, NodeRef.class);
    if (node == null) {
      String path = context.getCheckedParam(PARAM_TEMPLATE_PATH, String.class);
      if (path != null && path.length() > 0) {
        StoreRef storeRef = context.getDestinationNode().getStoreRef();
        ResultSet result = null;

        try {
          result = searchService.query(storeRef, SearchService.LANGUAGE_XPATH, path);
          if (result.length() != 1) {
            throw new RenditionServiceException("Could not find template node for path: " + path);
          }
          node = result.getNodeRef(0);
        } finally {
          if (result != null) {
            result.close();
          }
        }
      }
    }
    return node;
  }