Ejemplo n.º 1
0
        public void Execute(PullResponseBuilder response)
        {
            var @object = this.session.Instantiate(this.pull.Object);

            var @class = @object.Strategy?.Class;

            if (@class != null && this.pull.ObjectType is IComposite objectType)
            {
                if (!objectType.IsAssignableFrom(@class))
                {
                    return;
                }
            }

            if (this.pull.Results != null)
            {
                foreach (var result in this.pull.Results)
                {
                    try
                    {
                        var name = result.Name;

                        var fetch = result.Fetch;
                        if ((fetch == null) && result.FetchRef.HasValue)
                        {
                            fetch = this.fetchService.Get(result.FetchRef.Value);
                        }

                        if (fetch != null)
                        {
                            var include = fetch.Include ?? fetch.Step?.End.Include;

                            if (fetch.Step != null)
                            {
                                var propertyType = fetch.Step.End.PropertyType;

                                if (fetch.Step.IsOne)
                                {
                                    name ??= propertyType.SingularName;

                                    @object = (IObject)fetch.Step.Get(@object, this.acls);
                                    response.AddObject(name, @object, include);
                                }
                                else
                                {
                                    name ??= propertyType.PluralName;

                                    var stepResult = fetch.Step.Get(@object, this.acls);
                                    var objects = stepResult is HashSet <object> set?set.Cast <IObject>().ToArray() : ((Extent)stepResult)?.ToArray() ?? new IObject[0];

                                    if (result.Skip.HasValue || result.Take.HasValue)
                                    {
                                        var paged = result.Skip.HasValue ? objects.Skip(result.Skip.Value) : objects;
                                        if (result.Take.HasValue)
                                        {
                                            paged = paged.Take(result.Take.Value);
                                        }

                                        paged = paged.ToArray();

                                        response.AddValue(name + "_total", objects.Length.ToString());
                                        response.AddCollection(name, paged, include);
                                    }
                                    else
                                    {
                                        response.AddCollection(name, objects, include);
                                    }
                                }
                            }
                            else
                            {
                                name ??= this.pull.ObjectType?.Name ?? @object.Strategy.Class.SingularName;
                                response.AddObject(name, @object, include);
                            }
                        }
                        else
                        {
                            name ??= this.pull.ObjectType?.Name ?? @object.Strategy.Class.SingularName;
                            response.AddObject(name, @object);
                        }
                    }
                    catch (Exception e)
                    {
                        throw new Exception($"Instantiate: {@object?.Strategy.Class}[{@object?.Strategy.ObjectId}], {result}", e);
                    }
                }
            }
            else
            {
                var name = this.pull.ObjectType?.Name ?? @object.Strategy.Class.SingularName;
                response.AddObject(name, @object);
            }
        }
Ejemplo n.º 2
0
        public void Execute(PullResponseBuilder response)
        {
            var @object = this.session.Instantiate(this.pull.Object);

            var objectType = this.pull.ObjectType as IComposite;
            var @class     = @object.Strategy?.Class;

            if (@class != null && objectType != null)
            {
                if (!objectType.IsAssignableFrom(@class))
                {
                    return;
                }
            }

            if (this.pull.Results != null)
            {
                foreach (var result in this.pull.Results)
                {
                    var name = result.Name;

                    var fetch = result.Fetch;
                    if (fetch == null && result.FetchRef.HasValue)
                    {
                        fetch = this.fetchService.Get(result.FetchRef.Value);
                    }

                    if (fetch != null)
                    {
                        var include = fetch.Include ?? fetch.Step?.End.Include;

                        if (fetch.Step != null)
                        {
                            var aclCache = new AccessControlListFactory(this.user);

                            var propertyType = fetch.Step.End.PropertyType;

                            if (fetch.Step.IsOne)
                            {
                                name = name ?? propertyType.SingularName;

                                @object = (IObject)fetch.Step.Get(@object, aclCache);
                                response.AddObject(name, @object, include);
                            }
                            else
                            {
                                name = name ?? propertyType.PluralName;

                                var stepResult = fetch.Step.Get(@object, aclCache);
                                var objects = stepResult is HashSet <object> set?set.Cast <IObject>().ToArray() : ((Extent)stepResult).ToArray();

                                if (result.Skip.HasValue || result.Take.HasValue)
                                {
                                    var paged = result.Skip.HasValue ? objects.Skip(result.Skip.Value) : objects;
                                    if (result.Take.HasValue)
                                    {
                                        paged = paged.Take(result.Take.Value);
                                    }

                                    paged = paged.ToArray();

                                    response.AddValue(name + "_total", objects.Length);
                                    response.AddCollection(name, paged, include);
                                }
                                else
                                {
                                    response.AddCollection(name, objects, include);
                                }
                            }
                        }
                        else
                        {
                            name = name ?? this.pull.ObjectType?.Name ?? @object.Strategy.Class.SingularName;
                            response.AddObject(name, @object, include);
                        }
                    }
                    else
                    {
                        name = name ?? this.pull.ObjectType?.Name ?? @object.Strategy.Class.SingularName;
                        response.AddObject(name, @object);
                    }
                }
            }
            else
            {
                var name = this.pull.ObjectType?.Name ?? @object.Strategy.Class.SingularName;
                response.AddObject(name, @object);
            }
        }
