public List <Entity <T> > GetAncestors(Entity <T> entity, UserAccess access, out int maxDepth)
        {
            var ancestors = new List <Entity <T> >();
            var entityId  = entity.Data.ArchiveRecordId;

            maxDepth = 0;
            var items = entity.Data.ArchiveplanContext;

            if (items != null)
            {
                var depth = 0;
                foreach (var contextItem in items)
                {
                    var id = contextItem.ArchiveRecordId;
                    if (entityId.Equals(id))
                    {
                        continue;
                    }

                    var item = new Entity <T>
                    {
                        Data = new T
                        {
                            ArchiveRecordId = id,
                            Title           = contextItem.Title,
                            ReferenceCode   = contextItem.RefCode
                        }
                    };

                    var ancestorOptions = new EntityMetaOptions
                    {
                        SetDepth = depth
                    };

                    var context = GetAsDecoratedContext(item, access, ancestorOptions);
                    item.Context = context;

                    ancestors.Add(item);
                    maxDepth = Math.Max(maxDepth, depth);
                    depth   += 1;
                }
            }

            if (ancestors.Count > 0)
            {
                ancestors = ancestors.OrderBy(anc => anc.Depth).ToList();
            }

            return(ancestors);
        }
        public JObject GetAsDecoratedContext(Entity <T> entity, UserAccess access, EntityMetaOptions options = null)
        {
            var hasContext = false;

            var context = new JObject();

            entity.MetaData = GetMetadata(entity.Data, access);
            options         = options ?? EntityMetaOptions.DefaultOptions;
            var depth = options.SetDepth;

            // ancestors
            if (options.FetchAncestors)
            {
                var ancestors = GetAncestors(entity, access, out depth);
                if (ancestors.Count > 0)
                {
                    hasContext = true;
                    context.Add(ancestorsKey, JArray.FromObject(ancestors));
                    depth += 1;
                }
            }

            // own depth
            entity.Depth = depth;
            depth       += 1;

            // children
            if (options.FetchChildren)
            {
                var result = GetChildren(entity.Data, depth, access, options?.ChildrenPaging);
                if (result.Items.Count > 0)
                {
                    hasContext = true;
                    JsonHelper.AddOrSet(context, childrenKey, JArray.FromObject(result.Items), true);
                    if (result.Paging != null)
                    {
                        JsonHelper.AddOrSet(context, childrenPagingKey, JObject.FromObject(result.Paging), true);
                    }
                }
            }

            return(hasContext ? context : null);
        }