Example #1
0
        public XmlNode SerializeToXmlNode(XmlDocument xmlDoc, MarshalProperty mp)
        {
            XmlNode      xml = xmlDoc.CreateElement("property");
            XmlAttribute xmlAttr;

            xmlAttr       = xmlDoc.CreateAttribute("name");
            xmlAttr.Value = mp.Name;
            xml.Attributes.Append(xmlAttr);

            xmlAttr       = xmlDoc.CreateAttribute("null");
            xmlAttr.Value = BoolToStr(mp.IsNull);
            xml.Attributes.Append(xmlAttr);

            xmlAttr       = xmlDoc.CreateAttribute("org-null");
            xmlAttr.Value = BoolToStr(mp.WasNull);
            xml.Attributes.Append(xmlAttr);

            xmlAttr       = xmlDoc.CreateAttribute("has-org");
            xmlAttr.Value = BoolToStr(mp.HasOriginal);
            xml.Attributes.Append(xmlAttr);

            XmlNode xmlValue = xmlDoc.CreateElement("value");

            xmlValue.InnerText = mp.Value;
            xml.AppendChild(xmlValue);

            xmlValue           = xmlDoc.CreateElement("org-value");
            xmlValue.InnerText = mp.OriginalValue;
            xml.AppendChild(xmlValue);

            return(xml);
        }