Ejemplo n.º 3
0
        public void Execute(PullResponseBuilder response)
        {
            if (this.pull.Extent == null && !this.pull.ExtentRef.HasValue)
            {
                throw new Exception("Either an Extent or an ExtentRef is required.");
            }

            var extent  = this.pull.Extent ?? this.extentService.Get(this.pull.ExtentRef.Value);
            var objects = extent.Build(this.session, this.pull.Arguments).ToArray();

            if (this.pull.Results != null)
            {
                foreach (var result in this.pull.Results)
                {
                    var name = result.Name;

                    var fetch = result.Fetch;
                    if (fetch == null && result.FetchRef.HasValue)
                    {
                        fetch = this.fetchService.Get(result.FetchRef.Value);
                    }

                    if (fetch != null)
                    {
                        var include = fetch.Include ?? fetch.Step?.End.Include;

                        if (fetch.Step != null)
                        {
                            var aclCache = new AccessControlListFactory(this.user);

                            var propertyType = fetch.Step.End.PropertyType;

                            objects = propertyType.IsOne ?
                                      objects.Select(v => fetch.Step.Get(v, aclCache)).Where(v => v != null).Cast <IObject>().Distinct().ToArray() :
                                      objects.SelectMany(v => ((Extent)fetch.Step.Get(v, aclCache)).ToArray()).Distinct().ToArray();

                            name = name ?? propertyType.PluralName;
                        }

                        name = name ?? extent.ObjectType.PluralName;

                        if (result.Skip.HasValue || result.Take.HasValue)
                        {
                            var paged = result.Skip.HasValue ? objects.Skip(result.Skip.Value) : objects;
                            if (result.Take.HasValue)
                            {
                                paged = paged.Take(result.Take.Value);
                            }

                            paged = paged.ToArray();

                            response.AddValue(name + "_total", extent.Build(this.session, this.pull.Arguments).Count);
                            response.AddCollection(name, paged, include);
                        }
                        else
                        {
                            response.AddCollection(name, objects, include);
                        }
                    }
                    else
                    {
                        name = name ?? extent.ObjectType.PluralName;
                        response.AddCollection(name, objects);
                    }
                }
            }
            else
            {
                var name = extent.ObjectType.PluralName;
                response.AddCollection(name, objects);
            }
        }
Ejemplo n.º 4
0
        public void Execute(PullResponseBuilder response)
        {
            if (this.pull.Extent == null && !this.pull.ExtentRef.HasValue)
            {
                throw new Exception("Either an Extent or an ExtentRef is required.");
            }

            var extent  = this.pull.Extent ?? this.extentService.Get(this.pull.ExtentRef.Value);
            var objects = extent.Build(this.session, this.pull.Parameters).ToArray();

            if (this.pull.Results != null)
            {
                foreach (var result in this.pull.Results)
                {
                    try
                    {
                        var name = result.Name;

                        var fetch = result.Fetch;
                        if (fetch == null && result.FetchRef.HasValue)
                        {
                            fetch = this.fetchService.Get(result.FetchRef.Value);
                        }

                        if (fetch != null)
                        {
                            var include = fetch.Include ?? fetch.Step?.End.Include;

                            if (fetch.Step != null)
                            {
                                objects = fetch.Step.IsOne ?
                                          objects.Select(v => fetch.Step.Get(v, this.acls)).Where(v => v != null).Cast <IObject>().Distinct().ToArray() :
                                          objects.SelectMany(v =>
                                {
                                    var stepResult = fetch.Step.Get(v, this.acls);
                                    return(stepResult is HashSet <object> set ? set.Cast <IObject>().ToArray() : ((Extent)stepResult)?.ToArray() ?? Array.Empty <IObject>());
                                }).Distinct().ToArray();

                                var propertyType = fetch.Step.End.PropertyType;
                                name ??= propertyType.PluralName;
                            }

                            name ??= extent.ObjectType.PluralName;

                            if (result.Skip.HasValue || result.Take.HasValue)
                            {
                                var paged = result.Skip.HasValue ? objects.Skip(result.Skip.Value) : objects;
                                if (result.Take.HasValue)
                                {
                                    paged = paged.Take(result.Take.Value);
                                }

                                paged = paged.ToArray();

                                response.AddValue(name + "_total", extent.Build(this.session, this.pull.Parameters).Count.ToString());
                                response.AddCollection(name, paged, include);
                            }
                            else
                            {
                                response.AddCollection(name, objects, include);
                            }
                        }
                        else
                        {
                            name ??= extent.ObjectType.PluralName;

                            if (result.Skip.HasValue || result.Take.HasValue)
                            {
                                var paged = result.Skip.HasValue ? objects.Skip(result.Skip.Value) : objects;
                                if (result.Take.HasValue)
                                {
                                    paged = paged.Take(result.Take.Value);
                                }

                                paged = paged.ToArray();

                                response.AddValue(name + "_total", extent.Build(this.session, this.pull.Parameters).Count.ToString());
                                response.AddCollection(name, paged);
                            }
                            else
                            {
                                response.AddCollection(name, objects);
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        throw new Exception($"Extent: {extent.ObjectType}, {result}", e);
                    }
                }
            }
            else
            {
                var name = extent.ObjectType.PluralName;
                response.AddCollection(name, objects);
            }
        }