Exemplo n.º 1
0
        public bool Validate(EntityRecord entityRecord)
        {
            var instance = entityRecord.CreateInstance();
            var context = new ValidationContext(instance);
            var isValid = true;
            foreach (var propertyValue in entityRecord.Values)
            {
                if (propertyValue.Property.TypeInfo.IsFile)
                {
                    var result = _fileValidator.Validate(propertyValue);
                    if (result == false)
                        isValid = false;
                }

                context.DisplayName = propertyValue.Property.Display;
                foreach (var validator in propertyValue.Property.Validators)
                {
                    try
                    {
                        validator.Validate(propertyValue.Raw, context);
                    }
                    catch (ValidationException ex)
                    {
                        isValid = false;
                        _notificator.AddModelError(propertyValue.Property.Name, ex.Message);
                    }
                    catch (Exception ex)
                    {
                        _log.Error(ex);
                    }
                }
            }
            return isValid;
        }
Exemplo n.º 2
0
 private EntityRecord create_product_entity_record()
 {
     var entityRecord = new EntityRecord(_admin.GetEntity<Product>());
     var values = new Dictionary<string, object>
     {
         { "ProductName", "Test" }
     };
     entityRecord.Fill(values);
     return entityRecord;
 }
Exemplo n.º 3
0
 public DataRow(EntityRecord entityRecord)
     : this()
 {
     foreach (var propertyValue in entityRecord.Values)
     {
         Values.Add(new CellValue
         {
             Raw = propertyValue.Raw,
             Property = propertyValue.Property
         });
     }
 }
Exemplo n.º 4
0
        public IEnumerable<BaseFilter> BuildFilters(EntityRecord entityRecord)
        {
            var filters = GetAllFilters();

            foreach (var propertyValue in entityRecord.Values)
            {
                var filterType = GetMatchingFilter(propertyValue.Property, filters);
                if (filterType == null)
                    continue;

                yield return CreateInstance(filterType, propertyValue);
            }
        }
Exemplo n.º 5
0
        public static EntityRecord CreateRecord(
            Entity entity,
            IValueProvider valueProvider,
            HttpFileCollectionBase files,
            Func<Property, object> defaultValueResolver = null)
        {
            var entityRecord = new EntityRecord(entity);
            foreach (var property in entity.Properties.DistinctBy(x => x.Column))
            {
                var propertyValue = property.TypeInfo.IsFile ?
                    GetPropertyValueForFile(property, valueProvider, files) :
                    GetPropertyValue(property, valueProvider, defaultValueResolver);
                entityRecord.Values.Add(propertyValue);
            }

            return entityRecord;
        }
Exemplo n.º 6
0
        public void creates_record_and_does_not_create_entity_change_when_is_not_added()
        {
            register_default_entities();

            var values = new Dictionary<string, object>
            {
                { "ProductName", "Product" },
                { "Discontinued", false }
            };
            var entityRecord = new EntityRecord(_productEntity);
            entityRecord.Fill(values);
            _creator.Create(entityRecord);

            var products = DB.Products.All().ToList();
            Assert.Equal(1, products.Count);

            A.CallTo(() => _user.CurrentUserName()).MustNotHaveHappened();
            var changes = DB.EntityChanges.All().ToList();
            Assert.Equal(0, changes.Count);
        }
Exemplo n.º 7
0
        public void creates_record_with_one_to_many_foreign_property()
        {
            Entity<Category>.Register();
            register_default_entities();

            var categoryId = DB.Categories.Insert(CategoryName: "Category").CategoryID;

            var values = new Dictionary<string, object>
            {
                { "ProductName", "Product" },
                { "Discontinued", false },
                { "CategoryID", categoryId }
            };
            var entityRecord = new EntityRecord(_productEntity);
            entityRecord.Fill(values);
            _creator.Create(entityRecord);

            var products = (List<dynamic>)DB.Products.All().ToList();
            Assert.Equal(1, products.Count);
            Assert.Equal(categoryId, products.First().CategoryID);
        }
Exemplo n.º 8
0
        public void updates_record_with_many_to_one_foreign_property()
        {
            set_up_test();
            var category = DB.Categories.Insert(CategoryName: "Category");
            var product2 = DB.Products.Insert(ProductName: "Product2", CategoryId: category.CategoryID);

            _entityRecord = _source.GetEntityRecord(
                _admin.GetEntity<Category>(),
                category.CategoryID.ToString());

            _entityRecord["CategoryName"].Raw = "Category";
            _entityRecord["Products"].Values = new List<object> { _productId };
            _updater.Update(_entityRecord);

            var categories = (List<dynamic>)DB.Categories.All().ToList();
            Assert.Equal(1, categories.Count);
            var products = (List<dynamic>)DB.Products.All().ToList();
            Assert.Equal(2, products.Count);
            var product = products.First(x => x.ProductID == _productId);
            product2 = products.First(x => x.ProductID == product2.ProductID);
            Assert.Null(product2.CategoryID);
            Assert.Equal(category.CategoryID, product.CategoryID);
        }
