private void ApplyDisplayPatternAttributes(SchemaDetail schemaDetail, Type contractType) { var displayPatternAttributes = contractType.GetTypeInfo() .GetCustomAttributes <PictureparkDisplayPatternAttribute>(true); foreach (var displayPatternAttribute in displayPatternAttributes.GroupBy(g => new { g.Type, g.TemplateEngine })) { if (displayPatternAttribute.GroupBy(x => x.Language).Any(i => i.Count() > 1)) { throw new InvalidOperationException("Multiple display patterns for the same language are defined."); } // If no type is specified, set for all the types. var types = displayPatternAttribute.Key.Type.HasValue ? new[] { displayPatternAttribute.Key.Type.Value } : Enum.GetValues(typeof(DisplayPatternType)).OfType <DisplayPatternType>(); foreach (var type in types) { schemaDetail.DisplayPatterns.Add(new DisplayPattern { DisplayPatternType = type, TemplateEngine = displayPatternAttribute.Key.TemplateEngine, Templates = new TranslatedStringDictionary(displayPatternAttribute.ToDictionary(x => string.IsNullOrEmpty(x.Language) ? _defaultLanguage : x.Language, x => x.DisplayPattern)) }); } } }
/// <summary>Updates the given <see cref="SchemaDetail"/>.</summary> /// <param name="schemaDetail">The schema detail.</param> /// <param name="enableForBinaryFiles">Specifies whether to enable the schema for binary files.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns>The task.</returns> /// <exception cref="ApiException">A server side error occurred.</exception> public async Task UpdateAndWaitForCompletionAsync(SchemaDetail schemaDetail, bool enableForBinaryFiles, CancellationToken cancellationToken = default(CancellationToken)) { if (enableForBinaryFiles && schemaDetail.Types.Contains(SchemaType.Layer)) { var binarySchemas = new List <string> { nameof(FileMetadata), nameof(AudioMetadata), nameof(DocumentMetadata), nameof(ImageMetadata), nameof(VideoMetadata), }; schemaDetail.ReferencedInContentSchemaIds = binarySchemas; } var updateRequest = new SchemaUpdateRequest { Aggregations = schemaDetail.Aggregations, Descriptions = schemaDetail.Descriptions, DisplayPatterns = schemaDetail.DisplayPatterns, Fields = schemaDetail.Fields, SchemaPermissionSetIds = schemaDetail.SchemaPermissionSetIds, Names = schemaDetail.Names, Public = schemaDetail.Public, ReferencedInContentSchemaIds = schemaDetail.ReferencedInContentSchemaIds, Sort = schemaDetail.Sort, SortOrder = schemaDetail.SortOrder, Types = schemaDetail.Types, LayerSchemaIds = schemaDetail.LayerSchemaIds }; await UpdateAndWaitForCompletionAsync(schemaDetail.Id, updateRequest, cancellationToken).ConfigureAwait(false); }
public void BulkOperationsHelper_BuildValueSetFromDataTable_WithMultipleValues() { var schemaDetail = new SchemaDetail() { DateTimeTypePrecisionDic = new Dictionary <string, string>(), MaxCharDic = new Dictionary <string, string>(), NumericPrecisionTypeDic = new Dictionary <string, PrecisionType>(), NullableDic = new Dictionary <string, bool>() }; var bookList = GetBookList(); HashSet <string> columns = new HashSet <string>() { "Description", "Id", "ISBN", "Title" }; Dictionary <string, int> ordinalDic = new Dictionary <string, int>(); List <PropertyInfo> propertyInfoList = typeof(Book).GetProperties().OrderBy(x => x.Name).ToList(); var dataTable = BulkOperationsHelper.CreateDataTable <Book>(propertyInfoList, columns, null, ordinalDic); BulkOperationsHelper.ConvertListToDataTable(propertyInfoList, dataTable, bookList, columns, ordinalDic); var result = BulkOperationsHelper.BuildInsertQueryFromDataTable(new Dictionary <string, string>(), dataTable, "Id", columns, null, schemaDetail, Constants.TempTableName); var result2 = BulkOperationsHelper.BuildInsertQueryFromDataTable(new Dictionary <string, string>(), dataTable, "Id", columns, new BulkCopySettings() { SqlBulkCopyOptions = SqlBulkCopyOptions.KeepIdentity }, schemaDetail, Constants.TempTableName); Assert.AreEqual("INSERT INTO #TmpTable ([Description], [ISBN], [Title]) VALUES (@Description1, @ISBN1, @Title1), " + "(@Description2, @ISBN2, @Title2), (@Description3, @ISBN3, @Title3)", result.InsertQuery); Assert.AreEqual("INSERT INTO #TmpTable ([Description], [Id], [ISBN], [Title]) VALUES (@Description1, @Id1, @ISBN1, @Title1), " + "(@Description2, @Id2, @ISBN2, @Title2), (@Description3, @Id3, @ISBN3, @Title3)", result2.InsertQuery); }
/// <summary>Updates the given <see cref="SchemaDetail"/>.</summary> /// <param name="schemaDetail">The schema detail.</param> /// <param name="enableForBinaryFiles">Specifies whether to enable the schema for binary files.</param> /// <param name="timeout">Maximum time to wait for the operation to complete.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns>The task.</returns> /// <exception cref="ApiException">A server side error occurred.</exception> public async Task <SchemaUpdateResult> UpdateAsync(SchemaDetail schemaDetail, bool enableForBinaryFiles, TimeSpan?timeout = null, CancellationToken cancellationToken = default(CancellationToken)) { if (enableForBinaryFiles && schemaDetail.Types.Contains(SchemaType.Layer)) { var binarySchemas = new List <string> { nameof(FileMetadata), nameof(AudioMetadata), nameof(DocumentMetadata), nameof(ImageMetadata), nameof(VideoMetadata), }; schemaDetail.ReferencedInContentSchemaIds = binarySchemas; } var updateRequest = new SchemaUpdateRequest { Aggregations = schemaDetail.Aggregations, Descriptions = schemaDetail.Descriptions, DisplayPatterns = schemaDetail.DisplayPatterns, Fields = schemaDetail.Fields, SchemaPermissionSetIds = schemaDetail.SchemaPermissionSetIds, Names = schemaDetail.Names, ViewForAll = schemaDetail.ViewForAll, ReferencedInContentSchemaIds = schemaDetail.ReferencedInContentSchemaIds, Sort = schemaDetail.Sort, LayerSchemaIds = schemaDetail.LayerSchemaIds, FieldsOverwrite = schemaDetail.FieldsOverwrite }; return(await UpdateAsync(schemaDetail.Id, updateRequest, timeout, cancellationToken).ConfigureAwait(false)); }
/// <summary>Creates or updates the given <see cref="SchemaDetail"/>.</summary> /// <param name="schemaDetail">The schema detail.</param> /// <param name="enableForBinaryFiles">Specifies whether to enable the schema for binary files.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns>The task.</returns> public async Task CreateOrUpdateAndWaitForCompletionAsync(SchemaDetail schemaDetail, bool enableForBinaryFiles, CancellationToken cancellationToken = default(CancellationToken)) { if (await ExistsAsync(schemaDetail.Id, null, cancellationToken).ConfigureAwait(false)) { await UpdateAndWaitForCompletionAsync(schemaDetail, enableForBinaryFiles, cancellationToken).ConfigureAwait(false); } else { await CreateAndWaitForCompletionAsync(schemaDetail, enableForBinaryFiles, cancellationToken).ConfigureAwait(false); } }
/// <summary>Creates or updates the given <see cref="SchemaDetail"/>.</summary> /// <param name="schemaDetail">The schema detail.</param> /// <param name="enableForBinaryFiles">Specifies whether to enable the schema for binary files.</param> /// <param name="timeout">Maximum time to wait for the operation to complete.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns>The task.</returns> public async Task <ISchemaResult> CreateOrUpdateAsync(SchemaDetail schemaDetail, bool enableForBinaryFiles, TimeSpan?timeout = null, CancellationToken cancellationToken = default(CancellationToken)) { if (await ExistsAsync(schemaDetail.Id, null, cancellationToken).ConfigureAwait(false)) { return(await UpdateAsync(schemaDetail, enableForBinaryFiles, timeout, cancellationToken).ConfigureAwait(false)); } else { return(await CreateAsync(schemaDetail, enableForBinaryFiles, timeout, cancellationToken).ConfigureAwait(false)); } }
private void ApplyNameTranslationAttributes(SchemaDetail schemaDetail, Type type) { var nameTranslationAttributes = type.GetTypeInfo() .GetCustomAttributes(typeof(PictureparkNameTranslationAttribute), true) .Select(i => i as PictureparkNameTranslationAttribute) .ToList(); foreach (var translationAttribute in nameTranslationAttributes) { schemaDetail.Names[translationAttribute.LanguageAbbreviation] = translationAttribute.Translation; } }
private void ApplyDescriptionTranslationAttributes(SchemaDetail schemaDetail, Type type) { var descriptionTranslationAttributes = type.GetTypeInfo() .GetCustomAttributes(typeof(PictureparkDescriptionTranslationAttribute), true) .Select(i => i as PictureparkDescriptionTranslationAttribute) .ToList(); foreach (var translationAttribute in descriptionTranslationAttributes) { var language = string.IsNullOrEmpty(translationAttribute.LanguageAbbreviation) ? _defaultLanguage : translationAttribute.LanguageAbbreviation; schemaDetail.Descriptions[language] = translationAttribute.Translation; } }
internal void AppendSchemaIdSuffix(SchemaDetail schema, int schemaSuffix) { if (!SystemSchemaIds.Contains(schema.Id)) { schema.Id = schema.Id + schemaSuffix; foreach (var key in schema.Names.Keys.ToList()) { schema.Names[key] = schema.Names[key] + " " + schemaSuffix; } } if (!string.IsNullOrEmpty(schema.ParentSchemaId) && !SystemSchemaIds.Contains(schema.ParentSchemaId)) { schema.ParentSchemaId = schema.ParentSchemaId + schemaSuffix; } foreach (var field in schema.Fields.OfType <FieldSingleTagbox>().Where(f => !SystemSchemaIds.Contains(f.SchemaId))) { field.SchemaId = field.SchemaId + schemaSuffix; } foreach (var field in schema.Fields.OfType <FieldMultiTagbox>().Where(f => !SystemSchemaIds.Contains(f.SchemaId))) { field.SchemaId = field.SchemaId + schemaSuffix; } foreach (var field in schema.Fields.OfType <FieldSingleFieldset>().Where(f => !SystemSchemaIds.Contains(f.SchemaId))) { field.SchemaId = field.SchemaId + schemaSuffix; } foreach (var field in schema.Fields.OfType <FieldMultiFieldset>().Where(f => !SystemSchemaIds.Contains(f.SchemaId))) { field.SchemaId = field.SchemaId + schemaSuffix; } foreach (var field in schema.Fields.OfType <FieldSingleRelation>().Where(f => !SystemSchemaIds.Contains(f.SchemaId))) { field.SchemaId = field.SchemaId + schemaSuffix; } foreach (var field in schema.Fields.OfType <FieldMultiRelation>().Where(f => !SystemSchemaIds.Contains(f.SchemaId))) { field.SchemaId = field.SchemaId + schemaSuffix; } }
/// <summary>Creates the given <see cref="SchemaDetail"/>.</summary> /// <param name="schemaDetail">The schema detail.</param> /// <param name="enableForBinaryFiles">Specifies whether to enable the schema for binary files.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns>The task.</returns> public async Task CreateAndWaitForCompletionAsync(SchemaDetail schemaDetail, bool enableForBinaryFiles, CancellationToken cancellationToken = default(CancellationToken)) { // Map schema to binary schemas if (enableForBinaryFiles && schemaDetail.Types.Contains(SchemaType.Layer)) { var binarySchemas = new List <string> { nameof(FileMetadata), nameof(AudioMetadata), nameof(DocumentMetadata), nameof(ImageMetadata), nameof(VideoMetadata), }; schemaDetail.ReferencedInContentSchemaIds = binarySchemas; } await CreateAndWaitForCompletionAsync(schemaDetail, cancellationToken).ConfigureAwait(false); }
/// <summary>Creates the given <see cref="SchemaDetail"/>.</summary> /// <param name="schemaDetail">The schema detail.</param> /// <param name="enableForBinaryFiles">Specifies whether to enable the schema for binary files.</param> /// <param name="timeout">Maximum time to wait for the operation to complete.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns>The task.</returns> public async Task <SchemaCreateResult> CreateAsync(SchemaDetail schemaDetail, bool enableForBinaryFiles, TimeSpan?timeout = null, CancellationToken cancellationToken = default(CancellationToken)) { // Map schema to binary schemas if (enableForBinaryFiles && schemaDetail.Types.Contains(SchemaType.Layer)) { var binarySchemas = new List <string> { nameof(FileMetadata), nameof(AudioMetadata), nameof(DocumentMetadata), nameof(ImageMetadata), nameof(VideoMetadata), }; schemaDetail.ReferencedInContentSchemaIds = binarySchemas; } return(await CreateAsync(schemaDetail, timeout, cancellationToken).ConfigureAwait(false)); }
private SchemaCreateRequest MapSchemaDetailToCreateRequest(SchemaDetail schemaDetail) { var createRequest = new SchemaCreateRequest { Aggregations = schemaDetail.Aggregations, Descriptions = schemaDetail.Descriptions, DisplayPatterns = schemaDetail.DisplayPatterns, Fields = schemaDetail.Fields, Id = schemaDetail.Id, SchemaPermissionSetIds = schemaDetail.SchemaPermissionSetIds, Names = schemaDetail.Names, ParentSchemaId = schemaDetail.ParentSchemaId, ViewForAll = schemaDetail.ViewForAll, ReferencedInContentSchemaIds = schemaDetail.ReferencedInContentSchemaIds, Sort = schemaDetail.Sort, Types = schemaDetail.Types, LayerSchemaIds = schemaDetail.LayerSchemaIds }; return(createRequest); }
public async Task ShouldUpdate() { /// Arrange string schemaId = await _fixture.GetRandomSchemaIdAsync(20); SchemaDetail schemaDetail = await _client.Schemas.GetAsync(schemaId); string language = "es"; schemaDetail.Names.Remove(language); schemaDetail.Names.Add(language, schemaId); /// Act await _client.Schemas.UpdateAndWaitForCompletionAsync(schemaDetail, false); /// Assert SchemaDetail updatedSchema = await _client.Schemas.GetAsync(schemaId); updatedSchema.Names.TryGetValue(language, out string outString); Assert.Equal(schemaId, outString); }
private async Task <SchemaDetail> CreateTestSchemaAsync() { var schemaId = "Schema" + new Random().Next(0, 999999); var config = await _client.Info.GetInfoAsync().ConfigureAwait(false); var schemaItem = new SchemaDetail { Id = schemaId, ReferencedInContentSchemaIds = new List <string> { "ImageMetadata" }, Fields = new List <FieldBase> { new FieldString { Id = "name", Names = new TranslatedStringDictionary { { config.LanguageConfiguration.DefaultLanguage, "Name" } }, } }, FieldsOverwrite = new List <FieldOverwriteBase>(), Names = new TranslatedStringDictionary { { config.LanguageConfiguration.DefaultLanguage, schemaId } }, Descriptions = new TranslatedStringDictionary(), Types = new List <SchemaType> { SchemaType.Layer }, DisplayPatterns = new List <DisplayPattern>() }; var result = await _client.Schema.CreateAsync(schemaItem, false, TimeSpan.FromMinutes(1)).ConfigureAwait(false); return(result.Schema); }
private void ApplyDisplayPatternAttributes(SchemaDetail schemaDetail, Type type) { var displayPatternAttributes = type.GetTypeInfo() .GetCustomAttributes(typeof(PictureparkDisplayPatternAttribute), true) .Select(i => i as PictureparkDisplayPatternAttribute) .ToList(); foreach (var displayPatternAttribute in displayPatternAttributes) { var displayPattern = new DisplayPattern { DisplayPatternType = displayPatternAttribute.Type, TemplateEngine = displayPatternAttribute.TemplateEngine, Templates = new TranslatedStringDictionary { { "x-default", displayPatternAttribute.DisplayPattern } } }; schemaDetail.DisplayPatterns.Add(displayPattern); //// TODO: Implement fallback for not provided patterns? } }
/// <summary>Creates the given <see cref="SchemaDetail"/>.</summary> /// <param name="schemaDetail">The schema detail.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns>The task.</returns> /// <exception cref="ApiException">A server side error occurred.</exception> public async Task CreateAndWaitForCompletionAsync(SchemaDetail schemaDetail, CancellationToken cancellationToken = default(CancellationToken)) { var createRequest = new SchemaCreateRequest { Aggregations = schemaDetail.Aggregations, Descriptions = schemaDetail.Descriptions, DisplayPatterns = schemaDetail.DisplayPatterns, Fields = schemaDetail.Fields, Id = schemaDetail.Id, SchemaPermissionSetIds = schemaDetail.SchemaPermissionSetIds, Names = schemaDetail.Names, ParentSchemaId = schemaDetail.ParentSchemaId, Public = schemaDetail.Public, ReferencedInContentSchemaIds = schemaDetail.ReferencedInContentSchemaIds, Sort = schemaDetail.Sort, SortOrder = schemaDetail.SortOrder, Types = schemaDetail.Types, LayerSchemaIds = schemaDetail.LayerSchemaIds }; var businessProcess = await CreateAsync(createRequest, cancellationToken).ConfigureAwait(false); await _businessProcessClient.WaitForCompletionAsync(businessProcess.Id, cancellationToken : cancellationToken).ConfigureAwait(false); }
private void AppendSchemaIdSuffix(SchemaDetail schema, int schemaSuffix) => _fixture.AppendSchemaIdSuffix(schema, schemaSuffix);
/// <summary>Creates the given <see cref="SchemaDetail"/>.</summary> /// <param name="schemaDetail">The schema detail.</param> /// <param name="timeout">Maximum time to wait for the operation to complete.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns>The task.</returns> /// <exception cref="ApiException">A server side error occurred.</exception> public async Task <SchemaCreateResult> CreateAsync(SchemaDetail schemaDetail, TimeSpan?timeout = null, CancellationToken cancellationToken = default(CancellationToken)) { var createRequest = MapSchemaDetailToCreateRequest(schemaDetail); return(await CreateAsync(createRequest, timeout, cancellationToken).ConfigureAwait(false)); }
private SchemaDetail CreateSchemas(Type contractType, List <ContractPropertyInfo> properties, string parentSchemaId, List <SchemaDetail> schemaList, int levelOfCall = 0, bool generateDependencySchema = true) { var schemaId = contractType.Name; var typeAttributes = contractType.GetTypeInfo() .GetCustomAttributes(typeof(PictureparkSchemaTypeAttribute), true) .OfType <PictureparkSchemaTypeAttribute>() .ToList(); if (!typeAttributes.Any()) { throw new Exception("No PictureparkSchemaTypeAttribute set on class: " + contractType.Name); } var types = typeAttributes .Select(typeAttribute => typeAttribute.SchemaType) .ToList(); var schemaItem = new SchemaDetail { Id = schemaId, Fields = new List <FieldBase>(), FieldsOverwrite = new List <FieldOverwriteBase>(), ParentSchemaId = parentSchemaId, Names = new TranslatedStringDictionary { { "x-default", schemaId } }, Descriptions = new TranslatedStringDictionary(), Types = types, DisplayPatterns = new List <DisplayPattern>() }; ApplyDisplayPatternAttributes(schemaItem, contractType); ApplyNameTranslationAttributes(schemaItem, contractType); ApplyDescriptionTranslationAttributes(schemaItem, contractType); var customTypes = properties.FindAll(c => c.IsCustomType); if (customTypes.Any()) { foreach (var customType in customTypes) { var referencedSchemaId = customType.TypeName; if (schemaList.Any(d => d.Id == referencedSchemaId)) { continue; } // Exclusion, if the customType is the contractType (it would create itself again with zero fields) if (customType.FullName == contractType.FullName) { continue; } var type = Type.GetType($"{customType.FullName}, {customType.AssemblyFullName}"); var isSystemSchema = type.GetTypeInfo().GetCustomAttributes(typeof(PictureparkSystemSchemaAttribute), true).Any(); if (isSystemSchema == false) { var subLevelOfcall = levelOfCall + 1; var dependencySchema = CreateSchemas(type, customType.TypeProperties, string.Empty, schemaList, subLevelOfcall, generateDependencySchema); // the schema can be alredy added as dependency if (schemaItem.Dependencies.Any(d => d.Id == referencedSchemaId) == false) { schemaItem.Dependencies.Add(dependencySchema); } // the schema can be alredy created if (schemaList.Any(d => d.Id == referencedSchemaId) == false && generateDependencySchema) { schemaList.Add(dependencySchema); } } } } foreach (var property in properties) { if (property.IsOverwritten) { var fieldOverwrite = GetFieldOverwrite(property); schemaItem.FieldsOverwrite.Add(fieldOverwrite); } else { var field = GetField(property); schemaItem.Fields.Add(field); } } if (generateDependencySchema || levelOfCall == 0) { // the schema can be already created if (schemaList.Find(s => s.Id == schemaItem.Id) == null) { schemaList.Add(schemaItem); } } // Create schemas for all subtypes var subtypes = contractType.GetTypeInfo().Assembly.GetTypes().Where(t => t.GetTypeInfo().IsSubclassOf(contractType)); foreach (var subtype in subtypes) { CreateSchemas(subtype, GetProperties(subtype), schemaId, schemaList); } return(schemaItem); }
private ICollection <SchemaDetail> GenerateSchemas(IReadOnlyList <ContractTypeInfo> schemaInfos, IEnumerable <SchemaDetail> existingSchemas) { var result = new List <SchemaDetail>(); foreach (var schemaInfo in schemaInfos) { var type = schemaInfo.Type; var properties = schemaInfo.Properties; // ignore system schemas var isSystemSchema = type.GetTypeInfo().GetCustomAttributes(typeof(PictureparkSystemSchemaAttribute), true).Any(); if (isSystemSchema) { continue; } var schemaId = ResolveSchemaName(type); if (existingSchemas.Any(s => s.Id == schemaId)) { continue; } var typeAttributes = type.GetTypeInfo() .GetCustomAttributes <PictureparkSchemaAttribute>(true) .ToList(); if (!typeAttributes.Any()) { throw new Exception($"No PictureparkSchemaTypeAttribute set on class: {type.Name}"); } if (typeAttributes.Count > 1) { throw new Exception($"Multiple schema types not allowed for class: {type.Name}"); } var schemaType = typeAttributes.Single().Type; var schema = new SchemaDetail() { Id = schemaId, Fields = new List <FieldBase>(), FieldsOverwrite = new List <FieldOverwriteBase>(), ParentSchemaId = schemaInfo.ParentTypeName, Names = new TranslatedStringDictionary { { _defaultLanguage, schemaId } }, Descriptions = new TranslatedStringDictionary(), Types = new List <SchemaType> { schemaType }, DisplayPatterns = new List <DisplayPattern>() }; if (schemaType == SchemaType.Struct || schemaType == SchemaType.Content) { schema.ViewForAll = true; } ApplyDisplayPatternAttributes(schema, type); ApplyNameTranslationAttributes(schema, type); ApplyDescriptionTranslationAttributes(schema, type); foreach (var property in properties) { if (property.IsOverwritten) { var fieldOverwrite = GetFieldOverwrite(property); schema.FieldsOverwrite.Add(fieldOverwrite); } else { var field = GetField(property); schema.Fields.Add(field); } } result.Add(schema); } return(result); }