Exemplo n.º 1
0
        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);
        }
Exemplo n.º 2
0
        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);
        }