Example #1
0
        public Guid GetRecordUniqueId(EntityReference record)
        {
            if (string.IsNullOrWhiteSpace(record.LogicalName))
            {
                throw new InvalidOperationException("The entity logical name must not be null or empty.");
            }

            // Don't fail with invalid operation exception, if no record of this entity exists, but entity is known
            if (!Data.ContainsKey(record.LogicalName) && !EntityMetadata.ContainsKey(record.LogicalName))
            {
                if (ProxyTypesAssembly == null)
                {
                    throw new InvalidOperationException($"The entity logical name {record.LogicalName} is not valid.");
                }

                if (!ProxyTypesAssembly.GetTypes().Any(type => FindReflectedType(record.LogicalName) != null))
                {
                    throw new InvalidOperationException($"The entity logical name {record.LogicalName} is not valid.");
                }
            }

#if !FAKE_XRM_EASY && !FAKE_XRM_EASY_2013 && !FAKE_XRM_EASY_2015
            if (record.Id == Guid.Empty && record.HasKeyAttributes())
            {
                if (EntityMetadata.ContainsKey(record.LogicalName))
                {
                    var entityMetadata = EntityMetadata[record.LogicalName];
                    foreach (var key in entityMetadata.Keys)
                    {
                        if (record.KeyAttributes.Keys.Count == key.KeyAttributes.Length && key.KeyAttributes.All(x => record.KeyAttributes.Keys.Contains(x)))
                        {
                            var matchedRecord = Data[record.LogicalName].Values.SingleOrDefault(x => record.KeyAttributes.All(k => x.Attributes.ContainsKey(k.Key) && x.Attributes[k.Key] != null && x.Attributes[k.Key].Equals(k.Value)));
                            if (matchedRecord == null)
                            {
                                new FaultException <OrganizationServiceFault>(new OrganizationServiceFault(), $"{record.LogicalName} with the specified Alternate Keys Does Not Exist");
                            }
                            return(matchedRecord.Id);
                        }
                    }
                    throw new InvalidOperationException($"The requested key attributes do not exist for the entity {record.LogicalName}");
                }
                else
                {
                    throw new InvalidOperationException($"The requested key attributes do not exist for the entity {record.LogicalName}");
                }
            }
#endif

            /*
             * if (record.Id == Guid.Empty)
             * {
             *  throw new InvalidOperationException("The id must not be empty.");
             * }
             */

            return(record.Id);
        }
Example #2
0
        public bool GenerateEntity(EntityMetadata entityMetadata, IServiceProvider services)
        {
            if (!DefaultService.GenerateEntity(entityMetadata, services))
            {
                return(false);
            }

            if (!EntityMetadata.ContainsKey(entityMetadata.LogicalName))
            {
                EntityMetadata.Add(entityMetadata.LogicalName, entityMetadata);
            }
            return(!EntitiesToSkip.Contains(entityMetadata.LogicalName));
        }
        protected internal bool AttributeExistsInMetadata(string sEntityName, string sAttributeName)
        {
            var relationships = this.Relationships.Values.Where(value => new[] { value.Entity1LogicalName, value.Entity2LogicalName, value.IntersectEntity }.Contains(sEntityName, StringComparer.InvariantCultureIgnoreCase)).ToArray();

            if (relationships.Any(e => e.Entity1Attribute == sAttributeName || e.Entity2Attribute == sAttributeName))
            {
                return(true);
            }

            //Early bound types
            if (ProxyTypesAssembly != null)
            {
                //Check if attribute exists in the early bound type
                var earlyBoundType = FindReflectedType(sEntityName);
                if (earlyBoundType != null)
                {
                    //Get that type properties
                    var attributeFound = earlyBoundType
                                         .GetProperties()
                                         .Where(pi => pi.GetCustomAttributes(typeof(AttributeLogicalNameAttribute), true).Length > 0)
                                         .Where(pi => (pi.GetCustomAttributes(typeof(AttributeLogicalNameAttribute), true)[0] as AttributeLogicalNameAttribute).LogicalName.Equals(sAttributeName))
                                         .FirstOrDefault();

                    if (attributeFound != null)
                    {
                        return(true);
                    }

                    if (attributeFound == null && EntityMetadata.ContainsKey(sEntityName))
                    {
                        //Try with metadata
                        return(AttributeExistsInInjectedMetadata(sEntityName, sAttributeName));
                    }
                    else
                    {
                        return(false);
                    }
                }
                //Try with metadata
                return(false);
            }

            if (EntityMetadata.ContainsKey(sEntityName))
            {
                //Try with metadata
                return(AttributeExistsInInjectedMetadata(sEntityName, sAttributeName));
            }

            //Dynamic entities and not entity metadata injected for entity => just return true if not found
            return(true);
        }
Example #4
0
        public bool GenerateEntity(EntityMetadata entityMetadata, IServiceProvider services)
        {
            // Some entities are not normally create (attachment for example) not sure why.  Allowing Whitelist to Override here.
            if (!Approver.IsExplicitlyAllowed(entityMetadata.LogicalName) &&
                !DefaultService.GenerateEntity(entityMetadata, services))
            {
                return(false);
            }

            if (!EntityMetadata.ContainsKey(entityMetadata.LogicalName))
            {
                EntityMetadata.Add(entityMetadata.LogicalName, entityMetadata);
            }

            return(Approver.IsAllowed(entityMetadata.LogicalName));
        }
        public bool GenerateEntity(EntityMetadata entityMetadata, IServiceProvider services)
        {
            if (!DefaultService.GenerateEntity(entityMetadata, services))
            {
                return(false);
            }

            if (!EntityMetadata.ContainsKey(entityMetadata.LogicalName))
            {
                EntityMetadata.Add(entityMetadata.LogicalName, entityMetadata);
            }

            // If Whitelist is populated, Skip if not in Whitelist.
            if (EntitiesWhitelist.Count > 0 && !EntitiesWhitelist.Contains(entityMetadata.LogicalName))
            {
                return(false);
            }

            return(!EntitiesToSkip.Contains(entityMetadata.LogicalName) && !EntityPrefixesToSkip.Any(p => entityMetadata.LogicalName.StartsWith(p)));
        }