Example #1
0
 public static T GetObject(string id)
 {
     if (id == null)
     {
         return(null);
     }
     if (UnifiedSystem.UseOmniBase)
     {
         lock (UnifiedSystem.OmniBase)
             if (UnifiedSystem.OmniBase.ContainsKey(id))
             {
                 IUnifiedIMObject obj = UnifiedSystem.OmniBase[id];
                 if (obj is T)
                 {
                     return((T)obj);
                 }
                 else
                 {
                     return(null);
                 }
             }
             else
             {
                 return(null);
             }
     }
     else
     {
         return(Database.FirstOrDefault(x => x.ObjectID == id));
     }
 }
        internal static void DeleteReferences(IUnifiedIMObject obj)
        {
            Type t = obj.GetType();

            foreach (UnifiedIMReference rf in References)
            {
                DeleteReference(obj, rf);
            }
        }
 //Internals
 internal static void FixIndex(IUnifiedIMObject obj, Type t, string property, object oldValue)
 {
     if (Indexes.ContainsKey(t))
     {
         ObjectIndexer <IUnifiedIMObject> pIndexes = Indexes[t];
         object cVal = Property.Get(obj, property) ?? null;
         pIndexes.RemoveIndex(property, oldValue, obj);
         pIndexes.AddIndex(property, cVal, obj);
     }
 }
 internal static void DeleteIndexes(IUnifiedIMObject obj)
 {
     if (Indexes.ContainsKey(obj.DataType))
     {
         ObjectIndexer <IUnifiedIMObject> pIndexes = Indexes[obj.DataType];
         foreach (string prop in pIndexes.Properties)
         {
             object cVal = Property.Get(obj, prop) ?? null;
             pIndexes.RemoveIndex(prop, cVal, obj);
         }
     }
 }
 internal static void ResolveReferences(IUnifiedIMObject obj, Type type = null)
 {
     if (type == null)
     {
         type = obj.GetType();
     }
     if (TypeReferences.ContainsKey(type))
     {
         foreach (UnifiedIMReference reference in TypeReferences[type])
         {
             ResolveReference(obj, reference, type);
         }
     }
 }
 internal static void HandleObjectDeletion <T>(IUnifiedIMObject obj)
 {
     if (UnifiedSystem.AllowReferences)
     {
         UnifiedSystem.DeleteReferences(obj);
     }
     if (UnifiedSystem.UseOmniBase)
     {
         lock (UnifiedSystem.OmniBase)
             UnifiedSystem.OmniBase.Remove(obj.ObjectID);
     }
     if (UnifiedSystem.AllowIndexes)
     {
         UnifiedSystem.DeleteIndexes(obj);
     }
 }
        internal static void UpdateReference(UnifiedIMReference reference, IUnifiedIMObject host, IUnifiedIMObject target)
        {
            object hVal = reference.GetHostProperty(host);
            object tVal = reference.GetTargetProperty(target);

            if (Matches(reference, hVal, tVal))
            {
                if (typeof(IList).IsAssignableFrom(reference.SetPropertyType))
                {
                    IList list = (IList)reference.GetReferenceProperty(host);
                    if (list == null)
                    {
                        throw new ArgumentException("Reference Lists should be initiated with an instance. And never be null");
                    }
                    //if (!list.Contains(target))
                    list.Add(target);
                }
                else
                {
                    reference.SetReferenceProperty(host, target);
                }
                target.RefTo.Add(new KeyValuePair <UnifiedIMReference, IUnifiedIMObject>(reference, host));
            }
            else
            {
                if (typeof(IList).IsAssignableFrom(reference.SetPropertyType))
                {
                    IList list = (IList)reference.GetReferenceProperty(host);
                    if (list == null)
                    {
                        throw new ArgumentException("Reference Lists should be initiated with an instance. And never be null");
                    }
                    //if (list.Contains(target))
                    list.Remove(target);
                }
                else
                {
                    object curVal = reference.GetReferenceProperty(host);
                    if (curVal == target)
                    {
                        reference.SetReferenceProperty(host, null);
                    }
                }
                target.RefTo.RemoveAll(x => x.Key == reference && x.Value == host);
            }
        }
        internal static void HandleObjectChange <T>(IUnifiedIMObject obj)
        {
            string key = obj.ObjectID;
            Type   t   = typeof(T);


            if (UnifiedSystem.AllowReferences || UnifiedSystem.AllowIndexes)
            {
                //Loop over Changes
                foreach (KeyValuePair <string, UIMPropertyState> changeState in obj.PropertyStates)
                {
                    object cVal = Property.Get(obj, changeState.Key) ?? null;
                    changeState.Value.HasChangedAndUpdate(cVal, (oldVal, newVal) =>
                    {
                        //Index Updating
                        if (AllowIndexes)
                        {
                            FixIndex(obj, t, changeState.Key, changeState.Value.LastState);
                        }

                        //Reference Updating
                        if (AllowReferences)
                        {
                            if (HostReferences.ContainsKey(t))
                            {
                                UnifiedIMReference hRefs = HostReferences[t].FirstOrDefault(x => x.HostProperty == changeState.Key);
                                if (hRefs != null)
                                {
                                    DeleteReference(obj, hRefs);
                                    ResolveReference(obj, hRefs, t);
                                }
                            }
                            if (TargetReferences.ContainsKey(t))
                            {
                                List <UnifiedIMReference> tRefs = TargetReferences[t].Where(x => x.TargetProperty == changeState.Key).ToList();
                                foreach (UnifiedIMReference reff in tRefs)
                                {
                                    DeleteReference(obj, reff);
                                    ResolveReference(obj, reff);
                                }
                            }
                        }
                    });
                }
            }
        }
 internal static void DeleteReference(IUnifiedIMObject obj, UnifiedIMReference rf)
 {
     lock (obj.RefTo)
     {
         if (rf.TargetType == obj.DataType)
         {
             foreach (KeyValuePair <UnifiedIMReference, IUnifiedIMObject> o in obj.RefTo)
             {
                 if (o.Value.DataType == rf.HostType)
                 {
                     object sProperty = rf.GetReferenceProperty(o.Value);
                     if (typeof(IList).IsAssignableFrom(rf.SetPropertyType))
                     {
                         ((IList)sProperty).Remove(obj);
                     }
                     else if (sProperty == obj)
                     {
                         rf.SetReferenceProperty(o.Value, null);
                     }
                 }
             }
             obj.RefTo.RemoveAll(x => x.Key == rf);
         }
         else if (rf.HostType == obj.DataType)
         {
             object sProperty = rf.GetReferenceProperty(obj);
             if (typeof(IList).IsAssignableFrom(rf.SetPropertyType))
             {
                 foreach (object o in ((IList)sProperty))
                 {
                     ((IUnifiedIMObject)o).RefTo.RemoveAll(x => x.Key == rf && x.Value == obj);
                 }
                 ((IList)sProperty).Clear();
             }
             else if (sProperty == obj)
             {
                 rf.SetReferenceProperty(obj, null);
                 ((IUnifiedIMObject)sProperty).RefTo.RemoveAll(x => x.Key == rf && x.Value == obj);
             }
         }
     }
 }
        internal static void ResolveReference(IUnifiedIMObject obj, UnifiedIMReference reference, Type type = null)
        {
            if (type == null)
            {
                type = obj.GetType();
            }

            if (reference.TargetType == type)
            {
                if (UseOmniBase && reference.HostProperty == "ObjectID")
                {
                    string od = (string)reference.GetTargetProperty(obj);
                    if (od != null)
                    {
                        bool             hasKey  = false;
                        IUnifiedIMObject omniObj = null;
                        lock (UnifiedSystem.OmniBase)
                        {
                            hasKey = UnifiedSystem.OmniBase.ContainsKey(od);
                            if (hasKey)
                            {
                                omniObj = OmniBase[od];
                            }
                        }
                        if (hasKey)
                        {
                            UpdateReference(reference, omniObj, obj);
                        }
                    }
                }
                else
                {
                    if (AllowIndexes && Indexes.ContainsKey(reference.HostType) && Indexes[reference.HostType].HasTypeProperty(reference.HostProperty))
                    {
                        List <IUnifiedIMObject> objs = Indexes[reference.HostType].GetIndex(reference.HostProperty, reference.GetTargetProperty(obj));
                        foreach (object o in objs)
                        {
                            UpdateReference(reference, (IUnifiedIMObject)o, obj);
                        }
                    }
                    else
                    if (_databaseGetters.ContainsKey(reference.HostType))
                    {
                        foreach (object o in _databaseGetters[reference.HostType])
                        {
                            UpdateReference(reference, (IUnifiedIMObject)o, obj);
                        }
                    }
                }
            }
            else if (reference.HostType == type)
            {
                if (UseOmniBase && reference.TargetProperty == "ObjectID")
                {
                    string od = (string)reference.GetHostProperty(obj);
                    if (od != null)
                    {
                        bool             hasKey  = false;
                        IUnifiedIMObject omniObj = null;
                        lock (UnifiedSystem.OmniBase)
                        {
                            hasKey = UnifiedSystem.OmniBase.ContainsKey(od);
                            if (hasKey)
                            {
                                omniObj = OmniBase[od];
                            }
                        }
                        if (hasKey)
                        {
                            UpdateReference(reference, obj, omniObj);
                        }
                    }
                }
                else
                {
                    if (AllowIndexes && Indexes.ContainsKey(reference.TargetType) && Indexes[reference.TargetType].HasTypeProperty(reference.TargetProperty))
                    {
                        List <IUnifiedIMObject> objs = Indexes[reference.TargetType].GetIndex(reference.TargetProperty, reference.GetHostProperty(obj));
                        foreach (object o in objs)
                        {
                            UpdateReference(reference, obj, (IUnifiedIMObject)o);
                        }
                    }
                    else if (_databaseGetters.ContainsKey(reference.TargetType))
                    {
                        foreach (object o in _databaseGetters[reference.TargetType])
                        {
                            UpdateReference(reference, obj, (IUnifiedIMObject)o);
                        }
                    }
                }
            }
        }
        internal static void HandleObjectCreation <T>(IUnifiedIMObject obj)
        {
            if (UnifiedSystem.AllowReferences)
            {
                UnifiedSystem.ResolveReferences(obj, typeof(T));
            }
            if (UnifiedSystem.UseOmniBase)
            {
                lock (UnifiedSystem.OmniBase)
                    UnifiedSystem.OmniBase.Add(obj.ObjectID, obj);
            }

            List <string> indexesToAdd = new List <string>();

            if (UnifiedSystem.AllowReferences)
            {
                List <UnifiedIMReference> refs = new List <UnifiedIMReference>();
                if (TypeReferences.ContainsKey(typeof(T)))
                {
                    refs = TypeReferences[typeof(T)];
                }

                /*
                 * ObjectIndex<IUnifiedIMObject> oi = null;
                 * if (CreateReferenceIndexes)
                 *  if (Indexes.ContainsKey(typeof(T)))
                 *      oi = Indexes[typeof(T)];*/

                foreach (UnifiedIMReference reff in refs)
                {
                    if (reff.HostType == typeof(T))
                    {
                        //object value = reff.GetHostProperty(obj);
                        //if(!obj.PropertyStates.ContainsKey(reff.HostProperty))
                        //obj.PropertyStates.Add(reff.HostProperty, new UIMPropertyState(value));
                        if (CreateReferenceIndexes && reff.HostProperty != "ObjectID")
                        {
                            // oi.AddIndex(reff.HostProperty, value, obj);
                            indexesToAdd.Add(reff.HostProperty);
                        }
                    }

                    else if (reff.TargetType == typeof(T))
                    {
                        //object value = reff.GetTargetProperty(obj);
                        //if (!obj.PropertyStates.ContainsKey(reff.TargetProperty))
                        //    obj.PropertyStates.Add(reff.TargetProperty, new UIMPropertyState(value));
                        if (CreateReferenceIndexes && reff.TargetProperty != "ObjectID")
                        {
                            //oi.AddIndex(reff.TargetProperty, value, obj);
                            indexesToAdd.Add(reff.TargetProperty);
                        }
                    }
                }
            }

            if (AllowIndexes)
            {
                if (IndexProperties.ContainsKey(typeof(T)))
                {
                    indexesToAdd.AddRange(IndexProperties[typeof(T)]);
                }

                ObjectIndexer <IUnifiedIMObject> oi = null;
                if (Indexes.ContainsKey(typeof(T)))
                {
                    oi = Indexes[typeof(T)];
                }

                if (oi != null)
                {
                    foreach (string idx in indexesToAdd.Distinct())
                    {
                        object value = Property.Get(obj, idx);
                        if (!obj.PropertyStates.ContainsKey(idx))
                        {
                            obj.PropertyStates.Add(idx, new UIMPropertyState(value));
                        }
                        oi.AddIndex(idx, value, obj);
                    }
                }
            }
        }