public ServerObject SelectScalar(Type type, Query query)
        {
            query.Top = 1;

            ServerObjectCollection collection = Select(type, query);

            if (collection.Count != 1)
            {
                return(null);
            }

            return(collection[0]);
        }
Пример #2
0
        internal ServerObjectCollection GetValue(string key)
        {
            Trace.WriteLineIf(DebugOutput.Enabled, "Getting value for key " + key);

            ServerObjectCollection children = data[key] as ServerObjectCollection;

            if (children == null && obj.State == ObjectState.Added)
            {
                children  = new ServerObjectCollection();
                data[key] = children;
            }

            return(children);
        }
        private void CommitChildren(ServerObject obj, object key)
        {
            Trace.WriteLineIf(DebugOutput.Enabled, "Enumerating children");

            TypeSchema schema = SchemaCache.Current.GetSchema(obj.ServerObjectType);

            if (key != null)
            {
                foreach (ChildrenSchema childSchema in schema.ChildrenSchemas)
                {
                    ServerObjectCollection children = obj.Children.GetValue(childSchema.Property.Name);

                    if (children == null)
                    {
                        continue;
                    }

                    TypeSchema   childTypeSchema = SchemaCache.Current.GetSchema(childSchema.ChildType);
                    ParentSchema parentSchema    = childTypeSchema.FindParentSchema(childSchema.PropertyName);

                    foreach (ServerObject child in children)
                    {
                        if (child.Parents.GetValue(parentSchema.Property.Name) != obj)
                        {
                            continue;
                        }

                        child.Data.SetValue(parentSchema.Property.Name, key);
                    }
                }
            }

            foreach (ChildrenSchema childSchema in schema.ChildrenSchemas)
            {
                ServerObjectCollection children = obj.Children.GetValue(childSchema.Property.Name);

                if (children == null)
                {
                    continue;
                }

                foreach (ServerObject child in children)
                {
                    Commit(child, true);
                }
            }
        }
Пример #4
0
        /// <summary>
        /// Gets the child collection associated with the given property.
        /// </summary>
        /// <param name="key">The name of the property in the <see cref="ServerObject">ServerObject</see> which exposes this child collection.</param>
        /// <value>A <see cref="ServerObjectCollection">ServerObjectCollection</see> that is a child collection or if no children exists an empty collection.</value>
        public ServerObjectCollection this[string key]
        {
            get
            {
                if (data[key] == null)
                {
                    Trace.WriteLineIf(DebugOutput.Enabled, "Children not found, loading");

                    ServerObjectCollection children = obj.State == ObjectState.Added ? new ServerObjectCollection() : obj.Transaction.SelectChildren(obj, key);
                    data[key] = children;

                    return(children);
                }

                return(data[key] as ServerObjectCollection);
            }
        }
        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);
        }
        public ServerObjectCollection Select(Type type, Query query)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type", "type cannot be null.");
            }

            if (query == null)
            {
                throw new ArgumentNullException("query", "query cannot be null.");
            }

            ServerObjectCollection collection = new ServerObjectCollection();

            try
            {
                adapter.Open();

                collection = adapter.Select(type, query);
            }
            finally
            {
                adapter.Close();
            }

            for (int i = 0; i < collection.Count; i++)
            {
                collection[i].State = ObjectState.Unchanged;
                collection[i].SetTransaction(this);

                collection[i] = objectCache.Add(collection[i]);

                collection[i].PromptEvent(EventType.Initialised);
            }

            return(collection);
        }
Пример #7
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;
                            }
                        }
                    }
                }
            }
        }
        private void DeleteImpl(ServerObject obj)
        {
            if (obj.Processed)
            {
                return;
            }

            Trace.WriteLineIf(DebugOutput.Enabled, "Deleting ServerObject of type " + obj.ServerObjectType);

            switch (obj.State)
            {
            case ObjectState.Added:
                Trace.WriteLineIf(DebugOutput.Enabled, "Setting state to ObjectState.Detached");
                obj.State = ObjectState.Detached;
                break;

            case ObjectState.Modified:
            case ObjectState.Unchanged:
                Trace.WriteLineIf(DebugOutput.Enabled, "Setting state to ObjectState.Deleted");
                obj.State = ObjectState.Deleted;
                break;

            case ObjectState.Deleted:
            case ObjectState.Detached:
                break;

            default:
                throw new ObjectServerException("Invalid ObjectState found.");
            }
            ;

            obj.Processed = true;

            TypeSchema parentTypeSchema = SchemaCache.Current.GetSchema(obj.ServerObjectType);

            Trace.WriteLineIf(DebugOutput.Enabled, "Enumerating Childrean");

            foreach (ChildrenSchema childSchema in parentTypeSchema.ChildrenSchemas)
            {
                Trace.WriteLineIf(DebugOutput.Enabled, "Checking Children " + childSchema.Property.Name);

                ServerObjectCollection children = obj.Children[childSchema.Property.Name] as ServerObjectCollection;

                TypeSchema childTypeSchema = SchemaCache.Current.GetSchema(childSchema.ChildType);

                ParentSchema parentSchema = childTypeSchema.FindParentSchema(childSchema.PropertyName);

                int nonDeleted = 0;

                foreach (ServerObject child in children)
                {
                    if (child.State != ObjectState.Deleted && child.State != ObjectState.Detached)
                    {
                        nonDeleted++;
                    }
                }

                Trace.WriteLineIf(DebugOutput.Enabled, "Counted " + nonDeleted + " non-deleted children");

                if (nonDeleted > 0)
                {
                    if (parentSchema.DeleteAction == DeleteAction.Throw)
                    {
                        throw new ObjectServerException(String.Format(CultureInfo.CurrentCulture, "Could not delete, {0}.{1} has DeleteAction.Throw", childTypeSchema.Type.FullName, parentSchema.Property.Name));
                    }

                    if (parentSchema.DeleteAction == DeleteAction.Null)
                    {
                        Trace.WriteLineIf(DebugOutput.Enabled, "Changing key to DBNull.Value and updating state");

                        foreach (ServerObject child in children)
                        {
                            child.Data.SetValue(childSchema.PropertyName, DBNull.Value);

                            switch (child.State)
                            {
                            case ObjectState.Added:
                            case ObjectState.Modified:
                            case ObjectState.Detached:
                            case ObjectState.Deleted:
                                break;

                            case ObjectState.Unchanged:
                                child.State = ObjectState.Modified;
                                break;

                            default:
                                throw new ObjectServerException("Invalid ObjectState found.");
                            }
                            ;
                        }

                        continue;
                    }
                }

                Trace.WriteLineIf(DebugOutput.Enabled, "Processing children");

                foreach (ServerObject child in children)
                {
                    DeleteImpl(child);
                }
            }
        }