Пример #1
0
        private bool IsValid(UDFMetadata metadata, out IEnumerable <UDFMetadataError> errorMessages)
        {
            var errors = new List <UDFMetadataError>();

            if (string.IsNullOrWhiteSpace(metadata.Name) || metadata.Name.Equals("new", StringComparison.InvariantCultureIgnoreCase))
            {
                errors.Add(new UDFMetadataError()
                {
                    Field   = UDFMetadataField.Name,
                    Message = "Cannot be empty or 'new'"
                });
            }
            else if (!this.fieldNamePattern.IsMatch(metadata.Name))
            {
                errors.Add(new UDFMetadataError()
                {
                    Field   = UDFMetadataField.Name,
                    Message = "Not allowed (must start with a letter and len >= 3)"
                });
            }

            if (string.IsNullOrWhiteSpace(metadata.Display))
            {
                errors.Add(new UDFMetadataError()
                {
                    Field   = UDFMetadataField.Display,
                    Message = "Cannot be empty"
                });
            }

            errorMessages = errors;
            return(errors.Count == 0);
        }
Пример #2
0
        public bool AddFieldFor(string tenant, Type modelType, UDFMetadata metadata, out IEnumerable <UDFMetadataError> errorMessages)
        {
            if (modelType.GetInterface("IUDFModel") == null)
            {
                throw new InvalidOperationException("udfModelType should implement IUDFModel interface");
            }

            if (!this.IsValid(metadata, out errorMessages))
            {
                return(false);
            }

            var tenantData = this.tenantStore.GetTenant(tenant);

            if (tenantData.ExtensionDictionary == null)
            {
                tenantData.ExtensionDictionary = new Dictionary <string, List <UDFMetadata> >();
            }

            List <UDFMetadata> udfs;

            if (!tenantData.ExtensionDictionary.TryGetValue(modelType.Name, out udfs))
            {
                tenantData.ExtensionDictionary.Add(modelType.Name, new List <UDFMetadata>()
                {
                    metadata
                });
            }
            else
            {
                if (udfs.FirstOrDefault(i => i.Name.Equals(metadata.Name, StringComparison.InvariantCultureIgnoreCase)) != null)
                {
                    errorMessages = new List <UDFMetadataError>()
                    {
                        new UDFMetadataError()
                        {
                            Field   = UDFMetadataField.Name,
                            Message = "* Field name already used in this model"
                        }
                    };
                    return(false);
                }
                else
                {
                    udfs.Add(metadata);
                }
            }

            this.tenantStore.SaveTenant(tenantData);

            errorMessages = null;
            return(true);
        }
Пример #3
0
        public override void CopyFrom(UDFMetadata item)
        {
            base.CopyFrom(item);

            if (item is UDFItem)
            {
                this.Value = (item as UDFItem).Value;
            }
            else
            {
                this.SetUDFValue(item.DefaultValue);
            }
        }
Пример #4
0
        public override void CopyFrom(UDFMetadata item)
        {
            base.CopyFrom(item);

            if (item is UDFItem)
            {
                this.Value = (item as UDFItem).Value;
            }
            else
            {
                this.SetUDFValue(item.DefaultValue);
            }
        }
