Summary

Class:FakeXrmEasy.Metadata.MetadataGenerator
Assembly:FakeXrmEasy
File(s):C:\code\jordimontana82\fake-xrm-easy\FakeXrmEasy.Shared\Metadata\MetadataGenerator.cs
Covered lines:167
Uncovered lines:11
Coverable lines:178
Total lines:253
Line coverage:93.8%
Branch coverage:85%

Metrics

MethodCyclomatic ComplexitySequence CoverageBranch Coverage
FromEarlyBoundEntities(...)2110093.10
GetCustomAttribute(...)1100100
CreateAttributeMetadata(...)2085.4885.71
AddOneToManyRelationshipMetadata(...)710080

File(s)

C:\code\jordimontana82\fake-xrm-easy\FakeXrmEasy.Shared\Metadata\MetadataGenerator.cs

#LineLine coverage
 1using FakeXrmEasy.Extensions;
 2using Microsoft.Xrm.Sdk;
 3using Microsoft.Xrm.Sdk.Client;
 4using Microsoft.Xrm.Sdk.Metadata;
 5using System;
 6using System.Collections.Generic;
 7using System.Linq;
 8using System.Reflection;
 9
 10namespace FakeXrmEasy.Metadata
 11{
 12    internal class MetadataGenerator
 13    {
 14        public static IEnumerable<EntityMetadata> FromEarlyBoundEntities(Assembly earlyBoundEntitiesAssembly)
 1215        {
 1216            List<EntityMetadata> entityMetadatas = new List<EntityMetadata>();
 1708217            foreach (var earlyBoundEntity in earlyBoundEntitiesAssembly.GetTypes())
 852318            {
 852319                EntityLogicalNameAttribute entityLogicalNameAttribute = GetCustomAttribute<EntityLogicalNameAttribute>(e
 1411720                 if (entityLogicalNameAttribute == null) continue;
 292921                EntityMetadata metadata = new EntityMetadata();
 292922                metadata.LogicalName = entityLogicalNameAttribute.LogicalName;
 23
 292924                FieldInfo entityTypeCode = earlyBoundEntity.GetField("EntityTypeCode", BindingFlags.Static | BindingFlag
 292925                 if (entityTypeCode != null)
 292926                {
 292927                    metadata.SetFieldValue("_objectTypeCode", entityTypeCode.GetValue(null));
 292928                }
 29
 292930                List<AttributeMetadata> attributeMetadatas = new List<AttributeMetadata>();
 292931                List<ManyToManyRelationshipMetadata> manyToManyRelationshipMetadatas = new List<ManyToManyRelationshipMe
 292932                List<OneToManyRelationshipMetadata> oneToManyRelationshipMetadatas = new List<OneToManyRelationshipMetad
 292933                List<OneToManyRelationshipMetadata> manyToOneRelationshipMetadatas = new List<OneToManyRelationshipMetad
 34
 292935                var idProperty = earlyBoundEntity.GetProperty("Id");
 36                AttributeLogicalNameAttribute attributeLogicalNameAttribute;
 292937                 if (idProperty != null && (attributeLogicalNameAttribute = GetCustomAttribute<AttributeLogicalNameAttrib
 291738                {
 291739                    metadata.SetFieldValue("_primaryIdAttribute", attributeLogicalNameAttribute.LogicalName);
 291740                }
 41
 292942                var properties = earlyBoundEntity.GetProperties(BindingFlags.Instance | BindingFlags.Public)
 17961943                                    .Where(x => x.Name != "Id" && Attribute.IsDefined(x, typeof(AttributeLogicalNameAttr
 17961944                                             || Attribute.IsDefined(x, typeof(RelationshipSchemaNameAttribute)));
 45
 29822346                foreach (var property in properties)
 14471847                {
 14471848                    RelationshipSchemaNameAttribute relationshipSchemaNameAttribute = GetCustomAttribute<RelationshipSch
 14471849                    attributeLogicalNameAttribute = GetCustomAttribute<AttributeLogicalNameAttribute>(property);
 50
 14471851                     if (relationshipSchemaNameAttribute == null)
 8294252                    {
 53#if !FAKE_XRM_EASY
 7603554                         if (property.PropertyType == typeof(byte[]))
 26455                        {
 26456                            metadata.SetFieldValue("_primaryImageAttribute", attributeLogicalNameAttribute.LogicalName);
 26457                        }
 58#endif
 59                        AttributeMetadata attributeMetadata;
 8294260                         if (attributeLogicalNameAttribute.LogicalName == "statecode")
 98561                        {
 98562                            attributeMetadata = new StateAttributeMetadata();
 98563                        }
 8195764                         else if (attributeLogicalNameAttribute.LogicalName == "statuscode")
 99765                        {
 99766                            attributeMetadata = new StatusAttributeMetadata();
 99767                        }
 8096068                         else if (attributeLogicalNameAttribute.LogicalName == metadata.PrimaryIdAttribute)
 291769                        {
 291770                            attributeMetadata = new AttributeMetadata();
 291771                            attributeMetadata.SetSealedPropertyValue("AttributeType", AttributeTypeCode.Uniqueidentifier
 291772                        }
 73                        else
 7804374                        {
 7804375                            attributeMetadata = CreateAttributeMetadata(property.PropertyType);
 7804376                        }
 77
 8294278                        attributeMetadata.SetFieldValue("_entityLogicalName", entityLogicalNameAttribute.LogicalName);
 8294279                        attributeMetadata.SetFieldValue("_logicalName", attributeLogicalNameAttribute.LogicalName);
 80
 8294281                        attributeMetadatas.Add(attributeMetadata);
 8294282                    }
 83                    else
 6177684                    {
 6177685                         if (property.PropertyType.Name == "IEnumerable`1")
 3147686                        {
 349717287                             PropertyInfo peerProperty = property.PropertyType.GetGenericArguments()[0].GetProperties().S
 3147688                             if (peerProperty == null || peerProperty.PropertyType.Name == "IEnumerable`1") // N:N relati
 142889                            {
 142890                                ManyToManyRelationshipMetadata relationshipMetadata = new ManyToManyRelationshipMetadata
 142891                                relationshipMetadata.SchemaName = relationshipSchemaNameAttribute.SchemaName;
 92
 142893                                manyToManyRelationshipMetadatas.Add(relationshipMetadata);
 142894                            }
 95                            else // 1:N relationship
 3004896                            {
 3004897                                AddOneToManyRelationshipMetadata(earlyBoundEntity, property, property.PropertyType.GetGe
 3004898                            }
 3147699                        }
 100                        else //N:1 Property
 30300101                        {
 12118010102                             AddOneToManyRelationshipMetadata(property.PropertyType, property.PropertyType.GetProperties(
 30300103                        }
 61776104                    }
 144718105                }
 2929106                 if (attributeMetadatas.Any())
 2929107                {
 2929108                    metadata.SetSealedPropertyValue("Attributes", attributeMetadatas.ToArray());
 2929109                }
 2929110                 if (manyToManyRelationshipMetadatas.Any())
 660111                {
 660112                    metadata.SetSealedPropertyValue("ManyToManyRelationships", manyToManyRelationshipMetadatas.ToArray()
 660113                }
 2929114                 if (manyToOneRelationshipMetadatas.Any())
 2460115                {
 2460116                    metadata.SetSealedPropertyValue("ManyToOneRelationships", manyToOneRelationshipMetadatas.ToArray());
 2460117                }
 2929118                 if (oneToManyRelationshipMetadatas.Any())
 2424119                {
 2424120                    metadata.SetSealedPropertyValue("OneToManyRelationships", oneToManyRelationshipMetadatas.ToArray());
 2424121                }
 2929122                entityMetadatas.Add(metadata);
 2929123            }
 12124            return entityMetadatas;
 12125        }
 126
 127        private static T GetCustomAttribute<T>(MemberInfo member) where T : Attribute
 740568128        {
 740568129            return (T)Attribute.GetCustomAttribute(member, typeof(T));
 740568130        }
 131
 132        private static AttributeMetadata CreateAttributeMetadata(Type propertyType)
 78043133        {
 78043134             if (typeof(string) == propertyType)
 17197135            {
 17197136                return new StringAttributeMetadata();
 137            }
 60846138             else if (typeof(EntityReference).IsAssignableFrom(propertyType))
 19872139            {
 19872140                return new LookupAttributeMetadata();
 141            }
 142#if FAKE_XRM_EASY || FAKE_XRM_EASY_2013 || FAKE_XRM_EASY_2015 || FAKE_XRM_EASY_2016 || FAKE_XRM_EASY_365
 30726143             else if (typeof(Microsoft.Xrm.Client.CrmEntityReference).IsAssignableFrom(propertyType))
 8144            {
 8145                return new LookupAttributeMetadata();
 146            }
 147#endif
 40966148             else if (typeof(OptionSetValue).IsAssignableFrom(propertyType))
 4908149            {
 4908150                return new PicklistAttributeMetadata();
 151            }
 36058152             else if (typeof(Money).IsAssignableFrom(propertyType))
 3240153            {
 3240154                return new MoneyAttributeMetadata();
 155            }
 32818156             else if (propertyType.IsGenericType)
 32227157            {
 32227158                Type genericType = propertyType.GetGenericArguments().FirstOrDefault();
 32227159                 if (propertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
 31855160                {
 31855161                     if (typeof(int) == genericType)
 8535162                    {
 8535163                        return new IntegerAttributeMetadata();
 164                    }
 23320165                     else if (typeof(double) == genericType)
 684166                    {
 684167                        return new DoubleAttributeMetadata();
 168                    }
 22636169                     else if (typeof(bool) == genericType)
 5556170                    {
 5556171                        return new BooleanAttributeMetadata();
 172                    }
 17080173                     else if (typeof(decimal) == genericType)
 1416174                    {
 1416175                        return new DecimalAttributeMetadata();
 176                    }
 15664177                     else if (typeof(DateTime) == genericType)
 8571178                    {
 8571179                        return new DateTimeAttributeMetadata();
 180                    }
 7093181                     else if (typeof(Guid) == genericType)
 4380182                    {
 4380183                        return new LookupAttributeMetadata();
 184                    }
 2713185                     else if (typeof(long) == genericType)
 2713186                    {
 2713187                        return new BigIntAttributeMetadata();
 188                    }
 0189                     else if (typeof(Enum).IsAssignableFrom(genericType))
 0190                    {
 0191                        return new StateAttributeMetadata();
 192                    }
 193                    else
 0194                    {
 0195                        throw new Exception($"Type {propertyType.Name}{genericType.Name} has not been mapped to an Attri
 196                    }
 197                }
 372198                 else if (propertyType.GetGenericTypeDefinition() == typeof(IEnumerable<>))
 372199                {
 372200                    var partyList = new LookupAttributeMetadata();
 372201                    partyList.SetSealedPropertyValue("AttributeType", AttributeTypeCode.PartyList);
 372202                    return partyList;
 203                }
 204                else
 0205                {
 0206                    throw new Exception($"Type {propertyType.Name}{genericType.Name} has not been mapped to an Attribute
 207                }
 208            }
 591209             else if (typeof(BooleanManagedProperty) == propertyType)
 324210            {
 324211                var booleanManaged = new BooleanAttributeMetadata();
 324212                booleanManaged.SetSealedPropertyValue("AttributeType", AttributeTypeCode.ManagedProperty);
 324213                return booleanManaged;
 214            }
 215#if !FAKE_XRM_EASY && !FAKE_XRM_EASY_2013
 243216             else if (typeof(Guid) == propertyType)
 0217            {
 0218                return new UniqueIdentifierAttributeMetadata();
 219            }
 220#endif
 221#if !FAKE_XRM_EASY
 267222             else if (typeof(byte[]) == propertyType)
 264223            {
 224
 264225                return new ImageAttributeMetadata();
 226            }
 227#endif
 228#if FAKE_XRM_EASY_9
 3229             else if (typeof(OptionSetValueCollection).IsAssignableFrom(propertyType))
 3230            {
 3231                return new MultiSelectPicklistAttributeMetadata();
 232            }
 233#endif
 234            else
 0235            {
 0236                throw new Exception($"Type {propertyType.Name} has not been mapped to an AttributeMetadata.");
 237            }
 78043238        }
 239
 240        private static void AddOneToManyRelationshipMetadata(Type referencingEntity, PropertyInfo referencingAttribute, 
 60348241        {
 60600242             if (referencingEntity == null || referencingAttribute == null || referencedEntity == null || referencedAttri
 60096243            OneToManyRelationshipMetadata relationshipMetadata = new OneToManyRelationshipMetadata();
 60096244            relationshipMetadata.SchemaName = GetCustomAttribute<RelationshipSchemaNameAttribute>(referencingAttribute).
 60096245            relationshipMetadata.ReferencingEntity = GetCustomAttribute<EntityLogicalNameAttribute>(referencingEntity).L
 60096246             relationshipMetadata.ReferencingAttribute = GetCustomAttribute<AttributeLogicalNameAttribute>(referencingAtt
 60096247            relationshipMetadata.ReferencedEntity = GetCustomAttribute<EntityLogicalNameAttribute>(referencedEntity).Log
 60096248            relationshipMetadata.ReferencedAttribute = GetCustomAttribute<AttributeLogicalNameAttribute>(referencedAttri
 249
 60096250            relationshipMetadatas.Add(relationshipMetadata);
 60348251        }
 252    }
 253}