Ejemplo n.º 1
0
        public async Task EnrichAsync(Context context, IEnumerable <ContentEntity> contents, ProvideSchema schemas)
        {
            if (ShouldEnrich(context))
            {
                var ids = new HashSet <Guid>();

                foreach (var group in contents.GroupBy(x => x.SchemaId.Id))
                {
                    var schema = await schemas(group.Key);

                    AddAssetIds(ids, schema, group);
                }

                var assets = await GetAssetsAsync(context, ids);

                foreach (var group in contents.GroupBy(x => x.SchemaId.Id))
                {
                    var schema = await schemas(group.Key);

                    ResolveAssetsUrls(schema, group, assets);
                }
            }
        }
Ejemplo n.º 2
0
        public async Task EnrichAsync(Context context, IEnumerable <ContentEntity> contents, ProvideSchema schemas)
        {
            if (ShouldEnrich(context))
            {
                var ids = new HashSet <DomainId>();

                foreach (var group in contents.GroupBy(x => x.SchemaId.Id))
                {
                    var schema = await schemas(group.Key);

                    AddReferenceIds(ids, schema, group);
                }

                var references = await GetReferencesAsync(context, ids);

                foreach (var group in contents.GroupBy(x => x.SchemaId.Id))
                {
                    var schema = await schemas(group.Key);

                    await ResolveReferencesAsync(context, schema, group, references, schemas);
                }
            }
        }
Ejemplo n.º 3
0
        public async Task EnrichAsync(Context context, IEnumerable <ContentEntity> contents, ProvideSchema schemas)
        {
            var resolveDataDraft = context.ShouldProvideUnpublished() || context.IsFrontendClient;

            var referenceCleaner = await CleanReferencesAsync(context, contents, schemas);

            var converters = GenerateConverters(context, referenceCleaner).ToArray();

            foreach (var group in contents.GroupBy(x => x.SchemaId.Id))
            {
                var schema = await schemas(group.Key);

                foreach (var content in group)
                {
                    content.Data = content.Data.ConvertName2Name(schema.SchemaDef, converters);
                }
            }
        }
Ejemplo n.º 4
0
        private async Task <ValueConverter?> CleanReferencesAsync(Context context, IEnumerable <ContentEntity> contents, ProvideSchema schemas)
        {
            if (context.ShouldCleanup())
            {
                var ids = new HashSet <Guid>();

                foreach (var group in contents.GroupBy(x => x.SchemaId.Id))
                {
                    var schema = await schemas(group.Key);

                    foreach (var content in group)
                    {
                        content.Data.AddReferencedIds(schema.SchemaDef, ids);
                    }
                }

                if (ids.Count > 0)
                {
                    var taskForAssets   = QueryAssetIdsAsync(context, ids);
                    var taskForContents = QueryContentIdsAsync(context, ids);

                    await Task.WhenAll(taskForAssets, taskForContents);

                    var foundIds = new HashSet <Guid>(taskForAssets.Result.Union(taskForContents.Result));

                    return(ValueReferencesConverter.CleanReferences(foundIds));
                }
            }

            return(null);
        }
Ejemplo n.º 5
0
        private async Task ResolveReferencesAsync(Context context, ISchemaEntity schema, IEnumerable <ContentEntity> contents, ILookup <DomainId, IEnrichedContentEntity> references, ProvideSchema schemas)
        {
            var formatted = new Dictionary <IContentEntity, JsonObject>();

            foreach (var field in schema.SchemaDef.ResolvingReferences())
            {
                foreach (var content in contents)
                {
                    content.ReferenceData ??= new NamedContentData();

                    var fieldReference = content.ReferenceData.GetOrAdd(field.Name, _ => new ContentFieldData()) !;

                    try
                    {
                        if (content.Data.TryGetValue(field.Name, out var fieldData) && fieldData != null)
                        {
                            foreach (var(partition, partitionValue) in fieldData)
                            {
                                var referencedContents =
                                    field.GetReferencedIds(partitionValue)
                                    .Select(x => references[x])
                                    .SelectMany(x => x)
                                    .ToList();

                                if (referencedContents.Count == 1)
                                {
                                    var reference = referencedContents[0];

                                    var referencedSchema = await schemas(reference.SchemaId.Id);

                                    requestCache.AddDependency(referencedSchema.UniqueId, referencedSchema.Version);
                                    requestCache.AddDependency(reference.UniqueId, reference.Version);

                                    var value = formatted.GetOrAdd(reference, x => Format(x, context, referencedSchema));

                                    fieldReference.AddJsonValue(partition, value);
                                }
                                else if (referencedContents.Count > 1)
                                {
                                    var value = CreateFallback(context, referencedContents);

                                    fieldReference.AddJsonValue(partition, value);
                                }
                            }
                        }
                    }
                    catch (DomainObjectNotFoundException)
                    {
                        continue;
                    }
                }
            }
        }
