/// <summary> /// Creates a new generic instance of the collection operation by type. /// </summary> /// <param name="config">Json configuration of current collection</param> /// <returns>Returns a instance of the GenericCollectionOperator which fits the configuration type.</returns> private dynamic CreateOperationInstance(JToken config) { string dbCollectionName = JsonOperations.TryGetValue <string>(config, "collectionName"); _logger.LogDebug("Name of collection: " + dbCollectionName); string typeName = JsonOperations.TryGetValue <string>(config, "typeName"); _logger.LogDebug("Name of type: " + typeName); Type genericeType = GetTypeByName(typeName); if (!genericeType.IsPublic) { _logger.LogError(typeName + " is not public. Only public classes are supported."); throw new ApplicationException(typeName + " is not public. Only public classes are supported."); } _logger.LogDebug("Collection at data context found."); var operationType = typeof(GenericCollectionOperator <>); var genericInstanceHandler = operationType.MakeGenericType(genericeType); dynamic instance = Activator.CreateInstance(genericInstanceHandler, new object[] { GetCollectionByTypeInstance(genericeType, dbCollectionName), config, _updateOperationHandler, _logger }); _logger.LogDebug("Collection operator created for collection " + dbCollectionName); return(instance); }
/// <summary> /// Validates the json configration. /// </summary> /// <param name="configuration">Json configuration content</param> /// <returns>Returns true if the configuration is valid.</returns> private bool ValidateConfiguration(JObject configuration) { if (string.IsNullOrEmpty(JsonOperations.TryGetValue <string>(configuration, "version"))) { _logger.LogError("version element is missing at json configuration."); return(false); } if (JsonOperations.TryGetValue <object>(configuration, "data") == null) { _logger.LogError("data element is missing at json configuration."); return(false); } foreach (var element in configuration["data"]) { if (string.IsNullOrEmpty(JsonOperations.TryGetValue <string>(element, "collectionName"))) { _logger.LogError("collectionName element is missing at json configuration."); return(false); } if (string.IsNullOrEmpty(JsonOperations.TryGetValue <string>(element, "typeName"))) { _logger.LogError("typeName element is missing at json configuration."); return(false); } if (JsonOperations.TryGetValues <string>(element, "keys") == null) { _logger.LogError("keys element is missing at json configuration."); return(false); } if (JsonOperations.TryGetValues <string>(element, "items") == null) { _logger.LogError("items element is missing at json configuration."); return(false); } } return(true); }
/// <summary> /// Starts the database update process. /// </summary> /// <param name="data">Json configuration of item data</param> /// <returns>Returns the handling task.</returns> /// <remarks>Update process will be only started if the version of the json configuration is newer than the version which is stored in database.</remarks> public async Task RunAsync(JObject data) { string versionValue = JsonOperations.TryGetValue <string>(data, "version"); _logger.LogDebug("Config version: " + versionValue); string versionCollectionName = JsonOperations.TryGetValue <string>(data, "customVersionCollectionName"); _logger.LogDebug("Config version collection nane: " + versionCollectionName); // sets default version collection name if not exists if (string.IsNullOrEmpty(versionCollectionName)) { versionCollectionName = "DbInitConfig"; _logger.LogInformation("Version collection name was not set. Default name will be used. -> " + versionCollectionName); } // Checks if configuration version is newer than existing version if (await IsNewerConfigVersionAsync(versionValue, versionCollectionName)) { var collectionConfigurations = data["data"]; foreach (var config in collectionConfigurations) { var instance = CreateOperationInstance(config); await UpdateCollectionAsync(instance); } // Updates version at database await UpdateConfigVersionAsync(versionValue, versionCollectionName); _logger.LogDebug("Version update at database completed."); } else { _logger.LogInformation("No newer version found. -> " + versionValue); } }