/**
   * Reset the commit order from the session's descriptors. This uses the constraint dependencies in
   * the descriptor's mappings, to decide which descriptors are dependent on which other
   * descriptors. Multiple computations of the commit order should produce the same ordering. This
   * is done to improve performance on unit of work writes through decreasing the stack size, and
   * acts as a deadlock avoidance mechanism.
   */
  public void initializeCommitOrder() {
    Vector descriptors = Helper.buildVectorFromMapElements(getSession().getDescriptors());

    // Must ensure uniqueness, some descriptor my be register twice for interfaces.
    descriptors = Helper.addAllUniqueToVector(new Vector(descriptors.size()), descriptors);
    Object[] descriptorsArray = new Object[descriptors.size()];
    for (int index = 0; index < descriptors.size(); index++) {
      descriptorsArray[index] = descriptors.elementAt(index);
    }
    Arrays.sort(descriptorsArray, new DescriptorCompare());
    descriptors = new Vector(descriptors.size());
    for (int index = 0; index < descriptorsArray.length; index++) {
      descriptors.addElement(descriptorsArray[index]);
    }

    CommitOrderCalculator calculator = new CommitOrderCalculator(getSession());
    calculator.addNodes(descriptors);
    calculator.calculateMappingDependencies();
    calculator.orderCommits();
    descriptors = calculator.getOrderedDescriptors();

    calculator = new CommitOrderCalculator(getSession());
    calculator.addNodes(descriptors);
    calculator.calculateSpecifiedDependencies();
    calculator.orderCommits();

    setCommitOrder(calculator.getOrderedClasses());
  }
  /** PUBLIC: Add a type indicator conversion to this mapping. */
  public void addClassIndicator(Class implementer, Object typeIndicator) {
    if (typeIndicator == null) {
      typeIndicator = Helper.getNullWrapper();
    }

    getTypeIndicatorTranslation().put(implementer, typeIndicator);
    getTypeIndicatorTranslation().put(typeIndicator, implementer);
  }
  /** INTERNAL: Return the type for a specified implementor */
  protected Object getTypeForImplementor(Class implementor) {
    Object type = getTypeIndicatorTranslation().get(implementor);
    if (type == Helper.getNullWrapper()) {
      type = null;
    }

    return type;
  }
  /**
   * INTERNAL: Extract the direct values from the specified field value. Return them in a vector.
   * The field value better be an Array.
   */
  public Vector buildDirectValuesFromFieldValue(Object fieldValue) throws DatabaseException {

    if (fieldValue == null) {
      return null;
    }

    return Helper.vectorFromArray((Object[]) fieldValue);
  }
  /** INTERNAL: Return the implementor for a specified type */
  protected Object getImplementorForType(Object type, AbstractSession session) {
    if (type == null) {
      return getTypeIndicatorTranslation().get(Helper.getNullWrapper());
    }

    // Must ensure the type is the same, i.e. Integer != BigDecimal.
    try {
      type = session.getDatasourcePlatform().convertObject(type, getTypeField().getType());
    } catch (ConversionException e) {
      throw ConversionException.couldNotBeConverted(this, getDescriptor(), e);
    }

    return getTypeIndicatorTranslation().get(type);
  }
 /**
  * INTERNAL: Build the appropriate field value for the specified set of direct values. The
  * database better be expecting an ARRAY.
  */
 public Object buildFieldValueFromDirectValues(
     Vector directValues, String elementDataTypeName, AbstractSession session)
     throws DatabaseException {
   Object[] fields = Helper.arrayFromVector(directValues);
   try {
     ((DatabaseAccessor) session.getAccessor()).incrementCallCount(session);
     java.sql.Connection connection = ((DatabaseAccessor) session.getAccessor()).getConnection();
     return session.getPlatform().createArray(elementDataTypeName, fields, session, connection);
   } catch (java.sql.SQLException ex) {
     throw DatabaseException.sqlException(ex, session, false);
   } finally {
     ((DatabaseAccessor) session.getAccessor()).decrementCallCount();
   }
 }
 protected void verify() {
   if (results.size() != expectedResults.size()) {
     throw new TestErrorException("ReportQuery test failed: The result size are different");
   }
   BigDecimal expected =
       (BigDecimal) ((ReportQueryResult) expectedResults.firstElement()).getByIndex(0);
   BigDecimal result =
       (BigDecimal)
           ConversionManager.getDefaultManager()
               .convertObject(
                   ((ReportQueryResult) results.firstElement()).getByIndex(0), BigDecimal.class);
   if (!Helper.compareBigDecimals(expected, result)) {
     throw new TestErrorException(
         "ReportQuery test failed: The results did not match (" + expected + ", " + result + ")");
   }
 }
  /** INTERNAL: Set the type field classification through searching the indicators hashtable. */
  @Override
  public void preInitialize(AbstractSession session) throws DescriptorException {
    super.preInitialize(session);
    if (getTypeIndicatorTranslation().isEmpty()) {
      return;
    }
    Class type = null;
    for (Iterator typeValuesEnum = getTypeIndicatorTranslation().values().iterator();
        typeValuesEnum.hasNext() && (type == null); ) {
      Object value = typeValuesEnum.next();
      if ((value != Helper.getNullWrapper()) && (!(value instanceof Class))) {
        type = value.getClass();
      }
    }

    getTypeField().setType(type);
  }
 protected static boolean verifyFieldAndMapping(
     AbstractSession session,
     DatabaseField field,
     ClassDescriptor descriptor,
     DatabaseMapping mapping) {
   verifyField(session, field, descriptor);
   while (mapping.isAggregateObjectMapping()) {
     ClassDescriptor referenceDescriptor =
         ((AggregateObjectMapping) mapping).getReferenceDescriptor();
     mapping = referenceDescriptor.getObjectBuilder().getMappingForField(field);
     verifyFieldAndMapping(session, field, referenceDescriptor, mapping);
   }
   if (!mapping.isDirectToFieldMapping() && !mapping.isTransformationMapping()) {
     String mappingTypeName = Helper.getShortClassName(mapping);
     session
         .getIntegrityChecker()
         .handleError(
             DescriptorException.returningPolicyMappingNotSupported(
                 field.getName(), mappingTypeName, mapping));
     return false;
   } else {
     return true;
   }
 }
 /** INTERNAL: */
 public void validationAfterDescriptorInitialization(AbstractSession session) {
   Hashtable mapped = new Hashtable();
   for (int operation = INSERT; operation <= UPDATE; operation++) {
     if ((main[operation][MAPPED] != null) && !main[operation][MAPPED].isEmpty()) {
       Iterator it = main[operation][MAPPED].iterator();
       while (it.hasNext()) {
         DatabaseField field = (DatabaseField) it.next();
         mapped.put(field, field);
       }
     }
   }
   if (!mapped.isEmpty()) {
     for (Enumeration fields = getDescriptor().getFields().elements();
         fields.hasMoreElements(); ) {
       DatabaseField fieldInDescriptor = (DatabaseField) fields.nextElement();
       DatabaseField fieldInMain = (DatabaseField) mapped.get(fieldInDescriptor);
       if (fieldInMain != null) {
         if (fieldInMain.getType() == null) {
           if (getDescriptor().isReturnTypeRequiredForReturningPolicy()) {
             session
                 .getIntegrityChecker()
                 .handleError(
                     DescriptorException.returningPolicyMappedFieldTypeNotSet(
                         fieldInMain.getName(), getDescriptor()));
           }
         } else if (isThereATypeConflict(fieldInMain, fieldInDescriptor)) {
           session
               .getIntegrityChecker()
               .handleError(
                   DescriptorException.returningPolicyAndDescriptorFieldTypeConflict(
                       fieldInMain.getName(),
                       fieldInMain.getType().getName(),
                       fieldInDescriptor.getType().getName(),
                       getDescriptor()));
         }
       }
     }
   }
   if (!(session.getDatasourcePlatform() instanceof DatabasePlatform)) {
     // don't attempt further diagnostics on non-relational platforms
     return;
   }
   WriteObjectQuery[] query = {
     getDescriptor().getQueryManager().getInsertQuery(),
     getDescriptor().getQueryManager().getUpdateQuery()
   };
   String[] queryTypeName = {"InsertObjectQuery", "UpdateObjectQuery"};
   for (int operation = INSERT; operation <= UPDATE; operation++) {
     if ((main[operation][ALL] != null) && !main[operation][ALL].isEmpty()) {
       // this operation requires some fields to be returned
       if ((query[operation] == null) || (query[operation].getDatasourceCall() == null)) {
         if (!session.getPlatform().canBuildCallWithReturning()) {
           session
               .getIntegrityChecker()
               .handleError(
                   DescriptorException.noCustomQueryForReturningPolicy(
                       queryTypeName[operation],
                       Helper.getShortClassName(session.getPlatform()),
                       getDescriptor()));
         }
       } else if (query[operation].getDatasourceCall() instanceof StoredProcedureCall) {
         // SQLCall with custom SQL calculates its outputRowFields later (in prepare() method) -
         // that's why SQLCall can't be verified here.
         DatabaseCall customCall = (DatabaseCall) query[operation].getDatasourceCall();
         Enumeration outputRowFields = customCall.getOutputRowFields().elements();
         Collection notFoundInOutputRow = createCollection();
         notFoundInOutputRow.addAll(main[operation][ALL]);
         while (outputRowFields.hasMoreElements()) {
           notFoundInOutputRow.remove(outputRowFields.nextElement());
         }
         if (!notFoundInOutputRow.isEmpty()) {
           Iterator it = notFoundInOutputRow.iterator();
           while (it.hasNext()) {
             DatabaseField field = (DatabaseField) it.next();
             session
                 .getIntegrityChecker()
                 .handleError(
                     DescriptorException.customQueryAndReturningPolicyFieldConflict(
                         field.getName(), queryTypeName[operation], getDescriptor()));
           }
         }
       }
     }
   }
 }
 /**
  * INTERNAL: Add indicators by classname. For use by the Mapping Workbench to avoid classpath
  * dependencies
  */
 public void addClassNameIndicator(String className, Object typeIndicator) {
   if (typeIndicator == null) {
     typeIndicator = Helper.getNullWrapper();
   }
   getTypeIndicatorNameTranslation().put(className, typeIndicator);
 }
 /**
  * INTERNAL: Rehash any maps based on fields. This is used to clone descriptors for aggregates,
  * which hammer field names.
  */
 @Override
 public void rehashFieldDependancies(AbstractSession session) {
   setSourceToTargetQueryKeyFields(Helper.rehashMap(getSourceToTargetQueryKeyNames()));
 }
 /** Print the in progress depth. */
 public String toString() {
   Object[] args = {Integer.valueOf(this.commitDepth)};
   return Helper.getShortClassName(getClass())
       + ToStringLocalization.buildMessage("commit_depth", args);
 }