Example #2
0
        private MarshalProperty ToMarshalProperty(XmlNode xmlProp)
        {
            MarshalProperty mp = new MarshalProperty();
            XmlNode         xmlNode;

            if (!(xmlProp.Attributes["name"] == null))
            {
                mp.Name = xmlProp.Attributes["name"].Value;
            }
            if (!(xmlProp.Attributes["null"] == null))
            {
                mp.IsNull = ParseBool(xmlProp.Attributes["null"].Value);
            }
            if (!(xmlProp.Attributes["org-null"] == null))
            {
                mp.WasNull = ParseBool(xmlProp.Attributes["org-null"].Value);
            }
            if (!(xmlProp.Attributes["has-org"] == null))
            {
                mp.HasOriginal = ParseBool(xmlProp.Attributes["has-org"].Value);
            }
            xmlNode = xmlProp.SelectSingleNode("value");
            if (xmlNode != null)
            {
                mp.Value = xmlNode.InnerText;
            }
            xmlNode = xmlProp.SelectSingleNode("org-value");
            if (xmlNode != null)
            {
                mp.OriginalValue = xmlNode.InnerText;
            }
            return(mp);
        }
        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);
            }
        }
        public object CommitUnitOfWork(object obj, string domainKey)
        {
            if (useCompression && this.compressor != null)
            {
                obj = this.compressor.Decompress((string)obj);
                //domainKey = this.compressor.Decompress(domainKey);
            }

            IContext ctx = contextFactory.GetContext(domainKey);
            MarshalingTransformer transformer = new MarshalingTransformer(ctx);
            //Deserialize the uow
            MarshalUnitOfWork muow = (MarshalUnitOfWork)formater.Deserialize(obj, typeof(MarshalUnitOfWork));

            string            identity;
            string            serialized = "";
            IClassMap         classMap;
            Type              realType;
            object            ro;
            MarshalObjectList mol = new MarshalObjectList();
            Hashtable         insertedRealObjects = new Hashtable();

            ITransaction tx = null;

            try
            {
                tx = ctx.BeginTransaction();

                foreach (MarshalObject mo in muow.RemoveObjects)
                {
                    identity = transformer.GetIdentity(mo);
                    classMap = ctx.DomainMap.MustGetClassMap(mo.Type);
                    realType = ctx.AssemblyManager.MustGetTypeFromClassMap(classMap);
                    ro       = ctx.GetObjectById(identity, realType);
                    transformer.ToObject(mo, ref ro, 0, RefreshBehaviorType.OverwriteLoaded);
                    ctx.UnitOfWork.RegisterDeleted(ro);
                }

                foreach (MarshalObject mo in muow.UpdateObjects)
                {
                    identity = transformer.GetIdentity(mo);
                    classMap = ctx.DomainMap.MustGetClassMap(mo.Type);
                    realType = ctx.AssemblyManager.MustGetTypeFromClassMap(classMap);
                    ro       = ctx.GetObjectById(identity, realType);
                    transformer.ToObject(mo, ref ro, 0, RefreshBehaviorType.OverwriteLoaded);
                    ctx.UnitOfWork.RegisterDirty(ro);
                }

                foreach (MarshalObject mo in muow.InsertObjects)
                {
                    classMap = ctx.DomainMap.MustGetClassMap(mo.Type);
                    realType = ctx.AssemblyManager.MustGetTypeFromClassMap(classMap);
                    if (mo.TempId.Length > 0)
                    {
                        ro = ctx.CreateObject(realType);
                    }
                    else
                    {
                        identity = transformer.GetIdentity(mo);
                        ro       = ctx.CreateObject(identity, realType);
                    }
                    insertedRealObjects[mo] = ro;
                    transformer.ToObject(mo, ref ro, 0, RefreshBehaviorType.OverwriteDirty);
                }

                //Commit transaction
                tx.Commit();
            }
            catch (Exception ex)
            {
                if (tx != null)
                {
                    tx.Rollback();
                }
                ctx.Dispose();
                throw ex;
            }

            foreach (MarshalObject mo in muow.InsertObjects)
            {
                classMap = ctx.DomainMap.MustGetClassMap(mo.Type);
                if (classMap.HasAssignedBySource())
                {
                    foreach (IPropertyMap propertyMap in classMap.GetAllPropertyMaps())
                    {
                        if (propertyMap.GetIsAssignedBySource())
                        {
                            MarshalProperty mp = mo.GetProperty(propertyMap.Name);
                            if (mp == null)
                            {
                                mp      = new MarshalProperty();
                                mp.Name = propertyMap.Name;
                                mo.Properties.Add(mp);
                            }
                            ro       = insertedRealObjects[mo];
                            mp.Value = transformer.FromPropertyValue(ro, ctx.ObjectManager.GetPropertyValue(ro, propertyMap.Name), propertyMap);
                            mol.Objects.Add(mo);
                        }
                    }
                }
            }

            ctx.Dispose();

            serialized = (string)formater.Serialize(mol);

            if (useCompression && this.compressor != null)
            {
                return(this.compressor.Compress(serialized));
            }
            else
            {
                return(serialized);
            }
        }
        public override void LoadProperty(object obj, string propertyName)
        {
            if (this.url.Length < 1)
            {
                throw new NPersistException("You must specify an url to your NPersist Web Service in your WebServiceRemotingEngine!");
            }
            RemotingService        rs          = new RemotingService(this.Context, url);
            IMarshalingTransformer transformer = new MarshalingTransformer(Context);
            IObjectManager         om          = Context.ObjectManager;

            IClassMap        classMap    = Context.DomainMap.MustGetClassMap(obj.GetType());
            IPropertyMap     propertyMap = classMap.MustGetPropertyMap(propertyName);
            MarshalReference mr          = transformer.FromObjectAsReference(obj);
            string           xmlObject   = (string)Formatter.Serialize(mr);

            if (useCompression && this.compressor != null)
            {
                xmlObject = this.compressor.Compress(xmlObject);
            }

            bool doUseCompression = this.useCompression;

            if (this.compressor == null)
            {
                doUseCompression = false;
            }

            string result = rs.LoadProperty(xmlObject, propertyName, this.domainKey, doUseCompression);

            if (useCompression && this.compressor != null)
            {
                result = this.compressor.Decompress(result);
            }

            if (propertyMap.IsCollection)
            {
                if (propertyMap.ReferenceType == ReferenceType.None)
                {
                }
                else
                {
                    //Refresh issues!!! (the objects in the list being deserialized may exist in the cache!!)
                    MarshalObjectList mol       = (MarshalObjectList)Formatter.Deserialize(result, typeof(MarshalObjectList));
                    IList             freshList = transformer.ToObjectList(mol, RefreshBehaviorType.DefaultBehavior, new ArrayList());
                    //Context.IdentityMap.RegisterLoadedObject(obj);
                    IList orgList = (IList)om.GetPropertyValue(obj, propertyName);

                    LoadReferenceList(freshList, orgList, om, obj, propertyMap);
                    this.Context.InverseManager.NotifyPropertyLoad(obj, propertyMap, orgList);
                }
            }
            else
            {
                if (propertyMap.ReferenceType == ReferenceType.None)
                {
                    MarshalProperty mp = (MarshalProperty)Formatter.Deserialize(result, typeof(MarshalProperty));
                    transformer.ToProperty(obj, mp, propertyMap, RefreshBehaviorType.DefaultBehavior);
                }
                else
                {
                    if (result.Length > 0)
                    {
                        MarshalObject mo          = (MarshalObject)Formatter.Deserialize(result, typeof(MarshalObject));
                        string        identity    = transformer.GetIdentity(mo);
                        IClassMap     refClassMap = Context.DomainMap.MustGetClassMap(mo.Type);
                        Type          refType     = Context.AssemblyManager.MustGetTypeFromClassMap(refClassMap);

                        object refObject = Context.GetObjectById(identity, refType, true);
                        if (om.GetObjectStatus(refObject) == ObjectStatus.NotLoaded)
                        {
                            transformer.ToObject(mo, ref refObject);
                        }

//						object refObject = Context.TryGetObjectById(identity, refType, true);
//						if (refObject == null)
//						{
//							refObject = Context.GetObjectById(identity, refType, true);
//							transformer.ToObject(mo, ref refObject);
//						}
                        om.SetPropertyValue(obj, propertyName, refObject);
                        om.SetOriginalPropertyValue(obj, propertyName, refObject);
                        om.SetNullValueStatus(obj, propertyName, false);
                        this.Context.InverseManager.NotifyPropertyLoad(obj, propertyMap, refObject);
                    }
                    else
                    {
                        om.SetPropertyValue(obj, propertyName, null);
                        om.SetOriginalPropertyValue(obj, propertyName, null);
                        om.SetNullValueStatus(obj, propertyName, true);
                    }
                }
            }
        }