/**
   * If we have a work instance we have to check whether the instance to be indexed is contained in
   * any other indexed entities for a tenant.
   *
   * @param instance the instance to be indexed
   * @param workPlan the current work plan
   * @param currentRecursionContext the current {@link
   *     org.hibernate.search.engine.spi.ContainedInRecursionContext} object used to check the graph
   *     traversal
   * @param tenantIdentifier the identifier of the tenant or null, if there isn't one
   * @see #appendContainedInWorkForInstance(Object, WorkPlan, ContainedInRecursionContext)
   */
  public void appendContainedInWorkForInstance(
      Object instance,
      WorkPlan workPlan,
      ContainedInRecursionContext currentRecursionContext,
      String tenantIdentifier) {
    for (ContainedInMetadata containedInMetadata : typeMetadata.getContainedInMetadata()) {
      XMember member = containedInMetadata.getContainedInMember();
      Object unproxiedInstance = instanceInitializer.unproxy(instance);

      ContainedInRecursionContext recursionContext =
          updateContainedInRecursionContext(
              unproxiedInstance, containedInMetadata, currentRecursionContext);

      if (recursionContext.isTerminal()) {
        continue;
      }

      Object value = ReflectionHelper.getMemberValue(unproxiedInstance, member);

      if (value == null) {
        continue;
      }

      if (member.isArray()) {
        Object[] array = (Object[]) value;
        for (Object arrayValue : array) {
          processSingleContainedInInstance(
              workPlan, arrayValue, recursionContext, tenantIdentifier);
        }
      } else if (member.isCollection()) {
        Collection<?> collection = null;
        try {
          collection = getActualCollection(member, value);
          collection.size(); // load it
        } catch (Exception e) {
          if (e.getClass().getName().contains("org.hibernate.LazyInitializationException")) {
            /* A deleted entity not having its collection initialized
             * leads to a LIE because the collection is no longer attached to the session
             *
             * But that's ok as the collection update event has been processed before
             * or the fk would have been cleared and thus triggering the cleaning
             */
            collection = null;
          }
        }
        if (collection != null) {
          for (Object collectionValue : collection) {
            processSingleContainedInInstance(
                workPlan, collectionValue, recursionContext, tenantIdentifier);
          }
        }
      } else {
        processSingleContainedInInstance(workPlan, value, recursionContext, tenantIdentifier);
      }
    }
  }
 @Override
 public void performWork(Work work, TransactionContext transactionContext) {
   final Class<?> entityType = instanceInitializer.getClassFromWork(work);
   EntityIndexBinding indexBindingForEntity = factory.getIndexBinding(entityType);
   if (indexBindingForEntity == null
       && factory.getDocumentBuilderContainedEntity(entityType) == null) {
     throw new SearchException(
         "Unable to perform work. Entity Class is not @Indexed nor hosts @ContainedIn: "
             + entityType);
   }
   work = interceptWork(indexBindingForEntity, work);
   if (work == null) {
     // nothing to do
     return;
   }
   if (transactionContext.isTransactionInProgress()) {
     final Object transactionIdentifier = transactionContext.getTransactionIdentifier();
     WorkQueueSynchronization txSync = synchronizationPerTransaction.get(transactionIdentifier);
     if (txSync == null || txSync.isConsumed()) {
       txSync = createTransactionWorkQueueSynchronization(transactionIdentifier);
       transactionContext.registerSynchronization(txSync);
       synchronizationPerTransaction.put(transactionIdentifier, txSync);
     }
     txSync.add(work);
   } else {
     if (transactionExpected) {
       // this is a workaround: isTransactionInProgress should return "true"
       // for correct configurations.
       log.pushedChangesOutOfTransaction();
     }
     WorkQueue queue = new WorkQueue(factory);
     queueingProcessor.add(work, queue);
     queueingProcessor.prepareWorks(queue);
     queueingProcessor.performWorks(queue);
   }
 }