public async Task <CkTypeInfo> GetCkTypeInfoAsync(IOspSession session, CkEntity ckId)
        {
            var filter = Builders <CkEntity> .Filter.BuildIdFilter(ckId.CkId);

            var aggregate = CkEntities.Aggregate(session).Match(filter);

            return(await AggregateCkTypeInfo(aggregate).SingleOrDefaultAsync());
        }
        private static void ValidateTextSearchLanguage(CkEntity ckEntity, List <CkEntityAttribute> attributes)
        {
            StringBuilder errorMessageStringBuilder = new StringBuilder();

            List <string> missingAttributes = new List <string>();
            List <string> unknownAnalyzers  = new List <string>();

            foreach (var ckEntityIndex in ckEntity.Indexes)
            {
                if (ckEntityIndex.IndexType == IndexTypes.Text && !Constants.KnownAnalyzerLanguages.Contains(ckEntityIndex.Language))
                {
                    unknownAnalyzers.Add(ckEntityIndex.Language);
                }

                foreach (var ckIndexFields in ckEntityIndex.Fields)
                {
                    var missingAttributeList = ckIndexFields.AttributeNames.Where(s =>
                                                                                  !attributes.Exists(attribute => attribute.AttributeName == s)).ToArray();
                    if (missingAttributeList.Any())
                    {
                        missingAttributes.AddRange(missingAttributeList);
                    }

                    var duplicateAttributeNames = ckIndexFields.AttributeNames.GroupBy(a => a).Where(a => a.Count() > 1).ToList();
                    if (duplicateAttributeNames.Count > 0)
                    {
                        var attributeNames = string.Join(", ", duplicateAttributeNames.Select(x => x.Key));
                        errorMessageStringBuilder.AppendLine($"Text search language '{ckEntityIndex.Language}' at CkId '{ckEntity.CkId}' has duplicate attribute names in one definition: '{attributeNames}'");
                    }

                    if (ckEntityIndex.IndexType == IndexTypes.Text && ckIndexFields.Weight.HasValue && ckIndexFields.Weight < 1)
                    {
                        errorMessageStringBuilder.AppendLine($"Weight '{ckIndexFields.Weight}' for language '{ckEntityIndex.Language}' at CkId '{ckEntity.CkId}' is invalid.");
                    }
                }
            }

            if (missingAttributes.Any())
            {
                var attributeNames = string.Join(", ", missingAttributes);
                errorMessageStringBuilder.AppendLine($"Text search attribute names '{attributeNames}‘ does not exist at CkId: '{ckEntity.CkId}'");
            }
            if (unknownAnalyzers.Any())
            {
                var unknownAnalyzerTerms = string.Join(", ", unknownAnalyzers);
                errorMessageStringBuilder.AppendLine($"Text search languages '{unknownAnalyzerTerms}‘ are unknown at CkId: '{ckEntity.CkId}'");
            }

            if (errorMessageStringBuilder.Length > 0)
            {
                throw new ModelImportException($"Validation of Construction Kit Model failed: " + Environment.NewLine + errorMessageStringBuilder.ToString());
            }
        }
        internal static CkEntityDto CreateCkEntityDto(CkEntity ckEntity)
        {
            var ckEntityDto = new CkEntityDto
            {
                CkId       = ckEntity.CkId,
                TypeName   = ckEntity.CkId.GetGraphQLName(),
                IsFinal    = ckEntity.IsFinal,
                IsAbstract = ckEntity.IsAbstract,
                ScopeId    = (ScopeIdsDto)ckEntity.ScopeId
            };

            return(ckEntityDto);
        }
        private IEnumerable <CkEntityAttribute> GetAllDerivedAttributes(IList <CkEntityInheritance> ckEntityInheritances, IList <CkEntity> availableEntities, CkEntity ckEntity)
        {
            List <CkEntityAttribute> attributeList = new List <CkEntityAttribute>();
            var currentEntity = ckEntity;

            while (currentEntity != null)
            {
                attributeList.AddRange(currentEntity.Attributes);

                var result = ckEntityInheritances.FirstOrDefault(x => x.TargetCkId == currentEntity.CkId);
                if (result == null)
                {
                    break;
                }

                currentEntity = availableEntities.FirstOrDefault(x => x.CkId == result.OriginCkId);
            }

            return(attributeList);
        }