Exemple #1
0
        void UpdateEntityImageFields(Models.Entity entity)
        {
            Log.Progress($"Updating fields for {entity.Name} entity type.");
            var attributesRequest = new RetrieveEntityRequest
            {
                EntityFilters = EntityFilters.Entity,
                LogicalName   = entity.Name
            };
            var meta         = ((RetrieveEntityResponse)Cds.Execute(attributesRequest)).EntityMetadata;
            var imageField   = entity.Fields.Get("entityimage");
            var imageIdField = entity.Fields.Get("entityimageid");

            if (!string.IsNullOrEmpty(meta.PrimaryImageAttribute))
            {
                if (imageField == null)
                {
                    // Add field.
                    Log.Information($"Field {Field.EntityImage.Name} being ADDED to schema.");
                    entity.Fields.Add(Field.EntityImage);
                }
                else
                {
                    // Update field.
                    Log.Verbose($"Field {imageField.Name} being UPDATED in schema.");
                    entity.Fields.Set(Field.EntityImage);
                }
                if (imageIdField == null)
                {
                    // Add field.
                    Log.Information($"Field {Field.EntityImageId.Name} being ADDED to schema.");
                    entity.Fields.Add(Field.EntityImageId);
                }
                else
                {
                    // Update field.
                    Log.Verbose($"Field {imageIdField.Name} being UPDATED in schema.");
                    entity.Fields.Set(Field.EntityImageId);
                }
            }
            else
            {
                if (imageField != null)
                {
                    // Feature not enabled connected environment.
                    Log.Warning($"Field {imageField.Name} being REMOVED from schema because this feature is not in the connected environment.");
                    entity.Fields.Remove(imageField);
                }
                if (imageIdField != null)
                {
                    // Feature not enabled connected environment.
                    Log.Warning($"Field {imageIdField.Name} being REMOVED from schema because this feature is not in the connected environment.");
                    entity.Fields.Remove(imageIdField);
                }
            }
        }
Exemple #2
0
        void UpdateRelationships(Models.Entity entity, bool intelligently = false)
        {
            Log.Progress($"Updating relationships for {entity.Name} entity type.");
            var relationshipsRequest = new RetrieveEntityRequest
            {
                EntityFilters = EntityFilters.Relationships,
                LogicalName   = entity.Name
            };
            var relationships = ((RetrieveEntityResponse)Cds.Execute(relationshipsRequest)).EntityMetadata.ManyToManyRelationships
                                .Where(r => r.Entity1LogicalName == entity.Name)
                                .ToArray();
            // NOTE: CMT doesn't capture SchemaName as name for relationship - but probably should. It uses IntersectEntityName instead.
            var relationshipsCorrelatedLeft = from meta in relationships
                                              join schema in entity.Relationships
                                              on meta.IntersectEntityName equals schema.Name into joinGroup
                                              from j in joinGroup.DefaultIfEmpty()
                                              select(meta, j);
            var relationshipsCorrelatedRight = from schema in entity.Relationships
                                               join meta in relationships
                                               on schema.Name equals meta.IntersectEntityName into joinGroup
                                               from j in joinGroup.DefaultIfEmpty()
                                               select(j, schema);
            var relationshipsCorrelated = relationshipsCorrelatedRight
                                          .Union(relationshipsCorrelatedLeft)
                                          .OrderBy(x => x.Item1 != null ? x.Item1.IntersectEntityName : x.Item2.Name)
                                          .ToArray();

            foreach (var(meta, schema) in relationshipsCorrelated)
            {
                if (meta == null)
                {
                    // Relationship does not exist in connected environment.
                    Log.Warning($"Relationship {schema.Name} being REMOVED from schema.");
                    entity.Relationships.Remove(schema);
                }
                else if (schema == null)
                {
                    if (intelligently)
                    {
                        if (meta.IsCustomRelationship != true)
                        {
                            // Check if this is a foreign relationship.
                            var isForeignRelationship = !CurrentDocument.Entities.Exists(e => e.Name == meta.Entity2LogicalName);
                            if (isForeignRelationship)
                            {
                                // Check if schema already includes relationships with entities not included in schema.
                                var entityHasForeignRelationshipsInSchema = entity.Relationships.Any(r => !CurrentDocument.Entities.Exists(e => e.Name == r.TargetEntity));
                                if (!entityHasForeignRelationshipsInSchema)
                                {
                                    // Assuming that non-custom foreign relationships are not appropriate for automatic inclusion.
                                    Log.Information($"Relationship {meta.SchemaName} being IGNORED.");
                                    continue;
                                }
                            }
                        }
                    }
                    // Relationship does not exist in schema.
                    Log.Information($"Relationship {meta.SchemaName} being ADDED to schema.");
                    entity.Relationships.Add(Copy.Metadata.ToRelationship(meta));
                }
                else
                {
                    // Update relationship.
                    Log.Verbose($"Relationship {meta.SchemaName} being UPDATED in schema.");
                    Copy.Metadata.ToRelationship(meta, schema);
                }
            }
        }
