/**
   * INTERNAL: Return the value of the field from the row or a value holder on the query to obtain
   * the object. Check for batch + aggregation reading.
   */
  @Override
  public Object valueFromRow(
      AbstractRecord row,
      JoinedAttributeManager joinManager,
      ObjectBuildingQuery sourceQuery,
      AbstractSession executionSession)
      throws DatabaseException {
    // If any field in the foreign key is null then it means there are no referenced objects
    for (Enumeration enumeration = getFields().elements(); enumeration.hasMoreElements(); ) {
      DatabaseField field = (DatabaseField) enumeration.nextElement();
      if (row.get(field) == null) {
        return getIndirectionPolicy().nullValueFromRow();
      }
    }

    if (getTypeField() != null) {
      // If the query used batched reading, return a special value holder,
      // or retrieve the object from the query property.
      if (sourceQuery.isReadAllQuery()
          && (((ReadAllQuery) sourceQuery).isAttributeBatchRead(getDescriptor(), getAttributeName())
              || shouldUseBatchReading())) {
        return batchedValueFromRow(row, ((ReadAllQuery) sourceQuery));
      }

      // If the field is empty we cannot load the object because we do not know what class it will
      // be
      if (row.get(getTypeField()) == null) {
        return getIndirectionPolicy().nullValueFromRow();
      }
      Class implementerClass =
          (Class) getImplementorForType(row.get(getTypeField()), executionSession);
      ReadObjectQuery query = (ReadObjectQuery) getSelectionQuery().clone();
      query.setReferenceClass(implementerClass);
      query.setSelectionCriteria(getSelectionCriteria());
      query.setDescriptor(null); // Must set to null so the right descriptor is used

      if (sourceQuery.isObjectLevelReadQuery()
          && (sourceQuery.shouldCascadeAllParts()
              || (sourceQuery.shouldCascadePrivateParts() && isPrivateOwned())
              || (sourceQuery.shouldCascadeByMapping() && this.cascadeRefresh))) {
        query.setShouldRefreshIdentityMapResult(sourceQuery.shouldRefreshIdentityMapResult());
        query.setCascadePolicy(sourceQuery.getCascadePolicy());
        query.setShouldMaintainCache(sourceQuery.shouldMaintainCache());
        // For flashback.
        if (((ObjectLevelReadQuery) sourceQuery).hasAsOfClause()) {
          query.setAsOfClause(((ObjectLevelReadQuery) sourceQuery).getAsOfClause());
        }

        // CR #4365 - used to prevent infinit recursion on refresh object cascade all
        query.setQueryId(sourceQuery.getQueryId());
      }

      return getIndirectionPolicy().valueFromQuery(query, row, executionSession);
    } else {
      return super.valueFromRow(row, joinManager, sourceQuery, executionSession);
    }
  }
 /** Read employee and clear the cache, test database read. */
 public void test() throws Exception {
   ReadObjectQuery query = new ReadObjectQuery(Employee.class);
   ExpressionBuilder employee = new ExpressionBuilder();
   query.setSelectionCriteria(
       employee
           .get("firstName")
           .equal("Brendan")
           .and(employee.get("salary").equal(100000))
           .and(employee.get("address").get("city").like("%pean%"))
           .and(employee.anyOf("phoneNumbers").get("type").equal("Home")));
   query.checkCacheOnly();
   Employee result = (Employee) getSession().executeQuery(query);
 }
  public static ReadObjectTest getReadInterfaceObjectTest() {
    TestSuite suite = new TestSuite();
    PopulationManager manager = PopulationManager.getDefaultManager();
    Contact contact = ((Employee) manager.getObject(Employee.class, "example1")).getContact();

    ExpressionBuilder builder = new ExpressionBuilder();
    Expression expression = builder.get("id").equal(contact.getEmp().getId());

    ReadObjectQuery query = new ReadObjectQuery();
    query.setReferenceClass(ContactHolder.class);
    query.setSelectionCriteria(expression);

    ReadObjectTest test = new ReadObjectTest(contact.getEmp());
    test.setQuery(query);

    return test;
  }
  public void setup() {
    getSession().getIdentityMapAccessor().initializeIdentityMaps();
    getAbstractSession().beginTransaction();

    descriptor = getSession().getProject().getDescriptors().get(Employee.class);

    // Read Object
    readObjectQuery = descriptor.getQueryManager().getReadObjectQuery();
    QueryRedirector redirector =
        new MethodBaseQueryRedirector(RedirectorOnDescriptorTest.class, "readObject");
    ReadObjectQuery roq = new ReadObjectQuery(descriptor.getJavaClass());
    roq.setRedirector(redirector);
    descriptor.getQueryManager().setReadObjectQuery(roq);

    // Read All
    readAllQuery = descriptor.getQueryManager().getReadAllQuery();
    redirector = new MethodBaseQueryRedirector(RedirectorOnDescriptorTest.class, "readAll");
    ReadAllQuery raq = new ReadAllQuery(descriptor.getJavaClass());
    raq.setRedirector(redirector);
    descriptor.getQueryManager().setReadAllQuery(raq);

    // Delete Object
    deleteObjectQuery = descriptor.getQueryManager().getDeleteQuery();
    redirector = new MethodBaseQueryRedirector(RedirectorOnDescriptorTest.class, "deleteObject");
    DeleteObjectQuery doq = new DeleteObjectQuery();
    doq.setRedirector(redirector);
    descriptor.getQueryManager().setDeleteQuery(doq);

    // Insert Object
    insertQuery = descriptor.getQueryManager().getInsertQuery();
    redirector = new MethodBaseQueryRedirector(RedirectorOnDescriptorTest.class, "insertObject");
    InsertObjectQuery ioq = new InsertObjectQuery();
    ioq.setRedirector(redirector);
    descriptor.getQueryManager().setInsertQuery(ioq);

    // Update Object
    updateQuery = descriptor.getQueryManager().getUpdateQuery();
    redirector = new MethodBaseQueryRedirector(RedirectorOnDescriptorTest.class, "updateObject");
    UpdateObjectQuery uoq = new UpdateObjectQuery();
    uoq.setRedirector(redirector);
    descriptor.getQueryManager().setUpdateQuery(uoq);
  }