Exemplo n.º 9
0
        private void set_up_test()
        {
            Entity<Product>.Register().ReadAttributes();
            Entity<Category>.Register().ReadAttributes();
            _admin.Initialise(ConnectionStringName);

            _productId = DB.Products.Insert(ProductName: "Product").ProductID;
            _entityRecord = _source.GetEntityRecord(
                _admin.GetEntity<Product>(), 
                _productId.ToString());
        }
Exemplo n.º 10
0
        public ActionResult Create(string entityName, FormCollection collection)
        {
            var entity = _admin.GetEntity(entityName);
            if (entity == null)
            {
                return RedirectToAction("NotFound", new { entityName });
            }

            try
            {
                var savedId = _entityService.Create(entity, collection, Request.Files);
                if (savedId != null)
                {
                    _notificator.Success(IlaroAdminResources.AddSuccess, entity.Verbose.Singular);

                    return SaveOrUpdateSucceed(entityName, savedId);
                }
            }
            catch (Exception ex)
            {
                _log.Error(ex);
                _notificator.Error(ex.Message);
            }

            var entityRecord = new EntityRecord(entity);
            entityRecord.Fill(collection, Request.Files);

            var model = new EntityCreateModel
            {
                Entity = entity,
                PropertiesGroups = _entityService.PrepareGroups(entityRecord)
            };

            return View(model);
        }
Exemplo n.º 11
0
        public ActionResult Edit(string entityName, string key, FormCollection collection)
        {
            var entity = _admin.GetEntity(entityName);
            if (entity == null)
            {
                return RedirectToAction("NotFound", new { entityName });
            }

            try
            {
                var isSuccess = _entityService.Edit(entity, key, collection, Request.Files);
                if (isSuccess)
                {
                    _notificator.Success(IlaroAdminResources.EditSuccess, entity.Verbose.Singular);

                    return SaveOrUpdateSucceed(entityName, key);
                }
            }
            catch (Exception ex)
            {
                _log.Error(ex);
                _notificator.Error(ex.Message);
            }


            var entityRecord = new EntityRecord(entity);
            entityRecord.Fill(key, collection, Request.Files);

            var model = new EntityEditModel
            {
                Entity = entity,
                Record = entityRecord,
                PropertiesGroups = _entityService.PrepareGroups(entityRecord, getKey: false, key: key)
            };

            return View(model);
        }
Exemplo n.º 12
0
 internal static EntityRecord CreateEmpty(Entity entity)
 {
     var entityRecord = new EntityRecord(entity);
     entityRecord.Fill(new Dictionary<string, object>());
     return entityRecord;
 }
Exemplo n.º 13
0
        public static EntityRecord CreateRecord(
            Entity entity,
            IDictionary<string, object> item,
            string prefix = "",
            Func<object, object> valueMutator = null)
        {
            var entityRecord = new EntityRecord(entity);
            foreach (var property in entity.Properties)
            {
                var value = item.ContainsKey(prefix + property.Column.Undecorate()) ?
                        item[prefix + property.Column.Undecorate()] :
                        null;
                if (valueMutator != null)
                    value = valueMutator(value);
                entityRecord.Values.Add(new PropertyValue(property)
                {
                    Raw = value
                });
            }

            return entityRecord;
        }
Exemplo n.º 14
0
        public void creates_record_with_many_to_one_foreign_property()
        {
            register_default_entities();

            var productId = DB.Products.Insert(ProductName: "Product").ProductID;
            var categoryEntity = _admin.GetEntity<Category>();

            var entityRecord = new EntityRecord(categoryEntity);
            entityRecord.Fill(new Dictionary<string, object>());
            entityRecord["CategoryName"].Raw = "Category";
            entityRecord["Products"].Values.Add(productId);
            _creator.Create(entityRecord);

            var categories = (List<dynamic>)DB.Categories.All().ToList();
            Assert.Equal(1, categories.Count);
            var products = (List<dynamic>)DB.Products.All().ToList();
            Assert.Equal(1, products.Count);
            Assert.Equal(categories.First().CategoryID, products.First().CategoryID);
        }