Exemplo n.º 1
0
        public async Task ImportForeignGenerationTypeFuels(WRLDCWarehouseDbContext _context, ILogger _log, string oracleConnStr, EntityWriteOption opt)
        {
            GenTypeFuelExtract genTypeFuelExtract = new GenTypeFuelExtract();
            List <GenerationTypeFuelForeign> genTypeFuelsForeign = genTypeFuelExtract.ExtractGenerationTypeFuelsForeign(oracleConnStr);

            LoadGenerationTypeFuel loadGenTypeFuel = new LoadGenerationTypeFuel();

            foreach (GenerationTypeFuelForeign genTypeFuelForeign in genTypeFuelsForeign)
            {
                GenerationTypeFuel insertedGenTypeFuel = await loadGenTypeFuel.LoadSingleAsync(_context, _log, genTypeFuelForeign, opt);
            }
        }
Exemplo n.º 2
0
        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);
        }