/**
   * Extract an {@link EntityReference} from the given {@link SolrDocument} (e.g. search result).
   * The entity type is inferred from the "type" field which must be specified and must have a valid
   * value (that corresponds to an existing {@link EntityType}).
   *
   * @param document a {@link SolrDocument} to extract the {@link EntityReference} from (the "type"
   *     field must be specified)
   * @param parameters the parameters to pass to the reference resolver (e.g. in case some reference
   *     components are missing)
   * @return the reference to the entity associated with the given {@link SolrDocument}
   */
  public EntityReference resolve(SolrDocument document, Object... parameters) {
    EntityType type;
    try {
      type = EntityType.valueOf((String) document.get(FieldUtils.TYPE));
    } catch (IllegalArgumentException e) {
      return null;
    } catch (NullPointerException e) {
      return null;
    }

    return resolve(document, type, parameters);
  }
  @Override
  public EntityReference resolve(
      EntityReference referenceToResolve, EntityType type, Object... parameters) {
    EntityReference normalizedReference;

    if (referenceToResolve == null) {
      normalizedReference = new EntityReference(resolveDefaultValue(type, parameters), type);
    } else {
      // If the passed type is a supertype of the reference to resolve's type then we need to insert
      // a top level
      // reference.
      if (type.ordinal() > referenceToResolve.getType().ordinal()) {
        normalizedReference =
            new EntityReference(resolveDefaultValue(type, parameters), type, referenceToResolve);
      } else {
        normalizedReference = referenceToResolve;
      }
    }

    // Check all references and parent references which have a NULL name and replace them with
    // default values.
    // In addition insert references where needed.
    try {
      normalizedReference = normalizeReference(normalizedReference, parameters);
    } catch (InvalidEntityReferenceException e) {
      throw new InvalidEntityReferenceException("Invalid reference [" + referenceToResolve + "]");
    }

    if (referenceToResolve != null) {
      // If the passed type is a subtype of the reference to resolve's type then we extract the
      // reference.
      if (type.ordinal() < referenceToResolve.getType().ordinal()) {
        normalizedReference = normalizedReference.extractReference(type);
      }
    }

    return normalizedReference;
  }