private DataMapperViewModel InitializeViewModel(string mapName = null, int?entityId = null, List <string> csvColumnHeaders = null) { var vm = new DataMapperViewModel { MapName = mapName ?? string.Empty, MapToEntity = entityId, JsonMap = string.Empty, CsvColumnHeaders = csvColumnHeaders ?? new List <string>(), Entities = GetEntityList, DataSources = GetDataSourceList, SourceTables = GetSourceTableList, Fields = new List <DataMapper>() }; return(vm); }
private DataMapperViewModel GetEntityFields(DataMapperViewModel vm) { var entitySelected = dataFlowDbContext.Entities.FirstOrDefault(x => x.Id == vm.MapToEntity); if (!string.IsNullOrWhiteSpace(entitySelected?.Url)) { string apiUrl = base.ConfigurationService.GetConfigurationByKey(Constants.API_SERVER_URL).Value; apiUrl = DataFlow.Common.Helpers.UrlUtility.GetUntilOrEmpty(apiUrl.Trim(), "/api/"); //get just the base URL var entityJson = edFiMetadataProcessor.GetJsonFromUrl(apiUrl, entitySelected.Url); var apiFields = edFiMetadataProcessor.GetFieldListFromJson(entityJson, entitySelected.Name) .Where(x => x.Required || GetAdditionalFields(entitySelected.Name).Contains(x.Name)) .ToList(); apiFields.ForEach(x => { if (x.Name == "id") { return; } var dataMapperField = new DataMapper( name: x.Name, dataMapperProperty: new DataMapperProperty( dataType: x.Type, childType: !string.IsNullOrWhiteSpace(x.SubType) ? x.Name : string.Empty )); if (!string.IsNullOrWhiteSpace(x.SubType) && x.SubFields.Any()) { x.SubFields.ForEach(subField => { if (subField.Name == "id") { return; } ; var subDataMapper = new DataMapper(); if (subField.Required || GetAdditionalFields(x.Name).Contains(subField.Name)) { subDataMapper = new DataMapper( name: subField.Name, dataMapperProperty: new DataMapperProperty( dataType: subField.Type, childType: !string.IsNullOrWhiteSpace(subField.SubType) ? subField.Name : string.Empty )); dataMapperField.SubDataMappers.Add(subDataMapper); } if (!string.IsNullOrWhiteSpace(subField.SubType) && subField.SubFields.Any()) { subField.SubFields.ForEach(triField => { if (triField.Name == "id") { return; } if (triField.Required) { var triDataMapper = new DataMapper( name: triField.Name, dataMapperProperty: new DataMapperProperty( dataType: triField.Type, childType: !string.IsNullOrWhiteSpace(triField.SubType) ? triField.Name : string.Empty )); subDataMapper.SubDataMappers.Add(triDataMapper); } }); } }); } var dataMappers = new List <DataMapper> { dataMapperField }; var builder = DataMapperBuilder.BuildPropertyUniqueKey(dataMappers); vm.Fields.AddRange(builder); }); } return(vm); }
public ActionResult Index(DataMapperViewModel vm) { if (vm.MapToEntity == null) { ModelState.AddModelError("MapToEntity", "Please select an entity to map to."); return(View(vm)); } try { var isUpdate = vm.DataMapId > 0; DataMap map; if (isUpdate) { map = dataFlowDbContext.DataMaps.FirstOrDefault(x => x.Id == vm.DataMapId); if (map != null) { map.Id = vm.DataMapId; } } else { map = new DataMap(); } map.Name = vm.MapName; map.EntityId = vm.MapToEntity.Value; map.Manual = vm.Manual; map.Map = vm.JsonMap; map.CreateDate = isUpdate ? map.CreateDate : DateTime.Now; map.UpdateDate = DateTime.Now; dataFlowDbContext.DataMaps.AddOrUpdate(map); dataFlowDbContext.SaveChanges(); ModelState.Clear(); vm = new DataMapperViewModel { MapName = string.Empty, MapToEntity = null, JsonMap = string.Empty, Entities = GetEntityList, DataSources = GetDataSourceList, SourceTables = GetSourceTableList, Fields = new List <DataMapper>(), CsvColumnHeaders = new List <string>(), IsSuccess = true, ShowInfoMessage = true, InfoMessage = "Data Map was created successfully!" }; return(View(vm)); } catch (Exception ex) { LogService.Error("Error Saving Advanced Data Map", ex); vm.Entities = GetEntityList; vm.DataSources = GetDataSourceList; vm.SourceTables = GetSourceTableList; vm.IsSuccess = false; vm.ShowInfoMessage = true; vm.InfoMessage = "There was an error saving the data map."; return(View(vm)); } }