Пример #5
0
        public void CopyFromReturnsUpdatedMetadataProperties()
        {
            // Setup
            var metadata = new UDFMetadata { Type = UDFType.String, Name = "name", Display = "display", DefaultValue = "default", Mandatory = true };

            // Act
            var newMetadata = new UDFMetadata();
            newMetadata.CopyFrom(metadata);

            // Assert
            Assert.AreEqual(metadata.Type, newMetadata.Type);
            Assert.AreEqual(metadata.Name, newMetadata.Name);
            Assert.AreEqual(metadata.Display, newMetadata.Display);
            Assert.AreEqual(metadata.Mandatory, newMetadata.Mandatory);
        }
        public void AddFieldsForReturnsErrorWhenEmptyFieldNameIsAdded()
        {
            // Setup
            const string Tenant = "tenant";
            var mockTenant = new Mock<Tenant>();
            var metadata = new UDFMetadata { Name = string.Empty, Display = "custom" };
            var mockTenantStore = new Mock<ITenantStore>();
            mockTenantStore.Setup(m => m.GetTenant(Tenant)).Returns(mockTenant.Object);

            IEnumerable<UDFMetadataError> errors = new List<UDFMetadataError>();
            var udfDictionary = new UDFDictionary(mockTenantStore.Object);

            // Act
            var result = udfDictionary.AddFieldFor<TestExtension>(Tenant, metadata, out errors);

            // Assert
            Assert.IsFalse(result);
            Assert.IsTrue(errors.Count() > 0);
            Assert.AreEqual(UDFMetadataField.Name, errors.ToList()[0].Field);
            Assert.AreEqual("Cannot be empty or 'new'", errors.ToList()[0].Message);
        }
        public void AddFieldsForReturnsErrorWhenDuplicateFieldIsAdded()
        {
            // Setup
            const string Tenant = "tenant";
            var mockTenant = new Mock<Tenant>();
            var metadata = new UDFMetadata { Name = "custom", Display = "custom" };
            var duplicateMetadata = new UDFMetadata { Name = "custom", Display = "custom" };
            var mockTenantStore = new Mock<ITenantStore>();
            mockTenantStore.Setup(m => m.GetTenant(Tenant)).Returns(mockTenant.Object);

            IEnumerable<UDFMetadataError> errors = new List<UDFMetadataError>();
            var udfDictionary = new UDFDictionary(mockTenantStore.Object);
            udfDictionary.AddFieldFor<TestExtension>(Tenant, metadata, out errors);

            // Act
            var result = udfDictionary.AddFieldFor<TestExtension>(Tenant, duplicateMetadata, out errors);

            // Assert
            Assert.IsFalse(result);
            Assert.IsTrue(errors.Count() > 0);
            Assert.IsTrue(errors.ToList()[0].Field == UDFMetadataField.Name);
        }
        public void RemoveFieldForRemovesCustomField()
        {
            // Setup
            const string Tenant = "tenant";

            var mockTenant = new Tenant { Name = Tenant, SubscriptionKind = Shared.Models.SubscriptionKind.Premium };

            var metadata = new UDFMetadata { Name = "custom", Display = "display" };
            var mockTenantStore = new Mock<ITenantStore>();
            mockTenantStore.Setup(m => m.GetTenant(Tenant)).Returns(mockTenant);

            var udfDictionary = new UDFDictionary(mockTenantStore.Object);
            IEnumerable<UDFMetadataError> errors = new List<UDFMetadataError>();
            var result = udfDictionary.AddFieldFor<TestExtension>(Tenant, metadata, out errors);
            var f1 = udfDictionary.GetFieldsFor<TestExtension>(Tenant);

            // Act
            udfDictionary.RemoveFieldFor<TestExtension>(Tenant, "custom");
            var fields = udfDictionary.GetFieldsFor<TestExtension>(Tenant);

            // Assert
            mockTenantStore.Verify(m => m.SaveTenant(mockTenant));
            Assert.AreEqual(0, fields.Count());
        }
        public void GetFieldsForReturnsNullWhenEmptyList()
        {
            // Setup
            const string Tenant = "tenant";

            var mockTenant = new Tenant { Name = Tenant, SubscriptionKind = Shared.Models.SubscriptionKind.Premium };

            var metadata = new UDFMetadata { Name = "custom", Display = "display" };
            var mockTenantStore = new Mock<ITenantStore>();
            mockTenantStore.Setup(m => m.GetTenant(Tenant)).Returns(mockTenant);

            var udfDictionary = new UDFDictionary(mockTenantStore.Object);

            // Act
            var fields = udfDictionary.GetFieldsFor<TestExtension>(Tenant);

            // Assert
            Assert.IsNull(fields);
        }
