| | 1 | | using FakeXrmEasy.Extensions; |
| | 2 | | using Microsoft.Xrm.Sdk; |
| | 3 | | using Microsoft.Xrm.Sdk.Client; |
| | 4 | | using Microsoft.Xrm.Sdk.Metadata; |
| | 5 | | using System; |
| | 6 | | using System.Collections.Generic; |
| | 7 | | using System.Linq; |
| | 8 | | using System.Reflection; |
| | 9 | |
|
| | 10 | | namespace FakeXrmEasy.Metadata |
| | 11 | | { |
| | 12 | | internal class MetadataGenerator |
| | 13 | | { |
| | 14 | | public static IEnumerable<EntityMetadata> FromEarlyBoundEntities(Assembly earlyBoundEntitiesAssembly) |
| 12 | 15 | | { |
| 12 | 16 | | List<EntityMetadata> entityMetadatas = new List<EntityMetadata>(); |
| 17082 | 17 | | foreach (var earlyBoundEntity in earlyBoundEntitiesAssembly.GetTypes()) |
| 8523 | 18 | | { |
| 8523 | 19 | | EntityLogicalNameAttribute entityLogicalNameAttribute = GetCustomAttribute<EntityLogicalNameAttribute>(e |
| 14117 | 20 | | if (entityLogicalNameAttribute == null) continue; |
| 2929 | 21 | | EntityMetadata metadata = new EntityMetadata(); |
| 2929 | 22 | | metadata.LogicalName = entityLogicalNameAttribute.LogicalName; |
| | 23 | |
|
| 2929 | 24 | | FieldInfo entityTypeCode = earlyBoundEntity.GetField("EntityTypeCode", BindingFlags.Static | BindingFlag |
| 2929 | 25 | | if (entityTypeCode != null) |
| 2929 | 26 | | { |
| 2929 | 27 | | metadata.SetFieldValue("_objectTypeCode", entityTypeCode.GetValue(null)); |
| 2929 | 28 | | } |
| | 29 | |
|
| 2929 | 30 | | List<AttributeMetadata> attributeMetadatas = new List<AttributeMetadata>(); |
| 2929 | 31 | | List<ManyToManyRelationshipMetadata> manyToManyRelationshipMetadatas = new List<ManyToManyRelationshipMe |
| 2929 | 32 | | List<OneToManyRelationshipMetadata> oneToManyRelationshipMetadatas = new List<OneToManyRelationshipMetad |
| 2929 | 33 | | List<OneToManyRelationshipMetadata> manyToOneRelationshipMetadatas = new List<OneToManyRelationshipMetad |
| | 34 | |
|
| 2929 | 35 | | var idProperty = earlyBoundEntity.GetProperty("Id"); |
| | 36 | | AttributeLogicalNameAttribute attributeLogicalNameAttribute; |
| 2929 | 37 | | if (idProperty != null && (attributeLogicalNameAttribute = GetCustomAttribute<AttributeLogicalNameAttrib |
| 2917 | 38 | | { |
| 2917 | 39 | | metadata.SetFieldValue("_primaryIdAttribute", attributeLogicalNameAttribute.LogicalName); |
| 2917 | 40 | | } |
| | 41 | |
|
| 2929 | 42 | | var properties = earlyBoundEntity.GetProperties(BindingFlags.Instance | BindingFlags.Public) |
| 179619 | 43 | | .Where(x => x.Name != "Id" && Attribute.IsDefined(x, typeof(AttributeLogicalNameAttr |
| 179619 | 44 | | || Attribute.IsDefined(x, typeof(RelationshipSchemaNameAttribute))); |
| | 45 | |
|
| 298223 | 46 | | foreach (var property in properties) |
| 144718 | 47 | | { |
| 144718 | 48 | | RelationshipSchemaNameAttribute relationshipSchemaNameAttribute = GetCustomAttribute<RelationshipSch |
| 144718 | 49 | | attributeLogicalNameAttribute = GetCustomAttribute<AttributeLogicalNameAttribute>(property); |
| | 50 | |
|
| 144718 | 51 | | if (relationshipSchemaNameAttribute == null) |
| 82942 | 52 | | { |
| | 53 | | #if !FAKE_XRM_EASY |
| 76035 | 54 | | if (property.PropertyType == typeof(byte[])) |
| 264 | 55 | | { |
| 264 | 56 | | metadata.SetFieldValue("_primaryImageAttribute", attributeLogicalNameAttribute.LogicalName); |
| 264 | 57 | | } |
| | 58 | | #endif |
| | 59 | | AttributeMetadata attributeMetadata; |
| 82942 | 60 | | if (attributeLogicalNameAttribute.LogicalName == "statecode") |
| 985 | 61 | | { |
| 985 | 62 | | attributeMetadata = new StateAttributeMetadata(); |
| 985 | 63 | | } |
| 81957 | 64 | | else if (attributeLogicalNameAttribute.LogicalName == "statuscode") |
| 997 | 65 | | { |
| 997 | 66 | | attributeMetadata = new StatusAttributeMetadata(); |
| 997 | 67 | | } |
| 80960 | 68 | | else if (attributeLogicalNameAttribute.LogicalName == metadata.PrimaryIdAttribute) |
| 2917 | 69 | | { |
| 2917 | 70 | | attributeMetadata = new AttributeMetadata(); |
| 2917 | 71 | | attributeMetadata.SetSealedPropertyValue("AttributeType", AttributeTypeCode.Uniqueidentifier |
| 2917 | 72 | | } |
| | 73 | | else |
| 78043 | 74 | | { |
| 78043 | 75 | | attributeMetadata = CreateAttributeMetadata(property.PropertyType); |
| 78043 | 76 | | } |
| | 77 | |
|
| 82942 | 78 | | attributeMetadata.SetFieldValue("_entityLogicalName", entityLogicalNameAttribute.LogicalName); |
| 82942 | 79 | | attributeMetadata.SetFieldValue("_logicalName", attributeLogicalNameAttribute.LogicalName); |
| | 80 | |
|
| 82942 | 81 | | attributeMetadatas.Add(attributeMetadata); |
| 82942 | 82 | | } |
| | 83 | | else |
| 61776 | 84 | | { |
| 61776 | 85 | | if (property.PropertyType.Name == "IEnumerable`1") |
| 31476 | 86 | | { |
| 3497172 | 87 | | PropertyInfo peerProperty = property.PropertyType.GetGenericArguments()[0].GetProperties().S |
| 31476 | 88 | | if (peerProperty == null || peerProperty.PropertyType.Name == "IEnumerable`1") // N:N relati |
| 1428 | 89 | | { |
| 1428 | 90 | | ManyToManyRelationshipMetadata relationshipMetadata = new ManyToManyRelationshipMetadata |
| 1428 | 91 | | relationshipMetadata.SchemaName = relationshipSchemaNameAttribute.SchemaName; |
| | 92 | |
|
| 1428 | 93 | | manyToManyRelationshipMetadatas.Add(relationshipMetadata); |
| 1428 | 94 | | } |
| | 95 | | else // 1:N relationship |
| 30048 | 96 | | { |
| 30048 | 97 | | AddOneToManyRelationshipMetadata(earlyBoundEntity, property, property.PropertyType.GetGe |
| 30048 | 98 | | } |
| 31476 | 99 | | } |
| | 100 | | else //N:1 Property |
| 30300 | 101 | | { |
| 12118010 | 102 | | AddOneToManyRelationshipMetadata(property.PropertyType, property.PropertyType.GetProperties( |
| 30300 | 103 | | } |
| 61776 | 104 | | } |
| 144718 | 105 | | } |
| 2929 | 106 | | if (attributeMetadatas.Any()) |
| 2929 | 107 | | { |
| 2929 | 108 | | metadata.SetSealedPropertyValue("Attributes", attributeMetadatas.ToArray()); |
| 2929 | 109 | | } |
| 2929 | 110 | | if (manyToManyRelationshipMetadatas.Any()) |
| 660 | 111 | | { |
| 660 | 112 | | metadata.SetSealedPropertyValue("ManyToManyRelationships", manyToManyRelationshipMetadatas.ToArray() |
| 660 | 113 | | } |
| 2929 | 114 | | if (manyToOneRelationshipMetadatas.Any()) |
| 2460 | 115 | | { |
| 2460 | 116 | | metadata.SetSealedPropertyValue("ManyToOneRelationships", manyToOneRelationshipMetadatas.ToArray()); |
| 2460 | 117 | | } |
| 2929 | 118 | | if (oneToManyRelationshipMetadatas.Any()) |
| 2424 | 119 | | { |
| 2424 | 120 | | metadata.SetSealedPropertyValue("OneToManyRelationships", oneToManyRelationshipMetadatas.ToArray()); |
| 2424 | 121 | | } |
| 2929 | 122 | | entityMetadatas.Add(metadata); |
| 2929 | 123 | | } |
| 12 | 124 | | return entityMetadatas; |
| 12 | 125 | | } |
| | 126 | |
|
| | 127 | | private static T GetCustomAttribute<T>(MemberInfo member) where T : Attribute |
| 740568 | 128 | | { |
| 740568 | 129 | | return (T)Attribute.GetCustomAttribute(member, typeof(T)); |
| 740568 | 130 | | } |
| | 131 | |
|
| | 132 | | private static AttributeMetadata CreateAttributeMetadata(Type propertyType) |
| 78043 | 133 | | { |
| 78043 | 134 | | if (typeof(string) == propertyType) |
| 17197 | 135 | | { |
| 17197 | 136 | | return new StringAttributeMetadata(); |
| | 137 | | } |
| 60846 | 138 | | else if (typeof(EntityReference).IsAssignableFrom(propertyType)) |
| 19872 | 139 | | { |
| 19872 | 140 | | 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 |
| 30726 | 143 | | else if (typeof(Microsoft.Xrm.Client.CrmEntityReference).IsAssignableFrom(propertyType)) |
| 8 | 144 | | { |
| 8 | 145 | | return new LookupAttributeMetadata(); |
| | 146 | | } |
| | 147 | | #endif |
| 40966 | 148 | | else if (typeof(OptionSetValue).IsAssignableFrom(propertyType)) |
| 4908 | 149 | | { |
| 4908 | 150 | | return new PicklistAttributeMetadata(); |
| | 151 | | } |
| 36058 | 152 | | else if (typeof(Money).IsAssignableFrom(propertyType)) |
| 3240 | 153 | | { |
| 3240 | 154 | | return new MoneyAttributeMetadata(); |
| | 155 | | } |
| 32818 | 156 | | else if (propertyType.IsGenericType) |
| 32227 | 157 | | { |
| 32227 | 158 | | Type genericType = propertyType.GetGenericArguments().FirstOrDefault(); |
| 32227 | 159 | | if (propertyType.GetGenericTypeDefinition() == typeof(Nullable<>)) |
| 31855 | 160 | | { |
| 31855 | 161 | | if (typeof(int) == genericType) |
| 8535 | 162 | | { |
| 8535 | 163 | | return new IntegerAttributeMetadata(); |
| | 164 | | } |
| 23320 | 165 | | else if (typeof(double) == genericType) |
| 684 | 166 | | { |
| 684 | 167 | | return new DoubleAttributeMetadata(); |
| | 168 | | } |
| 22636 | 169 | | else if (typeof(bool) == genericType) |
| 5556 | 170 | | { |
| 5556 | 171 | | return new BooleanAttributeMetadata(); |
| | 172 | | } |
| 17080 | 173 | | else if (typeof(decimal) == genericType) |
| 1416 | 174 | | { |
| 1416 | 175 | | return new DecimalAttributeMetadata(); |
| | 176 | | } |
| 15664 | 177 | | else if (typeof(DateTime) == genericType) |
| 8571 | 178 | | { |
| 8571 | 179 | | return new DateTimeAttributeMetadata(); |
| | 180 | | } |
| 7093 | 181 | | else if (typeof(Guid) == genericType) |
| 4380 | 182 | | { |
| 4380 | 183 | | return new LookupAttributeMetadata(); |
| | 184 | | } |
| 2713 | 185 | | else if (typeof(long) == genericType) |
| 2713 | 186 | | { |
| 2713 | 187 | | return new BigIntAttributeMetadata(); |
| | 188 | | } |
| 0 | 189 | | else if (typeof(Enum).IsAssignableFrom(genericType)) |
| 0 | 190 | | { |
| 0 | 191 | | return new StateAttributeMetadata(); |
| | 192 | | } |
| | 193 | | else |
| 0 | 194 | | { |
| 0 | 195 | | throw new Exception($"Type {propertyType.Name}{genericType.Name} has not been mapped to an Attri |
| | 196 | | } |
| | 197 | | } |
| 372 | 198 | | else if (propertyType.GetGenericTypeDefinition() == typeof(IEnumerable<>)) |
| 372 | 199 | | { |
| 372 | 200 | | var partyList = new LookupAttributeMetadata(); |
| 372 | 201 | | partyList.SetSealedPropertyValue("AttributeType", AttributeTypeCode.PartyList); |
| 372 | 202 | | return partyList; |
| | 203 | | } |
| | 204 | | else |
| 0 | 205 | | { |
| 0 | 206 | | throw new Exception($"Type {propertyType.Name}{genericType.Name} has not been mapped to an Attribute |
| | 207 | | } |
| | 208 | | } |
| 591 | 209 | | else if (typeof(BooleanManagedProperty) == propertyType) |
| 324 | 210 | | { |
| 324 | 211 | | var booleanManaged = new BooleanAttributeMetadata(); |
| 324 | 212 | | booleanManaged.SetSealedPropertyValue("AttributeType", AttributeTypeCode.ManagedProperty); |
| 324 | 213 | | return booleanManaged; |
| | 214 | | } |
| | 215 | | #if !FAKE_XRM_EASY && !FAKE_XRM_EASY_2013 |
| 243 | 216 | | else if (typeof(Guid) == propertyType) |
| 0 | 217 | | { |
| 0 | 218 | | return new UniqueIdentifierAttributeMetadata(); |
| | 219 | | } |
| | 220 | | #endif |
| | 221 | | #if !FAKE_XRM_EASY |
| 267 | 222 | | else if (typeof(byte[]) == propertyType) |
| 264 | 223 | | { |
| | 224 | |
|
| 264 | 225 | | return new ImageAttributeMetadata(); |
| | 226 | | } |
| | 227 | | #endif |
| | 228 | | #if FAKE_XRM_EASY_9 |
| 3 | 229 | | else if (typeof(OptionSetValueCollection).IsAssignableFrom(propertyType)) |
| 3 | 230 | | { |
| 3 | 231 | | return new MultiSelectPicklistAttributeMetadata(); |
| | 232 | | } |
| | 233 | | #endif |
| | 234 | | else |
| 0 | 235 | | { |
| 0 | 236 | | throw new Exception($"Type {propertyType.Name} has not been mapped to an AttributeMetadata."); |
| | 237 | | } |
| 78043 | 238 | | } |
| | 239 | |
|
| | 240 | | private static void AddOneToManyRelationshipMetadata(Type referencingEntity, PropertyInfo referencingAttribute, |
| 60348 | 241 | | { |
| 60600 | 242 | | if (referencingEntity == null || referencingAttribute == null || referencedEntity == null || referencedAttri |
| 60096 | 243 | | OneToManyRelationshipMetadata relationshipMetadata = new OneToManyRelationshipMetadata(); |
| 60096 | 244 | | relationshipMetadata.SchemaName = GetCustomAttribute<RelationshipSchemaNameAttribute>(referencingAttribute). |
| 60096 | 245 | | relationshipMetadata.ReferencingEntity = GetCustomAttribute<EntityLogicalNameAttribute>(referencingEntity).L |
| 60096 | 246 | | relationshipMetadata.ReferencingAttribute = GetCustomAttribute<AttributeLogicalNameAttribute>(referencingAtt |
| 60096 | 247 | | relationshipMetadata.ReferencedEntity = GetCustomAttribute<EntityLogicalNameAttribute>(referencedEntity).Log |
| 60096 | 248 | | relationshipMetadata.ReferencedAttribute = GetCustomAttribute<AttributeLogicalNameAttribute>(referencedAttri |
| | 249 | |
|
| 60096 | 250 | | relationshipMetadatas.Add(relationshipMetadata); |
| 60348 | 251 | | } |
| | 252 | | } |
| | 253 | | } |