private static void CheckEnums(List <EnumerationData> enums, TypeSet t, Action CheckContents, ref ParameterType modifiedType, bool hidden) { EnumerationData Enumeration1 = new EnumerationData("enum1", ParameterType.Parse(hidden ? "e25f2768-ac00-4320-a558-12b51d85d771" : "f70fb4d1-59f5-4618-9bc4-8da19f943da2"), new[] { new EnumerationData.Element("value1", Guid.Parse("9ee98b1e-fb29-42c5-8a26-903a8aecacc0")), new EnumerationData.Element("value2", Guid.Parse("a3ed3b47-9985-4f17-b791-4155cc7196f4")) }); EnumerationData Enumeration2 = new EnumerationData("enum2", ParameterType.Parse(hidden ? "19bf99ff-98f2-4ab3-96d6-1f7e61d98ae7" : "a532c7c0-2806-4def-9fb2-5182b6803cbe"), new[] { new EnumerationData.Element("value1", Guid.Parse("e80fd4c3-eb10-4309-b683-79c3675b6fb8")), new EnumerationData.Element("value2", Guid.Parse("c0b0a09e-4825-479e-87dc-599bdacf045f")) }); EnumerationData Enumeration3 = new EnumerationData("enum3", ParameterType.Parse(hidden ? "b78e53cd-717b-446f-9ab2-fbbf5c41b067" : "aa274a73-95c1-4272-bb23-9c9cabeb183e"), new[] { new EnumerationData.Element("asd", Guid.Parse("27b59086-7e9b-4b7f-8796-a7a23eb727c1")), new EnumerationData.Element("sdasd", Guid.Parse("b08cf9e0-9d77-42cf-9f1c-a21def2b79d3")), new EnumerationData.Element("sdasd2", Guid.Parse("4f73eaad-86fe-4cbb-adbc-016724399ae5")) }); var a = Enumeration1; t.AddEnum(a, hidden); enums.Add(a); Assert.That(modifiedType, Is.EqualTo(a.TypeId)); modifiedType = null; CheckContents(); a = Enumeration2; t.AddEnum(a, hidden); enums.Add(a); Assert.That(modifiedType, Is.EqualTo(a.TypeId)); modifiedType = null; CheckContents(); a = Enumeration1; t.Remove(a.TypeId); enums.Remove(a); Assert.That(modifiedType, Is.EqualTo(a.TypeId)); modifiedType = null; CheckContents(); a = Enumeration3; t.AddEnum(a, hidden); enums.Add(a); Assert.That(modifiedType, Is.EqualTo(a.TypeId)); modifiedType = null; CheckContents(); EnumerationData Enum2Replacement = new EnumerationData("repeat", Enumeration2.TypeId, new[] { new EnumerationData.Element("asd", Guid.Parse("24be3852-b34f-4f20-ab72-3e391e939872")) }); t.ModifyEnum(Enum2Replacement); enums[enums.IndexOf(Enumeration2)] = Enum2Replacement; Assert.That(modifiedType, Is.EqualTo(Enum2Replacement.TypeId)); modifiedType = null; CheckContents(); EnumerationData Enumeration3Replacement = new EnumerationData("enum3 replacement", Enumeration3.TypeId, new[] { new EnumerationData.Element("asd", Guid.Parse("27b59086-7e9b-4b7f-8796-a7a23eb727c1")), new EnumerationData.Element("sdasd", Guid.Parse("b08cf9e0-9d77-42cf-9f1c-a21def2b79d3")), new EnumerationData.Element("sdasd2", Guid.Parse("4f73eaad-86fe-4cbb-adbc-016724399ae5")) }); t.RenameType(Enumeration3.TypeId, Enumeration3Replacement.Name); enums[enums.IndexOf(Enumeration3)] = Enumeration3Replacement; Assert.That(modifiedType, Is.EqualTo(Enumeration3Replacement.TypeId)); modifiedType = null; CheckContents(); }
protected override IEnumerable <IExportItem> ExportCore() { using (PersistenceScope scope = new PersistenceScope(PersistenceContextType.Read)) { IReadContext context = (IReadContext)PersistenceScope.CurrentContext; IMetadataBroker metaBroker = context.GetBroker <IMetadataBroker>(); IEnumBroker enumBroker = context.GetBroker <IEnumBroker>(); List <Type> enumClasses = CollectionUtils.Sort(metaBroker.ListEnumValueClasses(), delegate(Type x, Type y) { return(x.FullName.CompareTo(y.FullName)); }); foreach (Type enumClass in enumClasses) { EnumerationData data = new EnumerationData(); data.EnumerationClass = enumClass.FullName; data.Members = CollectionUtils.Map <EnumValue, EnumerationMemberData>(enumBroker.Load(enumClass, true), delegate(EnumValue v) { return(new EnumerationMemberData(v)); }); yield return(new ExportItem(data)); } scope.Complete(); } }
/// <summary> /// Populate a domain's various type data from xml /// </summary> /// <param name="domain">The domain to fill</param> /// <param name="root">The xml root node</param> private static void ReadTypes(DomainData domain, XElement root) { foreach (var a in root.Elements("DynamicEnumeration")) { DynamicEnumerationData typeData = new DynamicEnumerationData(a.Attribute("name").Value, Guid.Parse(a.Attribute("guid").Value)); domain.DynamicEnumerations.Add(typeData); } foreach (var a in root.Elements("Enumeration")) { var name = a.Attribute("name").Value; var guid = Guid.Parse(a.Attribute("guid").Value); var values = a.Elements("Value").Select(value => new EnumerationData.Element(value.Attribute("name").Value, Guid.Parse(value.Attribute("guid").Value))); EnumerationData typeData; if (a.Element("Default") != null) { if (a.Element("Default").Attributes("guid").Any()) { var def = Guid.Parse(a.Element("Default").Attribute("guid").Value); typeData = new EnumerationData(name, guid, values, def); } else if (a.Element("Default").Attributes("name").Any()) { var def = a.Element("Default").Attribute("name").Value; typeData = new EnumerationData(name, guid, values, def); } else { throw new Exception("Enumeration declared with a default node but no recognised default value"); } } else { typeData = new EnumerationData(name, guid, values); } domain.Enumerations.Add(typeData); } foreach (var a in root.Elements("Decimal")) { var aName = a.Attribute("name").Value; var guid = Guid.Parse(a.Attribute("guid").Value); var aMax = a.Attributes("max").Select <XAttribute, decimal?>(b => decimal.Parse(b.Value)).SingleOrDefault(); var aMin = a.Attributes("min").Select <XAttribute, decimal?>(b => decimal.Parse(b.Value)).SingleOrDefault(); var aDef = a.Attributes("default").Select <XAttribute, decimal?>(b => decimal.Parse(b.Value)).SingleOrDefault(); DecimalData typeData = new DecimalData(aName, guid, aMax, aMin, aDef); domain.Decimals.Add(typeData); } foreach (var a in root.Elements("Integer")) { var aName = a.Attribute("name").Value; var guid = Guid.Parse(a.Attribute("guid").Value); var aMax = a.Attributes("max").Select <XAttribute, int?>(b => int.Parse(b.Value)).SingleOrDefault(); var aMin = a.Attributes("min").Select <XAttribute, int?>(b => int.Parse(b.Value)).SingleOrDefault(); var aDef = a.Attributes("default").Select <XAttribute, int?>(b => int.Parse(b.Value)).SingleOrDefault(); IntegerData typeData = new IntegerData(aName, guid, aMax, aMin, aDef); domain.Integers.Add(typeData); } }
public ExportItem(EnumerationData data) { _data = data; }
protected override void ImportCore(IEnumerable <IImportItem> items) { using (PersistenceScope scope = new PersistenceScope(PersistenceContextType.Update)) { IUpdateContext context = (IUpdateContext)PersistenceScope.CurrentContext; context.ChangeSetRecorder.OperationName = this.GetType().FullName; IMetadataBroker metaBroker = context.GetBroker <IMetadataBroker>(); IEnumBroker enumBroker = context.GetBroker <IEnumBroker>(); IList <Type> enumClasses = metaBroker.ListEnumValueClasses(); foreach (IImportItem item in items) { EnumerationData data = (EnumerationData)Read(item.Read(), typeof(EnumerationData)); // find the enum class Type enumClass = CollectionUtils.SelectFirst(enumClasses, delegate(Type ec) { return(ec.FullName == data.EnumerationClass); }); if (enumClass == null) { Platform.Log(LogLevel.Error, string.Format("{0} is not a valid enumeration class name.", data.EnumerationClass)); continue; } IList <EnumValue> existingValues = enumBroker.Load(enumClass, true); foreach (EnumerationMemberData md in data.Members) { // check if a conflicting value exists // (this can happen if there is existing data in the db with the same value but different code) EnumValue conflict = CollectionUtils.SelectFirst(existingValues, delegate(EnumValue v) { return(v.Code != md.Code && v.Value == md.Value); }); if (conflict != null) { Platform.Log(LogLevel.Error, string.Format("{0} value {1} conflicts with existing value {2} and will not be imported.", data.EnumerationClass, md.Code, conflict.Code)); continue; } // check if the value already exists EnumValue value = CollectionUtils.SelectFirst(existingValues, delegate(EnumValue v) { return(v.Code == md.Code); }); if (value == null) { // value does not exist - add it value = enumBroker.AddValue(enumClass, md.Code, md.Value, md.Description, md.DisplayOrder, md.Deactivated); existingValues.Add(value); } else { // value exists - update it enumBroker.UpdateValue(enumClass, md.Code, md.Value, md.Description, md.DisplayOrder, md.Deactivated); } } context.SynchState(); } scope.Complete(); } }
protected override IEnumerable<IExportItem> ExportCore() { using (PersistenceScope scope = new PersistenceScope(PersistenceContextType.Read)) { IReadContext context = (IReadContext) PersistenceScope.CurrentContext; IMetadataBroker metaBroker = context.GetBroker<IMetadataBroker>(); IEnumBroker enumBroker = context.GetBroker<IEnumBroker>(); List<Type> enumClasses = CollectionUtils.Sort(metaBroker.ListEnumValueClasses(), delegate (Type x, Type y) { return x.FullName.CompareTo(y.FullName);}); foreach (Type enumClass in enumClasses) { EnumerationData data = new EnumerationData(); data.EnumerationClass = enumClass.FullName; data.Members = CollectionUtils.Map<EnumValue, EnumerationMemberData>(enumBroker.Load(enumClass, true), delegate(EnumValue v) { return new EnumerationMemberData(v); }); yield return new ExportItem(data); } scope.Complete(); } }
private IEnumerable <SelectListItem> GetEnumSelect(Type type, bool isNullable, int?value) { List <SelectListItem> retVal = new List <SelectListItem>(); var coll = new List <EnumerationData>(); var fields = type.GetFields().ToList(); var enumArr = Enum.GetValues(type); if (enumArr.Length <= 0) { return(retVal); } int?maxOrder = null; foreach (var item_ in enumArr) { var itm1_ = item_; var fi_ = fields.FirstOrDefault(o => o.Name == itm1_.ToString()); var enumData = new EnumerationData { Id = (int)fi_.GetRawConstantValue(), Code = item_.ToString(), Name = GetEnumAttributeValueString <DisplayAttribute>(item_ as Enum, y => y.Name), }; if (string.IsNullOrWhiteSpace(enumData.Name)) { enumData.Name = item_.ToString(); } enumData.Order = GetEnumAttributeValueIntegerNullable <DisplayAttribute>(item_ as Enum, y => y.GetOrder()); if (enumData.Order.HasValue) { if (maxOrder.HasValue) { maxOrder = maxOrder.Value < enumData.Order.Value ? enumData.Order.Value : maxOrder.Value; } else { maxOrder = enumData.Order.Value; } } coll.Add(enumData); } foreach (var item_ in coll.Where(o => !o.Order.HasValue).OrderBy(o => o.Name)) { item_.Order = int.MaxValue; } coll = coll.OrderBy(o => o.Order).ThenBy(o => o.Name).ToList(); var xxx = value; if (isNullable) { retVal.Add(new SelectListItem { Text = "Pick One", Value = string.Empty }); } else if (value == null) { retVal.Add(new SelectListItem { Text = "Pick One", Value = string.Empty }); } else if (!coll.Any(o => o.Id == value)) { retVal.Add(new SelectListItem { Text = "Pick One", Value = string.Empty }); } foreach (var item in coll.OrderBy(o => o.Order).ThenBy(o => o.Name).ToList()) { if (value != null && value == item.Id) { retVal.Add(new SelectListItem { Text = item.Name, Value = item.Code, Selected = true }); } else { retVal.Add(new SelectListItem { Text = item.Name, Value = item.Code }); } } return(retVal); }