public async Task ImportForeignFscs(WRLDCWarehouseDbContext _context, ILogger _log, string oracleConnStr, EntityWriteOption opt) { FscExtract fscExtract = new FscExtract(); List <FscForeign> fscForeignList = fscExtract.ExtractFscForeign(oracleConnStr); LoadFsc loadFsc = new LoadFsc(); foreach (FscForeign fscForeign in fscForeignList) { Fsc insertedFsc = await loadFsc.LoadSingleAsync(_context, _log, fscForeign, opt); } }
public async Task <FscOwner> LoadSingleAsync(WRLDCWarehouseDbContext _context, ILogger _log, FscOwnerForeign fscOwnerForeign, EntityWriteOption opt) { // check if entity already exists FscOwner existingFscOwner = await _context.FscOwners.SingleOrDefaultAsync(fO => fO.WebUatId == fscOwnerForeign.WebUatId); // check if we should not modify existing entities if (opt == EntityWriteOption.DontReplace && existingFscOwner != null) { return(existingFscOwner); } // find the Fsc via the Fsc WebUatId int fscWebUatId = fscOwnerForeign.FscWebUatId; Fsc fsc = await _context.Fscs.SingleOrDefaultAsync(c => c.WebUatId == fscWebUatId); // if Fsc doesnot exist, skip the import. Ideally, there should not be such case if (fsc == null) { _log.LogCritical($"Unable to find Fsc with webUatId {fscWebUatId} while inserting FscOwner with webUatId {fscOwnerForeign.WebUatId}"); return(null); } // find the Owner of the Fsc via the Owner WebUatId int ownerWebUatId = fscOwnerForeign.OwnerWebUatId; Owner owner = await _context.Owners.SingleOrDefaultAsync(o => o.WebUatId == ownerWebUatId); // if owner doesnot exist, skip the import. Ideally, there should not be such case if (owner == null) { _log.LogCritical($"Unable to find Owner with webUatId {ownerWebUatId} while inserting FscOwner with webUatId {fscOwnerForeign.WebUatId}"); return(null); } try { // check if we have to replace the entity completely if (opt == EntityWriteOption.Replace && existingFscOwner != null) { _context.FscOwners.Remove(existingFscOwner); } // if entity is not present, then insert or check if we have to replace the entity completely if (existingFscOwner == null || (opt == EntityWriteOption.Replace && existingFscOwner != null)) { FscOwner newFscOwner = new FscOwner(); newFscOwner.OwnerId = owner.OwnerId; newFscOwner.FscId = fsc.FscId; newFscOwner.WebUatId = fscOwnerForeign.WebUatId; _context.FscOwners.Add(newFscOwner); await _context.SaveChangesAsync(); return(newFscOwner); } // check if we have to modify the entity if (opt == EntityWriteOption.Modify && existingFscOwner != null) { existingFscOwner.OwnerId = owner.OwnerId; existingFscOwner.FscId = fsc.FscId; await _context.SaveChangesAsync(); return(existingFscOwner); } } catch (DbUpdateException e) { _log.LogCritical($"Error occured while inserting FscOwner with webUatId {fscOwnerForeign.WebUatId}, owner id {owner.OwnerId} and fscId {fsc.FscId}"); _log.LogCritical($"EntityWriteOption = {opt.ToString()}"); _log.LogCritical($"{e.Message}"); return(null); } return(null); }
public async Task <Fsc> LoadSingleAsync(WRLDCWarehouseDbContext _context, ILogger _log, FscForeign fscForeign, EntityWriteOption opt) { // check if entity already exists Fsc existingFsc = await _context.Fscs.SingleOrDefaultAsync(lr => lr.WebUatId == fscForeign.WebUatId); // check if we should not modify existing entities if (opt == EntityWriteOption.DontReplace && existingFsc != null) { return(existingFsc); } // find the Substation via the SubstationWebUatId int substationWebUatId = fscForeign.SubstationWebUatId; Substation substation = await _context.Substations.SingleOrDefaultAsync(ss => ss.WebUatId == substationWebUatId); // if Substation doesnot exist, skip the import. Ideally, there should not be such case if (substation == null) { _log.LogCritical($"Unable to find Substation with webUatId {substationWebUatId} while inserting Fsc with webUatId {fscForeign.WebUatId} and name {fscForeign.Name}"); return(null); } // find the State via the State WebUatId int stateWebUatId = fscForeign.StateWebUatId; State state = await _context.States.SingleOrDefaultAsync(s => s.WebUatId == stateWebUatId); // if state doesnot exist, skip the import. Ideally, there should not be such case if (state == null) { _log.LogCritical($"Unable to find state with webUatId {stateWebUatId} while inserting Fsc with webUatId {fscForeign.WebUatId} and name {fscForeign.Name}"); return(null); } int cktWebUatId = fscForeign.AcTransLineCktWebUatId; AcTransLineCkt ckt = await _context.AcTransLineCkts.SingleOrDefaultAsync(v => v.WebUatId == cktWebUatId); // if ckt doesnot exist, skip the import. Ideally, there should not be such case if (ckt == null) { _log.LogCritical($"Unable to find AcTransLineCkt with webUatId {cktWebUatId} while inserting Fsc with webUatId {fscForeign.WebUatId} and name {fscForeign.Name}"); return(null); } // check if we have to replace the entity completely if (opt == EntityWriteOption.Replace && existingFsc != null) { _context.Fscs.Remove(existingFsc); } // if entity is not present, then insert or check if we have to replace the entity completely if (existingFsc == null || (opt == EntityWriteOption.Replace && existingFsc != null)) { Fsc newFsc = new Fsc(); newFsc.Name = fscForeign.Name; newFsc.AcTransLineCktId = ckt.AcTransLineCktId; newFsc.SubstationId = substation.SubstationId; newFsc.StateId = state.StateId; newFsc.PercComp = fscForeign.PercComp; newFsc.LineReactance = fscForeign.LineReactance; newFsc.CapacitiveReactance = fscForeign.CapacitiveReactance; newFsc.RatedMvarPhase = fscForeign.RatedMvarPhase; newFsc.RatedCurrentAmps = fscForeign.RatedCurrentAmps; newFsc.CommDate = fscForeign.CommDate; newFsc.CodDate = fscForeign.CodDate; newFsc.DeCommDate = fscForeign.DeCommDate; newFsc.WebUatId = fscForeign.WebUatId; _context.Fscs.Add(newFsc); await _context.SaveChangesAsync(); return(newFsc); } // check if we have to modify the entity if (opt == EntityWriteOption.Modify && existingFsc != null) { existingFsc.Name = fscForeign.Name; existingFsc.AcTransLineCktId = ckt.AcTransLineCktId; existingFsc.SubstationId = substation.SubstationId; existingFsc.StateId = state.StateId; existingFsc.PercComp = fscForeign.PercComp; existingFsc.LineReactance = fscForeign.LineReactance; existingFsc.CapacitiveReactance = fscForeign.CapacitiveReactance; existingFsc.RatedMvarPhase = fscForeign.RatedMvarPhase; existingFsc.RatedCurrentAmps = fscForeign.RatedCurrentAmps; existingFsc.CommDate = fscForeign.CommDate; existingFsc.CodDate = fscForeign.CodDate; existingFsc.DeCommDate = fscForeign.DeCommDate; await _context.SaveChangesAsync(); return(existingFsc); } return(null); }