public static void UpdateMappingEnum(EnumAttributeMetadata picklist, MappingEnum mappingEnum)
        {
            if (picklist.SchemaName != null)
            {
                mappingEnum.DisplayName = Naming.GetProperVariableName(Naming.GetProperVariableName(picklist.SchemaName));
            }

            mappingEnum.LogicalName = picklist.LogicalName ?? mappingEnum.LogicalName;

            if (picklist.OptionSet != null)
            {
                mappingEnum.Items =
                    picklist.OptionSet.Options.Select(
                        o => new MapperEnumItem
                {
                    Attribute = new CrmPicklistAttribute
                    {
                        DisplayName     = o.Label.UserLocalizedLabel.Label,
                        Value           = o.Value ?? 1,
                        LocalizedLabels = o.Label.LocalizedLabels.Select(label => new LocalizedLabelSerialisable
                        {
                            LanguageCode = label.LanguageCode,
                            Label        = label.Label
                        }).ToArray()
                    },
                    Name = Naming.GetProperVariableName(o.Label.UserLocalizedLabel.Label)
                }
                        ).ToArray();
            }

            Dictionary <string, int> duplicates = new Dictionary <string, int>();

            foreach (var i in mappingEnum.Items)
            {
                if (duplicates.ContainsKey(i.Name))
                {
                    duplicates[i.Name] = duplicates[i.Name] + 1;
                    i.Name            += "_" + duplicates[i.Name];
                }
                else
                {
                    duplicates[i.Name] = 1;
                }
            }
        }
        public static MappingEnum GetMappingEnum(EnumAttributeMetadata picklist)
        {
            var enm = new MappingEnum
            {
                DisplayName = Naming.GetProperVariableName(Naming.GetProperVariableName(picklist.SchemaName)),
                LogicalName = picklist.LogicalName,
                Items       =
                    picklist.OptionSet.Options.Select(
                        o => new MapperEnumItem
                {
                    Attribute = new CrmPicklistAttribute
                    {
                        DisplayName     = o.Label.UserLocalizedLabel.Label,
                        Value           = o.Value ?? 1,
                        LocalizedLabels = o.Label.LocalizedLabels.Select(label => new LocalizedLabelSerialisable
                        {
                            LanguageCode = label.LanguageCode,
                            Label        = label.Label
                        }).ToArray()
                    },
                    Name = Naming.GetProperVariableName(o.Label.UserLocalizedLabel.Label)
                }
                        ).ToArray(),
                MetadataId = picklist.MetadataId
            };

            Dictionary <string, int> duplicates = new Dictionary <string, int>();

            foreach (var i in enm.Items)
            {
                if (duplicates.ContainsKey(i.Name))
                {
                    duplicates[i.Name] = duplicates[i.Name] + 1;
                    i.Name            += "_" + duplicates[i.Name];
                }
                else
                {
                    duplicates[i.Name] = 1;
                }
            }

            return(enm);
        }
예제 #3
0
        private static MappingEntity GetMappingEntity(EntityMetadata entityMetadata, string serverStamp)
        {
            var entity = new MappingEntity();

            entity.MetadataId  = entityMetadata.MetadataId;
            entity.ServerStamp = serverStamp;

            entity.Attribute             = new CrmEntityAttribute();
            entity.TypeCode              = entityMetadata.ObjectTypeCode;
            entity.Attribute.LogicalName = entityMetadata.LogicalName;
            entity.IsIntersect           = entityMetadata.IsIntersect ?? false;
            entity.Attribute.PrimaryKey  = entityMetadata.PrimaryIdAttribute;

            // entity.DisplayName = Helper.GetProperVariableName(entityMetadata.SchemaName);
            if (entityMetadata.DisplayName != null)
            {
                entity.Label = entityMetadata.DisplayName.UserLocalizedLabel != null
                                        ? entityMetadata.DisplayName.UserLocalizedLabel.Label
                                        : entityMetadata.SchemaName;
            }

            entity.SchemaName  = 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.GetMappingField(a, entity)).ToList();

            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();
            entity.States =
                entityMetadata.Attributes.Where(a => a is StateAttributeMetadata)
                .Select(a => MappingEnum.GetMappingEnum(a as EnumAttributeMetadata))
                .FirstOrDefault();
            entity.Enums = entityMetadata.Attributes
                           .Where(a => a is PicklistAttributeMetadata || a is StateAttributeMetadata || a is StatusAttributeMetadata)
                           .Select(a => MappingEnum.GetMappingEnum(a as EnumAttributeMetadata)).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.RelationshipsManyToOne = entityMetadata.ManyToOneRelationships.Select(r =>
                                                                                         MappingRelationshipN1.Parse(r,
                                                                                                                     entity.Fields)).ToArray();

            var relationshipsManyToMany =
                entityMetadata.ManyToManyRelationships.Select(r => MappingRelationshipMN.Parse(r, entity.LogicalName)).ToList();
            var selfReferenced = relationshipsManyToMany.Where(r => r.IsSelfReferenced).ToList();

            foreach (var referenced in selfReferenced)
            {
                var referencing = (MappingRelationshipMN)referenced.Clone();
                referencing.DisplayName = "Referencing" + Naming.GetProperVariableName(referenced.SchemaName);
                referencing.EntityRole  = "Microsoft.Xrm.Sdk.EntityRole.Referencing";
                relationshipsManyToMany.Add(referencing);
            }

            entity.RelationshipsManyToMany = relationshipsManyToMany.OrderBy(r => r.DisplayName).ToArray();

            return(entity);
        }
        public static MappingField GetMappingField(AttributeMetadata attribute, MappingEntity entity)
        {
            var result = new MappingField();

            result.Entity            = entity;
            result.MetadataId        = attribute.MetadataId;
            result.AttributeOf       = attribute.AttributeOf;
            result.IsValidForCreate  = attribute.IsValidForCreate ?? false;
            result.IsValidForRead    = attribute.IsValidForRead ?? false;
            result.IsValidForUpdate  = attribute.IsValidForUpdate ?? false;
            result.IsActivityParty   = attribute.AttributeType == AttributeTypeCode.PartyList;
            result.IsStateCode       = attribute.AttributeType == AttributeTypeCode.State;
            result.DeprecatedVersion = attribute.DeprecatedVersion;
            result.IsDeprecated      = !string.IsNullOrWhiteSpace(attribute.DeprecatedVersion);

            if (attribute is PicklistAttributeMetadata || attribute is StateAttributeMetadata ||
                attribute is StatusAttributeMetadata)
            {
                result.EnumData =
                    MappingEnum.GetMappingEnum(attribute as EnumAttributeMetadata);
            }

            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.SchemaName          = attribute.SchemaName;
            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.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;
                }
            }

            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);
        }