public void doesnt_returns_paged_results_when_page_and_take_not_specified()
        {
            var result = _source.GetRecords(_entity);

            Assert.Equal(3, result.Records.Count);
            Assert.Equal(0, result.TotalItems);
            Assert.Equal(0, result.TotalPages);
        }
        public void empty_search_gives_two_records()
        {
            var result = _source.GetRecords(
                _entity,
                searchQuery: "");

            Assert.Equal(2, result.Records.Count);
        }
Exemplo n.º 3
0
        public virtual ActionResult Index(string entityName, TableInfo tableInfo)
        {
            var entity = Admin.EntitiesTypes
                         .FirstOrDefault(x => x.Name == entityName);

            if (entity == null)
            {
                throw new NoNullAllowedException("entity is null");
            }
            entity.Fill(Request);
            var filters      = _filterFactory.BuildFilters(entity).ToList();
            var pagedRecords = _entitiesSource.GetRecords(
                entity,
                filters,
                tableInfo.SearchQuery,
                tableInfo.Order,
                tableInfo.OrderDirection,
                false,
                tableInfo.Page,
                tableInfo.PerPage);

            if (pagedRecords.Records.IsNullOrEmpty() && tableInfo.Page > 1)
            {
                return(RedirectToAction(
                           "Index",
                           PrepareRouteValues(
                               entityName,
                               "1",
                               filters,
                               tableInfo)));
            }

            var url = Url.Action(
                "Index",
                PrepareRouteValues(
                    entityName,
                    "-page-",
                    filters,
                    tableInfo))
                      .Replace("-page-", "{0}");

            var model = new EntitiesIndexModel
            {
                Data    = pagedRecords.Records,
                Columns = entity.DisplayProperties
                          .Select(x => new Column(x, tableInfo.Order, tableInfo.OrderDirection)).ToList(),
                Entity = entity,
                Pager  =
                    new PagerInfo(url, tableInfo.PerPage, tableInfo.Page, pagedRecords.TotalItems),
                Filters       = filters.Where(x => x.DisplayInUI).ToList(),
                TableInfo     = tableInfo,
                Configuration = _configuration
            };

            return(View(model));
        }
Exemplo n.º 4
0
        public void ascending_ordering_gives_correct_result()
        {
            var result = _source.GetRecords(
                _entity,
                order: "ProductName",
                orderDirection: "ASC");

            Assert.Equal(2, result.Records.Count);
            Assert.Equal("Product", result.Records[0].Values[1].AsString);
            Assert.Equal("Product2", result.Records[1].Values[1].AsString);
        }
        public void determine_value_using_to_string_method()
        {
            DB.Products.Insert(ProductName: "Product");

            var productEntity = _admin.GetEntity("Product");

            var result = _source.GetRecords(productEntity, determineDisplayValue: true);

            Assert.Equal(1, result.Records.Count);
            Assert.Equal("Product_ToString", result.Records[0].ToString());
        }
        public void determine_value_using_to_string_method()
        {
            DB.Products.Insert(ProductName: "Product");

            var productEntity =
                Admin.EntitiesTypes.FirstOrDefault(x => x.Name == "Product");

            var result = _source.GetRecords(productEntity, determineDisplayValue: true);

            Assert.Equal(1, result.Records.Count);
            Assert.Equal("Product_ToString", result.Records[0].DisplayName);
        }