Ejemplo n.º 6
0
        public async Task EnrichAsync(Context context, IEnumerable <ContentEntity> contents, ProvideSchema schemas)
        {
            foreach (var group in contents.GroupBy(x => x.SchemaId.Id))
            {
                var schema = await schemas(group.Key);

                var schemaName        = schema.SchemaDef.Name;
                var schemaDisplayName = schema.SchemaDef.DisplayNameUnchanged();

                foreach (var content in group)
                {
                    content.IsSingleton = schema.SchemaDef.IsSingleton;

                    content.SchemaName        = schemaName;
                    content.SchemaDisplayName = schemaDisplayName;
                }

                if (context.IsFrontendClient)
                {
                    var referenceFields = schema.SchemaDef.ReferenceFields().ToArray();

                    foreach (var content in group)
                    {
                        content.ReferenceFields = referenceFields;
                    }
                }
            }
        }
Ejemplo n.º 7
0
        private async Task <ValueConverter?> CleanReferencesAsync(Context context, IEnumerable <ContentEntity> contents, ProvideSchema schemas,
                                                                  CancellationToken ct)
        {
            if (!context.ShouldSkipCleanup())
            {
                var ids = new HashSet <DomainId>();

                foreach (var group in contents.GroupBy(x => x.SchemaId.Id))
                {
                    var(schema, components) = await schemas(group.Key);

                    foreach (var content in group)
                    {
                        content.Data.AddReferencedIds(schema.SchemaDef, ids, components);
                    }
                }

                if (ids.Count > 0)
                {
                    var(assets, refContents) = await AsyncHelper.WhenAll(
                        QueryAssetIdsAsync(context, ids, ct),
                        QueryContentIdsAsync(context, ids, ct));

                    var foundIds = assets.Union(refContents).ToHashSet();

                    return(ValueReferencesConverter.CleanReferences(foundIds));
                }
            }

            return(null);
        }
Ejemplo n.º 8
0
        public async Task EnrichAsync(Context context, IEnumerable <ContentEntity> contents, ProvideSchema schemas,
                                      CancellationToken ct)
        {
            if (ShouldEnrich(context))
            {
                foreach (var group in contents.GroupBy(x => x.SchemaId.Id))
                {
                    var schema = await schemas(group.Key);

                    var script = schema.SchemaDef.Scripts.Query;

                    if (!string.IsNullOrWhiteSpace(script))
                    {
                        await Task.WhenAll(group.Select(x => TransformAsync(context, script, x, ct)));
                    }
                }
            }
        }
Ejemplo n.º 9
0
        public async Task EnrichAsync(Context context, IEnumerable <ContentEntity> contents, ProvideSchema schemas,
                                      CancellationToken ct)
        {
            var referenceCleaner = await CleanReferencesAsync(context, contents, schemas, ct);

            foreach (var group in contents.GroupBy(x => x.SchemaId.Id))
            {
                ct.ThrowIfCancellationRequested();

                var(schema, components) = await schemas(group.Key);

                var converters = GenerateConverters(context, components, referenceCleaner).ToArray();

                foreach (var content in group)
                {
                    content.Data = content.Data.Convert(schema.SchemaDef, converters);
                }
            }
        }
Ejemplo n.º 10
0
 public async Task EnrichAsync(Context context, IEnumerable <ContentEntity> contents, ProvideSchema schemas,
                               CancellationToken ct)
 {
     foreach (var group in contents.GroupBy(x => x.SchemaId.Id))
     {
         Schema = (await schemas(group.Key)).Schema;
     }
 }
Ejemplo n.º 11
0
        public async Task EnrichAsync(Context context, IEnumerable <ContentEntity> contents, ProvideSchema schemas)
        {
            var app = context.App;

            foreach (var group in contents.GroupBy(x => x.SchemaId.Id))
            {
                var schema = await schemas(group.Key);

                foreach (var content in group)
                {
                    requestCache.AddDependency(content.Id, content.Version);
                    requestCache.AddDependency(app.Id, app.Version);
                    requestCache.AddDependency(schema.Id, schema.Version);
                }
            }
        }
Ejemplo n.º 12
0
        public async Task EnrichAsync(Context context, IEnumerable <ContentEntity> contents, ProvideSchema schemas)
        {
            var cache = new Dictionary <(Guid, Status), StatusInfo>();

            foreach (var content in contents)
            {
                await EnrichColorAsync(content, content, cache);

                if (ShouldEnrichWithStatuses(context))
                {
                    await EnrichNextsAsync(content, context);
                    await EnrichCanUpdateAsync(content, context);
                }
            }
        }
Ejemplo n.º 13
0
 public async Task EnrichAsync(Context context, IEnumerable <ContentEntity> contents, ProvideSchema schemas)
 {
     foreach (var group in contents.GroupBy(x => x.SchemaId.Id))
     {
         Schema = await schemas(group.Key);
     }
 }