private ICollection <EntityFieldValue> GetModifiedFieldValues(IReadOnlyEntity entity, Type type) { EntityInfo entityInfo = CacheManager.GetEntityInfo(type); ICollection <IColumn> typeColumns = entityInfo.Columns; ITypeFieldValueList currentValues = OperationUtils.ExtractEntityTypeFieldValues(entity, type); ICollection <EntityFieldValue> modifiedColumns = new List <EntityFieldValue>(); foreach (IColumn typeColumn in typeColumns) { if (typeColumn.Key) { continue; } EntityFieldValue classFieldValue = currentValues.GetFieldValue(typeColumn.AttributeName); EntityFieldValue originalFieldValue = entity.Context != null?entity.Context.ChangeTracker.GetFieldValue(typeColumn.AttributeName) : null; bool matches = originalFieldValue != null && classFieldValue != null && classFieldValue.Value == originalFieldValue.Value || (originalFieldValue != null && classFieldValue != null && classFieldValue.Value != null && classFieldValue.Value.Equals(originalFieldValue.Value)); if (!matches) { modifiedColumns.Add(classFieldValue); } } return(modifiedColumns); }
private static void SetParentRelationFieldsForNonIdentifyingRelations(IEntity parentEntity , IEnumerable <IEntity> childObjects, RelationColumnMapping mapping) { IReadOnlyEntity firstObject = null; IEnumerator <IEntity> childEnumerator = childObjects.GetEnumerator(); if (childEnumerator.MoveNext()) { firstObject = childEnumerator.Current; } if (firstObject == null) { return; } EntityInfo parentInfo = CacheManager.GetEntityInfo(parentEntity); EntityInfo childInfo = CacheManager.GetEntityInfo(firstObject); PropertyInfo setter = null; bool foundOnce = false; while (parentInfo != null) { IColumn parentMatchedColumn = parentInfo.FindColumnByAttribute(mapping.FromField); if (parentMatchedColumn != null) { foundOnce = true; setter = parentInfo.GetProperty(parentMatchedColumn); } parentInfo = parentInfo.SuperEntityInfo; } if (!foundOnce) { string message = String.Format("The field {0} does not have a matching field in the object {1}", mapping.ToField, firstObject.GetType().FullName); throw new NoMatchingColumnFoundException(message); } foundOnce = false; while (childInfo != null) { IColumn childMatchedColumn = childInfo.FindColumnByAttribute(mapping.ToField); if (childMatchedColumn != null) { foundOnce = true; foreach (IReadOnlyEntity dbObject in childObjects) { ITypeFieldValueList fieldValueList = OperationUtils.ExtractEntityTypeFieldValues(dbObject, childInfo.EntityType); EntityFieldValue childFieldValue = fieldValueList.GetFieldValue(childMatchedColumn.AttributeName); setter.SetValue(parentEntity, childFieldValue.Value, null); } } childInfo = childInfo.SuperEntityInfo; } if (!foundOnce) { string message = String.Format("The field {0} does not have a matching field in the object {1}", mapping.ToField, firstObject.GetType().FullName); throw new NoMatchingColumnFoundException(message); } }
private void SaveForType(IEntity entity, Type type, ITransaction tx) { EntityInfo entityInfo = CacheManager.GetEntityInfo(type); if (entityInfo == null) { return; } ProcessNonIdentifyingRelations(entity, entityInfo); ITypeFieldValueList fieldValues = OperationUtils.ExtractEntityTypeFieldValues(entity, type); if (entity.Status == EntityStatus.Unmodified) { //do nothing } else if (entity.Status == EntityStatus.New) { Insert(entity, fieldValues, type, tx); } else if (entity.Status == EntityStatus.Modified) { if (entityInfo.TableInfo.VerifyOnWriteStrategy == VerifyOnWriteStrategy.Verify) { if (!VersionValidated(entity, type, tx)) { throw new DataUpdatedFromAnotherSourceException(String.Format("The type {0} updated from another transaction", type)); } OperationUtils.IncrementVersion(fieldValues); SetValues(entity, fieldValues); } Update(entity, fieldValues, type, tx); } else if (entity.Status == EntityStatus.Deleted) { Delete(fieldValues, type, tx); } else { string message = String.Format("In-corret status for class {0}", type.FullName); throw new IncorrectStatusException(message); } fieldValues = OperationUtils.ExtractEntityTypeFieldValues(entity, type); IEntityContext entityContext = entity.Context; if (entityContext != null) { entityContext.ChangeTracker.AddFields(fieldValues.FieldValues); } entity.Context.AddToCurrentObjectGraphIndex(entity); ProcessIdentifyingRelations(entity, type, tx, entityInfo, fieldValues); }