Exemplo n.º 7
0
        public IList <GroupProperties> PrepareGroups(
            EntityRecord entityRecord,
            bool getKey = true,
            string key  = null)
        {
            var creatableProperties = entityRecord.Entity
                                      .GetDefaultCreateProperties(getKey);

            var groups = entityRecord.Values
                         .Where(value => creatableProperties.Contains(value.Property))
                         .GroupBy(x => x.Property.Group)
                         .Select(x => new GroupProperties
            {
                GroupName   = x.FirstOrDefault().Property.Group,
                IsCollapsed = entityRecord.Entity.Groups
                              .FirstOrDefault(y => y.GroupName == x.FirstOrDefault().Property.Group).IsCollapsed,
                PropertiesValues = x.ToList()
            });

            foreach (var foreignValue in groups.SelectMany(x => x.PropertiesValues)
                     .Where(x => x.Property.IsForeignKey && x.Property.ForeignEntity != null))
            {
                var entityToLoad = GetEntityToLoad(foreignValue.Property);
                var records      = _source.GetRecords(entityToLoad, determineDisplayValue: true, loadForeignKeys: foreignValue.Property.IsManyToMany).Records;
                foreignValue.PossibleValues = records.ToDictionary(x => x.JoinedKeysValues, x => x.ToString());
                if (foreignValue.Property.TypeInfo.IsCollection)
                {
                    foreignValue.Values = GetRecordValues(foreignValue.Property, key, records).ToList();
                }
            }

            return(groups.ToList());
        }
        public void empty_changes_filter_should_return_two_records()
        {
            var result = _source.GetRecords(_entity);

            Assert.Equal(2, result.Records.Count);

            var filters = new List <BaseFilter>();

            result = _source.GetRecords(_entity, filters);
            Assert.Equal(2, result.Records.Count);

            filters = new List <BaseFilter>
            {
                new ChangeEntityFilter(_property)
            };
            result = _source.GetRecords(_entity, filters);
            Assert.Equal(2, result.Records.Count);
        }
        public void empty_date_time_filter_should_return_four_records()
        {
            var result = _source.GetRecords(_entity);

            Assert.Equal(4, result.Records.Count);

            var filters = new List <BaseFilter>();

            result = _source.GetRecords(_entity, filters);
            Assert.Equal(4, result.Records.Count);

            filters = new List <BaseFilter>
            {
                new DateTimeEntityFilter(_fakeClock, _property)
            };
            result = _source.GetRecords(_entity, filters);
            Assert.Equal(4, result.Records.Count);
        }
        public void empty_enum_filter_should_return_three_records()
        {
            var result = _source.GetRecords(_entity);

            Assert.Equal(3, result.Records.Count);

            var filters = new List <BaseFilter>();

            result = _source.GetRecords(_entity, filters);
            Assert.Equal(3, result.Records.Count);

            filters = new List <BaseFilter>
            {
                new EnumEntityFilter(_property)
            };
            result = _source.GetRecords(_entity, filters);
            Assert.Equal(3, result.Records.Count);
        }
Exemplo n.º 11
0
        //private void FileHandle(Entity entity)
        //{
        //    foreach (var property in entity
        //        .CreateProperties(getForeignCollection: false)
        //        .Where(x => x.TypeInfo.DataType == DataType.File))
        //    {
        //        if (property.TypeInfo.Type == typeof(string))
        //        {
        //            // we must save file to disk and save file path in db
        //            var file = (HttpPostedFile)property.Value.Raw;
        //            var fileName = String.Empty;
        //            if (property.ImageOptions.NameCreation == NameCreation.UserInput)
        //            {
        //                fileName = "test.jpg";
        //            }
        //            fileName = FileUpload.SaveImage(file, fileName, property.ImageOptions.NameCreation, property.ImageOptions.Settings.ToArray());

        //            property.Value.Raw = fileName;
        //        }
        //        else
        //        {
        //            // we must save file in db as byte array

        //            var file = (HttpPostedFile)property.Value.Raw;
        //            var bytes = FileUpload.GetImageByte(file, property.ImageOptions.Settings.ToArray());
        //            property.Value.Raw = bytes;
        //        }
        //    }
        //}

        public IList <GroupProperties> PrepareGroups(Entity entity, bool getKey = true, string key = null)
        {
            var properties = entity.CreateProperties(getKey);

            foreach (var foreign in properties.Where(x => x.IsForeignKey))
            {
                var records = _source.GetRecords(foreign.ForeignEntity, determineDisplayValue: true);
                foreign.Value.PossibleValues = records.ToDictionary(x => x.KeyValue, x => x.DisplayName);
                if (foreign.TypeInfo.IsCollection)
                {
                    foreign.Value.Values = records.Where(x => x.Values.Any(y => y.Property.ForeignEntity == entity && y.AsString == key)).Select(x => x.KeyValue).OfType <object>().ToList();
                }
            }

            return(entity.Groups);
        }
