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); } } } } } } } }
protected virtual XmlNode FindNodeForObject(XmlNode xmlRoot, object obj, IClassMap classMap) { IObjectManager om = this.Context.ObjectManager; string element = classMap.GetDocElement(); string xpath = ""; foreach (IPropertyMap idPropertyMap in classMap.GetIdentityPropertyMaps()) { if (xpath.Length > 0) { xpath += " and "; // do not localize } if (idPropertyMap.DocElement.Length > 0) { xpath += idPropertyMap.Name + " = \"" + om.GetPropertyValue(obj, idPropertyMap.Name).ToString() + "\""; // do not localize } else { xpath += "@" + idPropertyMap.GetDocAttribute() + " = \"" + om.GetPropertyValue(obj, idPropertyMap.Name).ToString() + "\""; // do not localize } } xpath = element + "[" + xpath + "]"; XmlNode xmlNode = xmlRoot.SelectSingleNode(xpath); return(xmlNode); }
protected virtual void SerializeInlineReferenceList(XmlNode xmlObject, object obj, IClassMap classMap, IPropertyMap propertyMap, bool creating) { IObjectManager om = this.Context.ObjectManager; IListManager lm = this.Context.ListManager; IList list = (IList)om.GetPropertyValue(obj, propertyMap.Name); //Optimistic concurrency if (!(creating)) { //Check value in xmlDoc against property original value, make sure they match } XmlNode xmlList = GetNode(xmlObject, propertyMap.GetDocElement()); RemoveAllChildNodes(xmlList); foreach (object value in list) { XmlNode xmlElement = AddNode(xmlList, "item"); SerializeInlineReferenceElement(obj, xmlElement, value); } IList orgList = lm.CloneList(obj, propertyMap, ((IList)(om.GetPropertyValue(obj, propertyMap.Name)))); om.SetOriginalPropertyValue(obj, propertyMap.Name, orgList); }
protected virtual void HandleSlaveManyManyPropertySet(object obj, IPropertyMap propertyMap, IList newList, IList oldList) { this.Context.LogManager.Info(this, "Managing inverse many-many property relationship synchronization", "Writing to object of type: " + obj.GetType().ToString() + ", Property: " + propertyMap.Name); // do not localize IPropertyMap invPropertyMap = propertyMap.GetInversePropertyMap(); if (invPropertyMap == null) { return; } ArrayList added = GetListDiff(oldList, newList); ArrayList removed = GetListDiff(newList, oldList); IObjectManager om = this.Context.ObjectManager; IUnitOfWork uow = this.Context.UnitOfWork; IList list; IInterceptableList mList; bool stackMute = false; object value; foreach (object iValue in added) { value = iValue; om.EnsurePropertyIsLoaded(value, invPropertyMap); list = (IList)om.GetPropertyValue(value, invPropertyMap.Name); mList = list as IInterceptableList; if (mList != null) { stackMute = mList.MuteNotify; mList.MuteNotify = true; } list.Add(obj); if (mList != null) { mList.MuteNotify = stackMute; } uow.RegisterDirty(value); om.SetUpdatedStatus(value, invPropertyMap.Name, true); } foreach (object iValue in removed) { value = iValue; om.EnsurePropertyIsLoaded(value, invPropertyMap); list = (IList)om.GetPropertyValue(value, invPropertyMap.Name); mList = list as IInterceptableList; if (mList != null) { stackMute = mList.MuteNotify; mList.MuteNotify = true; } list.Remove(obj); if (mList != null) { mList.MuteNotify = stackMute; } uow.RegisterDirty(value); om.SetUpdatedStatus(value, invPropertyMap.Name, true); } }
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); }
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); } } } } } }
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); } }
protected virtual void PerformRemoveAction(InverseAction action) { object obj = action.Obj; string propertyName = action.PropertyName; object value = action.Value; IPropertyMap propertyMap = this.Context.DomainMap.MustGetClassMap(obj.GetType()).MustGetPropertyMap(propertyName); IObjectManager om = this.Context.ObjectManager; IList list; IInterceptableList mList; bool stackMute = false; om.EnsurePropertyIsLoaded(obj, propertyMap); list = (IList)om.GetPropertyValue(obj, propertyName); mList = list as IInterceptableList; if (mList != null) { stackMute = mList.MuteNotify; mList.MuteNotify = true; } list.Remove(value); if (mList != null) { mList.MuteNotify = stackMute; } om.SetUpdatedStatus(obj, propertyName, true); this.Context.LogManager.Debug(this, "Performed cached inverse action", "Action type: " + action.ActionType.ToString() + ", Wrote to object of type: " + obj.GetType().ToString() + ", Property: " + propertyName + ", Value object type: " + value.GetType().ToString()); // do not localize }
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); }
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 bool MayUpdate(object obj) { IClassMap classMap = this.Context.DomainMap.MustGetClassMap(obj.GetType()); IObjectManager om = this.Context.ObjectManager; object refObj; IList list; foreach (IPropertyMap propertyMap in classMap.GetAllPropertyMaps()) { if (!propertyMap.IsReadOnly && !propertyMap.IsSlave) { if (!(propertyMap.ReferenceType == ReferenceType.None)) { if (propertyMap.IsCollection) { list = (IList)om.GetPropertyValue(obj, propertyMap.Name); if (list != null) { foreach (object itemRefObj in list) { if (m_listCreated.Contains(itemRefObj)) { return(false); } } } } else { refObj = om.GetPropertyValue(obj, propertyMap.Name); if (refObj != null) { if (m_listCreated.Contains(refObj)) { return(false); } } } } } } return(true); }
protected virtual void SerializeInlineObjectList(XmlNode xmlObject, object obj, IClassMap classMap, IPropertyMap propertyMap, bool creating) { IObjectManager om = this.Context.ObjectManager; IListManager lm = this.Context.ListManager; IList list = (IList)om.GetPropertyValue(obj, propertyMap.Name); XmlNode xmlList = GetNode(xmlObject, propertyMap.GetDocElement()); RemoveAllChildNodes(xmlList); foreach (object value in list) { XmlNode xmlInline = AddNode(xmlObject, "item"); SerializeInlineObjectElement(xmlObject, obj, xmlInline, value, creating); } IList orgList = lm.CloneList(obj, propertyMap, ((IList)(om.GetPropertyValue(obj, propertyMap.Name)))); om.SetOriginalPropertyValue(obj, propertyMap.Name, orgList); }
private bool ExamineWaitForNode(IObjectManager om, object delObj, object waitForObj) { IClassMap waitForObjClassMap = this.Context.DomainMap.MustGetClassMap(waitForObj.GetType()); foreach (IPropertyMap propertyMap in waitForObjClassMap.GetAllPropertyMaps()) { if (!propertyMap.IsReadOnly && !propertyMap.IsSlave) { if (propertyMap.ReferenceType != ReferenceType.None) { if (propertyMap.IsCollection) { IList list = (IList)om.GetPropertyValue(waitForObj, propertyMap.Name); if (list != null) { foreach (object itemRefObj in list) { if (itemRefObj == delObj) { return(false); } } } } else { object refObj = om.GetPropertyValue(waitForObj, propertyMap.Name); if (refObj != null) { if (refObj == delObj) { return(false); } } } } } } return(true); }
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); }
public void Refresh(bool setup) { SetText(); if (isClipBoardItem) { return; } int i = 1; IObjectManager om = this.context.ObjectManager; foreach (IPropertyMap propertyMap in TypeClassMap.GetAllPropertyMaps()) { if (propertyMap.ReferenceType == ReferenceType.None) { if (propertyMap.IsCollection == false) { if (this.context.GetNullValueStatus(this.obj, propertyMap.Name)) { if (setup) { this.SubItems.Add("<Null>"); } else { this.SubItems[i].Text = "<Null>"; } } else { string text = "<Null>"; object value = om.GetPropertyValue(this.obj, propertyMap.Name); if (value != null) { text = value.ToString(); } if (setup) { this.SubItems.Add(text); } else { this.SubItems[i].Text = text; } } i++; } } } }
protected virtual void DeserializeInlineReferenceList(object obj, IClassMap classMap, IPropertyMap propertyMap, XmlNode xmlObject) { IObjectManager om = this.Context.ObjectManager; IListManager lm = this.Context.ListManager; XmlNode xmlList = xmlObject.SelectSingleNode(propertyMap.GetDocElement()); IList list = (IList)om.GetPropertyValue(obj, propertyMap.Name); if (list == null) { list = lm.CreateList(obj, propertyMap); om.SetPropertyValue(obj, propertyMap.Name, list); IList cloneList = lm.CloneList(obj, propertyMap, list); om.SetOriginalPropertyValue(obj, propertyMap.Name, cloneList); } IInterceptableList mList; bool stackMute = false; mList = list as IInterceptableList; if (mList != null) { stackMute = mList.MuteNotify; mList.MuteNotify = true; } foreach (XmlNode xmlItem in xmlList.SelectNodes("item")) { object value = ManageInlineReference(xmlObject, obj, xmlItem);; if (value != null) { list.Add(value); } } if (mList != null) { mList.MuteNotify = stackMute; } om.SetNullValueStatus(obj, propertyMap.Name, false); IList clone = lm.CloneList(obj, propertyMap, list); om.SetOriginalPropertyValue(obj, propertyMap.Name, clone); }
private void NullifyReferenceInInsertedObject(object obj, object refering, IPropertyMap refPropMap, IObjectManager om) { bool stackMute = false; IInterceptableList mList; IList refList; object thisObj; if (refPropMap.IsCollection) { if (!(this.Context.GetPropertyStatus(refering, refPropMap.Name) == PropertyStatus.NotLoaded)) { refList = ((IList)(om.GetPropertyValue(refering, refPropMap.Name))); if (refList.Contains(obj)) { mList = refList as IInterceptableList; if (mList != null) { stackMute = mList.MuteNotify; mList.MuteNotify = true; } refList.Remove(obj); if (mList != null) { mList.MuteNotify = stackMute; } om.SetUpdatedStatus(refering, refPropMap.Name, true); } } } else { if (!(this.Context.GetPropertyStatus(refering, refPropMap.Name) == PropertyStatus.NotLoaded)) { thisObj = this.Context.ObjectManager.GetPropertyValue(refering, refPropMap.Name); if (thisObj != null) { if (thisObj == obj) { om.SetPropertyValue(refering, refPropMap.Name, null); om.SetUpdatedStatus(refering, refPropMap.Name, true); } } } } }
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 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); } } }
public void CheckPartiallyLoadedList(string propertyName, ITransaction transaction) { IList partialList = GetPartiallyLoadedList(propertyName, transaction); int count = GetCount(propertyName, transaction); if (count == partialList.Count) { IContext context = ((IInterceptable)this.target).GetInterceptor().Context; IObjectManager om = context.ObjectManager; IInterceptableList iList = om.GetPropertyValue(target, propertyName) as IInterceptableList; if (iList == null) { throw new NPersistException(string.Format("Object of type {0} and identity {1} does not have an interceptable list injected in property {2}", target.GetType(), om.GetObjectIdentity(target), propertyName)); } bool stackMute = iList.MuteNotify; iList.MuteNotify = true; if (iList.Count < 1) { IList orgList = new ArrayList(); foreach (object refObj in partialList) { iList.Add(refObj); orgList.Add(refObj); } iList.MuteNotify = stackMute; om.SetPropertyValue(target, propertyName, iList); om.SetOriginalPropertyValue(target, propertyName, orgList); om.SetNullValueStatus(target, propertyName, false); om.SetUpdatedStatus(target, propertyName, false); context.InverseManager.NotifyPropertyGet(target, propertyName); } else { iList.MuteNotify = stackMute; } } }
public virtual ListValueTemplate BuildListValueTemplate(object obj) { ListValueTemplate template = new ListValueTemplate(); IClassMap classMap = this.Context.DomainMap.MustGetClassMap(obj.GetType()); IList value; IObjectManager om = this.Context.ObjectManager; foreach (IPropertyMap propertyMap in classMap.GetAllPropertyMaps()) { if (propertyMap.IsCollection) { value = (IList)om.GetPropertyValue(obj, propertyMap.Name); if (value == null) { template.PropertyMaps.Add(propertyMap); //om.SetPropertyValue(obj, propertyMap.Name, CreateList(obj, propertyMap)); //list should /not/ have original value or it will not be considered NotLoaded //om.SetOriginalPropertyValue(obj, propertyMap.Name, CreateList(obj, propertyMap)); } } } listValueTemplateCache[obj.GetType()] = template; return(template); }
protected virtual void EvaluatePropertySpecialBehavior(object obj, IPropertyMap propertyMap, PropertySpecialBehaviorType specialBehavior, IObjectManager om) { bool wrote = false; if (specialBehavior == PropertySpecialBehaviorType.Increase) { om.SetPropertyValue(obj, propertyMap.Name, Convert.ToInt64(om.GetPropertyValue(obj, propertyMap.Name)) + 1); wrote = true; } else if (specialBehavior == PropertySpecialBehaviorType.SetDateTime) { om.SetPropertyValue(obj, propertyMap.Name, DateTime.Now); wrote = true; } if (wrote) { om.SetNullValueStatus(obj, propertyMap.Name, false); om.SetUpdatedStatus(obj, propertyMap.Name, true); } }
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); } } } } }
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)); } } }
//[DebuggerStepThrough()] protected virtual void DoNotifyPropertySet(object obj, string propertyName, ref object value, object oldValue, bool hasOldValue, ref bool cancel) { IContext ctx = this.Context; IObjectManager om = ctx.ObjectManager; IPersistenceEngine pe = ctx.PersistenceEngine; PropertyCancelEventArgs e; if (hasOldValue) { e = new PropertyCancelEventArgs(obj, propertyName, value, oldValue, this.Context.ObjectManager.GetNullValueStatus(obj, propertyName)); } else { e = new PropertyCancelEventArgs(obj, propertyName, value, this.Context.ObjectManager.GetPropertyValue(obj, propertyName), this.Context.ObjectManager.GetNullValueStatus(obj, propertyName)); } this.Context.EventManager.OnWritingProperty(this, e); if (e.Cancel) { cancel = true; return; } value = e.NewValue; IClassMap classMap = ctx.DomainMap.MustGetClassMap(obj.GetType()); IPropertyMap propertyMap; string prevId; string newId; propertyMap = classMap.MustGetPropertyMap(propertyName); if (propertyMap.ReferenceType != ReferenceType.None && value != null) { if (propertyMap.ReferenceType == ReferenceType.OneToMany || propertyMap.ReferenceType == ReferenceType.OneToOne) { //parent object IInterceptable ivalue = value as IInterceptable; if (ivalue == null) { throw new NPersistException(string.Format("Object is not a NPersist managed object, do not use 'new' on Entities. (Property='{0}', Owner={1})", propertyName, obj)); } else { if (ivalue.GetInterceptor().Context != this.Context) { throw new NPersistException(string.Format("Object does not belong to the same context object as the property owner. (Property='{0}', Owner={1})", propertyName, obj)); } ObjectStatus valueObjectStatus = om.GetObjectStatus(value); if (valueObjectStatus == ObjectStatus.UpForDeletion || valueObjectStatus == ObjectStatus.Deleted) { throw new DeletedObjectException(string.Format("Object has been deleted. (Object={0})", value), value); } } } else if (propertyMap.ReferenceType == ReferenceType.ManyToOne || propertyMap.ReferenceType == ReferenceType.ManyToMany) { IInterceptableList ivalue = value as IInterceptableList; if (ivalue == null) { throw new NPersistException(string.Format("List is not a NPersist managed list, do not use 'new' to initialize lists, NPersist does this for you. (Property='{0}', Owner={1})", propertyName, obj)); } else if (ivalue.Interceptable.GetInterceptor().Context != this.Context) { throw new NPersistException(string.Format("List does not belong to the same context object as the property owner. (Property='{0}', Owner={1})", propertyName, obj)); } } } if (propertyMap.IsReadOnly) { //Let read-only inverse properties through if (!(propertyMap.ReferenceType != ReferenceType.None && propertyMap.Inverse.Length > 0 && propertyMap.NoInverseManagement == false)) { //Special - if someone forgot to make their ManyOne read-only, //why bug them about it? (so don't add an "else" with an exception...) if (propertyMap.ReferenceType != ReferenceType.ManyToOne) { throw new ReadOnlyException("Property '" + classMap.Name + "." + propertyName + "' is read-only!"); // do not localize } } } PropertyStatus propStatus = PropertyStatus.Clean; ObjectStatus objStatus = om.GetObjectStatus(obj); bool hasPropertyStatus = false; if (objStatus == ObjectStatus.Deleted) { throw new DeletedObjectException("The object has been deleted!", obj, propertyName); // do not localize } else if (objStatus == ObjectStatus.UpForDeletion) { throw new DeletedObjectException("The object has been registered as up for deletion!", obj, propertyName); // do not localize } this.Context.ObjectCloner.EnsureIsClonedIfEditing(obj); if (objStatus == ObjectStatus.UpForCreation) { } else if (objStatus == ObjectStatus.Clean) { propStatus = om.GetPropertyStatus(obj, propertyName); if (propStatus == PropertyStatus.NotLoaded) { pe.LoadProperty(obj, propertyName); } if (!(hasOldValue)) { if (!(om.ComparePropertyValues(obj, propertyName, value, om.GetPropertyValue(obj, propertyName)))) { this.Context.UnitOfWork.RegisterDirty(obj); } } } else if (objStatus == ObjectStatus.NotLoaded) { propertyMap = this.Context.DomainMap.MustGetClassMap(obj.GetType()).MustGetPropertyMap(propertyName); if (!(propertyMap.IsIdentity)) { propStatus = this.Context.ObjectManager.GetPropertyStatus(obj, propertyName); hasPropertyStatus = true; //it would be sweet to be able to determine beforehand if this property would be part of the span //that is loaded with LoadObject and only call LoadObject if that is the case.... if (propStatus == PropertyStatus.NotLoaded) { hasPropertyStatus = false; //this.Context.PersistenceEngine.LoadObject(ref obj); this.Context.IdentityMap.LoadObject(ref obj, true); if (obj == null) { throw new ObjectNotFoundException("Object not found!"); // do not localize } } if (!hasPropertyStatus) { propStatus = om.GetPropertyStatus(obj, propertyName); } if (propStatus == PropertyStatus.NotLoaded) { pe.LoadProperty(obj, propertyName); } } if (!(hasOldValue)) { if (!(om.ComparePropertyValues(obj, propertyName, value, om.GetPropertyValue(obj, propertyName)))) { this.Context.UnitOfWork.RegisterDirty(obj); } } } else if (objStatus == ObjectStatus.Dirty) { propStatus = om.GetPropertyStatus(obj, propertyName); if (propStatus == PropertyStatus.NotLoaded) { pe.LoadProperty(obj, propertyName); } } if (propertyMap.IsIdentity) { prevId = om.GetObjectIdentity(obj); newId = om.GetObjectIdentity(obj, propertyMap, value); if (prevId != newId) { ctx.IdentityMap.UpdateIdentity(obj, prevId, newId); } } om.SetNullValueStatus(obj, propertyName, false); om.SetUpdatedStatus(obj, propertyName, true); if (hasOldValue) { ctx.InverseManager.NotifyPropertySet(obj, propertyName, value, oldValue); ctx.UnitOfWork.RegisterDirty(obj); } else { ctx.InverseManager.NotifyPropertySet(obj, propertyName, 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 DeserializeClone(object obj, ReadOnlyClone clone) { 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; foreach (IPropertyMap propertyMap in classMap.GetAllPropertyMaps()) { if (propertyMap.ReferenceType == ReferenceType.None) { if (propertyMap.IsCollection) { IList values = (IList)clone.PropertyValues[propertyMap.Name]; IList list = (IList)om.GetPropertyValue(obj, propertyMap.Name); bool stackMute = false; IInterceptableList mList = list as IInterceptableList; if (mList != null) { stackMute = mList.MuteNotify; mList.MuteNotify = true; } list.Clear(); foreach (object value in values) { list.Add(value); } IList cloneList = new ArrayList(list); if (mList != null) { mList.MuteNotify = stackMute; } om.SetOriginalPropertyValue(obj, propertyMap.Name, cloneList); om.SetNullValueStatus(obj, propertyMap.Name, false); } else { object value = clone.PropertyValues[propertyMap.Name]; om.SetPropertyValue(obj, propertyMap.Name, value); om.SetOriginalPropertyValue(obj, propertyMap.Name, value); om.SetNullValueStatus(obj, propertyMap.Name, (bool)clone.NullValueStatuses[propertyMap.Name]); } } else { IClassMap refClassMap = propertyMap.MustGetReferencedClassMap(); if (refClassMap.IsReadOnly) { if (propertyMap.IsCollection) { IList values = (IList)clone.PropertyValues[propertyMap.Name]; IList list = (IList)om.GetPropertyValue(obj, propertyMap.Name); bool stackMute = false; IInterceptableList mList = list as IInterceptableList; if (mList != null) { stackMute = mList.MuteNotify; mList.MuteNotify = true; } list.Clear(); foreach (SerializedReference refId in values) { object value = null; if (refId != null) { refClassMap = dm.MustGetClassMap(refId.Type); Type refType = am.GetTypeFromClassMap(refClassMap); value = this.Context.GetObjectById(refId.Identity, refType, true); list.Add(value); } } IList cloneList = new ArrayList(list); if (mList != null) { mList.MuteNotify = stackMute; } om.SetOriginalPropertyValue(obj, propertyMap.Name, cloneList); om.SetNullValueStatus(obj, propertyMap.Name, false); } else { object value = null; SerializedReference refId = (SerializedReference)clone.PropertyValues[propertyMap.Name]; if (refId != null) { refClassMap = dm.MustGetClassMap(refId.Type); Type refType = am.GetTypeFromClassMap(refClassMap); value = this.Context.GetObjectById(refId.Identity, refType, true); } om.SetPropertyValue(obj, propertyMap.Name, value); om.SetOriginalPropertyValue(obj, propertyMap.Name, value); om.SetNullValueStatus(obj, propertyMap.Name, (bool)clone.NullValueStatuses[propertyMap.Name]); } } } } }
public object LoadProperty(object obj, string propertyName, string domainKey) { if (useCompression && this.compressor != null) { obj = this.compressor.Decompress((string)obj); //propertyName = this.compressor.Decompress(propertyName); //domainKey = this.compressor.Decompress(domainKey); } IContext ctx = contextFactory.GetContext(domainKey); IObjectManager om = ctx.ObjectManager; IMarshalingTransformer transformer = new MarshalingTransformer(ctx); MarshalReference mr = (MarshalReference)formater.Deserialize(obj, typeof(MarshalReference)); string identity = transformer.GetIdentity(mr, mr.Value); IClassMap classMap = ctx.DomainMap.MustGetClassMap(mr.Type); IPropertyMap propertyMap = classMap.MustGetPropertyMap(propertyName); Type realType = ctx.AssemblyManager.MustGetTypeFromClassMap(classMap); //object realObj = ctx.GetObjectById(identity, realType); object realObj = ctx.GetObjectById(identity, realType, true); ctx.LoadProperty(realObj, propertyName); object serialized = null; if (propertyMap.IsCollection) { if (propertyMap.ReferenceType == ReferenceType.None) { // IList list = (IList) om.GetPropertyValue(realObj, propertyName); // MarshalList ml = transformer.FromList(list) ; // serialized = formater.Serialize(ml); } else { //TODO: fix transformer.FromReferenceList //Even better: Add MarshalProperty.Object, OriginalObject, List, OriginalList, ReferenceList and OriginalReferenceList! IList list = (IList)om.GetPropertyValue(realObj, propertyName); MarshalObjectList mol = transformer.FromObjectList(list); serialized = formater.Serialize(mol); } } else { if (propertyMap.ReferenceType == ReferenceType.None) { MarshalProperty mp = transformer.FromProperty(obj, propertyMap); serialized = formater.Serialize(mp); } else { object value = om.GetPropertyValue(realObj, propertyName); if (value != null) { ObjectStatus objectStatus = ctx.GetObjectStatus(value); if (objectStatus == ObjectStatus.NotLoaded) { ctx.PersistenceEngine.LoadObject(ref value); } if (value != null) { MarshalObject mo = transformer.FromObject(value); serialized = formater.Serialize(mo); } } } } if (serialized == null) { serialized = ""; } ctx.Dispose(); if (useCompression && this.compressor != null) { return(this.compressor.Compress((string)serialized)); } else { return(serialized); } }
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); } } } } }
private void NullifyInverseReference(IPropertyMap propertyMap, object obj, IPropertyMap invPropertyMap, IObjectManager om) { bool stackMute = false; IInterceptableList mList; IList refList; IList list; object thisObj; object refObj; //Ensure that the property is loaded if (this.Context.GetPropertyStatus(obj, propertyMap.Name) == PropertyStatus.NotLoaded) { this.Context.LoadProperty(obj, propertyMap.Name); } if (propertyMap.IsCollection) { list = (IList) om.GetPropertyValue(obj, propertyMap.Name); if (list == null) { list = this.Context.ListManager.CreateList(obj, propertyMap); } if (list != null) { if (invPropertyMap.IsCollection) { foreach (object itemRefObj in list) { ObjectStatus refObjStatus = om.GetObjectStatus(itemRefObj); if (!(refObjStatus.Equals(ObjectStatus.UpForDeletion) && refObjStatus.Equals(ObjectStatus.Deleted))) { //Ensure inverse is loaded PropertyStatus invPropertyStatus = this.Context.GetPropertyStatus(itemRefObj, invPropertyMap.Name); if (invPropertyMap.IsSlave && invPropertyStatus == PropertyStatus.NotLoaded) { AddAction(InverseActionType.Remove, itemRefObj, invPropertyMap.Name, obj, obj); } else { if (invPropertyStatus == PropertyStatus.NotLoaded) this.Context.LoadProperty(itemRefObj, invPropertyMap.Name); refList = ((IList) (om.GetPropertyValue(itemRefObj, invPropertyMap.Name))); if (refList.Contains(obj)) { mList = refList as IInterceptableList; if (mList != null) { stackMute = mList.MuteNotify; mList.MuteNotify = true; } refList.Remove(obj); if (mList != null) { mList.MuteNotify = stackMute; } om.SetUpdatedStatus(itemRefObj, invPropertyMap.Name, true); } } } } } else { foreach (object itemRefObj in list) { ObjectStatus refObjStatus = om.GetObjectStatus(itemRefObj); if (!(refObjStatus.Equals(ObjectStatus.UpForDeletion) && refObjStatus.Equals(ObjectStatus.Deleted))) { PropertyStatus invPropertyStatus = this.Context.GetPropertyStatus(itemRefObj, invPropertyMap.Name); if (invPropertyMap.IsSlave && invPropertyStatus == PropertyStatus.NotLoaded) { AddAction(InverseActionType.Set, itemRefObj, invPropertyMap.Name, null, obj); } else { //Ensure inverse is loaded if (invPropertyStatus == PropertyStatus.NotLoaded) this.Context.LoadProperty(itemRefObj, invPropertyMap.Name); thisObj = om.GetPropertyValue(itemRefObj, invPropertyMap.Name); if (thisObj != null) { if (thisObj == obj) { om.SetPropertyValue(itemRefObj, invPropertyMap.Name, null); om.SetUpdatedStatus(itemRefObj, invPropertyMap.Name, true); } } } } } } } } else { refObj = om.GetPropertyValue(obj, propertyMap.Name); if (refObj != null) { ObjectStatus refObjStatus = om.GetObjectStatus(refObj); if (!(refObjStatus.Equals(ObjectStatus.UpForDeletion) && refObjStatus.Equals(ObjectStatus.Deleted))) { PropertyStatus invPropertyStatus = this.Context.GetPropertyStatus(refObj, invPropertyMap.Name); //Ensure inverse is loaded if (invPropertyMap.IsSlave && invPropertyStatus == PropertyStatus.NotLoaded) { if (invPropertyMap.IsCollection) { AddAction(InverseActionType.Remove, refObj, invPropertyMap.Name, obj, obj); } else { AddAction(InverseActionType.Set, refObj, invPropertyMap.Name, null, obj); } } else { if (invPropertyStatus == PropertyStatus.NotLoaded) this.Context.LoadProperty(refObj, invPropertyMap.Name); if (invPropertyMap.IsCollection) { refList = ((IList) (om.GetPropertyValue(refObj, invPropertyMap.Name))); if (refList.Contains(obj)) { mList = refList as IInterceptableList; if (mList != null) { stackMute = mList.MuteNotify; mList.MuteNotify = true; } refList.Remove(obj); if (mList != null) {mList.MuteNotify = stackMute;} om.SetUpdatedStatus(refObj, invPropertyMap.Name, true); } } else { thisObj = om.GetPropertyValue(refObj, invPropertyMap.Name); if (thisObj != null) { //only update back ref if it is actually pointing at me if (thisObj == obj) { om.SetPropertyValue(refObj, invPropertyMap.Name, null); om.SetUpdatedStatus(refObj, invPropertyMap.Name, true); } } } } } } } }
private bool ExamineWaitForNode(IObjectManager om, object delObj, object waitForObj) { IClassMap waitForObjClassMap = this.Context.DomainMap.MustGetClassMap(waitForObj.GetType()); foreach (IPropertyMap propertyMap in waitForObjClassMap.GetAllPropertyMaps()) { if (!propertyMap.IsReadOnly && !propertyMap.IsSlave) { if (propertyMap.ReferenceType != ReferenceType.None) { if (propertyMap.IsCollection) { IList list = (IList) om.GetPropertyValue(waitForObj, propertyMap.Name); if (list != null) { foreach (object itemRefObj in list) { if (itemRefObj == delObj) { return false; } } } } else { object refObj = om.GetPropertyValue(waitForObj, propertyMap.Name); if (refObj != null) { if (refObj == delObj) { return false; } } } } } } return true; }
private void MergePrimitiveProperty(IObjectManager om, object obj, IClassMap classMap, IPropertyMap propertyMap, object existing, bool forOrgValue, PropertyStatus propStatus, PropertyStatus extPropStatus, MergeBehaviorType mergeBehavior) { if (forOrgValue) { object value = om.GetPropertyValue(obj, propertyMap.Name); object extValue = om.GetPropertyValue(existing, propertyMap.Name); MergePrimitivePropertyValues(value, extValue, propStatus, extPropStatus, om, existing, classMap, propertyMap, obj, forOrgValue, mergeBehavior); } else { object orgValue = om.GetOriginalPropertyValue(obj, propertyMap.Name); object extOrgValue = om.GetOriginalPropertyValue(existing, propertyMap.Name); MergePrimitivePropertyValues(orgValue, extOrgValue, propStatus, extPropStatus, om, existing, classMap, propertyMap, obj, forOrgValue, mergeBehavior); } }
protected virtual void HandleManyManyPropertySet(object obj, IPropertyMap propertyMap, IList newList, IList oldList) { this.Context.LogManager.Info(this, "Managing inverse many-many property relationship synchronization", "Writing to object of type: " + obj.GetType().ToString() + ", Property: " + propertyMap.Name); // do not localize PropertyStatus propStatus; IPropertyMap invPropertyMap = propertyMap.GetInversePropertyMap(); if (invPropertyMap == null) { return; } ArrayList added = GetListDiff(oldList, newList); ArrayList removed = GetListDiff(newList, oldList); IObjectManager om = this.Context.ObjectManager; IList list; IInterceptableList mList; bool stackMute = false; object value; foreach (object iValue in added) { value = iValue; propStatus = om.GetPropertyStatus(value, invPropertyMap.Name); if (propStatus == PropertyStatus.NotLoaded) { value = iValue; AddAction(InverseActionType.Add, value, invPropertyMap.Name, obj, obj); } else { //we still ensure so that the object is also always loaded... om.EnsurePropertyIsLoaded(value, invPropertyMap); list = (IList)om.GetPropertyValue(value, invPropertyMap.Name); mList = list as IInterceptableList; if (mList != null) { stackMute = mList.MuteNotify; mList.MuteNotify = true; } list.Add(obj); if (mList != null) { mList.MuteNotify = stackMute; } om.SetUpdatedStatus(value, invPropertyMap.Name, true); } } foreach (object iValue in removed) { value = iValue; propStatus = om.GetPropertyStatus(value, invPropertyMap.Name); if (propStatus == PropertyStatus.NotLoaded) { value = iValue; AddAction(InverseActionType.Remove, value, invPropertyMap.Name, obj, obj); } else { //we still ensure so that the object is also always loaded... om.EnsurePropertyIsLoaded(value, invPropertyMap); list = (IList)om.GetPropertyValue(value, invPropertyMap.Name); mList = list as IInterceptableList; if (mList != null) { stackMute = mList.MuteNotify; mList.MuteNotify = true; } list.Remove(obj); if (mList != null) { mList.MuteNotify = stackMute; } om.SetUpdatedStatus(value, invPropertyMap.Name, true); } } }
private void NullifyUniReference(object obj, object refering, IPropertyMap uniRefPropertyMap, IObjectManager om) { bool stackMute = false; IInterceptableList mList; IList refList; object thisObj; if (uniRefPropertyMap.IsCollection) { if (!(this.Context.GetPropertyStatus(refering, uniRefPropertyMap.Name) == PropertyStatus.NotLoaded)) { refList = ((IList) (om.GetPropertyValue(refering, uniRefPropertyMap.Name))); if (refList.Contains(obj)) { mList = refList as IInterceptableList; if (mList != null) { stackMute = mList.MuteNotify; mList.MuteNotify = true; } refList.Remove(obj); if (mList != null) { mList.MuteNotify = stackMute; } om.SetUpdatedStatus(refering, uniRefPropertyMap.Name, true); } } } else { if (!(this.Context.GetPropertyStatus(refering, uniRefPropertyMap.Name) == PropertyStatus.NotLoaded)) { thisObj = this.Context.ObjectManager.GetPropertyValue(refering, uniRefPropertyMap.Name); if (thisObj != null) { if (thisObj == obj) { this.Context.ObjectManager.SetPropertyValue(refering, uniRefPropertyMap.Name, null); om.SetUpdatedStatus(refering, uniRefPropertyMap.Name, true); } } } } }
protected virtual bool MayInsert(object obj, bool checkAllReferences, bool weakCheck) { IClassMap classMap = this.Context.DomainMap.MustGetClassMap(obj.GetType()); IObjectManager om = this.Context.ObjectManager; IList list; object refObj; foreach (IPropertyMap propertyMap in classMap.GetAllPropertyMaps()) { if (!propertyMap.IsReadOnly && !propertyMap.IsSlave) { if (!(propertyMap.ReferenceType == ReferenceType.None)) { if (checkAllReferences || propertyMap.MustGetReferencedClassMap().HasIdAssignedBySource()) { if (propertyMap.IsCollection) { list = (IList)om.GetPropertyValue(obj, propertyMap.Name); if (list != null) { foreach (object itemRefObj in list) { if (m_listCreated.Contains(itemRefObj)) { if (weakCheck) { if (propertyMap.IsIdentity) { return(false); } } else { return(false); } } } } } else { refObj = om.GetPropertyValue(obj, propertyMap.Name); if (refObj != null) { if (m_listCreated.Contains(refObj)) { if (weakCheck) { if (propertyMap.IsIdentity) { return(false); } } else { return(false); } } } } } } } } return(true); }
protected virtual void HandleOneManyPropertySet(object obj, IPropertyMap propertyMap, object value, object oldValue) { this.Context.LogManager.Info(this, "Managing inverse one-many property relationship synchronization", "Writing to object of type: " + obj.GetType().ToString() + ", Property: " + propertyMap.Name); // do not localize PropertyStatus propStatus; IPropertyMap invPropertyMap = propertyMap.GetInversePropertyMap(); if (invPropertyMap == null) { return; } IObjectManager om = this.Context.ObjectManager; IList list; IInterceptableList mList; bool stackMute = false; if (value != null) { propStatus = om.GetPropertyStatus(value, invPropertyMap.Name); if (propStatus == PropertyStatus.NotLoaded) { AddAction(InverseActionType.Add, value, invPropertyMap.Name, obj, obj); } else { om.EnsurePropertyIsLoaded(value, invPropertyMap); list = (IList)om.GetPropertyValue(value, invPropertyMap.Name); mList = list as IInterceptableList; if (mList != null) { stackMute = mList.MuteNotify; mList.MuteNotify = true; } list.Add(obj); if (mList != null) { mList.MuteNotify = stackMute; } om.SetUpdatedStatus(value, invPropertyMap.Name, true); } } if (oldValue != null) { propStatus = om.GetPropertyStatus(oldValue, invPropertyMap.Name); if (propStatus == PropertyStatus.NotLoaded) { AddAction(InverseActionType.Remove, oldValue, invPropertyMap.Name, obj, obj); } else { om.EnsurePropertyIsLoaded(oldValue, invPropertyMap); list = (IList)om.GetPropertyValue(oldValue, invPropertyMap.Name); mList = list as IInterceptableList; if (mList != null) { stackMute = mList.MuteNotify; mList.MuteNotify = true; } list.Remove(obj); if (mList != null) { mList.MuteNotify = stackMute; } } } }
private void MergeSingleRefProperty(IObjectManager om, object obj, IClassMap classMap, IPropertyMap propertyMap, object existing, bool forOrgValue, PropertyStatus propStatus, PropertyStatus extPropStatus, MergeBehaviorType mergeBehavior) { string extOrgObjId; string refObjId; object extRefObj; object refObj; object extOrgObj; if (forOrgValue) { refObj = om.GetOriginalPropertyValue(obj, propertyMap.Name); extOrgObj = om.GetOriginalPropertyValue(existing, propertyMap.Name); } else { refObj = om.GetPropertyValue(obj, propertyMap.Name); extOrgObj = om.GetPropertyValue(existing, propertyMap.Name); } if (refObj != null && DBNull.Value.Equals(refObj) != true) { //hmmmm won't this fail if we have two objects of different classes but with the same id? //probably the type should be included (and preferably change to KeyStruct comparisons...) refObjId = om.GetObjectIdentity(refObj); extOrgObjId = ""; if (extOrgObj != null) extOrgObjId = om.GetObjectIdentity(extOrgObj); if (!refObjId.Equals(extOrgObjId)) { bool keepExisting = KeepExistingValue(refObj, extOrgObj, mergeBehavior, classMap, propertyMap, existing, obj, propStatus, extPropStatus, forOrgValue); if (keepExisting != true) { extRefObj = this.Context.GetObjectById(refObjId, refObj.GetType()); SetSingleRefPropetyValue(forOrgValue, om, existing, propertyMap, extRefObj, propStatus); } } } else { if (extOrgObj != null) { bool keepExisting = KeepExistingValue(refObj, extOrgObj, mergeBehavior, classMap, propertyMap, existing, obj, propStatus, extPropStatus, forOrgValue); if (keepExisting) { SetSingleRefPropetyValue(forOrgValue, om, existing, propertyMap, null, propStatus); } } } }
private void MergeSingleRefProperty(IObjectManager om, object obj, IClassMap classMap, IPropertyMap propertyMap, object existing, bool forOrgValue, PropertyStatus propStatus, PropertyStatus extPropStatus, MergeBehaviorType mergeBehavior) { string extOrgObjId; string refObjId; object extRefObj; object refObj; object extOrgObj; if (forOrgValue) { refObj = om.GetOriginalPropertyValue(obj, propertyMap.Name); extOrgObj = om.GetOriginalPropertyValue(existing, propertyMap.Name); } else { refObj = om.GetPropertyValue(obj, propertyMap.Name); extOrgObj = om.GetPropertyValue(existing, propertyMap.Name); } if (refObj != null && DBNull.Value.Equals(refObj) != true) { refObjId = om.GetObjectIdentity(refObj); extOrgObjId = ""; if (extOrgObj != null) extOrgObjId = om.GetObjectIdentity(extOrgObj); if (!refObjId.Equals(extOrgObjId)) { bool keepExisting = KeepExistingValue(refObj, extOrgObj, mergeBehavior, classMap, propertyMap, existing, obj, propStatus, extPropStatus, forOrgValue); if (keepExisting != true) { extRefObj = this.Context.GetObjectById(refObjId, refObj.GetType()); SetSingleRefPropetyValue(forOrgValue, om, existing, propertyMap, extRefObj, propStatus); } } } else { if (extOrgObj != null) { bool keepExisting = KeepExistingValue(refObj, extOrgObj, mergeBehavior, classMap, propertyMap, existing, obj, propStatus, extPropStatus, forOrgValue); if (keepExisting) { SetSingleRefPropetyValue(forOrgValue, om, existing, propertyMap, null, propStatus); } } } }
private void NullifyInverseReference(IPropertyMap propertyMap, object obj, IPropertyMap invPropertyMap, IObjectManager om) { bool stackMute = false; IInterceptableList mList; IList refList; IList list; object thisObj; object refObj; //Ensure that the property is loaded if (this.Context.GetPropertyStatus(obj, propertyMap.Name) == PropertyStatus.NotLoaded) { this.Context.LoadProperty(obj, propertyMap.Name); } if (propertyMap.IsCollection) { list = (IList)om.GetPropertyValue(obj, propertyMap.Name); if (list == null) { list = this.Context.ListManager.CreateList(obj, propertyMap); } if (list != null) { if (invPropertyMap.IsCollection) { foreach (object itemRefObj in list) { //Ensure inverse is loaded PropertyStatus invPropertyStatus = this.Context.GetPropertyStatus(itemRefObj, invPropertyMap.Name); if (invPropertyStatus == PropertyStatus.NotLoaded) { this.Context.LoadProperty(itemRefObj, invPropertyMap.Name); } refList = ((IList)(om.GetPropertyValue(itemRefObj, invPropertyMap.Name))); if (refList.Contains(obj)) { mList = refList as IInterceptableList; if (mList != null) { stackMute = mList.MuteNotify; mList.MuteNotify = true; } refList.Remove(obj); if (mList != null) { mList.MuteNotify = stackMute; } om.SetUpdatedStatus(itemRefObj, invPropertyMap.Name, true); } } } else { foreach (object itemRefObj in list) { //Ensure inverse is loaded PropertyStatus invPropertyStatus = this.Context.GetPropertyStatus(itemRefObj, invPropertyMap.Name); if (invPropertyStatus == PropertyStatus.NotLoaded) { this.Context.LoadProperty(itemRefObj, invPropertyMap.Name); } thisObj = om.GetPropertyValue(itemRefObj, invPropertyMap.Name); if (thisObj != null) { if (thisObj == obj) { om.SetPropertyValue(itemRefObj, invPropertyMap.Name, null); om.SetUpdatedStatus(itemRefObj, invPropertyMap.Name, true); } } } } } } else { refObj = om.GetPropertyValue(obj, propertyMap.Name); if (refObj != null) { PropertyStatus invPropertyStatus = this.Context.GetPropertyStatus(refObj, invPropertyMap.Name); //Ensure inverse is loaded if (invPropertyStatus == PropertyStatus.NotLoaded) { this.Context.LoadProperty(refObj, invPropertyMap.Name); } if (invPropertyMap.IsCollection) { refList = ((IList)(om.GetPropertyValue(refObj, invPropertyMap.Name))); if (refList.Contains(obj)) { mList = refList as IInterceptableList; if (mList != null) { stackMute = mList.MuteNotify; mList.MuteNotify = true; } refList.Remove(obj); if (mList != null) { mList.MuteNotify = stackMute; } om.SetUpdatedStatus(refObj, invPropertyMap.Name, true); } } else { //only update back ref if it is actually pointing at me thisObj = om.GetPropertyValue(refObj, invPropertyMap.Name); if (thisObj != null) { if (thisObj == obj) { om.SetPropertyValue(refObj, invPropertyMap.Name, null); om.SetUpdatedStatus(refObj, invPropertyMap.Name, true); } } } } } }
private void MergeListRefProperty(IObjectManager om, object obj, IClassMap classMap, IPropertyMap propertyMap, object existing, PropertyStatus propStatus, PropertyStatus extPropStatus, bool forOrgValue, MergeBehaviorType mergeBehavior) { IList list = ((IList) om.GetPropertyValue(obj, propertyMap.Name)); IList orgList = ((IList) om.GetPropertyValue(existing, propertyMap.Name)); MergeReferenceLists(list, orgList, om, obj, mergeBehavior, classMap, propertyMap, existing, propStatus, extPropStatus, forOrgValue); }
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)); } } }