/**
   * resolve attribute created-by.
   *
   * @param ctx EvaluationContext
   * @param attributeIdValue whole attribute
   * @param resolvableAttribute resolvable part of attribute
   * @param tail tail after resolvable part
   * @return Object[] result
   * @throws de.escidoc.core.common.exceptions.system.WebserverSystemException
   * @throws de.escidoc.core.common.exceptions.system.SqlDatabaseSystemException
   * @throws de.escidoc.core.common.exceptions.application.notfound.GrantNotFoundException
   * @throws de.escidoc.core.common.exceptions.application.notfound.ResourceNotFoundException
   */
  private Object[] resolveCreatedByAttribute(
      final EvaluationCtx ctx,
      final String attributeIdValue,
      final String resolvableAttribute,
      final String tail)
      throws GrantNotFoundException, SqlDatabaseSystemException, ResourceNotFoundException,
          WebserverSystemException {
    final String userOrGroupId =
        FinderModuleHelper.retrieveSingleResourceAttribute(ctx, Constants.URI_RESOURCE_ID, true);
    final String grantId =
        FinderModuleHelper.retrieveSingleResourceAttribute(ctx, Constants.URI_SUBRESOURCE_ID, true);
    if (grantId == null || grantId.length() == 0) {
      throw new GrantNotFoundException("no grantId found");
    }
    final RoleGrant grant =
        resolvableAttribute.matches(".*" + XmlUtility.NAME_USER_ACCOUNT + ".*")
            ? userAccountDao.retrieveGrant(userOrGroupId, grantId)
            : userGroupDao.retrieveGrant(grantId);
    assertGrant(grantId, grant);
    final String createdBy = grant.getCreatorId();

    final EvaluationResult result =
        CustomEvaluationResultBuilder.createSingleStringValueResult(createdBy);
    return new Object[] {result, resolvableAttribute};
  }
  /**
   * resolve attribute role.
   *
   * @param ctx EvaluationContext
   * @param attributeIdValue whole attribute
   * @param resolvableAttribute resolvable part of attribute
   * @param tail tail after resolvable part
   * @return Object[] result
   * @throws de.escidoc.core.common.exceptions.system.WebserverSystemException
   * @throws de.escidoc.core.common.exceptions.application.notfound.GrantNotFoundException
   * @throws de.escidoc.core.common.exceptions.application.notfound.ResourceNotFoundException
   */
  private Object[] resolveRoleAttribute(
      final EvaluationCtx ctx,
      final String attributeIdValue,
      final String resolvableAttribute,
      final String tail)
      throws GrantNotFoundException, WebserverSystemException, ResourceNotFoundException {
    final String userOrGroupId =
        FinderModuleHelper.retrieveSingleResourceAttribute(ctx, Constants.URI_RESOURCE_ID, true);
    final String grantId =
        FinderModuleHelper.retrieveSingleResourceAttribute(ctx, Constants.URI_SUBRESOURCE_ID, true);
    final String roleId;
    if (grantId == null || grantId.length() == 0) {
      // if no grantId is present
      // fetch grant-attribute from invocation-mapping
      roleId = fetchSingleResourceAttribute(ctx, resolvableAttribute + "-new");
    } else {
      final RoleGrant grant =
          resolvableAttribute.matches(".*" + XmlUtility.NAME_USER_ACCOUNT + ".*")
              ? getUserAccountGrant(ctx, userOrGroupId, grantId)
              : getUserGroupGrant(ctx, grantId);
      assertGrant(grantId, grant);
      roleId = grant.getRoleId();
    }

    final EvaluationResult result =
        CustomEvaluationResultBuilder.createSingleStringValueResult(roleId);
    return new Object[] {result, resolvableAttribute};
  }
  /**
   * resolve attribute assigned-on. check if tail is present and resolvable (dependent on variable
   * SUPPORTED_ASSIGNED_ON_OBJECT_ATTRIBUTES). if tail is not resolvable, mark whole attribute as
   * unresolvable.
   *
   * @param ctx EvaluationContext
   * @param attributeIdValue whole attribute
   * @param resolvableAttribute resolvable part of attribute
   * @param tail tail after resolvable part
   * @return Object[] result
   * @throws de.escidoc.core.common.exceptions.system.WebserverSystemException
   * @throws de.escidoc.core.common.exceptions.application.notfound.GrantNotFoundException
   * @throws de.escidoc.core.common.exceptions.system.TripleStoreSystemException
   * @throws de.escidoc.core.common.exceptions.application.notfound.ResourceNotFoundException
   * @throws de.escidoc.core.common.exceptions.system.SystemException
   */
  private Object[] resolveAssignedOnAttribute(
      final EvaluationCtx ctx,
      final String attributeIdValue,
      final String resolvableAttribute,
      final String tail)
      throws TripleStoreSystemException, SystemException, GrantNotFoundException,
          ResourceNotFoundException, WebserverSystemException {
    EvaluationResult result;
    final String userOrGroupId =
        FinderModuleHelper.retrieveSingleResourceAttribute(ctx, Constants.URI_RESOURCE_ID, true);
    final String grantId =
        FinderModuleHelper.retrieveSingleResourceAttribute(ctx, Constants.URI_SUBRESOURCE_ID, true);
    String assignedOnObjectId;
    if (grantId == null || grantId.length() == 0) {
      // if no grantId is present
      // fetch grant-attribute from invocation-mapping
      try {
        assignedOnObjectId = fetchSingleResourceAttribute(ctx, resolvableAttribute + "-new");
      } catch (final Exception e) {
        // not assigned to an object
        // so mark complete attribute as unresolvable
        result =
            CustomEvaluationResultBuilder.createSingleStringValueResult(
                de.escidoc.core.common.business.Constants.UNRESOLVED_ATTRIBUTE_VALUE);
        return new Object[] {result, attributeIdValue};
      }
    } else {
      final RoleGrant grant =
          resolvableAttribute.matches(".*" + XmlUtility.NAME_USER_ACCOUNT + ".*")
              ? getUserAccountGrant(ctx, userOrGroupId, grantId)
              : getUserGroupGrant(ctx, grantId);
      assertGrant(grantId, grant);
      assignedOnObjectId = grant.getObjectId();
    }
    if (assignedOnObjectId == null) {
      // not assigned on an object
      // so mark complete attribute as unresolvable
      result =
          CustomEvaluationResultBuilder.createSingleStringValueResult(
              de.escidoc.core.common.business.Constants.UNRESOLVED_ATTRIBUTE_VALUE);
      return new Object[] {result, attributeIdValue};
    }

    // check if tailing attribute is resolvable for assigned object-type
    if (tail != null) {
      final String objectType = fetchObjectType(ctx, assignedOnObjectId);
      if (objectType.equals(XmlUtility.NAME_COMPONENT) && tail.equals(XmlUtility.NAME_CONTEXT)) {
        // if we have to resolve the context of a component,
        // we first have to get the itemId and resolve context for
        // the itemId
        final List<String> itemIds =
            FinderModuleHelper.retrieveFromTripleStore(
                true,
                tsu.getRetrieveWhereClause(
                    true, TripleStoreUtility.PROP_COMPONENT, assignedOnObjectId, null, null, null),
                assignedOnObjectId,
                TripleStoreUtility.PROP_COMPONENT,
                this.tsu);
        if (itemIds == null || itemIds.isEmpty() || itemIds.size() != 1) {
          result =
              CustomEvaluationResultBuilder.createResourceNotFoundResult(
                  new ItemNotFoundException(
                      "item for component " + assignedOnObjectId + " not found"));
        } else {
          assignedOnObjectId = itemIds.get(0);
        }
      }
    }
    result = CustomEvaluationResultBuilder.createSingleStringValueResult(assignedOnObjectId);
    return new Object[] {result, resolvableAttribute};
  }