Exemplo n.º 12
0
        private void AddForeignsUpdate(DbCommand cmd, Entity entity)
        {
            var sbUpdates  = new StringBuilder();
            var paramIndex = cmd.Parameters.Count;

            foreach (var property in entity.GetForeignsForUpdate())
            {
                var actualRecords = _source.GetRecords(
                    property.ForeignEntity,
                    new List <IEntityFilter>
                {
                    new ForeignEntityFilter(
                        entity.Key,
                        entity.Key.Value.Raw.ToStringSafe())
                });
                var idsToRemoveRelation = actualRecords
                                          .Select(x => x.KeyValue)
                                          .Except(property.Value.Values.OfType <string>())
                                          .ToList();
                if (idsToRemoveRelation.Any())
                {
                    var removeValues = string.Join(",", idsToRemoveRelation.Select(x => "@" + paramIndex++));
                    sbUpdates.AppendLine();
                    sbUpdates.AppendFormat(
                        RelatedRecordsUpdateSqlFormat,
                        property.ForeignEntity.TableName,
                        entity.Key.ColumnName,
                        paramIndex++,
                        property.ForeignEntity.Key.ColumnName,
                        removeValues);
                    cmd.AddParams(idsToRemoveRelation);
                    cmd.AddParams(null);
                }

                var values = string.Join(",", property.Value.Values.Select(x => "@" + paramIndex++));
                cmd.CommandText +=
                    Environment.NewLine +
                    RelatedRecordsUpdateSqlFormat.Fill(
                        property.ForeignEntity.TableName,
                        entity.Key.ColumnName,
                        paramIndex++,
                        property.ForeignEntity.Key.ColumnName,
                        values);
                cmd.AddParams(property.Value.Values.ToArray());
                cmd.AddParam(entity.Key.Value.Raw);
            }
        }
Exemplo n.º 13
0
        private PagedRecords GetRecords(
            Entity entity,
            NameValueCollection request,
            TableInfo tableInfo,
            Action <IList <BaseFilter> > filtersMutator)
        {
            AddDefaultOrder(entity, tableInfo);

            var filters = BuildFilters(entity, request, filtersMutator).ToList();

            var pagedRecords = _entitiesSource.GetRecords(
                entity,
                filters,
                tableInfo.SearchQuery,
                tableInfo.Order,
                tableInfo.OrderDirection,
                false,
                tableInfo.Page,
                tableInfo.PerPage);

            pagedRecords.Filters = filters;

            return(pagedRecords);
        }
Exemplo n.º 14
0
        private void AddForeignsUpdate(DbCommand cmd, EntityRecord entityRecord)
        {
            if (entityRecord.Keys.Count > 1)
            {
                return;
            }
            var sbUpdates  = new StringBuilder();
            var paramIndex = cmd.Parameters.Count;

            foreach (var propertyValue in entityRecord.Values.WhereOneToMany()
                     .Where(x => x.Property.IsManyToMany == false))
            {
                var actualRecords = _source.GetRecords(
                    propertyValue.Property.ForeignEntity,
                    new List <BaseFilter>
                {
                    new ForeignEntityFilter(
                        entityRecord.Entity.Keys.FirstOrDefault(),
                        entityRecord.Keys.FirstOrDefault().Raw.ToStringSafe())
                }).Records;
                var idsToRemoveRelation = actualRecords
                                          .Select(x => x.JoinedKeysValues)
                                          .Except(propertyValue.Values.Select(x => x.ToStringSafe()))
                                          .ToList();
                if (idsToRemoveRelation.Any())
                {
                    var values2 =
                        idsToRemoveRelation.Select(
                            x => x.Split(Const.KeyColSeparator).Select(y => y.Trim()).ToList()).ToList();
                    var whereParts2    = new List <string>();
                    var addNullSqlPart = true;
                    for (int i = 0; i < propertyValue.Property.ForeignEntity.Keys.Count; i++)
                    {
                        var key          = propertyValue.Property.ForeignEntity.Keys[i];
                        var joinedValues = string.Join(",", values2.Select(x => "@" + paramIndex++));
                        whereParts2.Add("{0} In ({1})".Fill(key.Column, joinedValues));
                        addNullSqlPart = joinedValues.HasValue();
                        if (addNullSqlPart == false)
                        {
                            break;
                        }
                        cmd.AddParams(values2.Select(x => x[i]).OfType <object>().ToArray());
                    }
                    if (addNullSqlPart)
                    {
                        var wherePart2 = string.Join(" AND ", whereParts2);
                        sbUpdates.AppendLine();
                        sbUpdates.AppendLine("-- set to null update");
                        sbUpdates.AppendFormat(BuildForeignUpdateSql(
                                                   propertyValue.Property.ForeignEntity.Table,
                                                   entityRecord.Entity.Keys.FirstOrDefault().Column,
                                                   (paramIndex++).ToString(),
                                                   wherePart2));
                        cmd.AddParam(null);
                    }
                }

                var values =
                    propertyValue.Values.Select(
                        x => x.ToStringSafe().Split(Const.KeyColSeparator).Select(y => y.Trim()).ToList()).ToList();
                var whereParts = new List <string>();
                var addSqlPart = true;
                for (int i = 0; i < propertyValue.Property.ForeignEntity.Keys.Count; i++)
                {
                    var key          = propertyValue.Property.ForeignEntity.Keys[i];
                    var joinedValues = string.Join(",", values.Select(x => "@" + paramIndex++));
                    whereParts.Add("{0} In ({1})".Fill(key.Column, joinedValues));
                    addSqlPart = joinedValues.HasValue();
                    if (addSqlPart == false)
                    {
                        break;
                    }
                    cmd.AddParams(values.Select(x => x[i]).OfType <object>().ToArray());
                }
                if (addSqlPart)
                {
                    var wherePart = string.Join(" AND ", whereParts);
                    sbUpdates.AppendLine();
                    sbUpdates.Append(BuildForeignUpdateSql(
                                         propertyValue.Property.ForeignEntity.Table,
                                         entityRecord.Entity.Keys.FirstOrDefault().Column,
                                         (paramIndex++).ToString(),
                                         wherePart));
                    cmd.AddParam(entityRecord.Keys.FirstOrDefault().Raw);
                }
            }

            cmd.CommandText += Environment.NewLine + "-- update foreign entities records" + sbUpdates.ToString();
        }
