/**
  * Commit all of the objects of the class type in the change set. This allows for the order of the
  * classes to be processed optimally.
  */
 protected void commitNewObjectsForClassWithChangeSet(
     UnitOfWorkChangeSet uowChangeSet, Class theClass) {
   Map<ObjectChangeSet, ObjectChangeSet> newObjectChangesList =
       uowChangeSet.getNewObjectChangeSets().get(theClass);
   if (newObjectChangesList != null) { // may be no changes for that class type.
     AbstractSession session = getSession();
     ClassDescriptor descriptor = session.getDescriptor(theClass);
     List<ObjectChangeSet> newChangeSets = new ArrayList(newObjectChangesList.values());
     int size = newChangeSets.size();
     for (int index = 0; index < size; index++) {
       ObjectChangeSet changeSetToWrite = newChangeSets.get(index);
       Object objectToWrite = changeSetToWrite.getUnitOfWorkClone();
       if (!isProcessedCommit(objectToWrite)) {
         // PERF: Get the descriptor query, to avoid extra query creation.
         InsertObjectQuery commitQuery = descriptor.getQueryManager().getInsertQuery();
         if (commitQuery == null) {
           commitQuery = new InsertObjectQuery();
           commitQuery.setDescriptor(descriptor);
         } else {
           // Ensure original query has been prepared.
           commitQuery.checkPrepare(session, commitQuery.getTranslationRow());
           commitQuery = (InsertObjectQuery) commitQuery.clone();
         }
         commitQuery.setIsExecutionClone(true);
         commitQuery.setObjectChangeSet(changeSetToWrite);
         commitQuery.setObject(objectToWrite);
         commitQuery.cascadeOnlyDependentParts();
         commitQuery.setModifyRow(null);
         session.executeQuery(commitQuery);
       }
       uowChangeSet.putNewObjectInChangesList(changeSetToWrite, session);
     }
   }
 }
  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);
  }
 /** INTERNAL: Set the properties needed to be cascaded into the custom query. */
 protected void prepareCustomQuery(DatabaseQuery customQuery) {
   InsertObjectQuery customInsertQuery = (InsertObjectQuery) customQuery;
   customInsertQuery.setObject(getObject());
   customInsertQuery.setObjectChangeSet(getObjectChangeSet());
   customInsertQuery.setCascadePolicy(getCascadePolicy());
   customInsertQuery.setShouldMaintainCache(shouldMaintainCache());
   customInsertQuery.setModifyRow(null);
 }
  /** INTERNAL: Insert privately owned parts */
  public void preInsert(WriteObjectQuery query) throws DatabaseException, OptimisticLockException {
    // Checks if privately owned parts should be inserted or not.
    if (!shouldObjectModifyCascadeToParts(query)) {
      return;
    }

    // Get the privately owned parts
    Object object = getRealAttributeValueFromObject(query.getObject(), query.getSession());

    if (object == null) {
      return;
    }

    if (isPrivateOwned()) {
      // No need to set changeSet as insert is a straight copy anyway
      InsertObjectQuery insertQuery = new InsertObjectQuery();
      insertQuery.setIsExecutionClone(true);
      insertQuery.setObject(object);
      insertQuery.setCascadePolicy(query.getCascadePolicy());
      query.getSession().executeQuery(insertQuery);
    } else {
      ObjectChangeSet changeSet = null;
      UnitOfWorkChangeSet uowChangeSet = null;
      if (query.getSession().isUnitOfWork()
          && (((UnitOfWorkImpl) query.getSession()).getUnitOfWorkChangeSet() != null)) {
        uowChangeSet =
            (UnitOfWorkChangeSet) ((UnitOfWorkImpl) query.getSession()).getUnitOfWorkChangeSet();
        changeSet = (ObjectChangeSet) uowChangeSet.getObjectChangeSetForClone(object);
      }
      WriteObjectQuery writeQuery = new WriteObjectQuery();
      writeQuery.setIsExecutionClone(true);
      writeQuery.setObject(object);
      writeQuery.setObjectChangeSet(changeSet);
      writeQuery.setCascadePolicy(query.getCascadePolicy());
      query.getSession().executeQuery(writeQuery);
    }
  }