/// <summary> /// Method: AddEditExtensionsConfig /// Description: It is used to add/update connector in connectors table /// </summary> /// <param name="connectorConfig"></param> public void AddEditExtensionsConfig(ConnectorConfig connectorConfig) { using (var transaction = _context.Database.BeginTransaction()) { Connectors entity; DeDupSettings dedupSettings = null; bool isNew = false; int isSyncTableExist = 0; int isSyncGoldenTableExist = 0; if (connectorConfig.connectorId.HasValue) { entity = Find(connectorConfig.ccid, connectorConfig.connectorId); if (entity != null) { //delete sync process if it is in progress while updating connector if (!string.IsNullOrEmpty(entity.job_id) || entity.schedule_type != ScheduleType.MANUAL_SYNC) { //delete old jobs JobScheduler.Instance.DeleteJob(ccid: entity.ccid, connectorId: entity.connector_id, jobId: entity.job_id, scheduleType: entity.schedule_type); //reset job id once deleted entity.job_id = string.Empty; if (entity.sync_status == 1) { //set status interrupted entity.sync_status = 10; } else if (entity.sync_status.HasValue) { //reset sync status entity.sync_status = 0; } } else if (entity.sync_status.HasValue) { //reset sync status entity.sync_status = 0; } if (entity.sync_status != 2 && entity.sync_status != 10) { entity.sync_count = null; entity.unique_records_count = null; entity.sync_updated_count = null; entity.total_records_count = null; entity.sync_log_json = null; entity.sync_started_at = null; entity.sync_ended_at = null; entity.last_sync_at = null; entity.last_sync_status = null; } dedupSettings = entity.DeDupSetting; //check table exist or not. if not then create table and sync isSyncTableExist = SyncRepository.SyncTableIsExist(connectorConfig); isSyncGoldenTableExist = SyncRepository.SyncGoldenTableIsExist(connectorConfig); } } else { isNew = true; entity = new Connectors() { ccid = connectorConfig.ccid }; //Set next connector id entity.connector_id = GetMaxID(entity.ccid, ref dedupSettings) + 1; } entity.connector_name = connectorConfig.connectorName; if (isNew) { entity.sync_src = connectorConfig.dataSource; entity.src_object_name = connectorConfig.sourceObjectName; //commented by Kathir on 12-Aug-2020 // entity.src_object_fields_json = JsonConvert.SerializeObject(connectorConfig.sourceObjectFields); if (entity.sync_src == DataSource.Heroku_Postgres || entity.sync_src == DataSource.Azure_Postgres || entity.sync_src == DataSource.AWS_Postgres || entity.sync_src == DataSource.Azure_SQL) { entity.src_config_json = JsonConvert.SerializeObject(connectorConfig.dbConfig); } if (connectorConfig.dedup_type != DedupType.Full_Dedup) { entity.connector_type = (ConnectorType)connectorConfig.syncDestination; entity.dest_object_name = connectorConfig.destObjectName; } else { if (connectorConfig.dataSource == DataSource.Heroku_Postgres) { entity.connector_type = ConnectorType.Heroku_Postgres; } if (connectorConfig.dataSource == DataSource.AWS_Postgres) { entity.connector_type = ConnectorType.AWS_Postgres; } if (connectorConfig.dataSource == DataSource.Azure_Postgres) { entity.connector_type = ConnectorType.Azure_Postgres; } entity.dest_object_name = string.Empty; } entity.src_schema = connectorConfig.dbSchema; entity.dedup_type = connectorConfig.dedup_type; entity.dedup_source_type = connectorConfig.dedupSourceType; if ((connectorConfig.dedupSourceType == SourceType.Copy_Source_data_to_Destination_and_Remove_Duplicates_from_Destination || connectorConfig.dedupSourceType == SourceType.Merge_Table_A_Data_to_Table_B_and_Remove_Duplicates_from_Table_B)) { entity.compare_object_fields = JsonConvert.SerializeObject(connectorConfig.compareObjectFieldsMapping); entity.compare_config_json = JsonConvert.SerializeObject(connectorConfig.dbConfig_compare); } //if (IsMultipleConfigSupported) { if (entity.connector_type == ConnectorType.Heroku_Postgres || entity.connector_type == ConnectorType.Azure_Postgres || entity.connector_type == ConnectorType.AWS_Postgres || entity.connector_type == ConnectorType.Azure_SQL) { entity.dest_config_json = JsonConvert.SerializeObject(connectorConfig.destDBConfig); } } //set global db setting if (dedupSettings != null) { //save sync database url if (connectorConfig.dbConfig != null && !string.IsNullOrEmpty(connectorConfig.dbConfig.syncDefaultDatabaseUrl)) { if (string.IsNullOrEmpty(dedupSettings.database_config_json)) { dedupSettings.database_config_json = JsonConvert.SerializeObject((new List <DatabaseConfig>() { connectorConfig.dbConfig })); _context.Entry(dedupSettings).State = EntityState.Modified; } else { var dbConfigs = dedupSettings.ToModel <List <DatabaseConfig> >(); if (dbConfigs != null && dbConfigs.FirstOrDefault(p => p.databaseType == connectorConfig.dbConfig.databaseType) == null) { dbConfigs.Add(connectorConfig.dbConfig); dedupSettings.database_config_json = JsonConvert.SerializeObject(dbConfigs); _context.Entry(dedupSettings).State = EntityState.Modified; } } } } else { dedupSettings = new DeDupSettings() { ccid = connectorConfig.ccid }; //save sync database url if (connectorConfig.dbConfig != null && !string.IsNullOrEmpty(connectorConfig.dbConfig.syncDefaultDatabaseUrl)) { dedupSettings.database_config_json = JsonConvert.SerializeObject((new List <DatabaseConfig>() { connectorConfig.dbConfig })); } //add dedupsetting _context.Entry(dedupSettings).State = EntityState.Added; } entity.unique_records_count = null; entity.sync_updated_count = null; entity.sync_count = null; entity.total_records_count = null; } entity.schedule_type = connectorConfig.scheduleType; entity.src_new_record_filter = connectorConfig.srcNewRecordFilter; entity.src_update_record_filter = connectorConfig.srcUpdateRecordFilter; entity.two_way_sync_priority = connectorConfig.twoWaySyncPriority; entity.dest_schema = connectorConfig.destDBSchema; entity.custom_schedule_in_minutes = connectorConfig.customScheduleInMinutes; //Added by Kathir on 20-8-2020 entity.dedup_method = connectorConfig.dedup_method; //if (connectorConfig.dedup_method == SimilarityType.Fuzzy_Compare) //{ // entity.fuzzy_ratio = connectorConfig.fuzzy_ratio; //} entity.fuzzy_ratio = connectorConfig.fuzzy_ratio; entity.review_before_delete = connectorConfig.review_before_delete; entity.backup_before_delete = connectorConfig.backup_before_delete; if (connectorConfig.dedup_type == DedupType.Full_Dedup) { entity.simulation_count = -1; } else { entity.simulation_count = connectorConfig.simulation_count; } if (string.IsNullOrEmpty(entity.src_object_name)) { entity.src_object_name = connectorConfig.sourceObjectName; } if ((connectorConfig.dedupSourceType == SourceType.Copy_Source_data_to_Destination_and_Remove_Duplicates_from_Destination || connectorConfig.dedupSourceType == SourceType.Merge_Table_A_Data_to_Table_B_and_Remove_Duplicates_from_Table_B)) { //entity.compare_object_fields = JsonConvert.SerializeObject(connectorConfig.compareObjectFieldsMapping); entity.compare_config_json = JsonConvert.SerializeObject(connectorConfig.dbConfig_compare); } //commented by Kathir on 12-Aug-2020 //if (string.IsNullOrEmpty(entity.src_object_fields_json)) //{ // entity.src_object_fields_json = JsonConvert.SerializeObject(connectorConfig.sourceObjectFields); //} if (string.IsNullOrEmpty(entity.dest_object_name)) { if (connectorConfig.dedup_type == DedupType.Full_Dedup && connectorConfig.dedupSourceType == SourceType.Remove_Duplicates_from_a_Single_Table) { entity.dest_object_name = connectorConfig.sourceObjectName; } else if (connectorConfig.dedup_type == DedupType.Full_Dedup && (connectorConfig.dedupSourceType == SourceType.Copy_Source_data_to_Destination_and_Remove_Duplicates_from_Destination || connectorConfig.dedupSourceType == SourceType.Merge_Table_A_Data_to_Table_B_and_Remove_Duplicates_from_Table_B)) { entity.dest_object_name = (connectorConfig.dbConfig_compare.table_type == SelectedTableType.Create_New_Table ? connectorConfig.dbConfig_compare.new_table_name : connectorConfig.dbConfig_compare.object_name); } else { entity.dest_object_name = connectorConfig.destObjectName; } } //Assigned source object fields to compare object field json entity.compare_object_fields = JsonConvert.SerializeObject(connectorConfig.sourceObjectFields); //if (connectorConfig.dedupSourceType == SourceType.Remove_Duplicates_from_a_Single_Table) //{ // if (connectorConfig.sourceObjectFields != null && connectorConfig.sourceObjectFields.Count > 0) // { // foreach (string sel_fields in connectorConfig.sourceObjectFields) // { // connectorConfig.compareObjectFieldsMapping.Add(new KeyValuePair<string, string>(sel_fields, sel_fields)); // } // } // entity.compare_object_fields = JsonConvert.SerializeObject(connectorConfig.sourceObjectFields); //} if (connectorConfig.dedup_type == DedupType.Full_Dedup) { entity.dest_object_name = (connectorConfig.dedupSourceType == SourceType.Remove_Duplicates_from_a_Single_Table ? connectorConfig.sourceObjectName : (connectorConfig.dbConfig_compare.table_type == SelectedTableType.Create_New_Table ? connectorConfig.dbConfig_compare.new_table_name : connectorConfig.dbConfig_compare.object_name)); connectorConfig.destDBConfig.syncDefaultDatabaseUrl = (connectorConfig.dedupSourceType == SourceType.Remove_Duplicates_from_a_Single_Table ? connectorConfig.dbConfig.syncDefaultDatabaseUrl : connectorConfig.dbConfig_compare.syncDefaultDatabaseUrl); connectorConfig.destDBConfig.databaseType = (connectorConfig.dedupSourceType == SourceType.Remove_Duplicates_from_a_Single_Table ? connectorConfig.dbConfig.databaseType : connectorConfig.dbConfig_compare.databaseType); connectorConfig.destDBConfig.dataSource = (connectorConfig.dedupSourceType == SourceType.Remove_Duplicates_from_a_Single_Table ? connectorConfig.dbConfig.dataSource : connectorConfig.dbConfig_compare.dataSource); entity.dest_config_json = JsonConvert.SerializeObject(connectorConfig.destDBConfig); entity.dest_schema = (connectorConfig.dedupSourceType == SourceType.Remove_Duplicates_from_a_Single_Table ? connectorConfig.dbSchema : connectorConfig.dbConfig_compare.db_schema); } //save extension setting _context.Entry(entity).State = isNew ? EntityState.Added : EntityState.Modified; _context.SaveChanges(); //if (isSyncTableExist == 0 && connectorConfig.dedup_type != DedupType.Full_Dedup) //{ // var errorMessage = string.Empty; // if (connectorConfig.syncDestination == ConnectorType.Heroku_Postgres // || connectorConfig.syncDestination == ConnectorType.Azure_Postgres // || connectorConfig.syncDestination == ConnectorType.AWS_Postgres) // { // var dbStatus = SyncRepository.CreatePostgresTable(connectorConfig, out errorMessage); // if (dbStatus == -1) // { // transaction.Rollback(); // throw new Exception(message: (string.IsNullOrEmpty(errorMessage) ? "Not able to create postgres table." : errorMessage)) { }; // } // else if (dbStatus == 2) // { // transaction.Rollback(); // throw new Exception(message: "The " + connectorConfig.destObjectName + " postgres table already exists.") { }; // } // } //} //if (isSyncGoldenTableExist == 0 && connectorConfig.dbConfig_compare.table_type == SelectedTableType.Create_New_Table) //{ // var errorMessage = string.Empty; // if (connectorConfig.dbConfig_compare.dataSource == DataSource.Heroku_Postgres // || connectorConfig.dbConfig_compare.dataSource == DataSource.Azure_Postgres // || connectorConfig.dbConfig_compare.dataSource == DataSource.AWS_Postgres) // { // var dbStatus = SyncRepository.CreatePostgresGoldenTable(connectorConfig, out errorMessage); // if (dbStatus == -1) // { // transaction.Rollback(); // throw new Exception(message: (string.IsNullOrEmpty(errorMessage) ? "Not able to create postgres table." : errorMessage)) { }; // } // else if (dbStatus == 2) // { // transaction.Rollback(); // throw new Exception(message: "The " + connectorConfig.dbConfig_compare.new_table_name + " postgres table already exists.") { }; // } // } //} transaction.Commit(); //Schedule job if scheduletype is not manual if (entity.schedule_type != ScheduleType.MANUAL_SYNC) { if (entity.schedule_type == ScheduleType.CUSTOM) { JobScheduler.Instance.ScheduleJob(entity.ccid, entity.connector_id, entity.connector_type, entity.schedule_type, (entity.custom_schedule_in_minutes.HasValue ? entity.custom_schedule_in_minutes.Value : 1200)); } else { JobScheduler.Instance.ScheduleJob(entity.ccid, entity.connector_id, entity.connector_type, entity.schedule_type); } } //}); } }