Exemplo n.º 15
0
        private void AddForeignsUpdate(DbCommand cmd, Entity entity)
        {
            var sbUpdates  = new StringBuilder();
            var paramIndex = cmd.Parameters.Count;

            foreach (var property in entity.GetForeignsForUpdate())
            {
                var actualRecords = _source.GetRecords(
                    property.ForeignEntity,
                    new List <BaseFilter>
                {
                    new ForeignEntityFilter(
                        entity.Key.FirstOrDefault(),
                        entity.Key.FirstOrDefault().Value.Raw.ToStringSafe())
                }).Records;
                var idsToRemoveRelation = actualRecords
                                          .Select(x => x.JoinedKeyValue)
                                          .Except(property.Value.Values.Select(x => x.ToStringSafe()))
                                          .ToList();
                if (idsToRemoveRelation.Any())
                {
                    var values2 =
                        idsToRemoveRelation.Select(
                            x => x.Split(Const.KeyColSeparator).Select(y => y.Trim()).ToList()).ToList();
                    var whereParts2 = new List <string>();
                    for (int i = 0; i < property.ForeignEntity.Key.Count; i++)
                    {
                        var key          = property.ForeignEntity.Key[i];
                        var joinedValues = string.Join(",", values2.Select(x => "@" + paramIndex++));
                        whereParts2.Add("{0} In ({1})".Fill(key.ColumnName, joinedValues));
                        cmd.AddParams(values2.Select(x => x[i]).OfType <object>().ToArray());
                    }
                    var wherePart2 = string.Join(" AND ", whereParts2);
                    sbUpdates.AppendLine();
                    sbUpdates.AppendLine("-- set to null update");
                    sbUpdates.AppendFormat(
                        RelatedRecordsUpdateSqlFormat,
                        property.ForeignEntity.TableName,
                        entity.Key.FirstOrDefault().ColumnName,
                        paramIndex++,
                        wherePart2);
                    cmd.AddParam(null);
                }

                var values =
                    property.Value.Values.Select(
                        x => x.ToStringSafe().Split(Const.KeyColSeparator).Select(y => y.Trim()).ToList()).ToList();
                var whereParts = new List <string>();
                for (int i = 0; i < property.ForeignEntity.Key.Count; i++)
                {
                    var key          = property.ForeignEntity.Key[i];
                    var joinedValues = string.Join(",", values.Select(x => "@" + paramIndex++));
                    whereParts.Add("{0} In ({1})".Fill(key.ColumnName, joinedValues));
                    cmd.AddParams(values.Select(x => x[i]).OfType <object>().ToArray());
                }
                var wherePart = string.Join(" AND ", whereParts);
                sbUpdates.AppendLine();
                sbUpdates.AppendFormat(
                    RelatedRecordsUpdateSqlFormat,
                    property.ForeignEntity.TableName,
                    entity.Key.FirstOrDefault().ColumnName,
                    paramIndex++,
                    wherePart);
                cmd.AddParam(entity.Key.FirstOrDefault().Value.Raw);
            }

            cmd.CommandText += sbUpdates.ToString();
        }