Beispiel #1
0
        public EntityListResponse ReadEntities()
        {
            EntityListResponse response = new EntityListResponse
            {
                Success = true,
                Message = "The entity was successfully returned!",
            };

            try
            {
                List<IStorageEntity> storageEntityList = EntityRepository.Read();
                List<Entity> entities = storageEntityList.MapTo<Entity>();

                EntityRelationManager relationManager = new EntityRelationManager(Storage);
                EntityRelationListResponse relationListResponse = relationManager.Read();
                List<EntityRelation> relationList = new List<EntityRelation>();
                if (relationListResponse.Object != null)
                    relationList = relationListResponse.Object;

                List<RecordList> recordLists = new List<RecordList>();
                List<RecordView> recordViews = new List<RecordView>();
                List<Field> fields = new List<Field>();

                foreach (var entity in entities)
                {
                    recordLists.AddRange(entity.RecordLists);
                    recordViews.AddRange(entity.RecordViews);
                    fields.AddRange(entity.Fields);
                }

                foreach (var entity in entities)
                {
                    if (entity.RecordLists != null)
                    {
                        foreach (var recordList in entity.RecordLists)
                        {
                            if (recordList.Columns != null)
                            {
                                foreach (var column in recordList.Columns)
                                {
                                    if (column is RecordListFieldItem)
                                    {
                                        Field field = fields.FirstOrDefault(f => f.Id == ((RecordListFieldItem)column).FieldId);
                                        if (field != null)
                                        {
                                            //((RecordListFieldItem)column).DataName = string.Format("$field${0}", field.Name);
                                            ((RecordListFieldItem)column).DataName = field.Name;
                                            ((RecordListFieldItem)column).FieldName = field.Name;
                                            ((RecordListFieldItem)column).Meta = field;

                                            ((RecordListFieldItem)column).EntityName = entity.Name;
                                            ((RecordListFieldItem)column).EntityLabel = entity.Label;
                                            ((RecordListFieldItem)column).EntityLabelPlural = entity.LabelPlural;
                                        }
                                    }
                                    if (column is RecordListRelationFieldItem)
                                    {
                                        Entity relEntity = GetEntityByFieldId(((RecordListRelationFieldItem)column).FieldId, entities);
                                        if (relEntity != null)
                                        {
                                            ((RecordListRelationFieldItem)column).EntityName = relEntity.Name;
                                            ((RecordListRelationFieldItem)column).EntityLabel = relEntity.Label;
                                            ((RecordListRelationFieldItem)column).EntityLabelPlural = entity.LabelPlural;
                                        }

                                        var relation = relationList.FirstOrDefault(r => r.Id == ((RecordListRelationFieldItem)column).RelationId);
                                        ((RecordListRelationFieldItem)column).RelationName = relation != null ? relation.Name : string.Empty;

                                        Field field = fields.FirstOrDefault(f => f.Id == ((RecordListRelationFieldItem)column).FieldId);
                                        if (field != null)
                                        {
                                            ((RecordListRelationFieldItem)column).DataName = string.Format("$field${0}${1}", ((RecordListRelationFieldItem)column).RelationName, field.Name);
                                            ((RecordListRelationFieldItem)column).FieldName = field.Name;
                                            ((RecordListRelationFieldItem)column).Meta = field;
                                        }
                                    }
                                    if (column is RecordListViewItem)
                                    {
                                        RecordView view = recordViews.FirstOrDefault(v => v.Id == ((RecordListViewItem)column).ViewId);
                                        if (view != null)
                                        {
                                            ((RecordListViewItem)column).DataName = string.Format("$view${0}", view.Name);
                                            ((RecordListViewItem)column).ViewName = view.Name;
                                            ((RecordListViewItem)column).Meta = view;

                                            ((RecordListViewItem)column).EntityName = entity.Name;
                                            ((RecordListViewItem)column).EntityLabel = entity.Label;
                                            ((RecordListViewItem)column).EntityLabelPlural = entity.LabelPlural;
                                        }
                                    }
                                    if (column is RecordListRelationViewItem)
                                    {
                                        Entity relEntity = GetEntityByViewId(((RecordListRelationViewItem)column).ViewId, entities);
                                        if (relEntity != null)
                                        {
                                            ((RecordListRelationViewItem)column).EntityName = relEntity.Name;
                                            ((RecordListRelationViewItem)column).EntityLabel = relEntity.Label;
                                            ((RecordListRelationViewItem)column).EntityLabelPlural = entity.LabelPlural;
                                        }

                                        var relation = relationList.FirstOrDefault(r => r.Id == ((RecordListRelationViewItem)column).RelationId);
                                        ((RecordListRelationViewItem)column).RelationName = relation != null ? relation.Name : string.Empty;

                                        RecordView view = recordViews.FirstOrDefault(v => v.Id == ((RecordListRelationViewItem)column).ViewId);
                                        if (view != null)
                                        {
                                            ((RecordListRelationViewItem)column).DataName = string.Format("$view${0}${1}", ((RecordListRelationViewItem)column).RelationName, view.Name);
                                            ((RecordListRelationViewItem)column).ViewName = view.Name;
                                            ((RecordListRelationViewItem)column).Meta = view;
                                        }
                                    }
                                    if (column is RecordListListItem)
                                    {
                                        RecordList list = recordLists.FirstOrDefault(l => l.Id == ((RecordListListItem)column).ListId);
                                        if (list != null)
                                        {
                                            ((RecordListListItem)column).DataName = string.Format("list${0}", list.Name);
                                            ((RecordListListItem)column).ListName = list.Name;
                                            ((RecordListListItem)column).Meta = list;

                                            ((RecordListListItem)column).EntityName = entity.Name;
                                            ((RecordListListItem)column).EntityLabel = entity.Label;
                                            ((RecordListListItem)column).EntityLabelPlural = entity.LabelPlural;
                                        }
                                    }
                                    if (column is RecordListRelationListItem)
                                    {
                                        Entity relEntity = GetEntityByListId(((RecordListRelationListItem)column).ListId, entities);
                                        if (relEntity != null)
                                        {
                                            ((RecordListRelationListItem)column).EntityName = relEntity.Name;
                                            ((RecordListRelationListItem)column).EntityLabel = relEntity.Label;
                                            ((RecordListRelationListItem)column).EntityLabelPlural = entity.LabelPlural;
                                        }

                                        var relation = relationList.FirstOrDefault(r => r.Id == ((RecordListRelationListItem)column).RelationId);
                                        ((RecordListRelationListItem)column).RelationName = relation != null ? relation.Name : string.Empty;

                                        RecordList list = recordLists.FirstOrDefault(l => l.Id == ((RecordListRelationListItem)column).ListId);
                                        if (list != null)
                                        {
                                            ((RecordListRelationListItem)column).DataName = string.Format("$list${0}${1}", ((RecordListRelationListItem)column).RelationName, list.Name);
                                            ((RecordListRelationListItem)column).ListName = list.Name;
                                            ((RecordListRelationListItem)column).Meta = list;
                                        }
                                    }
                                }
                            }
                        }
                    }

                    if (entity.RecordViews != null)
                    {
                        foreach (var recordView in entity.RecordViews)
                        {
                            if (recordView.Regions == null)
                                continue;

                            foreach (var region in recordView.Regions)
                            {
                                if (region.Sections == null)
                                    continue;

                                foreach (var section in region.Sections)
                                {
                                    if (section.Rows == null)
                                        continue;

                                    foreach (var row in section.Rows)
                                    {
                                        if (row.Columns == null)
                                            continue;

                                        foreach (var column in row.Columns)
                                        {
                                            if (column.Items == null)
                                                continue;

                                            foreach (var item in column.Items)
                                            {
                                                if (item is RecordViewFieldItem)
                                                {
                                                    Field field = fields.FirstOrDefault(f => f.Id == ((RecordViewFieldItem)item).FieldId);
                                                    if (field != null)
                                                    {
                                                        //((RecordViewFieldItem)item).DataName = string.Format("$field${0}", field.Name);
                                                        ((RecordViewFieldItem)item).DataName = field.Name;
                                                        ((RecordViewFieldItem)item).FieldName = field.Name;
                                                        ((RecordViewFieldItem)item).Meta = field;

                                                        ((RecordViewFieldItem)item).EntityId = entity.Id;
                                                        ((RecordViewFieldItem)item).EntityName = entity.Name;
                                                        ((RecordViewFieldItem)item).EntityLabel = entity.Label;
                                                        ((RecordViewFieldItem)item).EntityLabelPlural = entity.LabelPlural;
                                                    }
                                                }
                                                if (item is RecordViewListItem)
                                                {
                                                    RecordList list = entity.RecordLists.FirstOrDefault(l => l.Id == ((RecordViewListItem)item).ListId);
                                                    if (list != null)
                                                    {
                                                        ((RecordViewListItem)item).DataName = string.Format("$list${0}", list.Name);
                                                        ((RecordViewListItem)item).Meta = list;
                                                        ((RecordViewListItem)item).ListName = list.Name;

                                                        ((RecordViewListItem)item).EntityId = entity.Id;
                                                        ((RecordViewListItem)item).EntityName = entity.Name;
                                                        ((RecordViewListItem)item).EntityLabel = entity.Label;
                                                        ((RecordViewListItem)item).EntityLabelPlural = entity.LabelPlural;
                                                    }

                                                }
                                                if (item is RecordViewViewItem)
                                                {
                                                    RecordView recView = entity.RecordViews.FirstOrDefault(v => v.Id == ((RecordViewViewItem)item).ViewId);
                                                    if (recView != null)
                                                    {
                                                        ((RecordViewViewItem)item).DataName = string.Format("$view${0}", recView.Name);
                                                        ((RecordViewViewItem)item).Meta = recView;
                                                        ((RecordViewViewItem)item).ViewName = recView.Name;

                                                        ((RecordViewViewItem)item).EntityId = entity.Id;
                                                        ((RecordViewViewItem)item).EntityName = entity.Name;
                                                        ((RecordViewViewItem)item).EntityLabel = entity.Label;
                                                        ((RecordViewViewItem)item).EntityLabelPlural = entity.LabelPlural;
                                                    }
                                                }

                                                if (item is RecordViewRelationFieldItem)
                                                {
                                                    Entity relEntity = GetEntityByFieldId(((RecordViewRelationFieldItem)item).FieldId, entities);
                                                    if (relEntity != null)
                                                    {
                                                        ((RecordViewRelationFieldItem)item).EntityId = relEntity.Id;
                                                        ((RecordViewRelationFieldItem)item).EntityName = relEntity.Name;
                                                        ((RecordViewRelationFieldItem)item).EntityLabel = relEntity.Label;
                                                    }

                                                    var relation = relationList.FirstOrDefault(r => r.Id == ((RecordViewRelationFieldItem)item).RelationId);
                                                    ((RecordViewRelationFieldItem)item).RelationName = relation != null ? relation.Name : string.Empty;

                                                    Field field = fields.FirstOrDefault(f => f.Id == ((RecordViewRelationFieldItem)item).FieldId);
                                                    if (field != null)
                                                    {
                                                        ((RecordViewRelationFieldItem)item).DataName = string.Format("$field${0}${1}", ((RecordViewRelationFieldItem)item).RelationName, field.Name);
                                                        ((RecordViewRelationFieldItem)item).Meta = field;
                                                        ((RecordViewRelationFieldItem)item).FieldName = field.Name;
                                                    }
                                                }

                                                if (item is RecordViewRelationViewItem)
                                                {
                                                    var relation = relationList.FirstOrDefault(r => r.Id == ((RecordViewRelationViewItem)item).RelationId);
                                                    ((RecordViewRelationViewItem)item).RelationName = relation != null ? relation.Name : string.Empty;

                                                    Entity relEntity = GetEntityByViewId(((RecordViewRelationViewItem)item).ViewId, entities);
                                                    if (relEntity != null)
                                                    {
                                                        ((RecordViewRelationViewItem)item).EntityId = relEntity.Id;
                                                        ((RecordViewRelationViewItem)item).EntityName = relEntity.Name;
                                                        ((RecordViewRelationViewItem)item).EntityLabel = relEntity.Label;

                                                        RecordView view = relEntity.RecordViews.FirstOrDefault(f => f.Id == ((RecordViewRelationViewItem)item).ViewId);
                                                        if (view != null)
                                                        {
                                                            ((RecordViewRelationViewItem)item).DataName = string.Format("$view${0}${1}", ((RecordViewRelationViewItem)item).RelationName, view.Name);
                                                            ((RecordViewRelationViewItem)item).Meta = view;
                                                            ((RecordViewRelationViewItem)item).ViewName = view.Name;
                                                        }
                                                    }
                                                }

                                                if (item is RecordViewRelationListItem)
                                                {
                                                    var relation = relationList.FirstOrDefault(r => r.Id == ((RecordViewRelationListItem)item).RelationId);
                                                    ((RecordViewRelationListItem)item).RelationName = relation != null ? relation.Name : string.Empty;

                                                    Entity relEntity = GetEntityByListId(((RecordViewRelationListItem)item).ListId, entities);
                                                    if (relEntity != null)
                                                    {
                                                        ((RecordViewRelationListItem)item).EntityId = relEntity.Id;
                                                        ((RecordViewRelationListItem)item).EntityName = relEntity.Name;
                                                        ((RecordViewRelationListItem)item).EntityLabel = relEntity.Label;
                                                        ((RecordViewRelationListItem)item).EntityLabelPlural = relEntity.LabelPlural;

                                                        RecordList list = relEntity.RecordLists.FirstOrDefault(f => f.Id == ((RecordViewRelationListItem)item).ListId);
                                                        if (list != null)
                                                        {
                                                            ((RecordViewRelationListItem)item).DataName = string.Format("$list${0}${1}", ((RecordViewRelationListItem)item).RelationName, list.Name);
                                                            ((RecordViewRelationListItem)item).Meta = list;
                                                            ((RecordViewRelationListItem)item).ListName = list.Name;
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }

                            if (recordView.Sidebar != null)
                            {
                                foreach (var item in recordView.Sidebar.Items)
                                {
                                    if (item is RecordViewSidebarListItem)
                                    {
                                        RecordList list = entity.RecordLists.FirstOrDefault(l => l.Id == ((RecordViewSidebarListItem)item).ListId);
                                        if (list != null)
                                        {
                                            ((RecordViewSidebarListItem)item).DataName = string.Format("$list${0}", list.Name);
                                            ((RecordViewSidebarListItem)item).Meta = list;
                                            ((RecordViewSidebarListItem)item).ListName = list.Name;

                                            ((RecordViewSidebarListItem)item).EntityId = entity.Id;
                                            ((RecordViewSidebarListItem)item).EntityName = entity.Name;
                                            ((RecordViewSidebarListItem)item).EntityLabel = entity.Label;
                                            ((RecordViewSidebarListItem)item).EntityLabelPlural = entity.LabelPlural;
                                        }

                                    }
                                    if (item is RecordViewSidebarViewItem)
                                    {
                                        RecordView recView = entity.RecordViews.FirstOrDefault(v => v.Id == ((RecordViewSidebarViewItem)item).ViewId);
                                        if (recView != null)
                                        {
                                            ((RecordViewSidebarViewItem)item).DataName = string.Format("$view${0}", recView.Name);
                                            ((RecordViewSidebarViewItem)item).Meta = recView;
                                            ((RecordViewSidebarViewItem)item).ViewName = recView.Name;

                                            ((RecordViewSidebarViewItem)item).EntityId = entity.Id;
                                            ((RecordViewSidebarViewItem)item).EntityName = entity.Name;
                                            ((RecordViewSidebarViewItem)item).EntityLabel = entity.Label;
                                            ((RecordViewSidebarViewItem)item).EntityLabelPlural = entity.LabelPlural;
                                        }
                                    }
                                    if (item is RecordViewSidebarRelationViewItem)
                                    {
                                        var relation = relationList.FirstOrDefault(r => r.Id == ((RecordViewSidebarRelationViewItem)item).RelationId);
                                        ((RecordViewSidebarRelationViewItem)item).RelationName = relation != null ? relation.Name : string.Empty;

                                        Entity relEntity = GetEntityByViewId(((RecordViewSidebarRelationViewItem)item).ViewId, entities);
                                        if (relEntity != null)
                                        {
                                            ((RecordViewSidebarRelationViewItem)item).EntityId = relEntity.Id;
                                            ((RecordViewSidebarRelationViewItem)item).EntityName = relEntity.Name;
                                            ((RecordViewSidebarRelationViewItem)item).EntityLabel = relEntity.Label;

                                            RecordView view = relEntity.RecordViews.FirstOrDefault(f => f.Id == ((RecordViewSidebarRelationViewItem)item).ViewId);
                                            if (view != null)
                                            {
                                                ((RecordViewSidebarRelationViewItem)item).DataName = string.Format("$view${0}${1}", ((RecordViewSidebarRelationViewItem)item).RelationName, view.Name);
                                                ((RecordViewSidebarRelationViewItem)item).Meta = view;
                                                ((RecordViewSidebarRelationViewItem)item).ViewName = view.Name;
                                            }
                                        }
                                    }

                                    if (item is RecordViewSidebarRelationListItem)
                                    {
                                        var relation = relationList.FirstOrDefault(r => r.Id == ((RecordViewSidebarRelationListItem)item).RelationId);
                                        ((RecordViewSidebarRelationListItem)item).RelationName = relation != null ? relation.Name : string.Empty;

                                        Entity relEntity = GetEntityByListId(((RecordViewSidebarRelationListItem)item).ListId, entities);
                                        if (relEntity != null)
                                        {
                                            ((RecordViewSidebarRelationListItem)item).EntityId = relEntity.Id;
                                            ((RecordViewSidebarRelationListItem)item).EntityName = relEntity.Name;
                                            ((RecordViewSidebarRelationListItem)item).EntityLabel = relEntity.Label;
                                            ((RecordViewSidebarRelationListItem)item).EntityLabelPlural = relEntity.LabelPlural;

                                            RecordList list = relEntity.RecordLists.FirstOrDefault(f => f.Id == ((RecordViewSidebarRelationListItem)item).ListId);
                                            if (list != null)
                                            {
                                                ((RecordViewSidebarRelationListItem)item).DataName = string.Format("$list${0}${1}", ((RecordViewSidebarRelationListItem)item).RelationName, list.Name);
                                                ((RecordViewSidebarRelationListItem)item).Meta = list;
                                                ((RecordViewSidebarRelationListItem)item).ListName = list.Name;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

                EntityList entityList = new EntityList();
                entityList.Entities = entities;
                response.Object = entityList;
            }
            catch (Exception e)
            {
                response.Timestamp = DateTime.UtcNow;
                response.Success = false;
            #if DEBUG
                response.Message = e.Message + e.StackTrace;
            #else
                response.Message = "An internal error occurred!";
            #endif
                return response;
            }

            response.Timestamp = DateTime.Now;

            return response;
        }
        public IActionResult GetRecordsByEntityName(string entityName)
        {
            //TODO - Test data
            var response = new EntityListResponse();
            response.Success = true;
            response.Object = new List<Entity>();
            switch (entityName)
            {
                case "role":

                    #region ///////////////////////// ROLES ////////////////////////

                    var roles = new List<Entity>();
                    var role = new Entity();

                    //Add Administrator
                    role = new Entity();
                    role.Id = new Guid("0b3fa332-6018-46e4-a70c-297b30c2b19c");
                    role.Name = "administrator";
                    role.Label = "Administrator";
                    role.LabelPlural = "Administrators";
                    roles.Add(role);

                    //Add Authenticated users
                    role = new Entity();
                    role.Id = new Guid("5cdf06ed-a627-4a71-a73b-43bdd390dbf1");
                    role.Name = "authenticated";
                    role.Label = "Authenticated user";
                    role.LabelPlural = "Authenticated users";
                    roles.Add(role);

                    //Add Guest
                    role = new Entity();
                    role.Id = new Guid("5cdf06ed-a627-4a71-a73b-43bdd390db22");
                    role.Name = "guest";
                    role.Label = "Guest";
                    role.LabelPlural = "Guests";
                    roles.Add(role);
                    response.Object = roles;
                break;
                #endregion

                case "area":
                    ///////////////////////// AREA ////////////////////////
                    var areas = new List<Entity>();
                    var area = new Entity();

                    //Entity 1
                    area = new Entity();
                    area.Id = Guid.NewGuid();
                    area.Name = "test";
                    area.Label = "Test";
                    area.Weight = 4;
                    area.IconName = "cloud";
                    areas.Add(area);
                    response.Object = areas;
                    break;

            }
            Thread.Sleep(100);
            //response.Object = siteMeta;
            return Json(response);
        }
Beispiel #3
0
        public EntityListResponse ReadEntities()
        {
            EntityListResponse response = new EntityListResponse
            {
                Success = true,
                Message = "The entity was successfully returned!",
            };

            //try return from cache
            var entities = Cache.GetEntities();
            if (entities != null)
            {
                response.Object = entities;
                response.Hash = Cache.GetEntitiesHash();
                return response;
            }

            try
            {
                List<DbEntity> storageEntityList = DbContext.Current.EntityRepository.Read();
                entities = storageEntityList.MapTo<Entity>();

                //EntityRelationManager relationManager = new EntityRelationManager(Storage);
                //EntityRelationListResponse relationListResponse = relationManager.Read();
                List<DbEntityRelation> relationList = DbContext.Current.RelationRepository.Read();
                //if (relationListResponse.Object != null)
                //	relationList = relationListResponse.Object;

                //TODO RUMEN - the unique key for finding fields, lists, views should be not only fieldId for example, but the fieldId+entityId combination.
                //The problem occurs when there are two fields in two different entities with the same id.Same applies for view and list.
                List<RecordList> recordLists = new List<RecordList>();
                List<RecordView> recordViews = new List<RecordView>();
                List<Field> fields = new List<Field>();

                foreach (var entity in entities)
                {
                    recordLists.AddRange(entity.RecordLists);
                    recordViews.AddRange(entity.RecordViews);
                    fields.AddRange(entity.Fields);
                }

                foreach (var entity in entities)
                {
                    #region Process Lists

                    if (entity.RecordLists != null)
                    {
                        foreach (var recordList in entity.RecordLists)
                        {
                            if (recordList.Columns != null)
                            {
                                foreach (var column in recordList.Columns)
                                {
                                    if (column is RecordListFieldItem)
                                    {
                                        Field field = fields.FirstOrDefault(f => f.Id == ((RecordListFieldItem)column).FieldId);
                                        if (field != null)
                                        {
                                            //((RecordListFieldItem)column).DataName = string.Format("$field${0}", field.Name);
                                            ((RecordListFieldItem)column).DataName = field.Name;
                                            ((RecordListFieldItem)column).FieldName = field.Name;
                                            ((RecordListFieldItem)column).Meta = field;

                                            ((RecordListFieldItem)column).EntityName = entity.Name;
                                            ((RecordListFieldItem)column).EntityLabel = entity.Label;
                                            ((RecordListFieldItem)column).EntityLabelPlural = entity.LabelPlural;
                                        }
                                    }
                                    if (column is RecordListRelationFieldItem)
                                    {
                                        Entity relEntity = GetEntityByFieldId(((RecordListRelationFieldItem)column).FieldId, entities);
                                        if (relEntity != null)
                                        {
                                            ((RecordListRelationFieldItem)column).EntityId = relEntity.Id;
                                            ((RecordListRelationFieldItem)column).EntityName = relEntity.Name;
                                            ((RecordListRelationFieldItem)column).EntityLabel = relEntity.Label;
                                            ((RecordListRelationFieldItem)column).EntityLabelPlural = entity.LabelPlural;
                                        }

                                        var relation = relationList.FirstOrDefault(r => r.Id == ((RecordListRelationFieldItem)column).RelationId);
                                        ((RecordListRelationFieldItem)column).RelationName = relation != null ? relation.Name : string.Empty;

                                        if (relation != null)
                                        {
                                            var relationOptions = recordList.RelationOptions.SingleOrDefault(x => x.RelationId == ((RecordListRelationFieldItem)column).RelationId);
                                            if (relationOptions != null)
                                                ((RecordListRelationFieldItem)column).RelationDirection = relationOptions.Direction;
                                            else
                                                ((RecordListRelationFieldItem)column).RelationDirection = "origin-target";
                                        }

                                        Field field = fields.FirstOrDefault(f => f.Id == ((RecordListRelationFieldItem)column).FieldId);
                                        if (field != null)
                                        {
                                            ((RecordListRelationFieldItem)column).DataName = string.Format("$field${0}${1}", ((RecordListRelationFieldItem)column).RelationName, field.Name);
                                            ((RecordListRelationFieldItem)column).FieldName = field.Name;
                                            ((RecordListRelationFieldItem)column).Meta = field;
                                        }
                                    }
                                    if (column is RecordListViewItem)
                                    {
                                        RecordView view = recordViews.FirstOrDefault(v => v.Id == ((RecordListViewItem)column).ViewId);
                                        if (view != null)
                                        {
                                            ((RecordListViewItem)column).DataName = string.Format("$view${0}", view.Name);
                                            ((RecordListViewItem)column).ViewName = view.Name;
                                            ((RecordListViewItem)column).Meta = view;

                                            ((RecordListViewItem)column).EntityName = entity.Name;
                                            ((RecordListViewItem)column).EntityLabel = entity.Label;
                                            ((RecordListViewItem)column).EntityLabelPlural = entity.LabelPlural;
                                        }
                                    }
                                    if (column is RecordListRelationViewItem)
                                    {
                                        Entity relEntity = GetEntityByViewId(((RecordListRelationViewItem)column).ViewId, entities);
                                        if (relEntity != null)
                                        {
                                            ((RecordListRelationViewItem)column).EntityId = relEntity.Id;
                                            ((RecordListRelationViewItem)column).EntityName = relEntity.Name;
                                            ((RecordListRelationViewItem)column).EntityLabel = relEntity.Label;
                                            ((RecordListRelationViewItem)column).EntityLabelPlural = entity.LabelPlural;
                                        }

                                        var relation = relationList.FirstOrDefault(r => r.Id == ((RecordListRelationViewItem)column).RelationId);
                                        ((RecordListRelationViewItem)column).RelationName = relation != null ? relation.Name : string.Empty;

                                        if (relation != null)
                                        {
                                            var relationOptions = recordList.RelationOptions.SingleOrDefault(x => x.RelationId == ((RecordListRelationViewItem)column).RelationId);
                                            if (relationOptions != null)
                                                ((RecordListRelationViewItem)column).RelationDirection = relationOptions.Direction;
                                            else
                                                ((RecordListRelationViewItem)column).RelationDirection = "origin-target";
                                        }

                                        RecordView view = recordViews.FirstOrDefault(v => v.Id == ((RecordListRelationViewItem)column).ViewId);
                                        if (view != null)
                                        {
                                            ((RecordListRelationViewItem)column).DataName = string.Format("$view${0}${1}", ((RecordListRelationViewItem)column).RelationName, view.Name);
                                            ((RecordListRelationViewItem)column).ViewName = view.Name;
                                            ((RecordListRelationViewItem)column).Meta = view;
                                        }
                                    }
                                    if (column is RecordListListItem)
                                    {
                                        RecordList list = recordLists.FirstOrDefault(l => l.Id == ((RecordListListItem)column).ListId);
                                        if (list != null)
                                        {
                                            ((RecordListListItem)column).DataName = string.Format("list${0}", list.Name);
                                            ((RecordListListItem)column).ListName = list.Name;
                                            ((RecordListListItem)column).Meta = list;

                                            ((RecordListListItem)column).EntityName = entity.Name;
                                            ((RecordListListItem)column).EntityLabel = entity.Label;
                                            ((RecordListListItem)column).EntityLabelPlural = entity.LabelPlural;
                                        }
                                    }
                                    if (column is RecordListRelationListItem)
                                    {
                                        Entity relEntity = GetEntityByListId(((RecordListRelationListItem)column).ListId, entities);
                                        if (relEntity != null)
                                        {
                                            ((RecordListRelationListItem)column).EntityId = relEntity.Id;
                                            ((RecordListRelationListItem)column).EntityName = relEntity.Name;
                                            ((RecordListRelationListItem)column).EntityLabel = relEntity.Label;
                                            ((RecordListRelationListItem)column).EntityLabelPlural = entity.LabelPlural;
                                        }

                                        var relation = relationList.FirstOrDefault(r => r.Id == ((RecordListRelationListItem)column).RelationId);
                                        ((RecordListRelationListItem)column).RelationName = relation != null ? relation.Name : string.Empty;

                                        if (relation != null)
                                        {
                                            var relationOptions = recordList.RelationOptions.SingleOrDefault(x => x.RelationId == ((RecordListRelationListItem)column).RelationId);
                                            if (relationOptions != null)
                                                ((RecordListRelationListItem)column).RelationDirection = relationOptions.Direction;
                                            else
                                                ((RecordListRelationListItem)column).RelationDirection = "origin-target";
                                        }

                                        RecordList list = recordLists.FirstOrDefault(l => l.Id == ((RecordListRelationListItem)column).ListId);
                                        if (list != null)
                                        {
                                            ((RecordListRelationListItem)column).DataName = string.Format("$list${0}${1}", ((RecordListRelationListItem)column).RelationName, list.Name);
                                            ((RecordListRelationListItem)column).ListName = list.Name;
                                            ((RecordListRelationListItem)column).Meta = list;
                                        }
                                    }
                                    if (column is RecordListRelationTreeItem)
                                    {
                                        Entity relEntity = GetEntityByTreeId(((RecordListRelationTreeItem)column).TreeId, entities);
                                        if (relEntity != null)
                                        {
                                            ((RecordListRelationTreeItem)column).EntityId = relEntity.Id;
                                            ((RecordListRelationTreeItem)column).EntityName = relEntity.Name;
                                            ((RecordListRelationTreeItem)column).EntityLabel = relEntity.Label;
                                            ((RecordListRelationTreeItem)column).EntityLabelPlural = entity.LabelPlural;
                                        }

                                        var relation = relationList.FirstOrDefault(r => r.Id == ((RecordListRelationTreeItem)column).RelationId);
                                        ((RecordListRelationTreeItem)column).RelationName = relation != null ? relation.Name : string.Empty;

                                        RecordTree tree = relEntity.RecordTrees.FirstOrDefault(l => l.Id == ((RecordListRelationTreeItem)column).TreeId);
                                        if (tree != null)
                                        {
                                            ((RecordListRelationTreeItem)column).DataName = string.Format("$tree${0}${1}", ((RecordListRelationTreeItem)column).RelationName, tree.Name);
                                            ((RecordListRelationTreeItem)column).TreeName = tree.Name;
                                            ((RecordListRelationTreeItem)column).Meta = tree;
                                        }
                                    }
                                }
                            }
                        }
                    }

                    #endregion

                    #region Process Views

                    if (entity.RecordViews != null)
                    {
                        foreach (var recordView in entity.RecordViews)
                        {
                            if (recordView.Regions == null)
                                continue;

                            foreach (var region in recordView.Regions)
                            {
                                if (region.Sections == null)
                                    continue;

                                foreach (var section in region.Sections)
                                {
                                    if (section.Rows == null)
                                        continue;

                                    foreach (var row in section.Rows)
                                    {
                                        if (row.Columns == null)
                                            continue;

                                        foreach (var column in row.Columns)
                                        {
                                            if (column.Items == null)
                                                continue;

                                            foreach (var item in column.Items)
                                            {
                                                if (item is RecordViewFieldItem)
                                                {
                                                    Entity fieldEntity = entities.Single(f => f.Id == ((RecordViewFieldItem)item).EntityId);
                                                    Field field = fieldEntity.Fields.Single(f => f.Id == ((RecordViewFieldItem)item).FieldId);
                                                    if (field != null)
                                                    {
                                                        //((RecordViewFieldItem)item).DataName = string.Format("$field${0}", field.Name);
                                                        ((RecordViewFieldItem)item).DataName = field.Name;
                                                        ((RecordViewFieldItem)item).FieldName = field.Name;
                                                        ((RecordViewFieldItem)item).Meta = field;

                                                        ((RecordViewFieldItem)item).EntityId = entity.Id;
                                                        ((RecordViewFieldItem)item).EntityName = entity.Name;
                                                        ((RecordViewFieldItem)item).EntityLabel = entity.Label;
                                                        ((RecordViewFieldItem)item).EntityLabelPlural = entity.LabelPlural;
                                                    }
                                                }
                                                if (item is RecordViewListItem)
                                                {
                                                    RecordList list = entity.RecordLists.FirstOrDefault(l => l.Id == ((RecordViewListItem)item).ListId);
                                                    if (list != null)
                                                    {
                                                        ((RecordViewListItem)item).DataName = string.Format("$list${0}", list.Name);
                                                        ((RecordViewListItem)item).Meta = list;
                                                        ((RecordViewListItem)item).ListName = list.Name;

                                                        ((RecordViewListItem)item).EntityId = entity.Id;
                                                        ((RecordViewListItem)item).EntityName = entity.Name;
                                                        ((RecordViewListItem)item).EntityLabel = entity.Label;
                                                        ((RecordViewListItem)item).EntityLabelPlural = entity.LabelPlural;
                                                    }

                                                }
                                                if (item is RecordViewViewItem)
                                                {
                                                    RecordView recView = entity.RecordViews.FirstOrDefault(v => v.Id == ((RecordViewViewItem)item).ViewId);
                                                    if (recView != null)
                                                    {
                                                        ((RecordViewViewItem)item).DataName = string.Format("$view${0}", recView.Name);
                                                        ((RecordViewViewItem)item).Meta = recView;
                                                        ((RecordViewViewItem)item).ViewName = recView.Name;

                                                        ((RecordViewViewItem)item).EntityId = entity.Id;
                                                        ((RecordViewViewItem)item).EntityName = entity.Name;
                                                        ((RecordViewViewItem)item).EntityLabel = entity.Label;
                                                        ((RecordViewViewItem)item).EntityLabelPlural = entity.LabelPlural;
                                                    }
                                                }

                                                if (item is RecordViewRelationFieldItem)
                                                {
                                                    Entity relEntity = GetEntityByFieldId(((RecordViewRelationFieldItem)item).FieldId, entities);
                                                    if (relEntity != null)
                                                    {
                                                        ((RecordViewRelationFieldItem)item).EntityId = relEntity.Id;
                                                        ((RecordViewRelationFieldItem)item).EntityName = relEntity.Name;
                                                        ((RecordViewRelationFieldItem)item).EntityLabel = relEntity.Label;
                                                    }

                                                    var relation = relationList.FirstOrDefault(r => r.Id == ((RecordViewRelationFieldItem)item).RelationId);
                                                    ((RecordViewRelationFieldItem)item).RelationName = relation != null ? relation.Name : string.Empty;

                                                    if (relation != null)
                                                    {
                                                        var relationOptions = recordView.RelationOptions.SingleOrDefault(x => x.RelationId == ((RecordViewRelationFieldItem)item).RelationId);
                                                        if (relationOptions != null)
                                                            ((RecordViewRelationFieldItem)item).RelationDirection = relationOptions.Direction;
                                                        else
                                                            ((RecordViewRelationFieldItem)item).RelationDirection = "origin-target";
                                                    }

                                                    Field field = fields.FirstOrDefault(f => f.Id == ((RecordViewRelationFieldItem)item).FieldId);
                                                    if (field != null)
                                                    {
                                                        ((RecordViewRelationFieldItem)item).DataName = string.Format("$field${0}${1}", ((RecordViewRelationFieldItem)item).RelationName, field.Name);
                                                        ((RecordViewRelationFieldItem)item).Meta = field;
                                                        ((RecordViewRelationFieldItem)item).FieldName = field.Name;
                                                    }
                                                }

                                                if (item is RecordViewRelationViewItem)
                                                {
                                                    var relation = relationList.FirstOrDefault(r => r.Id == ((RecordViewRelationViewItem)item).RelationId);
                                                    ((RecordViewRelationViewItem)item).RelationName = relation != null ? relation.Name : string.Empty;

                                                    if (relation != null)
                                                    {
                                                        var relationOptions = recordView.RelationOptions.SingleOrDefault(x => x.RelationId == ((RecordViewRelationViewItem)item).RelationId);
                                                        if (relationOptions != null)
                                                            ((RecordViewRelationViewItem)item).RelationDirection = relationOptions.Direction;
                                                        else
                                                            ((RecordViewRelationViewItem)item).RelationDirection = "origin-target";
                                                    }

                                                    Entity relEntity = GetEntityByViewId(((RecordViewRelationViewItem)item).ViewId, entities);
                                                    if (relEntity != null)
                                                    {
                                                        ((RecordViewRelationViewItem)item).EntityId = relEntity.Id;
                                                        ((RecordViewRelationViewItem)item).EntityName = relEntity.Name;
                                                        ((RecordViewRelationViewItem)item).EntityLabel = relEntity.Label;

                                                        RecordView view = relEntity.RecordViews.FirstOrDefault(f => f.Id == ((RecordViewRelationViewItem)item).ViewId);
                                                        if (view != null)
                                                        {
                                                            ((RecordViewRelationViewItem)item).DataName = string.Format("$view${0}${1}", ((RecordViewRelationViewItem)item).RelationName, view.Name);
                                                            ((RecordViewRelationViewItem)item).Meta = view;
                                                            ((RecordViewRelationViewItem)item).ViewName = view.Name;
                                                        }
                                                    }
                                                }

                                                if (item is RecordViewRelationListItem)
                                                {
                                                    var relation = relationList.FirstOrDefault(r => r.Id == ((RecordViewRelationListItem)item).RelationId);
                                                    ((RecordViewRelationListItem)item).RelationName = relation != null ? relation.Name : string.Empty;

                                                    if (relation != null)
                                                    {
                                                        var relationOptions = recordView.RelationOptions.SingleOrDefault(x => x.RelationId == ((RecordViewRelationListItem)item).RelationId);
                                                        if (relationOptions != null)
                                                            ((RecordViewRelationListItem)item).RelationDirection = relationOptions.Direction;
                                                        else
                                                            ((RecordViewRelationListItem)item).RelationDirection = "origin-target";
                                                    }

                                                    Entity relEntity = GetEntityByListId(((RecordViewRelationListItem)item).ListId, entities);
                                                    if (relEntity != null)
                                                    {
                                                        ((RecordViewRelationListItem)item).EntityId = relEntity.Id;
                                                        ((RecordViewRelationListItem)item).EntityName = relEntity.Name;
                                                        ((RecordViewRelationListItem)item).EntityLabel = relEntity.Label;
                                                        ((RecordViewRelationListItem)item).EntityLabelPlural = relEntity.LabelPlural;

                                                        RecordList list = relEntity.RecordLists.FirstOrDefault(f => f.Id == ((RecordViewRelationListItem)item).ListId);
                                                        if (list != null)
                                                        {
                                                            ((RecordViewRelationListItem)item).DataName = string.Format("$list${0}${1}", ((RecordViewRelationListItem)item).RelationName, list.Name);
                                                            ((RecordViewRelationListItem)item).Meta = list;
                                                            ((RecordViewRelationListItem)item).ListName = list.Name;
                                                        }
                                                    }
                                                }

                                                if (item is RecordViewRelationTreeItem)
                                                {
                                                    Entity relEntity = GetEntityByTreeId(((RecordViewRelationTreeItem)item).TreeId, entities);
                                                    if (relEntity != null)
                                                    {
                                                        ((RecordViewRelationTreeItem)item).EntityId = relEntity.Id;
                                                        ((RecordViewRelationTreeItem)item).EntityName = relEntity.Name;
                                                        ((RecordViewRelationTreeItem)item).EntityLabel = relEntity.Label;
                                                        ((RecordViewRelationTreeItem)item).EntityLabelPlural = entity.LabelPlural;
                                                    }

                                                    var relation = relationList.FirstOrDefault(r => r.Id == ((RecordViewRelationTreeItem)item).RelationId);
                                                    ((RecordViewRelationTreeItem)item).RelationName = relation != null ? relation.Name : string.Empty;

                                                    RecordTree tree = relEntity.RecordTrees.FirstOrDefault(l => l.Id == ((RecordViewRelationTreeItem)item).TreeId);
                                                    if (tree != null)
                                                    {

                                                        ((RecordViewRelationTreeItem)item).DataName = string.Format("$tree${0}${1}", ((RecordViewRelationTreeItem)item).RelationName, tree.Name);
                                                        ((RecordViewRelationTreeItem)item).TreeName = tree.Name;
                                                        ((RecordViewRelationTreeItem)item).Meta = tree;
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }

                            if (recordView.Sidebar != null)
                            {
                                foreach (var item in recordView.Sidebar.Items)
                                {
                                    if (item is RecordViewSidebarListItem)
                                    {
                                        RecordList list = entity.RecordLists.FirstOrDefault(l => l.Id == ((RecordViewSidebarListItem)item).ListId);
                                        if (list != null)
                                        {
                                            ((RecordViewSidebarListItem)item).DataName = string.Format("$list${0}", list.Name);
                                            ((RecordViewSidebarListItem)item).Meta = list;
                                            ((RecordViewSidebarListItem)item).ListName = list.Name;

                                            ((RecordViewSidebarListItem)item).EntityId = entity.Id;
                                            ((RecordViewSidebarListItem)item).EntityName = entity.Name;
                                            ((RecordViewSidebarListItem)item).EntityLabel = entity.Label;
                                            ((RecordViewSidebarListItem)item).EntityLabelPlural = entity.LabelPlural;
                                        }

                                    }
                                    if (item is RecordViewSidebarViewItem)
                                    {
                                        RecordView recView = entity.RecordViews.FirstOrDefault(v => v.Id == ((RecordViewSidebarViewItem)item).ViewId);
                                        if (recView != null)
                                        {
                                            ((RecordViewSidebarViewItem)item).DataName = string.Format("$view${0}", recView.Name);
                                            ((RecordViewSidebarViewItem)item).Meta = recView;
                                            ((RecordViewSidebarViewItem)item).ViewName = recView.Name;

                                            ((RecordViewSidebarViewItem)item).EntityId = entity.Id;
                                            ((RecordViewSidebarViewItem)item).EntityName = entity.Name;
                                            ((RecordViewSidebarViewItem)item).EntityLabel = entity.Label;
                                            ((RecordViewSidebarViewItem)item).EntityLabelPlural = entity.LabelPlural;
                                        }
                                    }
                                    if (item is RecordViewSidebarRelationViewItem)
                                    {
                                        var relation = relationList.FirstOrDefault(r => r.Id == ((RecordViewSidebarRelationViewItem)item).RelationId);
                                        ((RecordViewSidebarRelationViewItem)item).RelationName = relation != null ? relation.Name : string.Empty;

                                        if (relation != null)
                                        {
                                            var relationOptions = recordView.RelationOptions.SingleOrDefault(x => x.RelationId == ((RecordViewSidebarRelationViewItem)item).RelationId);
                                            if (relationOptions != null)
                                                ((RecordViewSidebarRelationViewItem)item).RelationDirection = relationOptions.Direction;
                                            else
                                                ((RecordViewSidebarRelationViewItem)item).RelationDirection = "origin-target";
                                        }

                                        Entity relEntity = GetEntityByViewId(((RecordViewSidebarRelationViewItem)item).ViewId, entities);
                                        if (relEntity != null)
                                        {
                                            ((RecordViewSidebarRelationViewItem)item).EntityId = relEntity.Id;
                                            ((RecordViewSidebarRelationViewItem)item).EntityName = relEntity.Name;
                                            ((RecordViewSidebarRelationViewItem)item).EntityLabel = relEntity.Label;

                                            RecordView view = relEntity.RecordViews.FirstOrDefault(f => f.Id == ((RecordViewSidebarRelationViewItem)item).ViewId);
                                            if (view != null)
                                            {
                                                ((RecordViewSidebarRelationViewItem)item).DataName = string.Format("$view${0}${1}", ((RecordViewSidebarRelationViewItem)item).RelationName, view.Name);
                                                ((RecordViewSidebarRelationViewItem)item).Meta = view;
                                                ((RecordViewSidebarRelationViewItem)item).ViewName = view.Name;
                                            }
                                        }
                                    }

                                    if (item is RecordViewSidebarRelationListItem)
                                    {
                                        var relation = relationList.FirstOrDefault(r => r.Id == ((RecordViewSidebarRelationListItem)item).RelationId);
                                        ((RecordViewSidebarRelationListItem)item).RelationName = relation != null ? relation.Name : string.Empty;

                                        if (relation != null)
                                        {
                                            var relationOptions = recordView.RelationOptions.SingleOrDefault(x => x.RelationId == ((RecordViewSidebarRelationListItem)item).RelationId);
                                            if (relationOptions != null)
                                                ((RecordViewSidebarRelationListItem)item).RelationDirection = relationOptions.Direction;
                                            else
                                                ((RecordViewSidebarRelationListItem)item).RelationDirection = "origin-target";
                                        }

                                        Entity relEntity = GetEntityByListId(((RecordViewSidebarRelationListItem)item).ListId, entities);
                                        if (relEntity != null)
                                        {
                                            ((RecordViewSidebarRelationListItem)item).EntityId = relEntity.Id;
                                            ((RecordViewSidebarRelationListItem)item).EntityName = relEntity.Name;
                                            ((RecordViewSidebarRelationListItem)item).EntityLabel = relEntity.Label;
                                            ((RecordViewSidebarRelationListItem)item).EntityLabelPlural = relEntity.LabelPlural;

                                            RecordList list = relEntity.RecordLists.FirstOrDefault(f => f.Id == ((RecordViewSidebarRelationListItem)item).ListId);
                                            if (list != null)
                                            {
                                                ((RecordViewSidebarRelationListItem)item).DataName = string.Format("$list${0}${1}", ((RecordViewSidebarRelationListItem)item).RelationName, list.Name);
                                                ((RecordViewSidebarRelationListItem)item).Meta = list;
                                                ((RecordViewSidebarRelationListItem)item).ListName = list.Name;
                                            }
                                        }
                                    }

                                    if (item is RecordViewSidebarRelationTreeItem)
                                    {
                                        Entity relEntity = GetEntityByTreeId(((RecordViewSidebarRelationTreeItem)item).TreeId, entities);
                                        if (relEntity != null)
                                        {
                                            ((RecordViewSidebarRelationTreeItem)item).EntityId = relEntity.Id;
                                            ((RecordViewSidebarRelationTreeItem)item).EntityName = relEntity.Name;
                                            ((RecordViewSidebarRelationTreeItem)item).EntityLabel = relEntity.Label;
                                            ((RecordViewSidebarRelationTreeItem)item).EntityLabelPlural = entity.LabelPlural;
                                        }

                                        var relation = relationList.FirstOrDefault(r => r.Id == ((RecordViewSidebarRelationTreeItem)item).RelationId);
                                        ((RecordViewSidebarRelationTreeItem)item).RelationName = relation != null ? relation.Name : string.Empty;

                                        RecordTree tree = relEntity.RecordTrees.FirstOrDefault(l => l.Id == ((RecordViewSidebarRelationTreeItem)item).TreeId);
                                        if (tree != null)
                                        {
                                            ((RecordViewSidebarRelationTreeItem)item).DataName = string.Format("$tree${0}${1}", ((RecordViewSidebarRelationTreeItem)item).RelationName, tree.Name);
                                            ((RecordViewSidebarRelationTreeItem)item).TreeName = tree.Name;
                                            ((RecordViewSidebarRelationTreeItem)item).Meta = tree;
                                        }
                                    }
                                }
                            }
                        }
                    }

                    #endregion

                    #region Process Trees

                    if (entity.RecordTrees != null)
                    {

                        foreach (var recordTree in entity.RecordTrees)
                        {
                            foreach (RecordTreeNode node in recordTree.RootNodes)
                            {
                                var recData = DbContext.Current.RecordRepository.FindTreeNodeRecord(entity.Name, node.RecordId);
                                if (recData != null)
                                {
                                    var idField = entity.Fields.SingleOrDefault(x => x.Id == recordTree.NodeIdFieldId);
                                    if (idField == null)
                                        throw new Exception("Cannot initialize tree '" + recordTree.Name + "'. Node id field is missing in entity meta.");

                                    var parentIdField = entity.Fields.SingleOrDefault(x => x.Id == recordTree.NodeParentIdFieldId);
                                    if (parentIdField == null)
                                        throw new Exception("Cannot initialize tree '" + recordTree.Name + "'. Parent id field is missing in entity meta.");

                                    var nameField = entity.Fields.SingleOrDefault(x => x.Id == recordTree.NodeNameFieldId);
                                    if (nameField == null)
                                        throw new Exception("Cannot initialize tree '" + recordTree.Name + "'. Node name field is missing in entity meta.");

                                    var labelField = entity.Fields.SingleOrDefault(x => x.Id == recordTree.NodeLabelFieldId);
                                    if (labelField == null)
                                        throw new Exception("Cannot initialize tree '" + recordTree.Name + "'. Node label field is missing in entity meta.");

                                    var weigthField = entity.Fields.SingleOrDefault(x => x.Id == recordTree.NodeWeightFieldId);
                                    if (weigthField == null)
                                        throw new Exception("Cannot initialize tree '" + recordTree.Name + "'. Node weigth field is missing in entity meta.");

                                    var value = recData[idField.Name];
                                    node.Id = (value as Guid?) ?? Guid.Empty;

                                    value = recData[parentIdField.Name];
                                    node.ParentId = value as Guid?;

                                    value = recData[nameField.Name];
                                    node.Name = (value ?? string.Empty).ToString();

                                    value = recData[labelField.Name];
                                    node.Label = (value ?? string.Empty).ToString();

                                    node.Weight = (int?)(recData[weigthField.Name] as decimal?);
                                }
                            }
                        }
                    }

                    #endregion

                    //compute hash code
                    entity.Hash = CryptoUtility.ComputeOddMD5Hash(JsonConvert.SerializeObject(entity));
                }

                Cache.AddEntities(entities);
                response.Object = entities;
                response.Hash = Cache.GetEntitiesHash();
            }
            catch (Exception e)
            {
                response.Timestamp = DateTime.UtcNow;
                response.Success = false;
            #if DEBUG
                response.Message = e.Message + e.StackTrace;
            #else
                response.Message = "An internal error occurred!";
            #endif
                return response;
            }

            response.Timestamp = DateTime.Now;

            return response;
        }
Beispiel #4
0
        public EntityListResponse ReadEntities()
        {
            EntityListResponse response = new EntityListResponse
            {
                Success = true,
                Message = "The entity was successfully returned!",
            };

            try
            {
                List<IStorageEntity> storageEntityList = EntityRepository.Read();
                List<Entity> entities = storageEntityList.MapTo<Entity>();

                EntityRelationManager relationManager = new EntityRelationManager(Storage);
                EntityRelationListResponse relationListResponse = relationManager.Read();
                List<EntityRelation> relationList = new List<EntityRelation>();
                if (relationListResponse.Object != null)
                    relationList = relationListResponse.Object;

                List<RecordList> recordLists = new List<RecordList>();
                List<RecordView> recordViews = new List<RecordView>();
                List<Field> fields = new List<Field>();

                foreach (var entity in entities)
                {
                    recordLists.AddRange(entity.RecordLists);
                    recordViews.AddRange(entity.RecordViews);
                    fields.AddRange(entity.Fields);
                }

                foreach (var entity in entities)
                {
                    #region Process Lists

                    if (entity.RecordLists != null)
                    {
                        foreach (var recordList in entity.RecordLists)
                        {
                            if (recordList.Columns != null)
                            {
                                foreach (var column in recordList.Columns)
                                {
                                    if (column is RecordListFieldItem)
                                    {
                                        Field field = fields.FirstOrDefault(f => f.Id == ((RecordListFieldItem)column).FieldId);
                                        if (field != null)
                                        {
                                            //((RecordListFieldItem)column).DataName = string.Format("$field${0}", field.Name);
                                            ((RecordListFieldItem)column).DataName = field.Name;
                                            ((RecordListFieldItem)column).FieldName = field.Name;
                                            ((RecordListFieldItem)column).Meta = field;

                                            ((RecordListFieldItem)column).EntityName = entity.Name;
                                            ((RecordListFieldItem)column).EntityLabel = entity.Label;
                                            ((RecordListFieldItem)column).EntityLabelPlural = entity.LabelPlural;
                                        }
                                    }
                                    if (column is RecordListRelationFieldItem)
                                    {
                                        Entity relEntity = GetEntityByFieldId(((RecordListRelationFieldItem)column).FieldId, entities);
                                        if (relEntity != null)
                                        {
                                            ((RecordListRelationFieldItem)column).EntityName = relEntity.Name;
                                            ((RecordListRelationFieldItem)column).EntityLabel = relEntity.Label;
                                            ((RecordListRelationFieldItem)column).EntityLabelPlural = entity.LabelPlural;
                                        }

                                        var relation = relationList.FirstOrDefault(r => r.Id == ((RecordListRelationFieldItem)column).RelationId);
                                        ((RecordListRelationFieldItem)column).RelationName = relation != null ? relation.Name : string.Empty;

                                        if (relation != null)
                                        {
                                            var relationOptions = recordList.RelationOptions.SingleOrDefault(x => x.RelationId == ((RecordListRelationFieldItem)column).RelationId);
                                            if (relationOptions != null)
                                                ((RecordListRelationFieldItem)column).RelationDirection = relationOptions.Direction;
                                            else
                                                ((RecordListRelationFieldItem)column).RelationDirection = "origin-target";
                                        }

                                        Field field = fields.FirstOrDefault(f => f.Id == ((RecordListRelationFieldItem)column).FieldId);
                                        if (field != null)
                                        {
                                            ((RecordListRelationFieldItem)column).DataName = string.Format("$field${0}${1}", ((RecordListRelationFieldItem)column).RelationName, field.Name);
                                            ((RecordListRelationFieldItem)column).FieldName = field.Name;
                                            ((RecordListRelationFieldItem)column).Meta = field;
                                        }
                                    }
                                    if (column is RecordListViewItem)
                                    {
                                        RecordView view = recordViews.FirstOrDefault(v => v.Id == ((RecordListViewItem)column).ViewId);
                                        if (view != null)
                                        {
                                            ((RecordListViewItem)column).DataName = string.Format("$view${0}", view.Name);
                                            ((RecordListViewItem)column).ViewName = view.Name;
                                            ((RecordListViewItem)column).Meta = view;

                                            ((RecordListViewItem)column).EntityName = entity.Name;
                                            ((RecordListViewItem)column).EntityLabel = entity.Label;
                                            ((RecordListViewItem)column).EntityLabelPlural = entity.LabelPlural;
                                        }
                                    }
                                    if (column is RecordListRelationViewItem)
                                    {
                                        Entity relEntity = GetEntityByViewId(((RecordListRelationViewItem)column).ViewId, entities);
                                        if (relEntity != null)
                                        {
                                            ((RecordListRelationViewItem)column).EntityName = relEntity.Name;
                                            ((RecordListRelationViewItem)column).EntityLabel = relEntity.Label;
                                            ((RecordListRelationViewItem)column).EntityLabelPlural = entity.LabelPlural;
                                        }

                                        var relation = relationList.FirstOrDefault(r => r.Id == ((RecordListRelationViewItem)column).RelationId);
                                        ((RecordListRelationViewItem)column).RelationName = relation != null ? relation.Name : string.Empty;

                                        if (relation != null)
                                        {
                                            var relationOptions = recordList.RelationOptions.SingleOrDefault(x => x.RelationId == ((RecordListRelationViewItem)column).RelationId);
                                            if (relationOptions != null)
                                                ((RecordListRelationViewItem)column).RelationDirection = relationOptions.Direction;
                                            else
                                                ((RecordListRelationViewItem)column).RelationDirection = "origin-target";
                                        }

                                        RecordView view = recordViews.FirstOrDefault(v => v.Id == ((RecordListRelationViewItem)column).ViewId);
                                        if (view != null)
                                        {
                                            ((RecordListRelationViewItem)column).DataName = string.Format("$view${0}${1}", ((RecordListRelationViewItem)column).RelationName, view.Name);
                                            ((RecordListRelationViewItem)column).ViewName = view.Name;
                                            ((RecordListRelationViewItem)column).Meta = view;
                                        }
                                    }
                                    if (column is RecordListListItem)
                                    {
                                        RecordList list = recordLists.FirstOrDefault(l => l.Id == ((RecordListListItem)column).ListId);
                                        if (list != null)
                                        {
                                            ((RecordListListItem)column).DataName = string.Format("list${0}", list.Name);
                                            ((RecordListListItem)column).ListName = list.Name;
                                            ((RecordListListItem)column).Meta = list;

                                            ((RecordListListItem)column).EntityName = entity.Name;
                                            ((RecordListListItem)column).EntityLabel = entity.Label;
                                            ((RecordListListItem)column).EntityLabelPlural = entity.LabelPlural;
                                        }
                                    }
                                    if (column is RecordListRelationListItem)
                                    {
                                        Entity relEntity = GetEntityByListId(((RecordListRelationListItem)column).ListId, entities);
                                        if (relEntity != null)
                                        {
                                            ((RecordListRelationListItem)column).EntityName = relEntity.Name;
                                            ((RecordListRelationListItem)column).EntityLabel = relEntity.Label;
                                            ((RecordListRelationListItem)column).EntityLabelPlural = entity.LabelPlural;
                                        }

                                        var relation = relationList.FirstOrDefault(r => r.Id == ((RecordListRelationListItem)column).RelationId);
                                        ((RecordListRelationListItem)column).RelationName = relation != null ? relation.Name : string.Empty;

                                        if (relation != null)
                                        {
                                            var relationOptions = recordList.RelationOptions.SingleOrDefault(x => x.RelationId == ((RecordListRelationListItem)column).RelationId);
                                            if (relationOptions != null)
                                                ((RecordListRelationListItem)column).RelationDirection = relationOptions.Direction;
                                            else
                                                ((RecordListRelationListItem)column).RelationDirection = "origin-target";
                                        }

                                        RecordList list = recordLists.FirstOrDefault(l => l.Id == ((RecordListRelationListItem)column).ListId);
                                        if (list != null)
                                        {
                                            ((RecordListRelationListItem)column).DataName = string.Format("$list${0}${1}", ((RecordListRelationListItem)column).RelationName, list.Name);
                                            ((RecordListRelationListItem)column).ListName = list.Name;
                                            ((RecordListRelationListItem)column).Meta = list;
                                        }
                                    }
                                    if (column is RecordListRelationTreeItem)
                                    {
                                        Entity relEntity = GetEntityByTreeId(((RecordListRelationTreeItem)column).TreeId, entities);
                                        if (relEntity != null)
                                        {
                                            ((RecordListRelationTreeItem)column).EntityName = relEntity.Name;
                                            ((RecordListRelationTreeItem)column).EntityLabel = relEntity.Label;
                                            ((RecordListRelationTreeItem)column).EntityLabelPlural = entity.LabelPlural;
                                        }

                                        var relation = relationList.FirstOrDefault(r => r.Id == ((RecordListRelationTreeItem)column).RelationId);
                                        ((RecordListRelationTreeItem)column).RelationName = relation != null ? relation.Name : string.Empty;

                                        RecordTree tree = relEntity.RecordTrees.FirstOrDefault(l => l.Id == ((RecordListRelationTreeItem)column).TreeId);
                                        if (tree != null)
                                        {
                                            ((RecordListRelationTreeItem)column).DataName = string.Format("$tree${0}${1}", ((RecordListRelationTreeItem)column).RelationName, tree.Name);
                                            ((RecordListRelationTreeItem)column).TreeName = tree.Name;
                                            ((RecordListRelationTreeItem)column).Meta = tree;
                                        }
                                    }
                                }
                            }
                        }
                    }

                    #endregion

                    #region Process Views

                    if (entity.RecordViews != null)
                    {
                        foreach (var recordView in entity.RecordViews)
                        {
                            if (recordView.Regions == null)
                                continue;

                            foreach (var region in recordView.Regions)
                            {
                                if (region.Sections == null)
                                    continue;

                                foreach (var section in region.Sections)
                                {
                                    if (section.Rows == null)
                                        continue;

                                    foreach (var row in section.Rows)
                                    {
                                        if (row.Columns == null)
                                            continue;

                                        foreach (var column in row.Columns)
                                        {
                                            if (column.Items == null)
                                                continue;

                                            foreach (var item in column.Items)
                                            {
                                                if (item is RecordViewFieldItem)
                                                {
                                                    Field field = fields.FirstOrDefault(f => f.Id == ((RecordViewFieldItem)item).FieldId);
                                                    if (field != null)
                                                    {
                                                        //((RecordViewFieldItem)item).DataName = string.Format("$field${0}", field.Name);
                                                        ((RecordViewFieldItem)item).DataName = field.Name;
                                                        ((RecordViewFieldItem)item).FieldName = field.Name;
                                                        ((RecordViewFieldItem)item).Meta = field;

                                                        ((RecordViewFieldItem)item).EntityId = entity.Id;
                                                        ((RecordViewFieldItem)item).EntityName = entity.Name;
                                                        ((RecordViewFieldItem)item).EntityLabel = entity.Label;
                                                        ((RecordViewFieldItem)item).EntityLabelPlural = entity.LabelPlural;
                                                    }
                                                }
                                                if (item is RecordViewListItem)
                                                {
                                                    RecordList list = entity.RecordLists.FirstOrDefault(l => l.Id == ((RecordViewListItem)item).ListId);
                                                    if (list != null)
                                                    {
                                                        ((RecordViewListItem)item).DataName = string.Format("$list${0}", list.Name);
                                                        ((RecordViewListItem)item).Meta = list;
                                                        ((RecordViewListItem)item).ListName = list.Name;

                                                        ((RecordViewListItem)item).EntityId = entity.Id;
                                                        ((RecordViewListItem)item).EntityName = entity.Name;
                                                        ((RecordViewListItem)item).EntityLabel = entity.Label;
                                                        ((RecordViewListItem)item).EntityLabelPlural = entity.LabelPlural;
                                                    }

                                                }
                                                if (item is RecordViewViewItem)
                                                {
                                                    RecordView recView = entity.RecordViews.FirstOrDefault(v => v.Id == ((RecordViewViewItem)item).ViewId);
                                                    if (recView != null)
                                                    {
                                                        ((RecordViewViewItem)item).DataName = string.Format("$view${0}", recView.Name);
                                                        ((RecordViewViewItem)item).Meta = recView;
                                                        ((RecordViewViewItem)item).ViewName = recView.Name;

                                                        ((RecordViewViewItem)item).EntityId = entity.Id;
                                                        ((RecordViewViewItem)item).EntityName = entity.Name;
                                                        ((RecordViewViewItem)item).EntityLabel = entity.Label;
                                                        ((RecordViewViewItem)item).EntityLabelPlural = entity.LabelPlural;
                                                    }
                                                }

                                                if (item is RecordViewRelationFieldItem)
                                                {
                                                    Entity relEntity = GetEntityByFieldId(((RecordViewRelationFieldItem)item).FieldId, entities);
                                                    if (relEntity != null)
                                                    {
                                                        ((RecordViewRelationFieldItem)item).EntityId = relEntity.Id;
                                                        ((RecordViewRelationFieldItem)item).EntityName = relEntity.Name;
                                                        ((RecordViewRelationFieldItem)item).EntityLabel = relEntity.Label;
                                                    }

                                                    var relation = relationList.FirstOrDefault(r => r.Id == ((RecordViewRelationFieldItem)item).RelationId);
                                                    ((RecordViewRelationFieldItem)item).RelationName = relation != null ? relation.Name : string.Empty;

                                                    if (relation != null)
                                                    {
                                                        var relationOptions = recordView.RelationOptions.SingleOrDefault(x => x.RelationId == ((RecordViewRelationFieldItem)item).RelationId);
                                                        if (relationOptions != null)
                                                            ((RecordViewRelationFieldItem)item).RelationDirection = relationOptions.Direction;
                                                        else
                                                            ((RecordViewRelationFieldItem)item).RelationDirection = "origin-target";
                                                    }

                                                    Field field = fields.FirstOrDefault(f => f.Id == ((RecordViewRelationFieldItem)item).FieldId);
                                                    if (field != null)
                                                    {
                                                        ((RecordViewRelationFieldItem)item).DataName = string.Format("$field${0}${1}", ((RecordViewRelationFieldItem)item).RelationName, field.Name);
                                                        ((RecordViewRelationFieldItem)item).Meta = field;
                                                        ((RecordViewRelationFieldItem)item).FieldName = field.Name;
                                                    }
                                                }

                                                if (item is RecordViewRelationViewItem)
                                                {
                                                    var relation = relationList.FirstOrDefault(r => r.Id == ((RecordViewRelationViewItem)item).RelationId);
                                                    ((RecordViewRelationViewItem)item).RelationName = relation != null ? relation.Name : string.Empty;

                                                    if (relation != null)
                                                    {
                                                        var relationOptions = recordView.RelationOptions.SingleOrDefault(x => x.RelationId == ((RecordViewRelationViewItem)item).RelationId);
                                                        if (relationOptions != null)
                                                            ((RecordViewRelationViewItem)item).RelationDirection = relationOptions.Direction;
                                                        else
                                                            ((RecordViewRelationViewItem)item).RelationDirection = "origin-target";
                                                    }

                                                    Entity relEntity = GetEntityByViewId(((RecordViewRelationViewItem)item).ViewId, entities);
                                                    if (relEntity != null)
                                                    {
                                                        ((RecordViewRelationViewItem)item).EntityId = relEntity.Id;
                                                        ((RecordViewRelationViewItem)item).EntityName = relEntity.Name;
                                                        ((RecordViewRelationViewItem)item).EntityLabel = relEntity.Label;

                                                        RecordView view = relEntity.RecordViews.FirstOrDefault(f => f.Id == ((RecordViewRelationViewItem)item).ViewId);
                                                        if (view != null)
                                                        {
                                                            ((RecordViewRelationViewItem)item).DataName = string.Format("$view${0}${1}", ((RecordViewRelationViewItem)item).RelationName, view.Name);
                                                            ((RecordViewRelationViewItem)item).Meta = view;
                                                            ((RecordViewRelationViewItem)item).ViewName = view.Name;
                                                        }
                                                    }
                                                }

                                                if (item is RecordViewRelationListItem)
                                                {
                                                    var relation = relationList.FirstOrDefault(r => r.Id == ((RecordViewRelationListItem)item).RelationId);
                                                    ((RecordViewRelationListItem)item).RelationName = relation != null ? relation.Name : string.Empty;

                                                    if (relation != null)
                                                    {
                                                        var relationOptions = recordView.RelationOptions.SingleOrDefault(x => x.RelationId == ((RecordViewRelationListItem)item).RelationId);
                                                        if (relationOptions != null)
                                                            ((RecordViewRelationListItem)item).RelationDirection = relationOptions.Direction;
                                                        else
                                                            ((RecordViewRelationListItem)item).RelationDirection = "origin-target";
                                                    }

                                                    Entity relEntity = GetEntityByListId(((RecordViewRelationListItem)item).ListId, entities);
                                                    if (relEntity != null)
                                                    {
                                                        ((RecordViewRelationListItem)item).EntityId = relEntity.Id;
                                                        ((RecordViewRelationListItem)item).EntityName = relEntity.Name;
                                                        ((RecordViewRelationListItem)item).EntityLabel = relEntity.Label;
                                                        ((RecordViewRelationListItem)item).EntityLabelPlural = relEntity.LabelPlural;

                                                        RecordList list = relEntity.RecordLists.FirstOrDefault(f => f.Id == ((RecordViewRelationListItem)item).ListId);
                                                        if (list != null)
                                                        {
                                                            ((RecordViewRelationListItem)item).DataName = string.Format("$list${0}${1}", ((RecordViewRelationListItem)item).RelationName, list.Name);
                                                            ((RecordViewRelationListItem)item).Meta = list;
                                                            ((RecordViewRelationListItem)item).ListName = list.Name;
                                                        }
                                                    }
                                                }

                                                if (item is RecordViewRelationTreeItem)
                                                {
                                                    Entity relEntity = GetEntityByTreeId(((RecordViewRelationTreeItem)item).TreeId, entities);
                                                    if (relEntity != null)
                                                    {
                                                        ((RecordViewRelationTreeItem)item).EntityId = relEntity.Id;
                                                        ((RecordViewRelationTreeItem)item).EntityName = relEntity.Name;
                                                        ((RecordViewRelationTreeItem)item).EntityLabel = relEntity.Label;
                                                        ((RecordViewRelationTreeItem)item).EntityLabelPlural = entity.LabelPlural;
                                                    }

                                                    var relation = relationList.FirstOrDefault(r => r.Id == ((RecordViewRelationTreeItem)item).RelationId);
                                                    ((RecordViewRelationTreeItem)item).RelationName = relation != null ? relation.Name : string.Empty;

                                                    RecordTree tree = relEntity.RecordTrees.FirstOrDefault(l => l.Id == ((RecordViewRelationTreeItem)item).TreeId);
                                                    if (tree != null)
                                                    {

                                                        ((RecordViewRelationTreeItem)item).DataName = string.Format("$tree${0}${1}", ((RecordViewRelationTreeItem)item).RelationName, tree.Name);
                                                        ((RecordViewRelationTreeItem)item).TreeName = tree.Name;
                                                        ((RecordViewRelationTreeItem)item).Meta = tree;
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }

                            if (recordView.Sidebar != null)
                            {
                                foreach (var item in recordView.Sidebar.Items)
                                {
                                    if (item is RecordViewSidebarListItem)
                                    {
                                        RecordList list = entity.RecordLists.FirstOrDefault(l => l.Id == ((RecordViewSidebarListItem)item).ListId);
                                        if (list != null)
                                        {
                                            ((RecordViewSidebarListItem)item).DataName = string.Format("$list${0}", list.Name);
                                            ((RecordViewSidebarListItem)item).Meta = list;
                                            ((RecordViewSidebarListItem)item).ListName = list.Name;

                                            ((RecordViewSidebarListItem)item).EntityId = entity.Id;
                                            ((RecordViewSidebarListItem)item).EntityName = entity.Name;
                                            ((RecordViewSidebarListItem)item).EntityLabel = entity.Label;
                                            ((RecordViewSidebarListItem)item).EntityLabelPlural = entity.LabelPlural;
                                        }

                                    }
                                    if (item is RecordViewSidebarViewItem)
                                    {
                                        RecordView recView = entity.RecordViews.FirstOrDefault(v => v.Id == ((RecordViewSidebarViewItem)item).ViewId);
                                        if (recView != null)
                                        {
                                            ((RecordViewSidebarViewItem)item).DataName = string.Format("$view${0}", recView.Name);
                                            ((RecordViewSidebarViewItem)item).Meta = recView;
                                            ((RecordViewSidebarViewItem)item).ViewName = recView.Name;

                                            ((RecordViewSidebarViewItem)item).EntityId = entity.Id;
                                            ((RecordViewSidebarViewItem)item).EntityName = entity.Name;
                                            ((RecordViewSidebarViewItem)item).EntityLabel = entity.Label;
                                            ((RecordViewSidebarViewItem)item).EntityLabelPlural = entity.LabelPlural;
                                        }
                                    }
                                    if (item is RecordViewSidebarRelationViewItem)
                                    {
                                        var relation = relationList.FirstOrDefault(r => r.Id == ((RecordViewSidebarRelationViewItem)item).RelationId);
                                        ((RecordViewSidebarRelationViewItem)item).RelationName = relation != null ? relation.Name : string.Empty;

                                        if (relation != null)
                                        {
                                            var relationOptions = recordView.RelationOptions.SingleOrDefault(x => x.RelationId == ((RecordViewSidebarRelationViewItem)item).RelationId);
                                            if (relationOptions != null)
                                                ((RecordViewSidebarRelationViewItem)item).RelationDirection = relationOptions.Direction;
                                            else
                                                ((RecordViewSidebarRelationViewItem)item).RelationDirection = "origin-target";
                                        }

                                        Entity relEntity = GetEntityByViewId(((RecordViewSidebarRelationViewItem)item).ViewId, entities);
                                        if (relEntity != null)
                                        {
                                            ((RecordViewSidebarRelationViewItem)item).EntityId = relEntity.Id;
                                            ((RecordViewSidebarRelationViewItem)item).EntityName = relEntity.Name;
                                            ((RecordViewSidebarRelationViewItem)item).EntityLabel = relEntity.Label;

                                            RecordView view = relEntity.RecordViews.FirstOrDefault(f => f.Id == ((RecordViewSidebarRelationViewItem)item).ViewId);
                                            if (view != null)
                                            {
                                                ((RecordViewSidebarRelationViewItem)item).DataName = string.Format("$view${0}${1}", ((RecordViewSidebarRelationViewItem)item).RelationName, view.Name);
                                                ((RecordViewSidebarRelationViewItem)item).Meta = view;
                                                ((RecordViewSidebarRelationViewItem)item).ViewName = view.Name;
                                            }
                                        }
                                    }

                                    if (item is RecordViewSidebarRelationListItem)
                                    {
                                        var relation = relationList.FirstOrDefault(r => r.Id == ((RecordViewSidebarRelationListItem)item).RelationId);
                                        ((RecordViewSidebarRelationListItem)item).RelationName = relation != null ? relation.Name : string.Empty;

                                        if (relation != null)
                                        {
                                            var relationOptions = recordView.RelationOptions.SingleOrDefault(x => x.RelationId == ((RecordViewSidebarRelationListItem)item).RelationId);
                                            if (relationOptions != null)
                                                ((RecordViewSidebarRelationListItem)item).RelationDirection = relationOptions.Direction;
                                            else
                                                ((RecordViewSidebarRelationListItem)item).RelationDirection = "origin-target";
                                        }

                                        Entity relEntity = GetEntityByListId(((RecordViewSidebarRelationListItem)item).ListId, entities);
                                        if (relEntity != null)
                                        {
                                            ((RecordViewSidebarRelationListItem)item).EntityId = relEntity.Id;
                                            ((RecordViewSidebarRelationListItem)item).EntityName = relEntity.Name;
                                            ((RecordViewSidebarRelationListItem)item).EntityLabel = relEntity.Label;
                                            ((RecordViewSidebarRelationListItem)item).EntityLabelPlural = relEntity.LabelPlural;

                                            RecordList list = relEntity.RecordLists.FirstOrDefault(f => f.Id == ((RecordViewSidebarRelationListItem)item).ListId);
                                            if (list != null)
                                            {
                                                ((RecordViewSidebarRelationListItem)item).DataName = string.Format("$list${0}${1}", ((RecordViewSidebarRelationListItem)item).RelationName, list.Name);
                                                ((RecordViewSidebarRelationListItem)item).Meta = list;
                                                ((RecordViewSidebarRelationListItem)item).ListName = list.Name;
                                            }
                                        }
                                    }

                                    if (item is RecordViewSidebarRelationTreeItem)
                                    {
                                        Entity relEntity = GetEntityByTreeId(((RecordViewSidebarRelationTreeItem)item).TreeId, entities);
                                        if (relEntity != null)
                                        {
                                            ((RecordViewSidebarRelationTreeItem)item).EntityName = relEntity.Name;
                                            ((RecordViewSidebarRelationTreeItem)item).EntityLabel = relEntity.Label;
                                            ((RecordViewSidebarRelationTreeItem)item).EntityLabelPlural = entity.LabelPlural;
                                        }

                                        var relation = relationList.FirstOrDefault(r => r.Id == ((RecordViewSidebarRelationTreeItem)item).RelationId);
                                        ((RecordViewSidebarRelationTreeItem)item).RelationName = relation != null ? relation.Name : string.Empty;

                                        RecordTree tree = relEntity.RecordTrees.FirstOrDefault(l => l.Id == ((RecordViewSidebarRelationTreeItem)item).TreeId);
                                        if (tree != null)
                                        {
                                            ((RecordViewSidebarRelationTreeItem)item).DataName = string.Format("$tree${0}${1}", ((RecordViewSidebarRelationTreeItem)item).RelationName, tree.Name);
                                            ((RecordViewSidebarRelationTreeItem)item).TreeName = tree.Name;
                                            ((RecordViewSidebarRelationTreeItem)item).Meta = tree;
                                        }
                                    }
                                }
                            }
                        }
                    }

                    #endregion

                    #region Process Trees

                    if (entity.RecordTrees != null)
                    {

                        foreach (var recordTree in entity.RecordTrees)
                        {
                            foreach (RecordTreeNode node in recordTree.RootNodes)
                            {
                                IStorageRecordRepository recRep = Storage.GetRecordRepository();
                                var recData = recRep.Find(entity.Name, node.RecordId);
                                if (recData != null)
                                {
                                    var idField = entity.Fields.SingleOrDefault(x => x.Id == recordTree.NodeIdFieldId);
                                    if (idField == null)
                                        throw new Exception("Cannot initialize tree '" + recordTree.Name + "'. Node id field is missing in entity meta.");

                                    var parentIdField = entity.Fields.SingleOrDefault(x => x.Id == recordTree.NodeParentIdFieldId);
                                    if (parentIdField == null)
                                        throw new Exception("Cannot initialize tree '" + recordTree.Name + "'. Parent id field is missing in entity meta.");

                                    var nameField = entity.Fields.SingleOrDefault(x => x.Id == recordTree.NodeNameFieldId);
                                    if (nameField == null)
                                        throw new Exception("Cannot initialize tree '" + recordTree.Name + "'. Node name field is missing in entity meta.");

                                    var labelField = entity.Fields.SingleOrDefault(x => x.Id == recordTree.NodeLabelFieldId);
                                    if (labelField == null)
                                        throw new Exception("Cannot initialize tree '" + recordTree.Name + "'. Node label field is missing in entity meta.");

                                    var valuePair = recData.SingleOrDefault(x => x.Key == idField.Name);
                                    node.Id = (valuePair.Value as Guid?) ?? Guid.Empty;

                                    valuePair = recData.SingleOrDefault(x => x.Key == parentIdField.Name);
                                    node.ParentId = valuePair.Value as Guid?;

                                    valuePair = recData.SingleOrDefault(x => x.Key == nameField.Name);
                                    node.Name = (valuePair.Value ?? string.Empty).ToString();

                                    valuePair = recData.SingleOrDefault(x => x.Key == labelField.Name);
                                    node.Label = (valuePair.Value ?? string.Empty).ToString();
                                }
                            }
                        }
                    }

                    #endregion
                }

                EntityList entityList = new EntityList();
                entityList.Entities = entities;
                response.Object = entityList;
            }
            catch (Exception e)
            {
                response.Timestamp = DateTime.UtcNow;
                response.Success = false;
            #if DEBUG
                response.Message = e.Message + e.StackTrace;
            #else
                response.Message = "An internal error occurred!";
            #endif
                return response;
            }

            response.Timestamp = DateTime.Now;

            return response;
        }