/** INTERNAL: */ protected Vector<DatabaseField> getVectorOfFieldsToGenerate(int operation, DatabaseTable table) { if (this.main[operation][ALL] == null) { return null; } if (this.tableToFieldsForGenerationMap == null) { // the method is called for the first time tableToFieldsForGenerationMap = new HashMap[NUM_OPERATIONS]; } if (this.tableToFieldsForGenerationMap[operation] == null) { // the method is called for the first time for this operation this.tableToFieldsForGenerationMap[operation] = new HashMap(); } Vector<DatabaseField> fieldsForGeneration = this.tableToFieldsForGenerationMap[operation].get(table); if (fieldsForGeneration == null) { // the method is called for the first time for this operation and this table fieldsForGeneration = new NonSynchronizedVector(); Iterator it = this.main[operation][ALL].iterator(); while (it.hasNext()) { DatabaseField field = (DatabaseField) it.next(); if (field.getTable().equals(table)) { fieldsForGeneration.add(field); } } this.tableToFieldsForGenerationMap[operation].put(table, fieldsForGeneration); } return fieldsForGeneration; }
/** PUBLIC: Add all of the elements. */ public void putAll(Map map) { Iterator entriesIterator = map.entrySet().iterator(); while (entriesIterator.hasNext()) { Map.Entry entry = (Map.Entry) entriesIterator.next(); put(entry.getKey(), entry.getValue()); } }
/** * INTERNAL: This methods clones all the fields and ensures that each collection refers to the * same clones. */ @Override public Object clone() { VariableOneToOneMapping clone = (VariableOneToOneMapping) super.clone(); Map setOfKeys = new HashMap(getSourceToTargetQueryKeyNames().size()); Map sourceToTarget = new HashMap(getSourceToTargetQueryKeyNames().size()); Vector foreignKeys = org.eclipse.persistence.internal.helper.NonSynchronizedVector.newInstance( getForeignKeyFields().size()); if (getTypeField() != null) { clone.setTypeField((DatabaseField) this.getTypeField().clone()); } for (Iterator enumtr = getSourceToTargetQueryKeyNames().keySet().iterator(); enumtr.hasNext(); ) { // Clone the SourceKeyFields DatabaseField field = (DatabaseField) enumtr.next(); DatabaseField clonedField = (DatabaseField) field.clone(); setOfKeys.put(field, clonedField); // on the next line I'm cloning the query key names sourceToTarget.put(clonedField, getSourceToTargetQueryKeyNames().get(field)); } for (Enumeration enumtr = getForeignKeyFields().elements(); enumtr.hasMoreElements(); ) { DatabaseField field = (DatabaseField) enumtr.nextElement(); foreignKeys.addElement(setOfKeys.get(field)); } clone.setSourceToTargetQueryKeyFields(sourceToTarget); clone.setForeignKeyFields(foreignKeys); clone.setTypeIndicatorTranslation(new HashMap(this.getTypeIndicatorTranslation())); return clone; }
/** * INTERNAL: Convert all the class-name-based settings in this mapping to actual class-based * settings. This method is used when converting a project that has been built with class names to * a project with classes. */ @Override public void convertClassNamesToClasses(ClassLoader classLoader) { super.convertClassNamesToClasses(classLoader); Iterator iterator = getTypeIndicatorNameTranslation().entrySet().iterator(); this.typeIndicatorTranslation = new HashMap(); while (iterator.hasNext()) { Map.Entry entry = (Map.Entry) iterator.next(); String referenceClassName = (String) entry.getKey(); Object indicator = entry.getValue(); Class referenceClass = null; try { if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) { try { referenceClass = (Class) AccessController.doPrivileged( new PrivilegedClassForName(referenceClassName, true, classLoader)); } catch (PrivilegedActionException exception) { throw ValidationException.classNotFoundWhileConvertingClassNames( referenceClassName, exception.getException()); } } else { referenceClass = PrivilegedAccessHelper.getClassForName(referenceClassName, true, classLoader); } } catch (ClassNotFoundException exception) { throw ValidationException.classNotFoundWhileConvertingClassNames( referenceClassName, exception); } addClassIndicator(referenceClass, indicator); } }
/** * INTERNAL: Return the classification for the field contained in the mapping. This is used to * convert the row value to a consistent java value. */ @Override public Class getFieldClassification(DatabaseField fieldToClassify) { if ((getTypeField() != null) && (fieldToClassify.equals(getTypeField()))) { return getTypeField().getType(); } String queryKey = (String) getSourceToTargetQueryKeyNames().get(fieldToClassify); if (queryKey == null) { return null; } // Search any of the implementor descriptors for a mapping for the query-key. Iterator iterator = getReferenceDescriptor().getInterfacePolicy().getChildDescriptors().iterator(); if (iterator.hasNext()) { ClassDescriptor firstChild = (ClassDescriptor) iterator.next(); DatabaseMapping mapping = firstChild.getObjectBuilder().getMappingForAttributeName(queryKey); if ((mapping != null) && (mapping.isDirectToFieldMapping())) { return ((AbstractDirectMapping) mapping).getAttributeClassification(); } QueryKey targetQueryKey = firstChild.getQueryKeyNamed(queryKey); if ((targetQueryKey != null) && (targetQueryKey.isDirectQueryKey())) { return firstChild .getObjectBuilder() .getFieldClassification(((DirectQueryKey) targetQueryKey).getField()); } } return null; }
/** INTERNAL: Transform the object-level value into a database-level value */ public Object getFieldValue(Object objectValue, AbstractSession session) { DatabaseMapping mapping = getMapping(); Object fieldValue = objectValue; if ((mapping != null) && (mapping.isDirectToFieldMapping() || mapping.isDirectCollectionMapping())) { // CR#3623207, check for IN Collection here not in mapping. if (objectValue instanceof Collection) { // This can actually be a collection for IN within expressions... however it would be better // for expressions to handle this. Collection values = (Collection) objectValue; Vector fieldValues = new Vector(values.size()); for (Iterator iterator = values.iterator(); iterator.hasNext(); ) { Object value = iterator.next(); if (!(value instanceof Expression)) { value = getFieldValue(value, session); } fieldValues.add(value); } fieldValue = fieldValues; } else { if (mapping.isDirectToFieldMapping()) { fieldValue = ((AbstractDirectMapping) mapping).getFieldValue(objectValue, session); } else if (mapping.isDirectCollectionMapping()) { fieldValue = ((DirectCollectionMapping) mapping).getFieldValue(objectValue, session); } } } return fieldValue; }
/** * INTERNAL: The foreign key names and their primary keys are converted to DatabaseField and * stored. */ protected void initializeForeignKeys(AbstractSession session) { HashMap newSourceToTargetQueryKeyNames = new HashMap(getSourceToTargetQueryKeyNames().size()); Iterator iterator = getSourceToTargetQueryKeyNames().entrySet().iterator(); while (iterator.hasNext()) { Map.Entry entry = (Map.Entry) iterator.next(); DatabaseField field = getDescriptor().buildField((DatabaseField) entry.getKey()); newSourceToTargetQueryKeyNames.put(field, entry.getValue()); } this.sourceToTargetQueryKeyNames = newSourceToTargetQueryKeyNames; }
/** * INTERNAL: writes the first field from each of the owned tables, used for fine-grained * pessimistic locking. */ protected void writeForUpdateOfFields( ExpressionSQLPrinter printer, SQLSelectStatement statement) { for (Iterator iterator = getForUpdateOfFields().iterator(); iterator.hasNext(); ) { DatabaseField field = (DatabaseField) iterator.next(); if (printer.getPlatform().shouldPrintAliasForUpdate()) { writeAlias(printer, field, statement); } else { writeField(printer, field, statement); } } }
/** PUBLIC: Return a collection of the field to query key associations. */ public Vector getSourceToTargetQueryKeyFieldAssociations() { Vector associations = new Vector(getSourceToTargetQueryKeyNames().size()); Iterator sourceFieldEnum = getSourceToTargetQueryKeyNames().keySet().iterator(); Iterator targetQueryKeyEnum = getSourceToTargetQueryKeyNames().values().iterator(); while (sourceFieldEnum.hasNext()) { Object fieldValue = ((DatabaseField) sourceFieldEnum.next()).getQualifiedName(); Object attributeValue = targetQueryKeyEnum.next(); associations.addElement(new Association(fieldValue, attributeValue)); } return associations; }
/** * INTERNAL: Compares two Collections as sets (ignoring the order of the elements). Note that the * passed Collections are cloned. Used for testing only. */ public static boolean areCollectionsEqualAsSets(Collection col1, Collection col2) { if (col1 == col2) { return true; } if (col1.size() != col2.size()) { return false; } Collection c1 = new ArrayList(col1); Collection c2 = new ArrayList(col2); for (Iterator i = c1.iterator(); i.hasNext(); ) { Object o = i.next(); c2.remove(o); } return c2.isEmpty(); }
/** * INTERNAL: Return the typeIndicatorName translation Used by the Mapping Workbench to avoid * classpath dependencies */ public Map getTypeIndicatorNameTranslation() { if (typeIndicatorNameTranslation.isEmpty() && !typeIndicatorTranslation.isEmpty()) { Iterator keysEnum = typeIndicatorTranslation.keySet().iterator(); Iterator valuesEnum = typeIndicatorTranslation.values().iterator(); while (keysEnum.hasNext()) { Object key = keysEnum.next(); Object value = valuesEnum.next(); if (key instanceof Class) { String className = ((Class) key).getName(); typeIndicatorNameTranslation.put(className, value); } } } return typeIndicatorNameTranslation; }
/** 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); }
/** * INTERNAL: Return the class indicator associations for XML. List of class-name/value * associations. */ public Vector getClassIndicatorAssociations() { Vector associations = new Vector(); Iterator classesEnum = getTypeIndicatorNameTranslation().keySet().iterator(); Iterator valuesEnum = getTypeIndicatorNameTranslation().values().iterator(); while (classesEnum.hasNext()) { Object className = classesEnum.next(); // If the project was built in runtime is a class, MW is a string. if (className instanceof Class) { className = ((Class) className).getName(); } Object value = valuesEnum.next(); associations.addElement(new TypedAssociation(className, value)); } return associations; }
/** * INTERNAL: Compare the references of the two objects are the same, not the objects themselves. * Used for independent relationships. This is used for testing and validation purposes. * * <p>Must get separate fields for the objects because we may be adding a different class to the * attribute because of the interface */ @Override protected boolean compareObjectsWithoutPrivateOwned( Object firstObject, Object secondObject, AbstractSession session) { Object firstPrivateObject = getRealAttributeValueFromObject(firstObject, session); Object secondPrivateObject = getRealAttributeValueFromObject(secondObject, session); if ((firstPrivateObject == null) && (secondPrivateObject == null)) { return true; } if ((firstPrivateObject == null) || (secondPrivateObject == null)) { return false; } if (firstPrivateObject.getClass() != secondPrivateObject.getClass()) { return false; } Iterator targetKeys = getSourceToTargetQueryKeyNames().values().iterator(); ClassDescriptor descriptor = session.getDescriptor(firstPrivateObject.getClass()); ClassDescriptor descriptor2 = session.getDescriptor(secondPrivateObject.getClass()); while (targetKeys.hasNext()) { String queryKey = (String) targetKeys.next(); DatabaseField field = descriptor.getObjectBuilder().getFieldForQueryKeyName(queryKey); Object firstObjectField = descriptor .getObjectBuilder() .extractValueFromObjectForField(firstPrivateObject, field, session); DatabaseField field2 = descriptor2.getObjectBuilder().getFieldForQueryKeyName(queryKey); Object secondObjectField = descriptor2 .getObjectBuilder() .extractValueFromObjectForField(secondPrivateObject, field2, session); if (!((firstObjectField == null) && (secondObjectField == null))) { if ((firstObjectField == null) || (secondObjectField == null)) { return false; } if (!firstObjectField.equals(secondObjectField)) { return false; } } } return true; }
/** * INTERNAL: Selection criteria is created with source foreign keys and target keys. This criteria * is then used to read target records from the table. */ public void initializeSelectionCriteria(AbstractSession session) { Expression selectionCriteria = null; Expression expression; ExpressionBuilder expBuilder = new ExpressionBuilder(); Iterator sourceKeysEnum = getSourceToTargetQueryKeyNames().keySet().iterator(); while (sourceKeysEnum.hasNext()) { DatabaseField sourceKey = (DatabaseField) sourceKeysEnum.next(); String target = (String) this.getSourceToTargetQueryKeyNames().get(sourceKey); expression = expBuilder.getParameter(sourceKey).equal(expBuilder.get(target)); if (selectionCriteria == null) { selectionCriteria = expression; } else { selectionCriteria = expression.and(selectionCriteria); } } setSelectionCriteria(selectionCriteria); }
/** INTERNAL: Both ReturningPolicies should be initialized */ public boolean hasEqualMains(ReturningPolicy policy) { Collection[][] mainToCompare = policy.main; if (main == mainToCompare) { return true; } for (int operation = INSERT; operation <= UPDATE; operation++) { for (int state = RETURN_ONLY; state < MAIN_SIZE; state++) { if ((main[operation][state] == null) && (mainToCompare[operation][state] != null)) { return false; } if ((main[operation][state] != null) && (mainToCompare[operation][state] == null)) { return false; } if (!main[operation][state].equals(mainToCompare[operation][state])) { return false; } } } // now compare types Hashtable allFields = new Hashtable(); for (int operation = INSERT; operation <= UPDATE; operation++) { if (main[operation][ALL] != null) { Iterator it = main[operation][ALL].iterator(); while (it.hasNext()) { DatabaseField field = (DatabaseField) it.next(); allFields.put(field, field); } } } for (int operation = INSERT; operation <= UPDATE; operation++) { if (mainToCompare[operation][ALL] != null) { Iterator it = mainToCompare[operation][ALL].iterator(); while (it.hasNext()) { DatabaseField fieldToCompare = (DatabaseField) it.next(); DatabaseField field = (DatabaseField) allFields.get(fieldToCompare); if (!field.getType().equals(fieldToCompare.getType())) { return false; } } } } return true; }
/** * Commit all of the objects as a single transaction. This should commit the object in the correct * order to maintain referential integrity. */ public void commitAllObjectsWithChangeSet(UnitOfWorkChangeSet uowChangeSet) throws RuntimeException, DatabaseException, OptimisticLockException { reinitialize(); this.isActive = true; this.session.beginTransaction(); try { // PERF: if the number of classes in the project is large this loop can be a perf issue. // If only one class types changed, then avoid loop. if ((uowChangeSet.getObjectChanges().size() + uowChangeSet.getNewObjectChangeSets().size()) <= 1) { Iterator<Class> classes = uowChangeSet.getNewObjectChangeSets().keySet().iterator(); if (classes.hasNext()) { Class theClass = classes.next(); commitNewObjectsForClassWithChangeSet(uowChangeSet, theClass); } classes = uowChangeSet.getObjectChanges().keySet().iterator(); if (classes.hasNext()) { Class theClass = classes.next(); commitChangedObjectsForClassWithChangeSet(uowChangeSet, theClass); } } else { // The commit order is all of the classes ordered by dependencies, this is done for deadlock // avoidance. List commitOrder = getCommitOrder(); int size = commitOrder.size(); for (int index = 0; index < size; index++) { Class theClass = (Class) commitOrder.get(index); commitAllObjectsForClassWithChangeSet(uowChangeSet, theClass); } } if (hasDeferredCalls()) { // Perform all batched up calls, done to avoid dependencies. for (List<Object[]> calls : this.deferredCalls.values()) { for (Object[] argument : calls) { ((DatabaseQueryMechanism) argument[1]) .executeDeferredCall((DatasourceCall) argument[0]); } } } if (hasDataModifications()) { // Perform all batched up data modifications, done to avoid dependencies. for (Map.Entry<DatabaseMapping, List<Object[]>> entry : this.dataModifications.entrySet()) { List<Object[]> events = entry.getValue(); int size = events.size(); DatabaseMapping mapping = entry.getKey(); for (int index = 0; index < size; index++) { Object[] event = events.get(index); mapping.performDataModificationEvent(event, getSession()); } } } if (hasObjectsToDelete()) { // These are orphaned objects, to be deleted from private ownership updates. // TODO: These should be added to the unit of work deleted so they are deleted in the // correct order. List objects = getObjectsToDelete(); int size = objects.size(); reinitialize(); for (int index = 0; index < size; index++) { this.session.deleteObject(objects.get(index)); } } this.session.commitTransaction(); } catch (RuntimeException exception) { this.session.rollbackTransaction(); throw exception; } finally { reinitialize(); this.isActive = false; } }
/** INTERNAL: Conform the result if specified. */ protected Object conformResult( Object result, UnitOfWorkImpl unitOfWork, AbstractRecord arguments, boolean buildDirectlyFromRows) { if (getSelectionCriteria() != null) { ExpressionBuilder builder = getSelectionCriteria().getBuilder(); builder.setSession(unitOfWork.getRootSession(null)); builder.setQueryClass(getReferenceClass()); } // If the query is redirected then the collection returned might no longer // correspond to the original container policy. CR#2342-S.M. ContainerPolicy cp; if (getRedirector() != null) { cp = ContainerPolicy.buildPolicyFor(result.getClass()); } else { cp = getContainerPolicy(); } // This code is now a great deal different... For one, registration is done // as part of conforming. Also, this should only be called if one actually // is conforming. // First scan the UnitOfWork for conforming instances. // This will walk through the entire cache of registered objects. // Let p be objects from result not in the cache. // Let c be objects from cache. // Presently p intersect c = empty set, but later p subset c. // By checking cache now doesConform will be called p fewer times. Map indexedInterimResult = unitOfWork.scanForConformingInstances( getSelectionCriteria(), getReferenceClass(), arguments, this); Cursor cursor = null; // In the case of cursors just conform/register the initially read collection. if (cp.isCursorPolicy()) { cursor = (Cursor) result; cp = ContainerPolicy.buildPolicyFor(ClassConstants.Vector_class); // In nested UnitOfWork session might have been session of the parent. cursor.setSession(unitOfWork); result = cursor.getObjectCollection(); // for later incremental conforming... cursor.setInitiallyConformingIndex(indexedInterimResult); cursor.setSelectionCriteriaClone(getSelectionCriteria()); cursor.setTranslationRow(arguments); } // Now conform the result from the database. // Remove any deleted or changed objects that no longer conform. // Deletes will only work for simple queries, queries with or's or anyof's may not return // correct results when untriggered indirection is in the model. Vector fromDatabase = null; // When building directly from rows, one of the performance benefits // is that we no longer have to wrap and then unwrap the originals. // result is just a vector, not a container of wrapped originals. if (buildDirectlyFromRows) { Vector rows = (Vector) result; fromDatabase = new Vector(rows.size()); for (int i = 0; i < rows.size(); i++) { Object object = rows.elementAt(i); // null is placed in the row collection for 1-m joining to filter duplicate rows. if (object != null) { Object clone = conformIndividualResult( object, unitOfWork, arguments, getSelectionCriteria(), indexedInterimResult, buildDirectlyFromRows); if (clone != null) { fromDatabase.addElement(clone); } } } } else { fromDatabase = new Vector(cp.sizeFor(result)); AbstractSession sessionToUse = unitOfWork.getParent(); for (Object iter = cp.iteratorFor(result); cp.hasNext(iter); ) { Object object = cp.next(iter, sessionToUse); Object clone = conformIndividualResult( object, unitOfWork, arguments, getSelectionCriteria(), indexedInterimResult, buildDirectlyFromRows); if (clone != null) { fromDatabase.addElement(clone); } } } // Now add the unwrapped conforming instances into an appropriate container. // Wrapping is done automatically. // Make sure a vector of exactly the right size is returned. Object conformedResult = cp.containerInstance(indexedInterimResult.size() + fromDatabase.size()); Object eachClone; for (Iterator enumtr = indexedInterimResult.values().iterator(); enumtr.hasNext(); ) { eachClone = enumtr.next(); cp.addInto(eachClone, conformedResult, unitOfWork); } for (Enumeration enumtr = fromDatabase.elements(); enumtr.hasMoreElements(); ) { eachClone = enumtr.nextElement(); cp.addInto(eachClone, conformedResult, unitOfWork); } if (cursor != null) { cursor.setObjectCollection((Vector) conformedResult); // For nested UOW must copy all in object collection to // initiallyConformingIndex, as some of these could have been from // the parent UnitOfWork. if (unitOfWork.isNestedUnitOfWork()) { for (Enumeration enumtr = cursor.getObjectCollection().elements(); enumtr.hasMoreElements(); ) { Object clone = enumtr.nextElement(); indexedInterimResult.put(clone, clone); } } return cursor; } else { return conformedResult; } }
/** 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())); } } } } } }