Ejemplo n.º 1
0
        public async Task ImportForeignCompensators(WRLDCWarehouseDbContext _context, ILogger _log, string oracleConnStr, EntityWriteOption opt)
        {
            CompensatorExtract        compensatorExtract     = new CompensatorExtract();
            List <CompensatorForeign> compensatorForeignList = compensatorExtract.ExtractCompensatorForeign(oracleConnStr);

            LoadCompensator loadCompensator = new LoadCompensator();

            foreach (CompensatorForeign compensatorForeign in compensatorForeignList)
            {
                Compensator insertedCompensator = await loadCompensator.LoadSingleAsync(_context, _log, compensatorForeign, opt);
            }
        }
Ejemplo n.º 2
0
        public async Task <CompensatorOwner> LoadSingleAsync(WRLDCWarehouseDbContext _context, ILogger _log, CompensatorOwnerForeign compensatorOwnerForeign, EntityWriteOption opt)
        {
            // check if entity already exists
            CompensatorOwner existingCompensatorOwner = await _context.CompensatorOwners.SingleOrDefaultAsync(cO => cO.WebUatId == compensatorOwnerForeign.WebUatId);

            // check if we should not modify existing entities
            if (opt == EntityWriteOption.DontReplace && existingCompensatorOwner != null)
            {
                return(existingCompensatorOwner);
            }

            // find the Compensator via the Compensator WebUatId
            int         compensatorWebUatId = compensatorOwnerForeign.CompensatorWebUatId;
            Compensator compensator         = await _context.Compensators.SingleOrDefaultAsync(c => c.WebUatId == compensatorWebUatId);

            // if Compensator doesnot exist, skip the import. Ideally, there should not be such case
            if (compensator == null)
            {
                _log.LogCritical($"Unable to find Compensator with webUatId {compensatorWebUatId} while inserting CompensatorOwner with webUatId {compensatorOwnerForeign.WebUatId}");
                return(null);
            }

            // find the Owner of the Compensator via the Owner WebUatId
            int   ownerWebUatId = compensatorOwnerForeign.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 CompensatorOwner with webUatId {compensatorOwnerForeign.WebUatId}");
                return(null);
            }

            try
            {
                // check if we have to replace the entity completely
                if (opt == EntityWriteOption.Replace && existingCompensatorOwner != null)
                {
                    _context.CompensatorOwners.Remove(existingCompensatorOwner);
                }

                // if entity is not present, then insert or check if we have to replace the entity completely
                if (existingCompensatorOwner == null || (opt == EntityWriteOption.Replace && existingCompensatorOwner != null))
                {
                    CompensatorOwner newCompensatorOwner = new CompensatorOwner();
                    newCompensatorOwner.OwnerId       = owner.OwnerId;
                    newCompensatorOwner.CompensatorId = compensator.CompensatorId;
                    newCompensatorOwner.WebUatId      = compensatorOwnerForeign.WebUatId;

                    _context.CompensatorOwners.Add(newCompensatorOwner);
                    await _context.SaveChangesAsync();

                    return(newCompensatorOwner);
                }

                // check if we have to modify the entity
                if (opt == EntityWriteOption.Modify && existingCompensatorOwner != null)
                {
                    existingCompensatorOwner.OwnerId       = owner.OwnerId;
                    existingCompensatorOwner.CompensatorId = compensator.CompensatorId;
                    await _context.SaveChangesAsync();

                    return(existingCompensatorOwner);
                }
            }
            catch (DbUpdateException e)
            {
                _log.LogCritical($"Error occured while inserting CompensatorOwner with webUatId {compensatorOwnerForeign.WebUatId}, owner id {owner.OwnerId} and compensatorId {compensator.CompensatorId}");
                _log.LogCritical($"EntityWriteOption = {opt.ToString()}");
                _log.LogCritical($"{e.Message}");
                return(null);
            }

            return(null);
        }
Ejemplo n.º 3
0
        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);
        }