private static void ParseMinMaxValues(AttributeMetadata attribute, MappingField result) { if (attribute is StringAttributeMetadata) { result.MaxLength = (attribute as StringAttributeMetadata).MaxLength ?? -1; } if (attribute is MemoAttributeMetadata) { result.MaxLength = (attribute as MemoAttributeMetadata).MaxLength ?? -1; } if (attribute is IntegerAttributeMetadata) { var attr = attribute as IntegerAttributeMetadata; result.Min = attr.MinValue ?? -1; result.Max = attr.MaxValue ?? -1; } if (attribute is DecimalAttributeMetadata) { var attr = attribute as DecimalAttributeMetadata; result.Min = attr.MinValue ?? -1; result.Max = attr.MaxValue ?? -1; } if (attribute is MoneyAttributeMetadata) { var attr = attribute as MoneyAttributeMetadata; result.Min = attr.MinValue != null ? (decimal)attr.MinValue.Value : -1; result.Max = attr.MaxValue != null ? (decimal)attr.MaxValue.Value : -1; } if (attribute is DoubleAttributeMetadata) { var attr = attribute as DoubleAttributeMetadata; result.Min = attr.MinValue != null ? (decimal)attr.MinValue.Value : -1; result.Max = attr.MaxValue != null ? (decimal)attr.MaxValue.Value : -1; } }
private static void ParseDateTime(AttributeMetadata attribute, MappingField result) { var metadata = attribute as DateTimeAttributeMetadata; var behaviour = metadata?.DateTimeBehavior?.Value; if (metadata != null) { if (result.FieldType == AttributeTypeCode.DateTime && !string.IsNullOrEmpty(behaviour)) { try { result.DateTimeBehavior = (DateTimeBehavior)Enum.Parse(typeof(DateTimeBehavior), behaviour); } catch { // ignored } } } }
private static string GetTargetType(MappingField field) { if (field.IsPrimaryKey) { return("System.Nullable<System.Guid>"); } switch (field.FieldType) { case AttributeTypeCode.Picklist: return("Microsoft.Xrm.Sdk.OptionSetValue"); case AttributeTypeCode.BigInt: return("System.Nullable<long>"); case AttributeTypeCode.Integer: return("System.Nullable<int>"); case AttributeTypeCode.Boolean: return("System.Nullable<bool>"); case AttributeTypeCode.DateTime: return("System.Nullable<System.DateTime>"); case AttributeTypeCode.Decimal: return("System.Nullable<decimal>"); case AttributeTypeCode.Money: return("Microsoft.Xrm.Sdk.Money"); case AttributeTypeCode.Double: return("System.Nullable<double>"); case AttributeTypeCode.Uniqueidentifier: return("System.Nullable<System.Guid>"); case AttributeTypeCode.Lookup: case AttributeTypeCode.Owner: case AttributeTypeCode.Customer: return("Microsoft.Xrm.Sdk.EntityReference"); case AttributeTypeCode.State: return("System.Nullable<" + field.Entity.StateName + ">"); case AttributeTypeCode.Status: return("Microsoft.Xrm.Sdk.OptionSetValue"); case AttributeTypeCode.Memo: case AttributeTypeCode.Virtual: case AttributeTypeCode.EntityName: case AttributeTypeCode.String: return("string"); case AttributeTypeCode.PartyList: return("System.Collections.Generic.IEnumerable<ActivityParty>"); case AttributeTypeCode.ManagedProperty: return("Microsoft.Xrm.Sdk.BooleanManagedProperty"); default: return("object"); } }
public static MappingField Parse(AttributeMetadata attribute, MappingEntity entity) { var result = new MappingField(); result.Entity = entity; result.AttributeOf = attribute.AttributeOf; result.IsValidForCreate = (bool)attribute.IsValidForCreate; result.IsValidForRead = (bool)attribute.IsValidForRead; result.IsValidForUpdate = (bool)attribute.IsValidForUpdate; result.IsActivityParty = attribute.AttributeType == AttributeTypeCode.PartyList ? true : false; result.IsStateCode = attribute.AttributeType == AttributeTypeCode.State ? true : false; result.IsOptionSet = attribute.AttributeType == AttributeTypeCode.Picklist; result.IsTwoOption = attribute.AttributeType == AttributeTypeCode.Boolean; result.DeprecatedVersion = attribute.DeprecatedVersion; result.IsDeprecated = !string.IsNullOrWhiteSpace(attribute.DeprecatedVersion); if (attribute is PicklistAttributeMetadata) { result.EnumData = MappingEnum.Parse(attribute as PicklistAttributeMetadata); } if (attribute is LookupAttributeMetadata) { var lookup = attribute as LookupAttributeMetadata; if (lookup.Targets.Count() == 1) { result.LookupSingleType = lookup.Targets[0]; } } ParseMinMaxValues(attribute, result); if (attribute.AttributeType != null) { result.FieldType = attribute.AttributeType.Value; } result.IsPrimaryKey = attribute.IsPrimaryId == true; result.LogicalName = attribute.LogicalName; result.DisplayName = Naming.GetProperVariableName(attribute); result.PrivatePropertyName = Naming.GetEntityPropertyPrivateName(attribute.SchemaName); result.HybridName = Naming.GetProperHybridFieldName(result.DisplayName, result.Attribute); if (attribute.Description != null) { if (attribute.Description.UserLocalizedLabel != null) { result.Description = attribute.Description.UserLocalizedLabel.Label; } } if (attribute.DisplayName != null) { if (attribute.DisplayName.UserLocalizedLabel != null) { result.Label = attribute.DisplayName.UserLocalizedLabel.Label; } } result.IsRequired = attribute.RequiredLevel != null && attribute.RequiredLevel.Value == AttributeRequiredLevel.ApplicationRequired; result.Attribute = new CrmPropertyAttribute { LogicalName = attribute.LogicalName, IsLookup = attribute.AttributeType == AttributeTypeCode.Lookup || attribute.AttributeType == AttributeTypeCode.Customer }; result.TargetTypeForCrmSvcUtil = GetTargetType(result); result.FieldTypeString = result.TargetTypeForCrmSvcUtil; return(result); }
private static void AddEntityImageCrm2013(List <MappingField> fields) { if (fields.All(f => f.DisplayName?.Equals("EntityImageId") != true)) { return; } var image = new MappingField { Attribute = new CrmPropertyAttribute { IsLookup = false, LogicalName = "entityimage", IsEntityReferenceHelper = false }, SchemaName = "EntityImage", DisplayName = "EntityImage", HybridName = "EntityImage", TargetTypeForCrmSvcUtil = "byte[]", IsValidForUpdate = true, Description = "", // TODO there is an Description for this entityimage, Need to figure out how to read it from the server GetMethod = "" }; SafeAddField(fields, image); var imageTimestamp = new MappingField { Attribute = new CrmPropertyAttribute { IsLookup = false, LogicalName = "entityimage_timestamp", IsEntityReferenceHelper = false }, SchemaName = "EntityImage_Timestamp", DisplayName = "EntityImage_Timestamp", HybridName = "EntityImage_Timestamp", TargetTypeForCrmSvcUtil = "System.Nullable<long>", FieldType = AttributeTypeCode.BigInt, IsValidForUpdate = false, IsValidForCreate = false, Description = " ", // CrmSvcUtil provides an empty description for this EntityImage_TimeStamp GetMethod = "" }; SafeAddField(fields, imageTimestamp); var imageUrl = new MappingField { Attribute = new CrmPropertyAttribute { IsLookup = false, LogicalName = "entityimage_url", IsEntityReferenceHelper = false }, SchemaName = "EntityImage_URL", DisplayName = "EntityImage_URL", HybridName = "EntityImage_URL", TargetTypeForCrmSvcUtil = "string", FieldType = AttributeTypeCode.String, IsValidForUpdate = false, IsValidForCreate = false, Description = " ", // CrmSvcUtil provides an empty description for this EntityImage_URL GetMethod = "" }; SafeAddField(fields, imageUrl); }
private static MappingEntity GetMappingEntity(EntityMetadata entityMetadata, string serverStamp, MappingEntity entity, bool isTitleCaseLogicalName) { entity = entity ?? new MappingEntity(); entity.MetadataId = entityMetadata.MetadataId; entity.ServerStamp = serverStamp; entity.Attribute = entity.Attribute ?? new CrmEntityAttribute(); entity.TypeCode = entityMetadata.ObjectTypeCode ?? entity.TypeCode; entity.Attribute.LogicalName = entityMetadata.LogicalName ?? entity.Attribute.LogicalName; entity.IsIntersect = (entityMetadata.IsIntersect ?? entity.IsIntersect); entity.Attribute.PrimaryKey = entityMetadata.PrimaryIdAttribute ?? entity.Attribute.PrimaryKey; if (entityMetadata.DisplayName?.UserLocalizedLabel != null) { entity.Label = entityMetadata.DisplayName.UserLocalizedLabel.Label; } if (entityMetadata.SchemaName != null) { entity.DisplayName = Naming.GetProperEntityName(entityMetadata.SchemaName); entity.SchemaName = entityMetadata.SchemaName; if (entityMetadata.LogicalName != null) { entity.HybridName = Naming.GetProperHybridName(entityMetadata.SchemaName, entityMetadata.LogicalName); } } entity.StateName = entity.HybridName + "State"; if (entityMetadata.Description != null && entityMetadata.Description.UserLocalizedLabel != null) { entity.Description = entityMetadata.Description.UserLocalizedLabel.Label; } var fields = (entity.Fields ?? new MappingField[0]).ToList(); foreach (var field in entityMetadata.Attributes.Where(a => a.AttributeOf == null)) { var existingField = fields.FirstOrDefault(fieldQ => fieldQ.MetadataId == field.MetadataId); // if it exists, remove it from the list if (existingField != null) { fields.RemoveAll(fieldQ => fieldQ.MetadataId == field.MetadataId); } // update/create and add to list fields.Add(MappingField.GetMappingField(field, entity, existingField, isTitleCaseLogicalName)); } fields.ForEach(f => { if (f.DisplayName == entity.DisplayName) { f.DisplayName += "1"; } if (f.HybridName == entity.HybridName) { f.HybridName += "1"; } } ); AddEntityImageCrm2013(fields); AddLookupFields(fields); entity.Fields = fields.ToArray(); if (entityMetadata.Attributes != null) { // get the states enum from the metadata entity.States = entityMetadata.Attributes.Where(a => a is StateAttributeMetadata && a.AttributeOf == null) .Select(a => MappingEnum.GetMappingEnum(a as EnumAttributeMetadata, null, isTitleCaseLogicalName)) .FirstOrDefault() ?? entity.States; // get all optionsets from the metadata var newEnums = entityMetadata.Attributes .Where(a => (a is EnumAttributeMetadata || a is BooleanAttributeMetadata) && a.AttributeOf == null); // if there was never any enums previously, then just take the ones sent if (entity.Enums == null) { entity.Enums = newEnums.Select(newEnum => MappingEnum.GetMappingEnum(newEnum, null, isTitleCaseLogicalName)).ToArray(); } else { var existingEnums = entity.Enums.ToList(); // else, update the changed ones newEnums.AsParallel() .ForAll(newEnum => { // has this enum been updated? var existingEnum = existingEnums.Find(existingEnumQ => existingEnumQ.MetadataId == newEnum.MetadataId); if (existingEnum != null) { // update it here entity.Enums[existingEnums.IndexOf(existingEnum)] = MappingEnum.GetMappingEnum(newEnum, existingEnum, isTitleCaseLogicalName); } else { // add new existingEnums.Add(MappingEnum.GetMappingEnum(newEnum, null, isTitleCaseLogicalName)); } }); entity.Enums = existingEnums.ToArray(); } } entity.PrimaryKey = entity.Fields.First(f => f.Attribute.LogicalName == entity.Attribute.PrimaryKey); entity.PrimaryKeyProperty = entity.PrimaryKey.DisplayName; entity.PrimaryNameAttribute = entityMetadata.PrimaryNameAttribute ?? entity.PrimaryNameAttribute; MappingRelationship1N.UpdateCache(entityMetadata.OneToManyRelationships.ToList(), entity, entity.Fields); MappingRelationshipN1.UpdateCache(entityMetadata.ManyToOneRelationships.ToList(), entity, entity.Fields); MappingRelationshipMN.UpdateCache(entityMetadata.ManyToManyRelationships.ToList(), entity, entity.LogicalName); // add a clone for self-referenced relation var relationshipsManyToMany = entity.RelationshipsManyToMany.ToList(); var selfReferenced = relationshipsManyToMany.Where(r => r.IsSelfReferenced).ToList(); foreach (var referenced in selfReferenced) { if (relationshipsManyToMany.All( rel => rel.DisplayName != "Referencing" + Naming.GetProperVariableName(referenced.SchemaName, false))) { var referencing = (MappingRelationshipMN)referenced.Clone(); referencing.DisplayName = "Referencing" + Naming.GetProperVariableName(referenced.SchemaName, false); referencing.EntityRole = "Microsoft.Xrm.Sdk.EntityRole.Referencing"; relationshipsManyToMany.Add(referencing); } } entity.RelationshipsManyToMany = relationshipsManyToMany.OrderBy(r => r.DisplayName).ToArray(); entity.FriendlyName = Naming.Clean(string.IsNullOrEmpty(entity.Label) ? Naming.Clean(entity.HybridName) : Naming.Clean(entity.Label)); // generate attribute friendly names and detect duplicates entity.Fields.AsParallel() .ForAll(field => { var cleanFieldName = Naming.Clean( string.IsNullOrEmpty(field.Label) ? Naming.Clean(field.DisplayName) : Naming.Clean(field.Label)) + (field == entity.PrimaryKey ? "Id" : ""); var isDuplicateName = entity.Fields.Count( fieldQ => Naming.Clean( string.IsNullOrEmpty(fieldQ.Label) ? Naming.Clean(fieldQ.DisplayName) : Naming.Clean(fieldQ.Label)) == cleanFieldName) > 1; isDuplicateName = isDuplicateName || cleanFieldName == "Attributes" || cleanFieldName == entity.FriendlyName || cleanFieldName == "LogicalName" || cleanFieldName == "EntityLogicalName" || cleanFieldName == "SchemaName" || cleanFieldName == "DisplayName" || cleanFieldName == "EntityTypeCode"; field.FriendlyName = cleanFieldName + (isDuplicateName ? "_" + field.DisplayName : ""); }); // generate enum friendly names entity.Enums.AsParallel() .ForAll(enm => { var attribute = entity.Fields.FirstOrDefault(field => field.LogicalName == enm.LogicalName); enm.FriendlyName = attribute == null ? enm.DisplayName : attribute.FriendlyName; }); return(entity); }
public static MappingEntity Parse(EntityMetadata entityMetadata) { var entity = new MappingEntity(); entity.Attribute = new CrmEntityAttribute(); entity.TypeCode = entityMetadata.ObjectTypeCode; entity.Attribute.LogicalName = entityMetadata.LogicalName; entity.IsIntersect = (bool)entityMetadata.IsIntersect; entity.Attribute.PrimaryKey = entityMetadata.PrimaryIdAttribute; // entity.DisplayName = Helper.GetProperVariableName(entityMetadata.SchemaName); entity.DisplayName = Naming.GetProperEntityName(entityMetadata.SchemaName); entity.HybridName = Naming.GetProperHybridName(entityMetadata.SchemaName, entityMetadata.LogicalName); entity.StateName = entity.HybridName + "State"; if (entityMetadata.Description != null) { if (entityMetadata.Description.UserLocalizedLabel != null) { entity.Description = entityMetadata.Description.UserLocalizedLabel.Label; } } var fields = entityMetadata.Attributes .Where(a => a.AttributeOf == null) .Select(a => MappingField.Parse(a, entity)).ToList(); fields.ForEach(f => { if (f.DisplayName == entity.DisplayName) { f.DisplayName += "1"; } //f.HybridName = Naming.GetProperHybridFieldName(f.DisplayName, f.Attribute); } ); AddEnityImageCRM2013(fields); AddLookupFields(fields); entity.Fields = fields.ToArray(); entity.States = entityMetadata.Attributes.Where(a => a is StateAttributeMetadata).Select(a => MappingEnum.Parse(a as EnumAttributeMetadata)).FirstOrDefault(); entity.Enums = entityMetadata.Attributes .Where(a => a is PicklistAttributeMetadata || a is StateAttributeMetadata || a is StatusAttributeMetadata || a is BooleanAttributeMetadata) .Select(a => MappingEnum.Parse(a)).ToArray(); entity.PrimaryKey = entity.Fields.First(f => f.Attribute.LogicalName == entity.Attribute.PrimaryKey); entity.PrimaryKeyProperty = entity.PrimaryKey.DisplayName; entity.PrimaryNameAttribute = entityMetadata.PrimaryNameAttribute; entity.RelationshipsOneToMany = entityMetadata.OneToManyRelationships.Select(r => MappingRelationship1N.Parse(r, entity.Fields)).ToArray(); entity.RelationshipsOneToMany.ToList().ForEach(r => { var newName = r.DisplayName; if (newName == entity.DisplayName || newName == entity.HybridName) { newName = r.DisplayName += "1"; } if (entity.Fields.Any(e => e.DisplayName == newName)) { newName = r.DisplayName += "2"; } }); entity.RelationshipsManyToOne = entityMetadata.ManyToOneRelationships.Select(r => MappingRelationshipN1.Parse(r, entity.Fields)).ToArray(); entity.RelationshipsManyToOne.ToList().ForEach(r => { var newName = r.DisplayName; if (newName == entity.DisplayName || newName == entity.HybridName) { newName = r.DisplayName += "1"; } if (entity.Fields.Any(e => e.DisplayName == newName)) { newName = r.DisplayName += "2"; } }); var RelationshipsManyToMany = entityMetadata.ManyToManyRelationships.Select(r => MappingRelationshipMN.Parse(r, entity.LogicalName)).ToList(); var selfReferenced = RelationshipsManyToMany.Where(r => r.IsSelfReferenced).ToList(); foreach (var referecned in selfReferenced) { var referencing = (MappingRelationshipMN)referecned.Clone(); referencing.DisplayName = "Referencing" + Naming.GetProperVariableName(referecned.SchemaName); referencing.EntityRole = "Microsoft.Xrm.Sdk.EntityRole.Referencing"; RelationshipsManyToMany.Add(referencing); } RelationshipsManyToMany.ForEach(r => { var newName = r.DisplayName; if (newName == entity.DisplayName || newName == entity.HybridName) { newName = r.DisplayName += "1"; } if (entity.Fields.Any(e => e.DisplayName == newName)) { newName = r.DisplayName += "2"; } }); entity.RelationshipsManyToMany = RelationshipsManyToMany.OrderBy(r => r.DisplayName).ToArray(); return(entity); }
private static string GetTargetType(MappingField field) { if (field.IsPrimaryKey) { return("Guid?"); } switch (field.FieldType) { case AttributeTypeCode.Picklist: return("OptionSetValue"); case AttributeTypeCode.BigInt: return("long?"); case AttributeTypeCode.Integer: return("int?"); case AttributeTypeCode.Boolean: return("bool?"); case AttributeTypeCode.DateTime: return("DateTime?"); case AttributeTypeCode.Decimal: return("decimal?"); case AttributeTypeCode.Money: return("Money"); case AttributeTypeCode.Double: return("double?"); case AttributeTypeCode.Uniqueidentifier: return("Guid?"); case AttributeTypeCode.Lookup: case AttributeTypeCode.Owner: case AttributeTypeCode.Customer: return("EntityReference"); case AttributeTypeCode.State: return(field.Entity.StateName + "?"); case AttributeTypeCode.Status: return("OptionSetValue"); case AttributeTypeCode.Memo: case AttributeTypeCode.Virtual: case AttributeTypeCode.EntityName: case AttributeTypeCode.String: return("string"); case AttributeTypeCode.PartyList: return("ActivityParty[]"); case AttributeTypeCode.ManagedProperty: return("BooleanManagedProperty"); default: return("object"); } }
public static MappingField GetMappingField(AttributeMetadata attribute, MappingEntity entity, MappingField result, bool isTitleCaseLogicalName) { result ??= new MappingField(); result.Entity = entity; result.MetadataId = attribute.MetadataId; result.LogicalName = attribute.LogicalName ?? result.LogicalName; result.AttributeOf = attribute.AttributeOf ?? result.AttributeOf; result.IsValidForCreate = attribute.IsValidForCreate ?? result.IsValidForCreate; result.IsValidForRead = attribute.IsValidForRead ?? result.IsValidForRead; result.IsValidForUpdate = attribute.IsValidForUpdate ?? result.IsValidForUpdate; if (attribute.AttributeType != null) { result.FieldType = attribute.AttributeType.Value; result.IsActivityParty = attribute.AttributeType == AttributeTypeCode.PartyList; result.IsStateCode = attribute.AttributeType == AttributeTypeCode.State; } result.DeprecatedVersion = attribute.DeprecatedVersion ?? result.DeprecatedVersion; if (attribute.DeprecatedVersion != null) { result.IsDeprecated = !string.IsNullOrWhiteSpace(attribute.DeprecatedVersion); } if (attribute is EnumAttributeMetadata || attribute is BooleanAttributeMetadata) { result.EnumData = MappingEnum.GetMappingEnum(attribute, result.EnumData, isTitleCaseLogicalName); } var lookup = attribute as LookupAttributeMetadata; if (lookup?.Targets != null && lookup.Targets.Length == 1) { result.LookupSingleType = lookup.Targets[0]; } ParseDateTime(attribute, result); ParseMinMaxValues(attribute, result); result.IsPrimaryKey = attribute.IsPrimaryId ?? result.IsPrimaryKey; if (attribute.SchemaName != null) { result.SchemaName = attribute.SchemaName ?? result.SchemaName; if (attribute.LogicalName != null) { result.DisplayName = Naming.GetProperVariableName(attribute, isTitleCaseLogicalName); } result.PrivatePropertyName = Naming.GetEntityPropertyPrivateName(attribute.SchemaName); } result.HybridName = Naming.GetProperHybridFieldName(result.DisplayName, new Yagasoft.CrmCodeGenerator.Models.Attributes.CrmPropertyAttribute { IsEntityReferenceHelper = result.Attribute.IsEntityReferenceHelper, IsImage = result.Attribute.IsImage, IsLookup = result.Attribute.IsLookup, IsMultiTyped = result.Attribute.IsMultiTyped, LogicalName = result.Attribute.LogicalName, SchemaName = result.Attribute.SchemaName, }); if (attribute.Description?.UserLocalizedLabel != null) { result.Description = attribute.Description.UserLocalizedLabel.Label; } if (attribute.DisplayName != null) { if (attribute.DisplayName.LocalizedLabels != null) { result.LocalizedLabels = attribute.DisplayName .LocalizedLabels.Select(label => new LocalizedLabelSerialisable { LanguageCode = label.LanguageCode, Label = label.Label }).ToArray(); } if (attribute.DisplayName.UserLocalizedLabel != null) { result.Label = attribute.DisplayName.UserLocalizedLabel.Label; } } if (attribute.RequiredLevel != null) { result.IsRequired = attribute.RequiredLevel.Value == AttributeRequiredLevel.ApplicationRequired; } if (attribute.AttributeType != null) { result.Attribute = new CrmPropertyAttribute { LogicalName = attribute.LogicalName, IsLookup = attribute.AttributeType == AttributeTypeCode.Lookup || attribute.AttributeType == AttributeTypeCode.Owner || attribute.AttributeType == AttributeTypeCode.Customer }; } result.TargetTypeForCrmSvcUtil = GetTargetType(result); result.FieldTypeString = result.TargetTypeForCrmSvcUtil; return(result); }