public async Task <HvdcLineCktOwner> LoadSingleAsync(WRLDCWarehouseDbContext _context, ILogger _log, HvdcLineCktOwnerForeign cktOwnerForeign, EntityWriteOption opt) { // check if entity already exists HvdcLineCktOwner existingCktOwner = await _context.HvdcLineCktOwners.SingleOrDefaultAsync(cO => cO.WebUatId == cktOwnerForeign.WebUatId); // check if we should not modify existing entities if (opt == EntityWriteOption.DontReplace && existingCktOwner != null) { return(existingCktOwner); } // find the HvdcLineCkt via the HvdcLineCkt WebUatId int cktWebUatId = cktOwnerForeign.HvdcLineCktWebUatId; HvdcLineCkt ckt = await _context.HvdcLineCkts.SingleOrDefaultAsync(c => c.WebUatId == cktWebUatId); // if HvdcLineCkt doesnot exist, skip the import. Ideally, there should not be such case if (ckt == null) { _log.LogCritical($"Unable to find HvdcLineCkt with webUatId {cktWebUatId} while inserting HvdcLineCktOwner with webUatId {cktOwnerForeign.WebUatId}"); return(null); } // find the Owner of the HvdcLineCkt via the Owner WebUatId int ownerWebUatId = cktOwnerForeign.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 HvdcLineCktOwner with webUatId {cktOwnerForeign.WebUatId}"); return(null); } try { // check if we have to replace the entity completely if (opt == EntityWriteOption.Replace && existingCktOwner != null) { _context.HvdcLineCktOwners.Remove(existingCktOwner); } // if entity is not present, then insert or check if we have to replace the entity completely if (existingCktOwner == null || (opt == EntityWriteOption.Replace && existingCktOwner != null)) { HvdcLineCktOwner newCktOwner = new HvdcLineCktOwner(); newCktOwner.OwnerId = owner.OwnerId; newCktOwner.HvdcLineCktId = ckt.HvdcLineCktId; newCktOwner.WebUatId = cktOwnerForeign.WebUatId; _context.HvdcLineCktOwners.Add(newCktOwner); await _context.SaveChangesAsync(); return(newCktOwner); } // check if we have to modify the entity if (opt == EntityWriteOption.Modify && existingCktOwner != null) { existingCktOwner.OwnerId = owner.OwnerId; existingCktOwner.HvdcLineCktId = ckt.HvdcLineCktId; await _context.SaveChangesAsync(); return(existingCktOwner); } } catch (DbUpdateException e) { _log.LogCritical($"Error occured while inserting HvdcLineCktOwner with webUatId {cktOwnerForeign.WebUatId}, owner id {owner.OwnerId} and cktId {ckt.HvdcLineCktId}"); _log.LogCritical($"EntityWriteOption = {opt.ToString()}"); _log.LogCritical($"{e.Message}"); return(null); } return(null); }
public async Task <Substation> LoadSingleAsync(WRLDCWarehouseDbContext _context, ILogger _log, SubstationForeign ssForeign, EntityWriteOption opt) { // check if entity already exists Substation existingSS = await _context.Substations.SingleOrDefaultAsync(ss => ss.WebUatId == ssForeign.WebUatId); // check if we should not modify existing entities if (opt == EntityWriteOption.DontReplace && existingSS != null) { return(existingSS); } // find the MajorSubstation of the substation via the MajorSubstation WebUatId int majorSSWebUatId = ssForeign.MajorSubstationWebUatId; MajorSubstation majorSS = await _context.MajorSubstations.SingleOrDefaultAsync(mss => mss.WebUatId == majorSSWebUatId); // if major Substation doesnot exist, skip the import. Ideally, there should not be such case if (majorSS == null) { _log.LogCritical($"Unable to find MajorSubstation with webUatId {majorSSWebUatId} while inserting Substation with webUatId {ssForeign.WebUatId} and name {ssForeign.Name}"); // uncomment this if vendor obeys non nullable major ss foreign id // return null; } // find the Voltage of the substation via the Voltage WebUatId int voltWebUatId = (int)ssForeign.VoltLevelWebUatId; VoltLevel voltLevel = await _context.VoltLevels.SingleOrDefaultAsync(vl => vl.WebUatId == voltWebUatId); // if voltage level doesnot exist, skip the import. Ideally, there should not be such case if (voltLevel == null) { _log.LogCritical($"Unable to find VoltLevel with webUatId {voltWebUatId} while inserting Substation with webUatId {ssForeign.WebUatId} and name {ssForeign.Name}"); return(null); } // find the State of the substation via the State WebUatId int stateWebUatId = -1; if (int.TryParse(ssForeign.StateWebUatId, out int j)) { stateWebUatId = j; } else { Console.WriteLine($"Could not parse state WebUatId {ssForeign.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 Substation with webUatId {ssForeign.WebUatId} and name {ssForeign.Name}"); return(null); } // check if we have to replace the entity completely if (opt == EntityWriteOption.Replace && existingSS != null) { _context.Substations.Remove(existingSS); } // if entity is not present, then insert or check if we have to replace the entity completely if (existingSS == null || (opt == EntityWriteOption.Replace && existingSS != null)) { Substation newSS = new Substation(); newSS.Name = ssForeign.Name; newSS.VoltLevelId = voltLevel.VoltLevelId; if (majorSS != null) { newSS.MajorSubstationId = majorSS.MajorSubstationId; } newSS.StateId = state.StateId; newSS.Classification = ssForeign.Classification; newSS.BusbarScheme = ssForeign.BusbarScheme; newSS.CodDate = ssForeign.CodDate; newSS.CommDate = ssForeign.CommDate; newSS.DecommDate = ssForeign.DecommDate; newSS.WebUatId = ssForeign.WebUatId; _context.Substations.Add(newSS); await _context.SaveChangesAsync(); return(newSS); } // check if we have to modify the entity if (opt == EntityWriteOption.Modify && existingSS != null) { existingSS.Name = ssForeign.Name; existingSS.VoltLevelId = voltLevel.VoltLevelId; if (majorSS != null) { existingSS.MajorSubstationId = majorSS.MajorSubstationId; } existingSS.StateId = state.StateId; existingSS.Classification = ssForeign.Classification; if (ssForeign.BusbarScheme != null) { existingSS.BusbarScheme = ssForeign.BusbarScheme; } existingSS.CodDate = ssForeign.CodDate; existingSS.CommDate = ssForeign.CommDate; existingSS.DecommDate = ssForeign.DecommDate; await _context.SaveChangesAsync(); return(existingSS); } return(null); }
public async Task <HvdcLine> LoadSingleAsync(WRLDCWarehouseDbContext _context, ILogger _log, HvdcLineForeign hvdcLineForeign, EntityWriteOption opt) { // check if entity already exists HvdcLine existingHvdcLine = await _context.HvdcLines.SingleOrDefaultAsync(lr => lr.WebUatId == hvdcLineForeign.WebUatId); // check if we should not modify existing entities if (opt == EntityWriteOption.DontReplace && existingHvdcLine != null) { return(existingHvdcLine); } // find the FromSubstation via the FromSSWebUatId int fromSSWebUatId = hvdcLineForeign.FromSSWebUatId; Substation fromSS = await _context.Substations.SingleOrDefaultAsync(ss => ss.WebUatId == fromSSWebUatId); // if FromSubstation doesnot exist, skip the import. Ideally, there should not be such case if (fromSS == null) { _log.LogCritical($"Unable to find FromSubstation with webUatId {fromSSWebUatId} while inserting HvdcLine with webUatId {hvdcLineForeign.WebUatId} and name {hvdcLineForeign.Name}"); return(null); } // find the ToSubstation via the ToSSWebUatId int toSSWebUatId = hvdcLineForeign.ToSSWebUatId; Substation toSS = await _context.Substations.SingleOrDefaultAsync(ss => ss.WebUatId == toSSWebUatId); // if ToSubstation doesnot exist, skip the import. Ideally, there should not be such case if (toSS == null) { _log.LogCritical($"Unable to find ToSubstation with webUatId {toSSWebUatId} while inserting HvdcLine with webUatId {hvdcLineForeign.WebUatId} and name {hvdcLineForeign.Name}"); return(null); } // find the FromState via the State WebUatId int fromStateWebUatId = hvdcLineForeign.FromStateWebUatId; State fromState = await _context.States.SingleOrDefaultAsync(s => s.WebUatId == fromStateWebUatId); // if state doesnot exist, skip the import. Ideally, there should not be such case if (fromState == null) { _log.LogCritical($"Unable to find FromState with webUatId {fromStateWebUatId} while inserting HvdcLine with webUatId {hvdcLineForeign.WebUatId} and name {hvdcLineForeign.Name}"); return(null); } // find the ToState via the State WebUatId int toStateWebUatId = hvdcLineForeign.ToStateWebUatId; State toState = await _context.States.SingleOrDefaultAsync(s => s.WebUatId == toStateWebUatId); // if toState doesnot exist, skip the import. Ideally, there should not be such case if (toState == null) { _log.LogCritical($"Unable to find ToState with webUatId {toStateWebUatId} while inserting HvdcLine with webUatId {hvdcLineForeign.WebUatId} and name {hvdcLineForeign.Name}"); return(null); } int voltLevelWebUatId = hvdcLineForeign.VoltLevelWebUatId; VoltLevel voltLevel = await _context.VoltLevels.SingleOrDefaultAsync(v => v.WebUatId == voltLevelWebUatId); // if voltLevel doesnot exist, skip the import. Ideally, there should not be such case if (voltLevel == null) { _log.LogCritical($"Unable to find VoltLevel with webUatId {voltLevelWebUatId} while inserting HvdcLine with webUatId {hvdcLineForeign.WebUatId} and name {hvdcLineForeign.Name}"); return(null); } // check if we have to replace the entity completely if (opt == EntityWriteOption.Replace && existingHvdcLine != null) { _context.HvdcLines.Remove(existingHvdcLine); } // if entity is not present, then insert or check if we have to replace the entity completely if (existingHvdcLine == null || (opt == EntityWriteOption.Replace && existingHvdcLine != null)) { HvdcLine newHvdcLine = new HvdcLine(); newHvdcLine.Name = hvdcLineForeign.Name; newHvdcLine.FromStateId = fromState.StateId; newHvdcLine.ToStateId = toState.StateId; newHvdcLine.FromSubstationId = fromSS.SubstationId; newHvdcLine.ToSubstationId = toSS.SubstationId; newHvdcLine.VoltLevelId = voltLevel.VoltLevelId; newHvdcLine.WebUatId = hvdcLineForeign.WebUatId; _context.HvdcLines.Add(newHvdcLine); await _context.SaveChangesAsync(); return(newHvdcLine); } // check if we have to modify the entity if (opt == EntityWriteOption.Modify && existingHvdcLine != null) { existingHvdcLine.Name = hvdcLineForeign.Name; existingHvdcLine.FromStateId = fromState.StateId; existingHvdcLine.ToStateId = toState.StateId; existingHvdcLine.FromSubstationId = fromSS.SubstationId; existingHvdcLine.ToSubstationId = toSS.SubstationId; existingHvdcLine.VoltLevelId = voltLevel.VoltLevelId; await _context.SaveChangesAsync(); return(existingHvdcLine); } return(null); }
public async Task <TransformerOwner> LoadSingleAsync(WRLDCWarehouseDbContext _context, ILogger _log, TransformerOwnerForeign trOwnerForeign, EntityWriteOption opt) { // check if entity already exists TransformerOwner existingTrOwner = await _context.TransformerOwners.SingleOrDefaultAsync(trO => trO.WebUatId == trOwnerForeign.WebUatId); // check if we should not modify existing entities if (opt == EntityWriteOption.DontReplace && existingTrOwner != null) { return(existingTrOwner); } // find the Transformer via the TransformerWebUatId int trWebUatId = trOwnerForeign.TransformerWebUatId; Transformer transformer = await _context.Transformers.SingleOrDefaultAsync(tr => tr.WebUatId == trWebUatId); // if Transformer doesnot exist, skip the import. Ideally, there should not be such case if (transformer == null) { _log.LogCritical($"Unable to find Transformer with webUatId {trWebUatId} while inserting TransformerOwner with webUatId {trOwnerForeign.WebUatId}"); return(null); } // find the Owner of the substation via the Owner WebUatId int ownerWebUatId = trOwnerForeign.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 TransformerOwner with webUatId {trOwnerForeign.WebUatId}"); return(null); } // check if we have to replace the entity completely if (opt == EntityWriteOption.Replace && existingTrOwner != null) { _context.TransformerOwners.Remove(existingTrOwner); } // if entity is not present, then insert or check if we have to replace the entity completely if (existingTrOwner == null || (opt == EntityWriteOption.Replace && existingTrOwner != null)) { TransformerOwner newTrOwner = new TransformerOwner(); newTrOwner.OwnerId = owner.OwnerId; newTrOwner.TransformerId = transformer.TransformerId; newTrOwner.WebUatId = trOwnerForeign.WebUatId; _context.TransformerOwners.Add(newTrOwner); await _context.SaveChangesAsync(); return(newTrOwner); } // check if we have to modify the entity if (opt == EntityWriteOption.Modify && existingTrOwner != null) { existingTrOwner.OwnerId = owner.OwnerId; existingTrOwner.TransformerId = transformer.TransformerId; await _context.SaveChangesAsync(); return(existingTrOwner); } return(null); }
public async Task <LineReactor> LoadSingleAsync(WRLDCWarehouseDbContext _context, ILogger _log, LineReactorForeign lrForeign, EntityWriteOption opt) { // check if entity already exists LineReactor existingLr = await _context.LineReactors.SingleOrDefaultAsync(lr => lr.WebUatId == lrForeign.WebUatId); // check if we should not modify existing entities if (opt == EntityWriteOption.DontReplace && existingLr != null) { return(existingLr); } // find the Substation of the LineReactor via the Substation WebUatId int ssWebUatId = lrForeign.SubstationWebUatId; Substation substation = await _context.Substations.SingleOrDefaultAsync(ss => ss.WebUatId == ssWebUatId); // 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 {ssWebUatId} while inserting LineReactor with webUatId {lrForeign.WebUatId} and name {lrForeign.Name}"); return(null); } // find the AcTransLineCkt of the LineReactor via the AcTransLineCkt WebUatId int lineCktWebUatId = lrForeign.AcTransLineCktWebUatId; AcTransLineCkt acCkt = await _context.AcTransLineCkts.SingleOrDefaultAsync(ckt => ckt.WebUatId == lineCktWebUatId); // if AcTransLineCkt doesnot exist, skip the import. Ideally, there should not be such case if (acCkt == null) { _log.LogCritical($"Unable to find AcTransLineCkt with webUatId {lineCktWebUatId} while inserting LineReactor with webUatId {lrForeign.WebUatId} and name {lrForeign.Name}"); return(null); } // find the State of the LineReactor via the State WebUatId int stateWebUatId = lrForeign.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 LineReactor with webUatId {lrForeign.WebUatId} and name {lrForeign.Name}"); return(null); } // check if we have to replace the entity completely if (opt == EntityWriteOption.Replace && existingLr != null) { _context.LineReactors.Remove(existingLr); } // if entity is not present, then insert or check if we have to replace the entity completely if (existingLr == null || (opt == EntityWriteOption.Replace && existingLr != null)) { LineReactor newLr = new LineReactor(); newLr.Name = lrForeign.Name; newLr.MvarCapacity = lrForeign.MvarCapacity; newLr.CommDate = lrForeign.CommDate; newLr.CodDate = lrForeign.CodDate; newLr.DecommDate = lrForeign.DecommDate; newLr.AcTransLineCktId = acCkt.AcTransLineCktId; newLr.SubstationId = substation.SubstationId; newLr.StateId = substation.StateId; newLr.IsConvertible = lrForeign.IsConvertible == 0 ? false : true; newLr.IsSwitchable = lrForeign.IsSwitchable == 0 ? false : true; newLr.WebUatId = lrForeign.WebUatId; _context.LineReactors.Add(newLr); await _context.SaveChangesAsync(); return(newLr); } // check if we have to modify the entity if (opt == EntityWriteOption.Modify && existingLr != null) { existingLr.Name = lrForeign.Name; existingLr.MvarCapacity = lrForeign.MvarCapacity; existingLr.CommDate = lrForeign.CommDate; existingLr.CodDate = lrForeign.CodDate; existingLr.DecommDate = lrForeign.DecommDate; existingLr.AcTransLineCktId = acCkt.AcTransLineCktId; existingLr.SubstationId = substation.SubstationId; existingLr.StateId = substation.StateId; existingLr.IsConvertible = lrForeign.IsConvertible == 0 ? false : true; existingLr.IsSwitchable = lrForeign.IsSwitchable == 0 ? false : true; await _context.SaveChangesAsync(); return(existingLr); } return(null); }
public async Task <HvdcPole> LoadSingleAsync(WRLDCWarehouseDbContext _context, ILogger _log, HvdcPoleForeign hvdcPoleForeign, EntityWriteOption opt) { // check if entity already exists HvdcPole existingHvdcPole = await _context.HvdcPoles.SingleOrDefaultAsync(lr => lr.WebUatId == hvdcPoleForeign.WebUatId); // check if we should not modify existing entities if (opt == EntityWriteOption.DontReplace && existingHvdcPole != null) { return(existingHvdcPole); } // find the Substation via the SubstationWebUatId int substationWebUatId = hvdcPoleForeign.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 HvdcPole with webUatId {hvdcPoleForeign.WebUatId} and name {hvdcPoleForeign.Name}"); return(null); } // find the State via the State WebUatId int stateWebUatId = hvdcPoleForeign.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 HvdcPole with webUatId {hvdcPoleForeign.WebUatId} and name {hvdcPoleForeign.Name}"); return(null); } int voltLevelWebUatId = hvdcPoleForeign.VoltLevelWebUatId; VoltLevel voltLevel = await _context.VoltLevels.SingleOrDefaultAsync(v => v.WebUatId == voltLevelWebUatId); // if voltLevel doesnot exist, skip the import. Ideally, there should not be such case if (voltLevel == null) { _log.LogCritical($"Unable to find VoltLevel with webUatId {voltLevelWebUatId} while inserting HvdcPole with webUatId {hvdcPoleForeign.WebUatId} and name {hvdcPoleForeign.Name}"); return(null); } // check if we have to replace the entity completely if (opt == EntityWriteOption.Replace && existingHvdcPole != null) { _context.HvdcPoles.Remove(existingHvdcPole); } // if entity is not present, then insert or check if we have to replace the entity completely if (existingHvdcPole == null || (opt == EntityWriteOption.Replace && existingHvdcPole != null)) { HvdcPole newHvdcPole = new HvdcPole(); newHvdcPole.Name = hvdcPoleForeign.Name; newHvdcPole.PoleNumber = hvdcPoleForeign.PoleNumber.ToString(); newHvdcPole.SubstationId = substation.SubstationId; newHvdcPole.StateId = state.StateId; newHvdcPole.VoltLevelId = voltLevel.VoltLevelId; newHvdcPole.PoleType = hvdcPoleForeign.PoleType; newHvdcPole.MaxFiringAngleDegrees = hvdcPoleForeign.MaxFiringAngleDegrees; newHvdcPole.MinFiringAngleDegrees = hvdcPoleForeign.MinFiringAngleDegrees; newHvdcPole.ThermalLimitMVA = hvdcPoleForeign.ThermalLimitMVA; newHvdcPole.CommDate = hvdcPoleForeign.CommDate; newHvdcPole.CodDate = hvdcPoleForeign.CodDate; newHvdcPole.DeCommDate = hvdcPoleForeign.DeCommDate; newHvdcPole.WebUatId = hvdcPoleForeign.WebUatId; _context.HvdcPoles.Add(newHvdcPole); await _context.SaveChangesAsync(); return(newHvdcPole); } // check if we have to modify the entity if (opt == EntityWriteOption.Modify && existingHvdcPole != null) { existingHvdcPole.Name = hvdcPoleForeign.Name; existingHvdcPole.PoleNumber = hvdcPoleForeign.PoleNumber.ToString(); existingHvdcPole.SubstationId = substation.SubstationId; existingHvdcPole.StateId = state.StateId; existingHvdcPole.VoltLevelId = voltLevel.VoltLevelId; existingHvdcPole.PoleType = hvdcPoleForeign.PoleType; existingHvdcPole.MaxFiringAngleDegrees = hvdcPoleForeign.MaxFiringAngleDegrees; existingHvdcPole.MinFiringAngleDegrees = hvdcPoleForeign.MinFiringAngleDegrees; existingHvdcPole.ThermalLimitMVA = hvdcPoleForeign.ThermalLimitMVA; existingHvdcPole.CommDate = hvdcPoleForeign.CommDate; existingHvdcPole.CodDate = hvdcPoleForeign.CodDate; existingHvdcPole.DeCommDate = hvdcPoleForeign.DeCommDate; await _context.SaveChangesAsync(); return(existingHvdcPole); } return(null); }
public async Task <AcTransLineCktOwner> LoadSingleAsync(WRLDCWarehouseDbContext _context, ILogger _log, AcTransmissionLineCircuitOwnerForeign acCktOwnerForeign, EntityWriteOption opt) { // check if entity already exists AcTransLineCktOwner existingAcCktOwner = await _context.AcTransLineCktOwners.SingleOrDefaultAsync(aco => aco.WebUatId == acCktOwnerForeign.WebUatId); // check if we should not modify existing entities if (opt == EntityWriteOption.DontReplace && existingAcCktOwner != null) { return(existingAcCktOwner); } // find the AcTranLineCkt via the AcTranLineCkt WebUatId int acCktWebUatId = acCktOwnerForeign.AcTranLineCktWebUatId; AcTransLineCkt acTransCkt = await _context.AcTransLineCkts.SingleOrDefaultAsync(ss => ss.WebUatId == acCktWebUatId); // if AcTransLineCkt doesnot exist, skip the import. Ideally, there should not be such case if (acTransCkt == null) { _log.LogCritical($"Could not find AcTransLineCkt with WebUatId {acCktWebUatId} in warehouse while creating AcTransLineCktOwner with WebUat Id {acCktOwnerForeign.WebUatId}"); return(null); } // find the Owner of the AcTransLineCkt via the Owner WebUatId int ownerWebUatId = acCktOwnerForeign.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($"Could not find Owner with WebUatId {ownerWebUatId} in warehouse while creating AcTransLineCktOwner with WebUat Id {acCktOwnerForeign.WebUatId}"); return(null); } // check if we have to replace the entity completely if (opt == EntityWriteOption.Replace && existingAcCktOwner != null) { _context.AcTransLineCktOwners.Remove(existingAcCktOwner); } // if entity is not present, then insert or check if we have to replace the entity completely if (existingAcCktOwner == null || (opt == EntityWriteOption.Replace && existingAcCktOwner != null)) { AcTransLineCktOwner acCktOwner = new AcTransLineCktOwner(); acCktOwner.OwnerId = owner.OwnerId; acCktOwner.AcTransLineCktId = acTransCkt.AcTransLineCktId; acCktOwner.WebUatId = acCktOwnerForeign.WebUatId; _context.AcTransLineCktOwners.Add(acCktOwner); await _context.SaveChangesAsync(); return(acCktOwner); } // check if we have to modify the entity if (opt == EntityWriteOption.Modify && existingAcCktOwner != null) { existingAcCktOwner.OwnerId = owner.OwnerId; existingAcCktOwner.AcTransLineCktId = acTransCkt.AcTransLineCktId; await _context.SaveChangesAsync(); return(existingAcCktOwner); } return(null); }
public async Task <Transformer> LoadSingleAsync(WRLDCWarehouseDbContext _context, ILogger _log, TransformerForeign trForeign, EntityWriteOption opt) { // check if entity already exists Transformer existingTr = await _context.Transformers.SingleOrDefaultAsync(tr => tr.WebUatId == trForeign.WebUatId); // check if we should not modify existing entities if (opt == EntityWriteOption.DontReplace && existingTr != null) { return(existingTr); } // check if substation type is valid string ssTypeSubstation = "SubStation"; string ssTypeGenStation = "Generating Station"; if (!(trForeign.StationType == ssTypeSubstation || trForeign.StationType == ssTypeGenStation)) { _log.LogCritical($"substation type is not {ssTypeSubstation} or {ssTypeGenStation} while inserting Transformer with webUatId {trForeign.WebUatId} and name {trForeign.Name}"); return(null); } MajorSubstation hvSubstation = null; int hvSubstationWebUatId = -1; if (trForeign.StationType == ssTypeSubstation) { // The transformer is in Substation hvSubstationWebUatId = trForeign.HVStationWebUatId; hvSubstation = await _context.MajorSubstations.SingleOrDefaultAsync(hvss => hvss.WebUatId == hvSubstationWebUatId); if (hvSubstation == null) { _log.LogCritical($"Unable to find MajorSubstation with webUatId {hvSubstationWebUatId} while inserting Transformer with webUatId {trForeign.WebUatId} and name {trForeign.Name}"); return(null); } } GeneratingStation hvGenStation = null; int hvGenstationWebUatId = -1; if (trForeign.StationType == ssTypeGenStation) { // The transformer is in GeneratingStation hvGenstationWebUatId = trForeign.HVStationWebUatId; hvGenStation = await _context.GeneratingStations.SingleOrDefaultAsync(hvgt => hvgt.WebUatId == hvGenstationWebUatId); if (hvGenStation == null) { _log.LogCritical($"Unable to find GeneratingStation with webUatId {hvGenstationWebUatId} while inserting Transformer with webUatId {trForeign.WebUatId} and name {trForeign.Name}"); return(null); } } // find the HV Voltage of the Transformer via the Voltage WebUatId int hvVoltWebUatId = trForeign.HighVoltLevelWebUatId; VoltLevel hvVolt = await _context.VoltLevels.SingleOrDefaultAsync(vl => vl.WebUatId == hvVoltWebUatId); // if voltage level doesnot exist, skip the import. Ideally, there should not be such case if (hvVolt == null) { _log.LogCritical($"Unable to find HV VoltLevel with webUatId {hvVoltWebUatId} while inserting Transformer with webUatId {trForeign.WebUatId} and name {trForeign.Name}"); return(null); } // find the LV Voltage of the Transformer via the Voltage WebUatId int lvVoltWebUatId = trForeign.LowVoltLevelWebUatId; VoltLevel lvVolt = await _context.VoltLevels.SingleOrDefaultAsync(vl => vl.WebUatId == lvVoltWebUatId); // if voltage level doesnot exist, skip the import. Ideally, there should not be such case if (lvVolt == null) { _log.LogCritical($"Unable to find LV VoltLevel with webUatId {lvVoltWebUatId} while inserting Transformer with webUatId {trForeign.WebUatId} and name {trForeign.Name}"); // uncomment this after vendor complies for non null LV voltage types // return null; } // find the State of the substation via the State WebUatId int stateWebUatId = trForeign.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 Transformer with webUatId {trForeign.WebUatId} and name {trForeign.Name}"); return(null); } // find the TransformerType of the Transformer via the TransformerTypeWebUatId int trTypeWebUatId = trForeign.TransTypeWebUatId; TransformerType trType = await _context.TransformerTypes.SingleOrDefaultAsync(trt => trt.WebUatId == trTypeWebUatId); // if TransformerType doesnot exist, skip the import. Ideally, there should not be such case if (trType == null) { _log.LogCritical($"Unable to find TransformerType with webUatId {trTypeWebUatId} while inserting Transformer with webUatId {trForeign.WebUatId} and name {trForeign.Name}"); return(null); } // check if we have to replace the entity completely if (opt == EntityWriteOption.Replace && existingTr != null) { _context.Transformers.Remove(existingTr); } // if entity is not present, then insert or check if we have to replace the entity completely if (existingTr == null || (opt == EntityWriteOption.Replace && existingTr != null)) { Transformer newTr = new Transformer(); newTr.Name = trForeign.Name; newTr.StationType = trForeign.StationType; newTr.HighVoltLevelId = hvVolt.VoltLevelId; if (lvVolt != null) { newTr.LowVoltLevelId = lvVolt.VoltLevelId; } newTr.TransformerNumber = trForeign.TransformerNumber; newTr.TransformerTypeId = trType.TransformerTypeId; newTr.StateId = state.StateId; newTr.MVACapacity = trForeign.MVACapacity; newTr.CodDate = trForeign.CodDate; newTr.CommDate = trForeign.CommDate; newTr.DecommDate = trForeign.DecommDate; newTr.WebUatId = trForeign.WebUatId; if (trForeign.StationType == ssTypeSubstation) { newTr.HvSubstationId = hvSubstation.MajorSubstationId; } else if (trForeign.StationType == ssTypeGenStation) { newTr.HvGeneratingStationId = hvGenStation.GeneratingStationId; } _context.Transformers.Add(newTr); await _context.SaveChangesAsync(); return(newTr); } // check if we have to modify the entity if (opt == EntityWriteOption.Modify && existingTr != null) { existingTr.Name = trForeign.Name; existingTr.StationType = trForeign.StationType; existingTr.HighVoltLevelId = hvVolt.VoltLevelId; if (lvVolt != null) { existingTr.LowVoltLevelId = lvVolt.VoltLevelId; } existingTr.TransformerNumber = trForeign.TransformerNumber; existingTr.TransformerTypeId = trType.TransformerTypeId; existingTr.StateId = state.StateId; existingTr.MVACapacity = trForeign.MVACapacity; existingTr.CodDate = trForeign.CodDate; existingTr.CommDate = trForeign.CommDate; existingTr.DecommDate = trForeign.DecommDate; if (trForeign.StationType == ssTypeSubstation) { existingTr.HvSubstationId = hvSubstation.MajorSubstationId; } else if (trForeign.StationType == ssTypeGenStation) { existingTr.HvGeneratingStationId = hvGenStation.GeneratingStationId; } await _context.SaveChangesAsync(); return(existingTr); } return(null); }
public async Task <AcTransLineCkt> LoadSingleAsync(WRLDCWarehouseDbContext _context, ILogger _log, AcTransmissionLineCircuitForeign acTrLineCktForeign, EntityWriteOption opt) { // check if entity already exists AcTransLineCkt existingAcTrLineCkt = await _context.AcTransLineCkts.SingleOrDefaultAsync(acCkt => acCkt.WebUatId == acTrLineCktForeign.WebUatId); // check if we should not modify existing entity if (opt == EntityWriteOption.DontReplace && existingAcTrLineCkt != null) { return(existingAcTrLineCkt); } // find the from AcTransmissionLine via FromSubstattion WebUatId int acTransLineWebUatId = acTrLineCktForeign.AcTransLineWebUatId; AcTransmissionLine acTrLine = await _context.AcTransmissionLines.SingleOrDefaultAsync(atl => atl.WebUatId == acTransLineWebUatId); // if AcTransmissionLine doesnot exist, skip the import. Ideally, there should not be such case if (acTrLine == null) { _log.LogCritical($"Unable to find AcTransmissionLine with webUatId {acTransLineWebUatId} while inserting AcTransLineCkt with webUatId {acTrLineCktForeign.WebUatId} and name {acTrLineCktForeign.Name}"); return(null); } // check if we have to replace the entity completely if (opt == EntityWriteOption.Replace && existingAcTrLineCkt != null) { _context.AcTransLineCkts.Remove(existingAcTrLineCkt); } // if entity is not present, then insert or check if we have to replace the entity completely if (existingAcTrLineCkt == null || (opt == EntityWriteOption.Replace && existingAcTrLineCkt != null)) { AcTransLineCkt newAcTrLineCkt = new AcTransLineCkt(); newAcTrLineCkt.Name = acTrLineCktForeign.Name; newAcTrLineCkt.AcTransmissionLineId = acTrLine.AcTransmissionLineId; newAcTrLineCkt.CktNumber = acTrLineCktForeign.CktNumber.ToString(); newAcTrLineCkt.CODDate = acTrLineCktForeign.CODDate; newAcTrLineCkt.CommDate = acTrLineCktForeign.CommDate; newAcTrLineCkt.DeCommDate = acTrLineCktForeign.DeCommDate; newAcTrLineCkt.FtcDate = acTrLineCktForeign.FtcDate; newAcTrLineCkt.Length = acTrLineCktForeign.Length; newAcTrLineCkt.SIL = acTrLineCktForeign.SIL; newAcTrLineCkt.ThermalLimitMVA = acTrLineCktForeign.ThermalLimitMVA; newAcTrLineCkt.TrialOperationDate = acTrLineCktForeign.TrialOperationDate; newAcTrLineCkt.WebUatId = acTrLineCktForeign.WebUatId; _context.AcTransLineCkts.Add(newAcTrLineCkt); await _context.SaveChangesAsync(); return(newAcTrLineCkt); } // check if we have to modify the entity if (opt == EntityWriteOption.Modify && existingAcTrLineCkt != null) { existingAcTrLineCkt.Name = acTrLineCktForeign.Name; existingAcTrLineCkt.AcTransmissionLineId = acTrLine.AcTransmissionLineId; existingAcTrLineCkt.CktNumber = acTrLineCktForeign.CktNumber.ToString(); existingAcTrLineCkt.CODDate = acTrLineCktForeign.CODDate; existingAcTrLineCkt.CommDate = acTrLineCktForeign.CommDate; existingAcTrLineCkt.DeCommDate = acTrLineCktForeign.DeCommDate; existingAcTrLineCkt.FtcDate = acTrLineCktForeign.FtcDate; existingAcTrLineCkt.Length = acTrLineCktForeign.Length; existingAcTrLineCkt.SIL = acTrLineCktForeign.SIL; existingAcTrLineCkt.ThermalLimitMVA = acTrLineCktForeign.ThermalLimitMVA; existingAcTrLineCkt.TrialOperationDate = acTrLineCktForeign.TrialOperationDate; await _context.SaveChangesAsync(); return(existingAcTrLineCkt); } return(null); }
public async Task <MajorSubstation> LoadSingleAsync(WRLDCWarehouseDbContext _context, ILogger _log, MajorSubstationForeign majorSSForeign, EntityWriteOption opt) { // check if entity already exists MajorSubstation existingMajorSS = await _context.MajorSubstations.SingleOrDefaultAsync(mss => mss.WebUatId == majorSSForeign.WebUatId); // check if we should not modify existing entities if (opt == EntityWriteOption.DontReplace && existingMajorSS != null) { return(existingMajorSS); } // find the region of the state via the region WebUatId int stateWebUatId = majorSSForeign.StateWebUatId; State majorSSState = await _context.States.SingleOrDefaultAsync(s => s.WebUatId == stateWebUatId); // if state doesnot exist, skip the import. Ideally, there should not be such case if (majorSSState == null) { _log.LogCritical($"Unable to find State with webUatId {stateWebUatId} while inserting MajorSubstation with webUatId {majorSSForeign.WebUatId} and name {majorSSForeign.Name}"); return(null); } // if entity is not present, then insert if (existingMajorSS == null) { MajorSubstation newMajorSS = new MajorSubstation(); newMajorSS.Name = majorSSForeign.Name; newMajorSS.StateId = majorSSState.StateId; newMajorSS.WebUatId = majorSSForeign.WebUatId; _context.MajorSubstations.Add(newMajorSS); await _context.SaveChangesAsync(); return(newMajorSS); } // check if we have to replace the entity completely if (opt == EntityWriteOption.Replace && existingMajorSS != null) { _context.MajorSubstations.Remove(existingMajorSS); MajorSubstation newMajorSS = new MajorSubstation(); newMajorSS.Name = majorSSForeign.Name; newMajorSS.StateId = majorSSState.StateId; newMajorSS.WebUatId = majorSSForeign.WebUatId; _context.MajorSubstations.Add(newMajorSS); await _context.SaveChangesAsync(); return(newMajorSS); } // check if we have to modify the entity if (opt == EntityWriteOption.Modify && existingMajorSS != null) { existingMajorSS.Name = majorSSForeign.Name; existingMajorSS.StateId = majorSSState.StateId; await _context.SaveChangesAsync(); return(existingMajorSS); } return(null); }
public async Task <GenerationTypeFuel> LoadSingleAsync(WRLDCWarehouseDbContext _context, ILogger _log, GenerationTypeFuelForeign genTypeFuelForeign, EntityWriteOption opt) { // check if entity already exists GenerationTypeFuel existingGenTypeFuel = await _context.GenerationTypeFuels.SingleOrDefaultAsync(gtf => gtf.WebUatId == genTypeFuelForeign.WebUatId); // check if we should not modify existing entities if (opt == EntityWriteOption.DontReplace && existingGenTypeFuel != null) { return(existingGenTypeFuel); } // find the GenerationType via the Substation WebUatId int genTypeWebUatId = genTypeFuelForeign.GenerationTypeWebUatId; GenerationType genType = await _context.GenerationTypes.SingleOrDefaultAsync(gt => gt.WebUatId == genTypeWebUatId); // if GenerationType doesnot exist, skip the import. Ideally, there should not be such case if (genType == null) { _log.LogCritical($"Unable to find GenerationType with webUatId {genTypeWebUatId} while inserting GenerationTypeFuel with webUatId {genTypeFuelForeign.WebUatId}"); return(null); } // find the Fuel via the Fuel WebUatId int fuelWebUatId = genTypeFuelForeign.FuelWebUatId; Fuel fuel = await _context.Fuels.SingleOrDefaultAsync(f => f.WebUatId == fuelWebUatId); // if fuel doesnot exist, skip the import. Ideally, there should not be such case if (fuel == null) { _log.LogCritical($"Unable to find Fuel with webUatId {fuelWebUatId} while inserting GenerationTypeFuel with webUatId {genTypeFuelForeign.WebUatId}"); return(null); } // check if we have to replace the entity completely if (opt == EntityWriteOption.Replace && existingGenTypeFuel != null) { _context.GenerationTypeFuels.Remove(existingGenTypeFuel); } // if entity is not present, then insert or check if we have to replace the entity completely if (existingGenTypeFuel == null || (opt == EntityWriteOption.Replace && existingGenTypeFuel != null)) { GenerationTypeFuel newGenTypeFuel = new GenerationTypeFuel(); newGenTypeFuel.FuelId = fuel.FuelId; newGenTypeFuel.GenerationTypeId = genType.GenerationTypeId; newGenTypeFuel.WebUatId = genTypeFuelForeign.WebUatId; _context.GenerationTypeFuels.Add(newGenTypeFuel); await _context.SaveChangesAsync(); return(newGenTypeFuel); } // check if we have to modify the entity if (opt == EntityWriteOption.Modify && existingGenTypeFuel != null) { existingGenTypeFuel.FuelId = fuel.FuelId; existingGenTypeFuel.GenerationTypeId = genType.GenerationTypeId; await _context.SaveChangesAsync(); return(existingGenTypeFuel); } return(null); }
public async Task <BusReactor> LoadSingleAsync(WRLDCWarehouseDbContext _context, ILogger _log, BusReactorForeign brForeign, EntityWriteOption opt) { // check if entity already exists BusReactor existingBr = await _context.BusReactors.SingleOrDefaultAsync(br => br.WebUatId == brForeign.WebUatId); // check if we should not modify existing entities if (opt == EntityWriteOption.DontReplace && existingBr != null) { return(existingBr); } // find the Substation of the BusReactor via the Substation WebUatId int ssWebUatId = brForeign.SubstationWebUatId; Substation substation = await _context.Substations.SingleOrDefaultAsync(ss => ss.WebUatId == ssWebUatId); // 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 {ssWebUatId} while inserting BusReactor with webUatId {brForeign.WebUatId} and name {brForeign.Name}"); return(null); } // find the Bus of the BusReactor via the Bus WebUatId int busWebUatId = brForeign.BusWebUatId; Bus bus = await _context.Buses.SingleOrDefaultAsync(b => b.WebUatId == busWebUatId); // if Bus doesnot exist, skip the import. Ideally, there should not be such case if (bus == null) { _log.LogCritical($"Unable to find Bus with webUatId {busWebUatId} while inserting BusReactor with webUatId {brForeign.WebUatId} and name {brForeign.Name}"); return(null); } // find the State of the BusReactor via the State WebUatId int stateWebUatId = brForeign.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 Transformer with webUatId {brForeign.WebUatId} and name {brForeign.Name}"); return(null); } // check if we have to replace the entity completely if (opt == EntityWriteOption.Replace && existingBr != null) { _context.BusReactors.Remove(existingBr); } // if entity is not present, then insert or check if we have to replace the entity completely if (existingBr == null || (opt == EntityWriteOption.Replace && existingBr != null)) { BusReactor newBr = new BusReactor(); newBr.Name = brForeign.Name; newBr.BusReactorNumber = brForeign.BusReactorNumber; newBr.MvarCapacity = brForeign.MvarCapacity; newBr.CommDate = brForeign.CommDate; newBr.CodDate = brForeign.CodDate; newBr.DecommDate = brForeign.DecommDate; newBr.BusId = bus.BusId; newBr.SubstationId = substation.SubstationId; newBr.StateId = substation.StateId; newBr.WebUatId = brForeign.WebUatId; _context.BusReactors.Add(newBr); await _context.SaveChangesAsync(); return(newBr); } // check if we have to modify the entity if (opt == EntityWriteOption.Modify && existingBr != null) { existingBr.Name = brForeign.Name; existingBr.BusReactorNumber = brForeign.BusReactorNumber; existingBr.MvarCapacity = brForeign.MvarCapacity; existingBr.CommDate = brForeign.CommDate; existingBr.CodDate = brForeign.CodDate; existingBr.DecommDate = brForeign.DecommDate; existingBr.BusId = bus.BusId; existingBr.SubstationId = substation.SubstationId; existingBr.StateId = substation.StateId; await _context.SaveChangesAsync(); return(existingBr); } 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); }
public async Task <AcTransmissionLine> LoadSingleAsync(WRLDCWarehouseDbContext _context, ILogger _log, AcTransmissionLineForeign acTrForeign, EntityWriteOption opt) { // check if entity already exists AcTransmissionLine existingAcTrLine = await _context.AcTransmissionLines.SingleOrDefaultAsync(acTr => acTr.WebUatId == acTrForeign.WebUatId); // check if we should not modify existing entity if (opt == EntityWriteOption.DontReplace && existingAcTrLine != null) { return(existingAcTrLine); } // find the from Substation via FromSubstattion WebUatId int fromSSebUatId = acTrForeign.FromSSWebUatId; Substation fromSS = await _context.Substations.SingleOrDefaultAsync(ss => ss.WebUatId == fromSSebUatId); // if FromSubstation doesnot exist, skip the import. Ideally, there should not be such case if (fromSS == null) { _log.LogCritical($"Unable to find FromSubstation with webUatId {fromSSebUatId} while inserting AcTransmissionLine with webUatId {acTrForeign.WebUatId} and name {acTrForeign.Name}"); return(null); } // find the To Substation via ToSubstattion WebUatId int toSSebUatId = acTrForeign.ToSSWebUatId; Substation toSS = await _context.Substations.SingleOrDefaultAsync(ss => ss.WebUatId == toSSebUatId); // if ToSubstation doesnot exist, skip the import. Ideally, there should not be such case if (toSS == null) { _log.LogCritical($"Unable to find ToSubstation with webUatId {toSSebUatId} while inserting AcTransmissionLine with webUatId {acTrForeign.WebUatId} and name {acTrForeign.Name}"); return(null); } // find the Voltage of the substation via the Voltage WebUatId int voltWebUatId = acTrForeign.VoltLevelWebUatId; VoltLevel voltLevel = await _context.VoltLevels.SingleOrDefaultAsync(vl => vl.WebUatId == voltWebUatId); // if voltage level doesnot exist, skip the import. Ideally, there should not be such case if (voltLevel == null) { _log.LogCritical($"Unable to find VoltLevel with webUatId {voltWebUatId} while inserting AcTransmissionLine with webUatId {acTrForeign.WebUatId} and name {acTrForeign.Name}"); return(null); } // check if we have to replace the entity completely if (opt == EntityWriteOption.Replace && existingAcTrLine != null) { _context.AcTransmissionLines.Remove(existingAcTrLine); } // if entity is not present, then insert or check if we have to replace the entity completely if (existingAcTrLine == null || (opt == EntityWriteOption.Replace && existingAcTrLine != null)) { AcTransmissionLine newAcTrLine = new AcTransmissionLine(); newAcTrLine.Name = acTrForeign.Name; newAcTrLine.VoltLevelId = voltLevel.VoltLevelId; newAcTrLine.FromSubstationId = fromSS.SubstationId; newAcTrLine.ToSubstationId = toSS.SubstationId; newAcTrLine.WebUatId = acTrForeign.WebUatId; _context.AcTransmissionLines.Add(newAcTrLine); await _context.SaveChangesAsync(); return(newAcTrLine); } // check if we have to modify the entity if (opt == EntityWriteOption.Modify && existingAcTrLine != null) { existingAcTrLine.Name = acTrForeign.Name; existingAcTrLine.VoltLevelId = voltLevel.VoltLevelId; existingAcTrLine.FromSubstationId = fromSS.SubstationId; existingAcTrLine.ToSubstationId = toSS.SubstationId; await _context.SaveChangesAsync(); return(existingAcTrLine); } return(null); }
public async Task <GeneratingStation> LoadSingleAsync(WRLDCWarehouseDbContext _context, ILogger _log, GeneratingStationForeign genStationForeign, EntityWriteOption opt) { // check if entity already exists GeneratingStation existingGenStation = await _context.GeneratingStations.SingleOrDefaultAsync(ss => ss.WebUatId == genStationForeign.WebUatId); // check if we should not modify existing entities if (opt == EntityWriteOption.DontReplace && existingGenStation != null) { return(existingGenStation); } // find the GeneratorClassification of the substation via the GenClassification WebUatId int genClassificationWebUatId = genStationForeign.GenClassificationWebUatId; GeneratorClassification genClassification = await _context.GeneratorClassifications.SingleOrDefaultAsync(gc => gc.WebUatId == genClassificationWebUatId); // if GeneratorClassification doesnot exist, skip the import. Ideally, there should not be such case if (genClassification == null) { _log.LogCritical($"Could not find GeneratorClassification with WebUatId {genClassificationWebUatId} in warehouse while creating Generating Station with WebUat Id {genStationForeign.WebUatId} and name {genStationForeign.Name}"); return(null); } // find the Generation Type of the substation via the Voltage WebUatId int genTypeWebUatId = genStationForeign.GenerationTypeWebUatId; GenerationType genType = await _context.GenerationTypes.SingleOrDefaultAsync(gt => gt.WebUatId == genTypeWebUatId); // if GenerationType doesnot exist, skip the import. Ideally, there should not be such case if (genType == null) { _log.LogCritical($"Could not find GenerationType with WebUatId {genTypeWebUatId} in warehouse while creating Generating Station with WebUat Id {genStationForeign.WebUatId} and name {genStationForeign.Name}"); return(null); } // find the State of the substation via the State WebUatId int stateWebUatId = genStationForeign.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($"Could not find State with WebUatId {stateWebUatId} in warehouse while creating Generating Station with WebUat Id {genStationForeign.WebUatId} and name {genStationForeign.Name}"); return(null); } // find the fuel of the substation via the State WebUatId int fuelWebUatId = genStationForeign.FuelWebUatId; Fuel fuel = await _context.Fuels.SingleOrDefaultAsync(f => f.WebUatId == fuelWebUatId); // if fuel doesnot exist, skip the import. Ideally, there should not be such case if (fuel == null) { _log.LogCritical($"Could not find Fuel with WebUatId {fuelWebUatId} in warehouse while creating Generating Station with WebUat Id {genStationForeign.WebUatId} and name {genStationForeign.Name}"); // uncomment this after vendor complies to non null fuel types // return null; } // check if we have to replace the entity completely if (opt == EntityWriteOption.Replace && existingGenStation != null) { _context.GeneratingStations.Remove(existingGenStation); } // if entity is not present, then insert or check if we have to replace the entity completely if (existingGenStation == null || (opt == EntityWriteOption.Replace && existingGenStation != null)) { GeneratingStation genStation = new GeneratingStation(); genStation.Name = genStationForeign.Name; genStation.GenerationTypeId = genType.GenerationTypeId; genStation.GeneratorClassificationId = genClassification.GeneratorClassificationId; genStation.StateId = state.StateId; if (fuel != null) { genStation.FuelId = fuel.FuelId; } genStation.WebUatId = genStationForeign.WebUatId; _context.GeneratingStations.Add(genStation); await _context.SaveChangesAsync(); return(genStation); } // check if we have to modify the entity if (opt == EntityWriteOption.Modify && existingGenStation != null) { existingGenStation.Name = genStationForeign.Name; existingGenStation.GenerationTypeId = genType.GenerationTypeId; existingGenStation.GeneratorClassificationId = genClassification.GeneratorClassificationId; existingGenStation.StateId = state.StateId; if (fuel != null) { existingGenStation.FuelId = fuel.FuelId; } await _context.SaveChangesAsync(); return(existingGenStation); } return(null); }
public async Task <Bus> LoadSingleAsync(WRLDCWarehouseDbContext _context, ILogger _log, BusForeign busForeign, EntityWriteOption opt) { // check if entity already exists Bus existingBus = await _context.Buses.SingleOrDefaultAsync(b => b.WebUatId == busForeign.WebUatId); // check if we should not modify existing entities if (opt == EntityWriteOption.DontReplace && existingBus != null) { return(existingBus); } // find the substation of the bus via the AssSubstation WebUatId int subWebUatId = busForeign.AssSubstationWebUatId; Substation busSubstation = await _context.Substations.SingleOrDefaultAsync(s => s.WebUatId == subWebUatId); // if Substation doesnot exist, skip the import. Ideally, there should not be such case if (busSubstation == null) { _log.LogCritical($"Unable to find Substation with webUatId {subWebUatId} while inserting Bus with webUatId {busForeign.WebUatId} and name {busForeign.Name}"); return(null); } // find the voltage of the bus via the VoltLevel WebUatId int voltLevelWebUatId = busForeign.VoltageWebUatId; VoltLevel voltLevel = await _context.VoltLevels.SingleOrDefaultAsync(v => v.WebUatId == voltLevelWebUatId); // if voltLevel doesnot exist, skip the import. Ideally, there should not be such case if (voltLevel == null) { _log.LogCritical($"Unable to find VoltLevel with webUatId {voltLevelWebUatId} while inserting Bus with webUatId {busForeign.WebUatId} and name {busForeign.Name}"); return(null); } // check if we have to replace the entity completely if (opt == EntityWriteOption.Replace && existingBus != null) { _context.Buses.Remove(existingBus); } // if entity is not present, then insert if (existingBus == null || (opt == EntityWriteOption.Replace && existingBus != null)) { Bus newBus = new Bus(); newBus.Name = busForeign.Name; newBus.BusNumber = busForeign.BusNumber.ToString(); newBus.SubstationId = busSubstation.SubstationId; newBus.VoltLevelId = voltLevel.VoltLevelId; newBus.WebUatId = busForeign.WebUatId; _context.Buses.Add(newBus); await _context.SaveChangesAsync(); return(newBus); } // check if we have to modify the entity if (opt == EntityWriteOption.Modify && existingBus != null) { existingBus.Name = busForeign.Name; existingBus.BusNumber = busForeign.BusNumber.ToString(); existingBus.SubstationId = busSubstation.SubstationId; existingBus.VoltLevelId = voltLevel.VoltLevelId; await _context.SaveChangesAsync(); return(existingBus); } return(null); }
public async Task <GeneratingStationOwner> LoadSingleAsync(WRLDCWarehouseDbContext _context, ILogger _log, GeneratingStationOwnerForeign genStationOwnerForeign, EntityWriteOption opt) { // check if entity already exists GeneratingStationOwner existingGenStationOwner = await _context.GeneratingStationOwners.SingleOrDefaultAsync(gso => gso.WebUatId == genStationOwnerForeign.WebUatId); // check if we should not modify existing entities if (opt == EntityWriteOption.DontReplace && existingGenStationOwner != null) { return(existingGenStationOwner); } // find the GeneratingStation via the GeneratingStation WebUatId int genStationWebUatId = genStationOwnerForeign.GeneratingStationWebUatId; GeneratingStation genStation = await _context.GeneratingStations.SingleOrDefaultAsync(gs => gs.WebUatId == genStationWebUatId); // if GeneratingStation doesnot exist, skip the import. Ideally, there should not be such case if (genStation == null) { _log.LogCritical($"Unable to find GeneratingStation with webUatId {genStationWebUatId} while inserting GeneratingStationOwner with webUatId {genStationOwnerForeign.WebUatId}"); return(null); } // find the Owner of the substation via the Owner WebUatId int ownerWebUatId = genStationOwnerForeign.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 GeneratingStationOwner with webUatId {genStationOwnerForeign.WebUatId}"); return(null); } // check if we have to replace the entity completely if (opt == EntityWriteOption.Replace && existingGenStationOwner != null) { _context.GeneratingStationOwners.Remove(existingGenStationOwner); } // if entity is not present, then insert or check if we have to replace the entity completely if (existingGenStationOwner == null || (opt == EntityWriteOption.Replace && existingGenStationOwner != null)) { GeneratingStationOwner genStationOwner = new GeneratingStationOwner(); genStationOwner.OwnerId = owner.OwnerId; genStationOwner.GeneratingStationId = genStation.GeneratingStationId; genStationOwner.WebUatId = genStationOwnerForeign.WebUatId; _context.GeneratingStationOwners.Add(genStationOwner); await _context.SaveChangesAsync(); return(genStationOwner); } // check if we have to modify the entity if (opt == EntityWriteOption.Modify && existingGenStationOwner != null) { existingGenStationOwner.OwnerId = owner.OwnerId; existingGenStationOwner.GeneratingStationId = genStation.GeneratingStationId; await _context.SaveChangesAsync(); return(existingGenStationOwner); } return(null); }
public async Task <Compensator> LoadSingleAsync(WRLDCWarehouseDbContext _context, ILogger _log, CompensatorForeign compensatorForeign, EntityWriteOption opt) { // check if entity already exists Compensator existingCompensator = await _context.Compensators.SingleOrDefaultAsync(lr => lr.WebUatId == compensatorForeign.WebUatId); // check if we should not modify existing entities if (opt == EntityWriteOption.DontReplace && existingCompensator != null) { return(existingCompensator); } // find the CompensatorType via the CompensatorTypeWebUatId int compTypeWebUatId = compensatorForeign.CompensatorTypeWebUatId; CompensatorType compType = await _context.CompensatorTypes.SingleOrDefaultAsync(ct => ct.WebUatId == compTypeWebUatId); // if CompensatorType doesnot exist, skip the import. Ideally, there should not be such case if (compType == null) { _log.LogCritical($"Unable to find CompensatorType with webUatId {compTypeWebUatId} while inserting Compensator with webUatId {compensatorForeign.WebUatId} and name {compensatorForeign.Name}"); return(null); } // find the Substation via the SubstationWebUatId int substationWebUatId = compensatorForeign.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 Compensator with webUatId {compensatorForeign.WebUatId} and name {compensatorForeign.Name}"); return(null); } // find the State via the State WebUatId int stateWebUatId = compensatorForeign.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 Compensator with webUatId {compensatorForeign.WebUatId} and name {compensatorForeign.Name}"); return(null); } // find the attachElement Id of the Compensator int attachElementId = -1; int attachElementType = compensatorForeign.AttachElementType; int attachElementWebUatId = compensatorForeign.AttachElementWebUatId; if (attachElementType == 1) { // attach element is a bus attachElementId = (await _context.Buses.SingleOrDefaultAsync(b => b.WebUatId == attachElementWebUatId)).BusId; } else if (attachElementType == 2) { // attach element is an AC Transmission line attachElementId = (await _context.AcTransLineCkts.SingleOrDefaultAsync(b => b.WebUatId == attachElementWebUatId)).AcTransLineCktId; } else { // encountered an unknown attach element type, ideally this should not happen _log.LogCritical($"Encountered an unknown attach element type {attachElementType} while inserting Compensator with webUatId {compensatorForeign.WebUatId} and name {compensatorForeign.Name}"); return(null); } // check if we have to replace the entity completely if (opt == EntityWriteOption.Replace && existingCompensator != null) { _context.Compensators.Remove(existingCompensator); } // if entity is not present, then insert or check if we have to replace the entity completely if (existingCompensator == null || (opt == EntityWriteOption.Replace && existingCompensator != null)) { Compensator newCompensator = new Compensator(); newCompensator.Name = compensatorForeign.Name; newCompensator.CompensatorTypeId = compType.CompensatorTypeId; newCompensator.SubstationId = substation.SubstationId; newCompensator.StateId = state.StateId; newCompensator.AttachElementType = compensatorForeign.AttachElementType; newCompensator.AttachElementId = attachElementId; newCompensator.CompensatorNumber = compensatorForeign.CompensatorNumber.ToString(); newCompensator.PercVariableComp = compensatorForeign.PercVariableComp; newCompensator.PercFixedComp = compensatorForeign.PercFixedComp; newCompensator.LineReactanceOhms = compensatorForeign.LineReactanceOhms; newCompensator.CommDate = compensatorForeign.CommDate; newCompensator.CodDate = compensatorForeign.CodDate; newCompensator.DeCommDate = compensatorForeign.DeCommDate; newCompensator.WebUatId = compensatorForeign.WebUatId; _context.Compensators.Add(newCompensator); await _context.SaveChangesAsync(); return(newCompensator); } // check if we have to modify the entity if (opt == EntityWriteOption.Modify && existingCompensator != null) { existingCompensator.Name = compensatorForeign.Name; existingCompensator.CompensatorTypeId = compType.CompensatorTypeId; existingCompensator.SubstationId = substation.SubstationId; existingCompensator.StateId = state.StateId; existingCompensator.AttachElementType = compensatorForeign.AttachElementType; existingCompensator.AttachElementId = attachElementId; existingCompensator.CompensatorNumber = compensatorForeign.CompensatorNumber.ToString(); existingCompensator.PercVariableComp = compensatorForeign.PercVariableComp; existingCompensator.PercFixedComp = compensatorForeign.PercFixedComp; existingCompensator.LineReactanceOhms = compensatorForeign.LineReactanceOhms; existingCompensator.CommDate = compensatorForeign.CommDate; existingCompensator.CodDate = compensatorForeign.CodDate; existingCompensator.DeCommDate = compensatorForeign.DeCommDate; await _context.SaveChangesAsync(); return(existingCompensator); } return(null); }
public async Task <Bay> LoadSingleAsync(WRLDCWarehouseDbContext _context, ILogger _log, BayForeign bayForeign, EntityWriteOption opt) { // check if entity already exists Bay existingBay = await _context.Bays.SingleOrDefaultAsync(b => b.WebUatId == bayForeign.WebUatId); // check if we should not modify existing entities if (opt == EntityWriteOption.DontReplace && existingBay != null) { return(existingBay); } // find the BayType via the BayTypeWebUatId int bayTypeWebUatId = bayForeign.BayTypeWebUatId; BayType bayType = await _context.BayTypes.SingleOrDefaultAsync(ct => ct.WebUatId == bayTypeWebUatId); // if BayType doesnot exist, skip the import. Ideally, there should not be such case if (bayType == null) { _log.LogCritical($"Unable to find BayType with webUatId {bayTypeWebUatId} while inserting Bay with webUatId {bayForeign.WebUatId} and name {bayForeign.Name}"); return(null); } // find the VoltLevel via the VoltLevelWebUatId int voltWebUatId = bayForeign.VoltageWebUatId; VoltLevel voltLevel = await _context.VoltLevels.SingleOrDefaultAsync(v => v.WebUatId == voltWebUatId); // if VoltLevel doesnot exist, skip the import. Ideally, there should not be such case if (voltLevel == null) { _log.LogCritical($"Unable to find voltLevel with webUatId {voltWebUatId} while inserting Bay with webUatId {bayForeign.WebUatId} and name {bayForeign.Name}"); return(null); } // find the Substation via the SubstationWebUatId int substationWebUatId = bayForeign.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 Bay with webUatId {bayForeign.WebUatId} and name {bayForeign.Name}"); return(null); } async Task <int> getElementId(int entWebUatId, string entType) { // find the sourceEntityId of the Bay int entityId = -1; try { // entityType can be AC LINE CIRCUIT,TCSC,BUS REACTOR,TRANSFORMER,Filter Bank,BUS,FSC,LINE REACTOR string entityType = bayForeign.SourceEntityType; int elementWebUatId = bayForeign.SourceEntityWebUatId; if (entityType == "AC LINE CIRCUIT") { // attach element is AcTransLineCkt entityId = (await _context.AcTransLineCkts.SingleOrDefaultAsync(ckt => ckt.WebUatId == elementWebUatId)).AcTransLineCktId; } else if (entityType == "TCSC") { // attach element is Compensator entityId = (await _context.Compensators.SingleOrDefaultAsync(c => c.WebUatId == elementWebUatId)).CompensatorId; } else if (entityType == "BUS REACTOR") { // attach element is BUS REACTOR entityId = (await _context.BusReactors.SingleOrDefaultAsync(br => br.WebUatId == elementWebUatId)).BusReactorId; } else if (entityType == "TRANSFORMER") { // attach element is TRANSFORMER entityId = (await _context.Transformers.SingleOrDefaultAsync(br => br.WebUatId == elementWebUatId)).TransformerId; } else if (entityType == "Filter Bank") { // attach element is Filter Bank entityId = (await _context.FilterBanks.SingleOrDefaultAsync(br => br.WebUatId == elementWebUatId)).FilterBankId; } else if (entityType == "BUS") { // attach element is BUS entityId = (await _context.Buses.SingleOrDefaultAsync(br => br.WebUatId == elementWebUatId)).BusId; } else if (entityType == "FSC") { // attach element is FSC entityId = (await _context.Fscs.SingleOrDefaultAsync(br => br.WebUatId == elementWebUatId)).FscId; } else if (entityType == "LINE REACTOR") { // attach element is LINE REACTOR entityId = (await _context.LineReactors.SingleOrDefaultAsync(br => br.WebUatId == elementWebUatId)).LineReactorId; } } catch (Exception e) { Console.WriteLine(e.Message); } return(entityId); } // find the sourceEntityId of the Bay // sourceEntityType can be AC LINE CIRCUIT,TCSC,BUS REACTOR,TRANSFORMER,Filter Bank,BUS,FSC,LINE REACTOR string sourceEntityType = bayForeign.SourceEntityType; int sourceElementWebUatId = bayForeign.SourceEntityWebUatId; int sourceEntityId = await getElementId(sourceElementWebUatId, sourceEntityType); if (sourceEntityId == -1) { // encountered an unknown source element type, ideally this should not happen _log.LogCritical($"Encountered an unknown source element type {sourceEntityType} while inserting Bay with webUatId {bayForeign.WebUatId} and name {bayForeign.Name}"); return(null); } // find the destEntityId of the Bay // sourceEntityType can be AC LINE CIRCUIT,TCSC,BUS REACTOR,TRANSFORMER,Filter Bank,BUS,FSC,LINE REACTOR string destEntityType = bayForeign.DestEntityType; int destEntityId = -1; if (destEntityType != null) { int destElementWebUatId = bayForeign.DestEntityWebUatId; destEntityId = await getElementId(destElementWebUatId, destEntityType); if (destEntityId == -1) { // encountered an unknown destination element type, ideally this should not happen _log.LogCritical($"Encountered an unknown destination element type {destEntityType} while inserting Bay with webUatId {bayForeign.WebUatId} and name {bayForeign.Name}"); return(null); } } // check if we have to replace the entity completely if (opt == EntityWriteOption.Replace && existingBay != null) { _context.Bays.Remove(existingBay); } // if entity is not present, then insert or check if we have to replace the entity completely if (existingBay == null || (opt == EntityWriteOption.Replace && existingBay != null)) { Bay newBay = new Bay(); newBay.Name = bayForeign.Name; newBay.BayNumber = bayForeign.BayNumber; newBay.SourceEntityId = sourceEntityId; newBay.SourceEntityType = sourceEntityType; newBay.SourceEntityName = bayForeign.SourceEntityName; if (destEntityType != null) { newBay.DestEntityId = destEntityId; newBay.DestEntityType = destEntityType; newBay.DestEntityName = bayForeign.DestEntityName; } newBay.BayTypeId = bayType.BayTypeId; newBay.VoltLevelId = voltLevel.VoltLevelId; newBay.SubstationId = substation.SubstationId; newBay.WebUatId = bayForeign.WebUatId; _context.Bays.Add(newBay); await _context.SaveChangesAsync(); return(newBay); } // check if we have to modify the entity if (opt == EntityWriteOption.Modify && existingBay != null) { existingBay.Name = bayForeign.Name; existingBay.BayNumber = bayForeign.BayNumber.ToString(); existingBay.SourceEntityId = sourceEntityId; existingBay.SourceEntityType = sourceEntityType; existingBay.SourceEntityName = bayForeign.SourceEntityName; if (destEntityType != null) { existingBay.DestEntityId = destEntityId; existingBay.DestEntityType = destEntityType; existingBay.DestEntityName = bayForeign.DestEntityName; } existingBay.BayTypeId = bayType.BayTypeId; existingBay.VoltLevelId = voltLevel.VoltLevelId; existingBay.SubstationId = substation.SubstationId; await _context.SaveChangesAsync(); return(existingBay); } return(null); }
public async Task <GeneratorUnit> LoadSingleAsync(WRLDCWarehouseDbContext _context, ILogger _log, GeneratorUnitForeign genUnitForeign, EntityWriteOption opt) { // check if entity already exists GeneratorUnit existingGenUnit = await _context.GeneratorUnits.SingleOrDefaultAsync(r => r.WebUatId == genUnitForeign.WebUatId); // check if we should not modify existing entities if (opt == EntityWriteOption.DontReplace && existingGenUnit != null) { return(existingGenUnit); } // find the GeneratingStation of the unit via the GeneratingStationWebUatId int generatingStationWebUatId = genUnitForeign.GeneratingStationWebUatId; GeneratingStation genStation = await _context.GeneratingStations.SingleOrDefaultAsync(r => r.WebUatId == generatingStationWebUatId); // if genStation doesnot exist, skip the import. Ideally, there should not be such case if (genStation == null) { _log.LogCritical($"Could not find GeneratingStation with WebUatId {generatingStationWebUatId} in warehouse while creating GeneratorUnit with WebUat Id {genUnitForeign.WebUatId} and name {genUnitForeign.Name}"); return(null); } // find the GeneratorStage of the unit via the GeneratingStationWebUatId int generatorStageWebUatId = genUnitForeign.GeneratorStageWebUatId; GeneratorStage genStage = await _context.GeneratorStages.SingleOrDefaultAsync(r => r.WebUatId == generatorStageWebUatId); // if genStage doesnot exist, skip the import. Ideally, there should not be such case if (genStage == null) { _log.LogCritical($"Could not find GeneratorStage with WebUatId {generatorStageWebUatId} in warehouse while creating GeneratorUnit with WebUat Id {genUnitForeign.WebUatId} and name {genUnitForeign.Name}"); return(null); } // check if we have to replace the entity completely if (opt == EntityWriteOption.Replace && existingGenUnit != null) { _context.GeneratorUnits.Remove(existingGenUnit); } // if entity is not present, then insert or check if we have to replace the entity completely if (existingGenUnit == null || (opt == EntityWriteOption.Replace && existingGenUnit != null)) { GeneratorUnit newGenUnit = new GeneratorUnit(); newGenUnit.Name = genUnitForeign.Name; newGenUnit.GeneratingStationId = genStation.GeneratingStationId; newGenUnit.GeneratorStageId = genStage.GeneratorStageId; newGenUnit.UnitNumber = genUnitForeign.UnitNumber.ToString(); newGenUnit.GenVoltageKV = genUnitForeign.GenVoltageKV; newGenUnit.GenHighVoltageKV = genUnitForeign.GenHighVoltageKV; newGenUnit.MvaCapacity = genUnitForeign.MvaCapacity; newGenUnit.InstalledCapacity = genUnitForeign.InstalledCapacity; newGenUnit.CodDateTime = genUnitForeign.CodDateTime; newGenUnit.CommDateTime = genUnitForeign.CommDateTime; newGenUnit.DeCommDateTime = genUnitForeign.DeCommDateTime; newGenUnit.WebUatId = genUnitForeign.WebUatId; _context.GeneratorUnits.Add(newGenUnit); await _context.SaveChangesAsync(); return(newGenUnit); } // check if we have to modify the entity if (opt == EntityWriteOption.Modify && existingGenUnit != null) { existingGenUnit.Name = genUnitForeign.Name; existingGenUnit.GeneratingStationId = genStation.GeneratingStationId; existingGenUnit.GeneratorStageId = genStage.GeneratorStageId; existingGenUnit.UnitNumber = genUnitForeign.UnitNumber.ToString(); existingGenUnit.GenVoltageKV = genUnitForeign.GenVoltageKV; existingGenUnit.GenHighVoltageKV = genUnitForeign.GenHighVoltageKV; existingGenUnit.MvaCapacity = genUnitForeign.MvaCapacity; existingGenUnit.InstalledCapacity = genUnitForeign.InstalledCapacity; existingGenUnit.CodDateTime = genUnitForeign.CodDateTime; existingGenUnit.CommDateTime = genUnitForeign.CommDateTime; existingGenUnit.DeCommDateTime = genUnitForeign.DeCommDateTime; await _context.SaveChangesAsync(); return(existingGenUnit); } return(null); }
public async Task <FilterBankOwner> LoadSingleAsync(WRLDCWarehouseDbContext _context, ILogger _log, FilterBankOwnerForeign fbOwnerForeign, EntityWriteOption opt) { // check if entity already exists FilterBankOwner existingFbOwner = await _context.FilterBankOwners.SingleOrDefaultAsync(fbO => fbO.WebUatId == fbOwnerForeign.WebUatId); // check if we should not modify existing entities if (opt == EntityWriteOption.DontReplace && existingFbOwner != null) { return(existingFbOwner); } // find the FilterBank via the FilterBank WebUatId int fbWebUatId = fbOwnerForeign.FilterBankWebUatId; FilterBank filterBank = await _context.FilterBanks.SingleOrDefaultAsync(ss => ss.WebUatId == fbWebUatId); // if FilterBank doesnot exist, skip the import. Ideally, there should not be such case if (filterBank == null) { _log.LogCritical($"Unable to find FilterBank with webUatId {fbWebUatId} while inserting FilterBankOwner with webUatId {fbOwnerForeign.WebUatId}"); return(null); } // find the Owner of the substation via the Owner WebUatId int ownerWebUatId = fbOwnerForeign.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 FilterBank with webUatId {ownerWebUatId} while inserting FilterBankOwner with webUatId {fbOwnerForeign.WebUatId}"); return(null); } try { // check if we have to replace the entity completely if (opt == EntityWriteOption.Replace && existingFbOwner != null) { _context.FilterBankOwners.Remove(existingFbOwner); } // if entity is not present, then insert or check if we have to replace the entity completely if (existingFbOwner == null || (opt == EntityWriteOption.Replace && existingFbOwner != null)) { FilterBankOwner newFbOwner = new FilterBankOwner(); newFbOwner.OwnerId = owner.OwnerId; newFbOwner.FilterBankId = filterBank.FilterBankId; newFbOwner.WebUatId = fbOwnerForeign.WebUatId; _context.FilterBankOwners.Add(newFbOwner); await _context.SaveChangesAsync(); return(newFbOwner); } // check if we have to modify the entity if (opt == EntityWriteOption.Modify && existingFbOwner != null) { existingFbOwner.OwnerId = owner.OwnerId; existingFbOwner.FilterBankId = filterBank.FilterBankId; await _context.SaveChangesAsync(); return(existingFbOwner); } } catch (DbUpdateException e) { _log.LogCritical($"Error occured while inserting FilterBankOwner with webUatId {fbOwnerForeign.WebUatId}, owner id {owner.OwnerId} and ssId {filterBank.SubstationId}"); _log.LogCritical($"EntityWriteOption = {opt.ToString()}"); _log.LogCritical($"{e.Message}"); return(null); } return(null); }