public static void UpdateCache(List <OneToManyRelationshipMetadata> relMetadataList, MappingEntity mappingEntity
                                       , MappingField[] properties)
        {
            // update modified fields
            var modifiedRelations = mappingEntity.RelationshipsOneToMany
                                    .Where(relation => relMetadataList.Exists(relMeta => relMeta.MetadataId == relation.MetadataId)).ToList();

            modifiedRelations.ForEach(
                rel => Update(relMetadataList.First(relMeta => relMeta.MetadataId == rel.MetadataId), rel, properties));

            // add new attributes
            var newRelMeta = relMetadataList
                             .Where(
                relMeta =>
                !Array.Exists(mappingEntity.RelationshipsOneToMany, relation => relation.MetadataId == relMeta.MetadataId))
                             .ToList();

            newRelMeta.ForEach(
                relMeta =>
            {
                var newRelations = new MappingRelationship1N[mappingEntity.RelationshipsOneToMany.Length + 1];
                Array.Copy(mappingEntity.RelationshipsOneToMany, newRelations, mappingEntity.RelationshipsOneToMany.Length);
                mappingEntity.RelationshipsOneToMany = newRelations;
                mappingEntity.RelationshipsOneToMany[mappingEntity.RelationshipsOneToMany.Length - 1] = Parse(relMeta, properties);
            });
        }
        private static void Update(OneToManyRelationshipMetadata rel, MappingRelationship1N relationshipOneToMany,
                                   MappingField[] properties)
        {
            var propertyName = relationshipOneToMany.ForeignKey;

            if (rel.ReferencedAttribute != null)
            {
                var property = properties.First(p => p.Attribute.LogicalName.ToLower() == rel.ReferencedAttribute.ToLower());
                propertyName = property.DisplayName;
            }

            relationshipOneToMany.Attribute.FromEntity         = rel.ReferencedEntity ?? relationshipOneToMany.Attribute.FromEntity;
            relationshipOneToMany.Attribute.FromKey            = rel.ReferencedAttribute ?? relationshipOneToMany.Attribute.FromKey;
            relationshipOneToMany.Attribute.ToEntity           = rel.ReferencingEntity ?? relationshipOneToMany.Attribute.ToEntity;
            relationshipOneToMany.Attribute.ToKey              = rel.ReferencingAttribute ?? relationshipOneToMany.Attribute.ToKey;
            relationshipOneToMany.Attribute.IntersectingEntity = "";
            relationshipOneToMany.ForeignKey = propertyName;

            if (rel.SchemaName != null)
            {
                relationshipOneToMany.DisplayName = Naming.GetProperVariableName(rel.SchemaName);
                relationshipOneToMany.SchemaName  = Naming.GetProperVariableName(rel.SchemaName);
                relationshipOneToMany.PrivateName = Naming.GetEntityPropertyPrivateName(rel.SchemaName);
                relationshipOneToMany.HybridName  = Naming.GetPluralName(Naming.GetProperVariableName(rel.SchemaName));
            }

            relationshipOneToMany.LogicalName = rel.ReferencingAttribute ?? relationshipOneToMany.LogicalName;

            relationshipOneToMany.EntityRole = "null";
            relationshipOneToMany.Type       = Naming.GetProperVariableName(rel.ReferencingEntity);

            relationshipOneToMany.MetadataId = rel.MetadataId;


            if (rel.ReferencedEntity != null && rel.ReferencingEntity != null &&
                rel.ReferencedEntity == rel.ReferencingEntity)
            {
                relationshipOneToMany.DisplayName = "Referenced" + relationshipOneToMany.DisplayName;
                relationshipOneToMany.EntityRole  = "Microsoft.Xrm.Sdk.EntityRole.Referenced";
            }
        }
        public static MappingRelationship1N Parse(OneToManyRelationshipMetadata rel, MappingField[] properties)
        {
            var property =
                properties.First(p => p.Attribute.LogicalName.ToLower() == rel.ReferencedAttribute.ToLower());

            var propertyName = property.DisplayName;

            var result = new MappingRelationship1N
            {
                Attribute = new CrmRelationshipAttribute
                {
                    FromEntity         = rel.ReferencedEntity,
                    FromKey            = rel.ReferencedAttribute,
                    ToEntity           = rel.ReferencingEntity,
                    ToKey              = rel.ReferencingAttribute,
                    IntersectingEntity = ""
                },
                ForeignKey  = propertyName,
                DisplayName = Naming.GetProperVariableName(rel.SchemaName),
                SchemaName  = Naming.GetProperVariableName(rel.SchemaName),
                LogicalName = rel.ReferencingAttribute,
                PrivateName = Naming.GetEntityPropertyPrivateName(rel.SchemaName),
                HybridName  = Naming.GetPluralName(Naming.GetProperVariableName(rel.SchemaName)),
                EntityRole  = "null",
                Type        = Naming.GetProperVariableName(rel.ReferencingEntity),
                MetadataId  = rel.MetadataId
            };

            if (rel.ReferencedEntity == rel.ReferencingEntity)
            {
                result.DisplayName = "Referenced" + result.DisplayName;
                result.EntityRole  = "Microsoft.Xrm.Sdk.EntityRole.Referenced";
            }

            return(result);
        }
Esempio n. 4
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);
        }
Esempio n. 5
0
        private static void UpdateMappingEntity(EntityMetadata entityMetadata, MappingEntity entity, string serverStamp)
        {
            entity.ServerStamp = serverStamp;

            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;

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

            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)
            {
                if (entityMetadata.Description.UserLocalizedLabel != null)
                {
                    entity.Description = entityMetadata.Description.UserLocalizedLabel.Label;
                }
            }

            MappingField.UpdateCache(entityMetadata.Attributes.ToList(), entity);

            var fields = entity.Fields.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();
            var states = entity.Fields.FirstOrDefault(a => a.IsStateCode);

            entity.States = (states != null) ? states.EnumData : null;
            entity.Enums  = entity.Fields
                            .Where(a => a.EnumData != null)
                            .Select(a => a.EnumData).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);

            var relationshipsManyToMany = entity.RelationshipsManyToMany.ToList();
            var selfReferenced          = entity.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();
        }