Пример #10
0
        public void AreValidForReturnsTrueWhenExtensionListMatchesModelList()
        {
            // Setup
            const string Tenant = "tenant";
            var mockTenant = new Tenant { Name = Tenant, SubscriptionKind = Shared.Models.SubscriptionKind.Premium };

            var list = new List<UDFItem> { new UDFItem { DefaultValue = "defaultvalue", Name = "name", Value = "value", Display = "display" } };
            var testModel = new TestExtension();

            var mockTenantStore = new Mock<ITenantStore>();
            mockTenantStore.Setup(m => m.GetTenant(Tenant)).Returns(mockTenant);

            var metadata = new UDFMetadata { Name = "custom", Display = "display" };

            var udfDictionary = new UDFDictionary(mockTenantStore.Object);
            IEnumerable<UDFMetadataError> errors = new List<UDFMetadataError>();
            udfDictionary.AddFieldFor<TestExtension>(Tenant, metadata, out errors);

            // Act
            var result = udfDictionary.AreValidFor<TestExtension>(Tenant, list);

            // Assert
            Assert.IsTrue(result);
        }
Пример #11
0
        public void AddFieldsForReturnsTrue()
        {
            // Setup
            const string Tenant = "tenant";
            var mockTenant = new Mock<Tenant>();
            var metadata = new UDFMetadata { Name = "custom", Display = "display" };
            var mockTenantStore = new Mock<ITenantStore>();
            mockTenantStore.Setup(m => m.GetTenant(Tenant)).Returns(mockTenant.Object);

            // Act
            var udfDictionary = new UDFDictionary(mockTenantStore.Object);
            IEnumerable<UDFMetadataError> errors = new List<UDFMetadataError>();

            var result = udfDictionary.AddFieldFor<TestExtension>(Tenant, metadata, out errors);

            // Assert
            Assert.IsTrue(result);
        }
Пример #12
0
        private void TestGetEdmType(string type, UDFType udfType)
        {
            // Setup
            var metadata = new UDFMetadata { Type = udfType };

            // Act
            var result = metadata.GetEdmType();

            // Assert
            Assert.AreEqual(type, result);
        }
Пример #13
0
 public bool AddFieldFor <T>(string tenant, UDFMetadata metadata, out IEnumerable <UDFMetadataError> errorMessages) where T : IUDFModel
 {
     return(this.AddFieldFor(tenant, typeof(T), metadata, out errorMessages));
 }
Пример #14
0
 public UDFItem(UDFMetadata source)
 {
     this.CopyFrom(source);
 }
Пример #15
0
        private void TestSetUdfType(string type)
        {
            // Setup
            var metadata = new UDFMetadata();

            // Act
            metadata.SetUDFType(type);

            // Assert
            Assert.AreEqual(type, metadata.GetEdmType());
        }
Пример #16
0
        public ActionResult UdfNew(string tenant, string model, UDFMetadata metadata)
        {
            IEnumerable<UDFMetadataError> errorMessages;
            if (!this.udfDictionary.AddFieldFor(this.TenantName, this.extendableTypes[model], metadata, out errorMessages))
            {
                foreach (var error in errorMessages)
                {
                    switch (error.Field)
                    {
                        case UDFMetadataField.Name:
                            this.ModelState.AddModelError(
                                "ContentModel.Name",
                                string.Format(CultureInfo.InvariantCulture, "* {0}", error.Message));
                            break;
                        case UDFMetadataField.Display:
                            this.ModelState.AddModelError(
                                "ContentModel.Display",
                                string.Format(CultureInfo.InvariantCulture, "* {0}", error.Message));
                            break;
                    }
                }
            }

            if (!this.ModelState.IsValid)
            {
                var mvcModel = this.CreateTenantUdfPageViewData(model, metadata);
                mvcModel.Title = string.Format("New User Defined Field for {0} model : Error!", model);
                return this.View(mvcModel);
            }

            this.ViewBag.Model = model;
            return this.RedirectToAction("UdfIndex");
        }
Пример #17
0
 public UDFItem(UDFMetadata source)
 {
     this.CopyFrom(source);
 }