public async Task ImportForeignMajorSubstations(WRLDCWarehouseDbContext _context, ILogger _log, string oracleConnStr, EntityWriteOption opt) { MajorSubstationExtract majorSSExtract = new MajorSubstationExtract(); List <MajorSubstationForeign> majorSSForeigns = majorSSExtract.ExtractMajorSubstationsForeign(oracleConnStr); LoadMajorSubstation loadMajorSS = new LoadMajorSubstation(); foreach (MajorSubstationForeign majorSSForeign in majorSSForeigns) { MajorSubstation insertedMajorSS = await loadMajorSS.LoadSingleAsync(_context, _log, majorSSForeign, opt); } }
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 <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 <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); }