private string BuildObjectIdentity(object obj, IPropertyMap newPropertyMap, object newValue) { string id = ""; IClassMap classMap = m_ObjectManager.Context.DomainMap.MustGetClassMap(obj.GetType()); string sep = classMap.IdentitySeparator; // bool gotObjectStatus = false; // ObjectStatus objStatus = ObjectStatus.Clean; if (sep == "") { sep = "|"; } object value; foreach (IPropertyMap propertyMap in classMap.GetIdentityPropertyMaps()) { if (propertyMap == newPropertyMap) { value = newValue; if (propertyMap.ReferenceType != ReferenceType.None) { value = m_ObjectManager.GetObjectIdentity(value); } } else { value = m_ObjectManager.GetPropertyValue(obj, propertyMap.Name); if (value == null || m_ObjectManager.GetNullValueStatus(obj, propertyMap.Name) == true) { if (!(m_hashTempIds.ContainsKey(obj))) { m_hashTempIds[obj] = Guid.NewGuid().ToString(); } return((string)m_hashTempIds[obj]); } else if (propertyMap.ReferenceType != ReferenceType.None) { //this ensures that a complete id can be created ahead in case of auto-in //m_ObjectManager.GetPropertyValue(obj, propertyMap.Name); value = m_ObjectManager.GetObjectIdentity(value); } } id += Convert.ToString(value) + sep; } if (id.Length > sep.Length) { id = id.Substring(0, id.Length - sep.Length); } return(id); }
protected virtual void SerializeInlineObject(XmlNode xmlObject, object obj, IClassMap classMap, IPropertyMap propertyMap, bool creating) { IObjectManager om = this.Context.ObjectManager; object value = null; bool isNull = om.GetNullValueStatus(obj, propertyMap.Name); if (!(isNull)) { value = om.GetPropertyValue(obj, propertyMap.Name); if (value == null) { isNull = true; } } //Optimistic concurrency if (!(creating)) { //Check value in xmlDoc against property original value, make sure they match } if (isNull) { om.SetOriginalPropertyValue(obj, propertyMap.Name, System.DBNull.Value); RemoveNode(xmlObject, propertyMap.GetDocElement()); } else { om.SetOriginalPropertyValue(obj, propertyMap.Name, value); XmlNode xmlInline = GetNode(xmlObject, propertyMap.GetDocElement()); SerializeInlineObjectElement(xmlObject, obj, xmlInline, value, creating); } }
public IObjectClone CloneObject(object obj) { IObjectManager om = this.Context.ObjectManager; IObjectClone clone = new ObjectClone(); IClassMap classMap = this.Context.DomainMap.MustGetClassMap(obj.GetType()); foreach (IPropertyMap propertyMap in classMap.GetAllPropertyMaps()) { if (propertyMap.IsCollection) { } else { if (om.HasOriginalValues(obj, propertyMap.Name)) { clone.SetPropertyValue(propertyMap.Name, om.GetPropertyValue(obj, propertyMap.Name)); clone.SetOriginalPropertyValue(propertyMap.Name, om.GetOriginalPropertyValue(obj, propertyMap.Name)); clone.SetNullValueStatus(propertyMap.Name, om.GetNullValueStatus(obj, propertyMap.Name)); clone.SetUpdatedStatus(propertyMap.Name, om.GetUpdatedStatus(obj, propertyMap.Name)); } } } clone.SetObjectStatus(om.GetObjectStatus(obj)); this.clonedObjects.Add(obj); return(clone); }
private void ValidateMaxLength(object obj, IPropertyMap propertyMap, IList exceptions) { int max = propertyMap.GetMaxLength(); if (max > -1) { IObjectManager om = this.Context.ObjectManager; if (propertyMap.IsCollection) { IList value = (IList)om.GetPropertyValue(obj, propertyMap.Name); if (value != null) { if (value.Count > max) { string msg = "Too many elements in list"; if (propertyMap.IsCollection) { HandleException( obj, propertyMap.Name, exceptions, new ValidationException(GetExceptionMessage(obj, propertyMap, msg, max, value.Count)), max, value.Count, value.Count); } } } } else { if (propertyMap.ReferenceType == ReferenceType.None) { if (!(om.GetNullValueStatus(obj, propertyMap.Name))) { object objValue = om.GetPropertyValue(obj, propertyMap.Name); if (objValue != null) { string value = objValue.ToString(); if (value.Length > max) { string msg = "String too long"; if (propertyMap.IsCollection) { HandleException( obj, propertyMap.Name, exceptions, new ValidationException(GetExceptionMessage(obj, propertyMap, msg, max, value.Length)), max, value.Length, value); } } } } } } } }
public IObjectClone CloneObject(object obj) { IObjectManager om = this.Context.ObjectManager; IObjectClone clone = new ObjectClone(); IClassMap classMap = this.Context.DomainMap.MustGetClassMap(obj.GetType()); foreach (IPropertyMap propertyMap in classMap.GetAllPropertyMaps()) { if (propertyMap.IsCollection) { //TODO: Implement this } else { clone.SetPropertyValue(propertyMap.Name, om.GetPropertyValue(obj, propertyMap.Name)); clone.SetNullValueStatus(propertyMap.Name, om.GetNullValueStatus(obj, propertyMap.Name)); clone.SetUpdatedStatus(propertyMap.Name, om.GetUpdatedStatus(obj, propertyMap.Name)); if (om.HasOriginalValues(obj, propertyMap.Name)) { clone.SetOriginalPropertyValue(propertyMap.Name, om.GetOriginalPropertyValue(obj, propertyMap.Name)); } } } clone.SetObjectStatus(om.GetObjectStatus(obj)); IIdentityHelper identityHelper = obj as IIdentityHelper; if (identityHelper != null) { clone.SetIdentity(identityHelper.GetIdentity()); if (identityHelper.HasIdentityKeyParts()) { foreach (object keyPart in identityHelper.GetIdentityKeyParts()) { clone.GetIdentityKeyParts().Add(keyPart); } } if (identityHelper.HasKeyStruct()) { clone.SetKeyStruct(identityHelper.GetKeyStruct()); } } this.clonedObjects.Add(obj); return(clone); }
private void ValidateMinLength(object obj, IPropertyMap propertyMap, IList exceptions) { int min = propertyMap.MinLength; if (min > -1) { IObjectManager om = this.Context.ObjectManager; if (propertyMap.IsCollection) { IList value = (IList)om.GetPropertyValue(obj, propertyMap.Name); if (value != null) { string msg = "Too few elements in list"; if (value.Count < min) { HandleException( obj, propertyMap.Name, exceptions, new ValidationException(GetExceptionMessage(obj, propertyMap, msg, min, value.Count)), min, value.Count, value.Count); } } } else { if (!(om.GetNullValueStatus(obj, propertyMap.Name))) { string value = (string)om.GetPropertyValue(obj, propertyMap.Name); if (value != null) { string msg = "String too short"; if (value.Length < min) { HandleException( obj, propertyMap.Name, exceptions, new ValidationException(GetExceptionMessage(obj, propertyMap, msg, min, value.Length)), min, value.Length, value); } } } } } }
private void ValidateDatabaseDateRange(object obj, IPropertyMap propertyMap, IList exceptions) { if (propertyMap.Column.Length > 0) { IObjectManager om = this.Context.ObjectManager; IColumnMap columnMap = propertyMap.GetColumnMap(); if (columnMap != null) { if (columnMap.DataType.Equals(DbType.DateTime) || columnMap.DataType.Equals(DbType.Time) || columnMap.DataType.Equals(DbType.Date)) { if (!(om.GetNullValueStatus(obj, propertyMap.Name))) { ISourceMap sourceMap = propertyMap.GetSourceMap(); if (sourceMap != null) { object rawValue = om.GetPropertyValue(obj, propertyMap.Name); if (rawValue == null) { //all ok } else { DateTime value = (DateTime)rawValue; if (sourceMap.SourceType == SourceType.MSSqlServer) { DateTime minDate = new DateTime(1753, 1, 1, 0, 0, 0); if (value < minDate) { string template = "Validation error in object {0}.{1} , property {2}: " + Environment.NewLine + "Sql server can not handle date/time values lower than 1753-01-01 00:00:00"; string result = String.Format( template, propertyMap.ClassMap.Name, this.Context.ObjectManager.GetObjectKeyOrIdentity(obj), propertyMap.Name); HandleException(obj, propertyMap.Name, exceptions, new ValidationException(result), minDate, value, value); } } } } } } } } }
protected void CopyValuesToOriginals(IPropertyMap propertyMap, IListManager lm, object obj, IObjectManager om) { if (propertyMap.IsCollection) { IList list = lm.CloneList(obj, propertyMap, ((IList)(om.GetPropertyValue(obj, propertyMap.Name)))); om.SetOriginalPropertyValue(obj, propertyMap.Name, list); } else { if (om.GetNullValueStatus(obj, propertyMap.Name)) { om.SetOriginalPropertyValue(obj, propertyMap.Name, System.DBNull.Value); } else { om.SetOriginalPropertyValue(obj, propertyMap.Name, om.GetPropertyValue(obj, propertyMap.Name)); } } }
protected virtual void SerializeInlineProperty(XmlNode xmlObject, object obj, IClassMap classMap, IPropertyMap propertyMap, bool creating) { IObjectManager om = this.Context.ObjectManager; string element = propertyMap.DocElement; object value = null; bool isNull = om.GetNullValueStatus(obj, propertyMap.Name); //Optimistic concurrency if (!(creating)) { //Check value in xmlDoc against property original value, make sure they match } if (isNull) { om.SetOriginalPropertyValue(obj, propertyMap.Name, System.DBNull.Value); //if the attribute/element exists, remove it if (element.Length > 0) { RemoveNode(xmlObject, element); } else { RemoveAttribute(xmlObject, propertyMap.GetDocAttribute()); } } else { value = om.GetPropertyValue(obj, propertyMap.Name); om.SetOriginalPropertyValue(obj, propertyMap.Name, value); if (element.Length > 0) { SetNodeValue(xmlObject, element, value); } else { SetAttributeValue(xmlObject, propertyMap.GetDocAttribute(), value); } } }
private void MergePrimitivePropertyValues(object value, object extValue, PropertyStatus propStatus, PropertyStatus extPropStatus, IObjectManager om, object existing, IClassMap classMap, IPropertyMap propertyMap, object obj, bool forOrgValue, MergeBehaviorType mergeBehavior) { if (!value.Equals(extValue)) // May be to naive - possibly should use some advanced method like ComparePropertyValues.. { bool keepExisting = KeepExistingValue(value, extValue, mergeBehavior, classMap, propertyMap, existing, obj, propStatus, extPropStatus, forOrgValue); if (!keepExisting) { if (forOrgValue) { om.SetPropertyValue(existing, propertyMap.Name, value); om.SetNullValueStatus(existing, propertyMap.Name, om.GetNullValueStatus(obj, propertyMap.Name)); if (propStatus == PropertyStatus.Dirty) { this.Context.UnitOfWork.RegisterDirty(existing); om.SetUpdatedStatus(existing, propertyMap.Name, true); } } else { om.SetOriginalPropertyValue(existing, propertyMap.Name, value); } } } }
private void ValidateMaxValue(object obj, IPropertyMap propertyMap, IList exceptions) { string max = propertyMap.MaxValue; if (max.Length > 0) { IObjectManager om = this.Context.ObjectManager; if (!(om.GetNullValueStatus(obj, propertyMap.Name))) { object value = om.GetPropertyValue(obj, propertyMap.Name); if (value != null) { bool ok = true; string msg = "Value too high"; if (value is string) { if (max.CompareTo((string)value) < 0) { ok = false; } } else if (value is DateTime) { if ((DateTime.Parse(max)).CompareTo((DateTime)value) < 0) { ok = false; } } else if (value is Int16) { if ((Int16.Parse(max)).CompareTo((Int16)value) < 0) { ok = false; } } else if (value is Int32) { if ((Int32.Parse(max)).CompareTo((Int32)value) < 0) { ok = false; } } else if (value is Int64) { if ((Int64.Parse(max)).CompareTo((Int64)value) < 0) { ok = false; } } else if (value is Decimal) { if ((Decimal.Parse(max)).CompareTo((Decimal)value) < 0) { ok = false; } } else if (value is Double) { if ((Double.Parse(max)).CompareTo((Double)value) < 0) { ok = false; } } else if (value is Single) { if ((Single.Parse(max)).CompareTo((Single)value) < 0) { ok = false; } } if (ok == false) { HandleException(obj, propertyMap.Name, exceptions, new ValidationException(GetExceptionMessage(obj, propertyMap, msg, max, value)), max, value, value); } } } } }
private void LoadProperty(object obj, IPropertyMap propertyMap, IObjectManager om, object source, IPropertyMap sourcePropertyMap, IObjectManager sourceOm) { if (!Monitor.TryEnter(sourceContext, this.Context.Timeout)) throw new NPersistTimeoutException("Could not aquire exclusive lock on root context before timeout: " + this.Context.Timeout.ToString() + " ms" ); try { bool nullValueStatus; object value; if (propertyMap.ReferenceType == ReferenceType.None) { if (!(propertyMap.IsCollection)) { value = sourceOm.GetPropertyValue(source, sourcePropertyMap.Name); nullValueStatus = sourceOm.GetNullValueStatus(source, sourcePropertyMap.Name); om.SetPropertyValue(obj, propertyMap.Name, value); if (nullValueStatus) { om.SetOriginalPropertyValue(obj, propertyMap.Name, DBNull.Value); } else { om.SetOriginalPropertyValue(obj, propertyMap.Name, value); } om.SetNullValueStatus(obj, propertyMap.Name, nullValueStatus); } else { //Using CloneList will work when it is only primitive values //(or immutable objects such as strings and dates) //that can be copied between contexts IList list = (IList) sourceOm.GetPropertyValue(source, sourcePropertyMap.Name); IList listClone = this.Context.ListManager.CloneList(obj, propertyMap, list); IList listCloneOrg = this.Context.ListManager.CloneList(obj, propertyMap, list); om.SetPropertyValue(obj, propertyMap.Name, listClone); om.SetOriginalPropertyValue(obj, propertyMap.Name, listCloneOrg); } } else { if (!(propertyMap.IsCollection)) { object refObject = sourceOm.GetPropertyValue(source, sourcePropertyMap.Name); if (refObject == null) { om.SetPropertyValue(obj, propertyMap.Name, null); om.SetOriginalPropertyValue(obj, propertyMap.Name, null); om.SetNullValueStatus(obj, propertyMap.Name, true); } else { string identity = sourceOm.GetObjectIdentity(refObject); Type refType = ToLeafType(refObject); //Impossible to solve for inheritance scenarios when mapping presentation model to domain model!!!! //We could try checking which presentation domain map which class map that maps to the //domain class map, but that could be a many-one relationship (many presentation model classes //map to the same domain model class!) // IClassMap sourceOrgClassMap = this.sourceContext.DomainMap.GetClassMap(orgObject.GetType() ); // IClassMap leafOrgClassMap = this.sourceContext.DomainMap.GetClassMap(sourceOrgClassMap.Name ); // IClassMap theClassMap = this.sourceContext.DomainMap.GetClassMap (sourceOrgClassMap.Name ); value = this.Context.GetObjectById(identity, refType, true); nullValueStatus = sourceOm.GetNullValueStatus(source, sourcePropertyMap.Name); om.SetPropertyValue(obj, propertyMap.Name, value); om.SetOriginalPropertyValue(obj, propertyMap.Name, value); om.SetNullValueStatus(obj, propertyMap.Name, nullValueStatus); } } else { //Using CloneList will not work when there are reference values (to mutable objects) //that can be copied between contexts IList orgList = (IList) sourceOm.GetPropertyValue(source, sourcePropertyMap.Name); IList list = (IList) om.GetPropertyValue(obj, sourcePropertyMap.Name); this.LoadReferenceList(list, orgList); //for the org-list we can use ListManager.CloneList and clone the list of leaf objects IList listOrg = this.Context.ListManager.CloneList(obj, propertyMap, list); om.SetPropertyValue(obj, propertyMap.Name, list); om.SetOriginalPropertyValue(obj, propertyMap.Name, listOrg); } } } finally { Monitor.Exit(sourceContext); } }
protected virtual void SerializeClone(object obj, ReadOnlyClone clone, string id, string key) { IObjectManager om = this.Context.ObjectManager; IDomainMap dm = this.Context.DomainMap; IAssemblyManager am = this.Context.AssemblyManager; IClassMap classMap = dm.MustGetClassMap(obj.GetType()); IListManager lm = this.Context.ListManager; clone.Identity = id; clone.Key = key; clone.Type = classMap.GetFullName(); foreach (IPropertyMap propertyMap in classMap.GetAllPropertyMaps()) { if (propertyMap.ReferenceType == ReferenceType.None) { if (propertyMap.IsCollection) { IList values = new ArrayList(); IList list = (IList)om.GetPropertyValue(obj, propertyMap.Name); foreach (object value in list) { values.Add(value); } clone.PropertyValues[propertyMap.Name] = values; clone.NullValueStatuses[propertyMap.Name] = false; } else { object value = om.GetPropertyValue(obj, propertyMap.Name); clone.PropertyValues[propertyMap.Name] = value; clone.NullValueStatuses[propertyMap.Name] = om.GetNullValueStatus(obj, propertyMap.Name); } } else { IClassMap refClassMap = propertyMap.MustGetReferencedClassMap(); if (refClassMap.IsReadOnly) { if (propertyMap.IsCollection) { IList values = new ArrayList(); IList list = (IList)om.GetPropertyValue(obj, propertyMap.Name); foreach (object value in list) { if (value != null) { refClassMap = dm.MustGetClassMap(value.GetType()); string refIdentity = om.GetObjectIdentity(value); SerializedReference refId = new SerializedReference(refIdentity, refClassMap.GetFullName()); values.Add(refId); } } clone.PropertyValues[propertyMap.Name] = values; clone.NullValueStatuses[propertyMap.Name] = false; } else { object value = om.GetPropertyValue(obj, propertyMap.Name); if (value != null) { refClassMap = dm.MustGetClassMap(value.GetType()); string refIdentity = om.GetObjectIdentity(value); SerializedReference refId = new SerializedReference(refIdentity, refClassMap.GetFullName()); value = refId; } clone.PropertyValues[propertyMap.Name] = value; clone.NullValueStatuses[propertyMap.Name] = om.GetNullValueStatus(obj, propertyMap.Name); } } } } }
protected void CopyValuesToOriginals(IPropertyMap propertyMap, IListManager lm, object obj, IObjectManager om) { if (propertyMap.IsCollection) { IList list = lm.CloneList(obj, propertyMap, ((IList) (om.GetPropertyValue(obj, propertyMap.Name)))); om.SetOriginalPropertyValue(obj, propertyMap.Name, list); } else { if (om.GetNullValueStatus(obj, propertyMap.Name)) { om.SetOriginalPropertyValue(obj, propertyMap.Name, System.DBNull.Value); } else { om.SetOriginalPropertyValue(obj, propertyMap.Name, om.GetPropertyValue(obj, propertyMap.Name)); } } }
protected void CopyValuesToOriginals(IPropertyMap propertyMap, IListManager lm, object obj, IObjectManager om) { if (propertyMap.IsCollection) { //IList list = lm.CloneList(obj, propertyMap, ((IList) (om.GetPropertyValue(obj, propertyMap.Name)))); IInterceptableList list = om.GetPropertyValue(obj, propertyMap.Name) as IInterceptableList; IList orgList = null; if (list != null) { bool stackMute = list.MuteNotify; list.MuteNotify = true; orgList = new ArrayList( list ); list.MuteNotify = stackMute; } om.SetOriginalPropertyValue(obj, propertyMap.Name, orgList); } else { if (om.GetNullValueStatus(obj, propertyMap.Name)) { om.SetOriginalPropertyValue(obj, propertyMap.Name, System.DBNull.Value); } else { om.SetOriginalPropertyValue(obj, propertyMap.Name, om.GetPropertyValue(obj, propertyMap.Name)); } } }