Exemple #3
0
        void UpdateFields(Models.Entity entity, bool intelligently = false)
        {
            if (intelligently)
            {
                if (entity.Fields?.Any() != true && entity.Relationships?.Any() == true)
                {
                    // Assuming only the associations are wanted.
                    Log.Progress($"Skip updating fields for {entity.Name} entity type.");
                    return;
                }
            }
            Log.Progress($"Updating fields for {entity.Name} entity type.");
            var attributesRequest = new RetrieveEntityRequest
            {
                EntityFilters = EntityFilters.Attributes,
                LogicalName   = entity.Name
            };
            var attributes = ((RetrieveEntityResponse)Cds.Execute(attributesRequest)).EntityMetadata.Attributes;
            var fields     = entity.Fields
                             .Where(f => f.Name != Field.EntityImage.Name && f.Name != Field.EntityImageId.Name)
                             .ToArray();
            var attributesCorrelatedLeft = from a in attributes
                                           join f in fields
                                           on a.LogicalName equals f.Name into joinGroup
                                           from j in joinGroup.DefaultIfEmpty()
                                           select(a, j);
            var attributesCorrelatedRight = from f in fields
                                            join a in attributes
                                            on f.Name equals a.LogicalName into joinGroup
                                            from j in joinGroup.DefaultIfEmpty()
                                            select(j, f);
            var attributesCorrelated = attributesCorrelatedLeft
                                       .Union(attributesCorrelatedRight)
                                       .OrderBy(x => x.Item1 != null ? x.Item1.LogicalName : x.Item2.Name)
                                       .ToArray();

            foreach (var(attribute, field) in attributesCorrelated)
            {
                if (attribute == null)
                {
                    // Attribute does not exist in connected environment.
                    Log.Warning($"Field {field.Name} being REMOVED from schema because it does not exist in the connected environment.");
                    entity.Fields.Remove(field);
                }
                else if (Copy.Metadata.IsUnsupported(attribute))
                {
                    Log.Silently($"Attribute {attribute.LogicalName} being IGNORED because it is not supported.");
                    if (field != null)
                    {
                        Log.Warning($"Field {field.Name} being REMOVED from schema because it is not supported.");
                        entity.Fields.Remove(field);
                    }
                }
                else if (!Copy.Metadata.IsWritable(attribute))
                {
                    Log.Silently($"Attribute {attribute.LogicalName} being IGNORED because it is not writable.");
                    if (field != null)
                    {
                        Log.Warning($"Field {field.Name} being REMOVED from schema because it is not writable.");
                        entity.Fields.Remove(field);
                    }
                }
                else if (field == null)
                {
                    // Attribute does not exist in schema.
                    Log.Information($"Attribute {attribute.LogicalName} being ADDED to schema.");
                    entity.Fields.Add(Copy.Metadata.ToField(attribute));
                }
                else
                {
                    // Update field.
                    Log.Verbose($"Attribute {attribute.LogicalName} being UPDATED in schema.");
                    Copy.Metadata.ToField(attribute, field);
                }
            }
        }