Ejemplo n.º 1
0
        /// <summary>
        /// Gets or sets the <see cref="ServerObject">ServerObject</see> that is a Parent object.
        /// </summary>
        /// <remarks>
        /// On object access if a parent exists but has not be retrieved by ObjectServer then it will be retrieved and returned.
        /// Otherwise the existing object will be returned. If you wish a null value to be set the assign null to the parent.
        /// When an object is assigned as a parent, ObjectServer will locate the correct children collection and automatically append the current object.
        /// </remarks>
        /// <param name="key">The name of the property in the <see cref="ServerObject">ServerObject</see> which exposes this Parent.</param>
        /// <value>A <see cref="ServerObject">ServerObject</see> that is a Parent object or if no parent exists null.</value>
        public ServerObject this[string key]
        {
            get
            {
                if (data[key] == null)
                {
                    Trace.WriteLineIf(DebugOutput.Enabled, "Parent not found, loading");

                    TypeSchema   schema       = SchemaCache.Current.GetSchema(obj.ServerObjectType);
                    ParentSchema parentSchema = schema.FindParentSchema(key);

                    ServerObject parent = obj.Transaction.Select(parentSchema.Property.PropertyType, obj.Data.GetValue(key));

                    data[key] = parent;
                    return(parent);
                }
                else if (Convert.IsDBNull(data[key]))
                {
                    Trace.WriteLineIf(DebugOutput.Enabled, "Key Value is DBNull.Value returning null");
                    return(null);
                }

                return(data[key] as ServerObject);
            }
            set
            {
                if (value == null)
                {
                    Trace.WriteLineIf(DebugOutput.Enabled, "Parent set to null, setting key value to DBNull.Value");

                    obj.Data.SetValue(key, DBNull.Value);
                    data[key] = DBNull.Value;

                    UpdateState(key);
                }
                else
                {
                    if (!value.Equals(data[key]))
                    {
                        Trace.WriteLineIf(DebugOutput.Enabled, "New Parent set");

                        ServerObject newParent = value as ServerObject;

                        ServerObject oldParent = data[key] != null ? data[key] as ServerObject : null;

                        TypeSchema schema    = SchemaCache.Current.GetSchema(newParent.ServerObjectType);
                        object     parentKey = newParent.Data.GetValue(schema.PrimaryKey.Property.Name);

                        obj.Data.SetValue(key, parentKey);

                        data[key] = newParent;
                        UpdateState(key);

                        foreach (ChildrenSchema childSchema in schema.ChildrenSchemas)
                        {
                            if (childSchema.PropertyName == key && childSchema.ChildType == obj.ServerObjectType)
                            {
                                if (oldParent != null)
                                {
                                    Trace.WriteLineIf(DebugOutput.Enabled, "Removing from children collection " + childSchema.Property.Name);
                                    ServerObjectCollection oldChildren = oldParent.Children.GetValue(childSchema.Property.Name);
                                    if (oldChildren != null && oldChildren.Contains(obj))
                                    {
                                        oldChildren.Remove(obj);
                                    }
                                }

                                Trace.WriteLineIf(DebugOutput.Enabled, "Adding to children collection " + childSchema.Property.Name);
                                ServerObjectCollection newChildren = newParent.Children.GetValue(childSchema.Property.Name);
                                if (newChildren != null && !newChildren.Contains(obj))
                                {
                                    newChildren.Add(obj);
                                }

                                break;
                            }
                        }
                    }
                }
            }
        }
        internal ServerObjectCollection SelectChildren(ServerObject obj, string propertyName)
        {
            Trace.WriteLineIf(DebugOutput.Enabled, "Selecting children for " + propertyName);

            TypeSchema     schema         = SchemaCache.Current.GetSchema(obj.ServerObjectType);
            ChildrenSchema childrenSchema = schema.FindChildrenSchema(propertyName);

            ServerObjectCollection children = null;

            object primaryKey = obj.Data.GetValue(schema.PrimaryKey.Property.Name);

            Query query = new Query(new Condition(childrenSchema.PropertyName, Expression.Equal, primaryKey));

            children = Select(childrenSchema.ChildType, query);

            Trace.WriteLineIf(DebugOutput.Enabled, "Checking cache for more items");

            foreach (ServerObject cacheItem in objectCache.GetCacheItem(childrenSchema.ChildType))
            {
                if (primaryKey.Equals(cacheItem.Data.GetValue(childrenSchema.PropertyName)) && !children.Contains(cacheItem))
                {
                    children.Add(cacheItem);
                }
            }

            ServerObjectCollection toRemove = new ServerObjectCollection();

            foreach (ServerObject child in children)
            {
                if (!primaryKey.Equals(child.Data.GetValue(childrenSchema.PropertyName)))
                {
                    toRemove.Add(child);
                }
            }

            foreach (ServerObject child in toRemove)
            {
                children.Remove(child);
            }

            return(children);
        }