Exemple #1
0
        public IHttpActionResult SetInfusionPreference([FromBody] InfusionGlobalPreference value)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            try
            {
                service.InfusionPreference = value;
                return(StatusCode(HttpStatusCode.NoContent));
            }
            catch (NotSupportedException)
            {
                return(BadRequest());
            }
            catch (UnauthorizedAccessException)
            {
                return(StatusCode(HttpStatusCode.Forbidden));
            }
            catch (KeyNotFoundException)
            {
                return(NotFound());
            }
            catch (DuplicateKeyException)
            {
                ModelState.AddModelError(nameof(InfusionGlobalPreference.Name), nameof(DuplicateKeyException));
                return(BadRequest(ModelState));
            }
        }
Exemple #2
0
        public void Create(InfusionGlobalPreference value)
        {
            using (log.Activity(m => m($"Creating {nameof(InfusionGlobalPreference)} by {Thread.CurrentPrincipal?.Identity?.Name}")))
            {
                using (log.Activity(m => m("Authorization")))
                {
                    try
                    {
                        security.ValidateCreate(value);
                    }
                    catch (UnauthorizedAccessException)
                    {
                        log.Warn($"Authorization Denied");
                        throw;
                    }
                    catch (Exception e)
                    {
                        log.Error($"Authorization Error", e);
                        throw;
                    }
                }

                var entity = null as GlobalPreference;
                using (log.Activity(m => m("Create Entity")))
                {
                    try
                    {
                        entity = context.GlobalPreferences.Add(value);
                        context.SaveChanges();
                    }
                    catch (Exception e) when(e.HasDuplicateKeyNumber())
                    {
                        log.Warn($"Duplicate {nameof(InfusionGlobalPreference.Name)}:\"{value.Name}\"", e);
                        throw new DuplicateKeyException(value.Name);
                    }
                    catch (Exception e)
                    {
                        log.Error($"Update Error", e);
                        throw;
                    }
                }
                var newValue = entity.Filter();

                using (log.Activity(m => m("Emit Event")))
                {
                    try
                    {
                        emitter.OnCreated(newValue);
                    }
                    catch (Exception e)
                    {
                        log.Error($"Emit Event Error", e);
                        throw;
                    }
                }

                log.Info(m => m($"Created {nameof(InfusionGlobalPreference)}[{entity.Id}] by {Thread.CurrentPrincipal?.Identity?.Name}"));
            }
        }
Exemple #3
0
        public static InfusionSettings ConvertInfusionPreferencesToSettings(InfusionGlobalPreference infusionPreferences, string currentUserId)
        {
            if (infusionPreferences == null)
            {
                throw new ArgumentNullException($"InfusionPreferences can't be null");
            }

            var infusionSettings = new InfusionSettings
            {
                ToleranceValue       = infusionPreferences.ContainerTolerance,
                RecordRetainingHours = infusionPreferences.PreserveRecords,
                InfusionDrugFilters  = new List <InfusionDrugFilter>()
            };


            if (infusionPreferences.ExcludedInfusions == null || infusionPreferences.ExcludedInfusions.Count == 0)
            {
                return(infusionSettings);
            }

            var nullRecordCount =
                infusionPreferences.ExcludedInfusions.Count(x => string.IsNullOrEmpty(x.Name));

            if (nullRecordCount > 0)
            {
                throw new Exception($"ExcludedInfusions collection has null or empty InfusionSetting object");
            }

            foreach (var excludedInfusion in infusionPreferences?.ExcludedInfusions)
            {
                infusionSettings.InfusionDrugFilters.Add(new InfusionDrugFilter()
                {
                    Name = excludedInfusion.Name,
                    IsManuallyEntered    = excludedInfusion.AddedByUser,
                    LastModifiedBy       = currentUserId,
                    LastModifiedDateTime = DateTime.Now
                });
            }

            return(infusionSettings);
        }
Exemple #4
0
        public void Update(string key, InfusionGlobalPreference value)
        {
            using (log.Activity(m => m($"Update {nameof(InfusionGlobalPreference)}[{key}] by {Thread.CurrentPrincipal?.Identity?.Name}")))
            {
                using (log.Activity(m => m($"Validate {nameof(value)}")))
                {
                    if (key != value.Name)
                    {
                        //Cannot change primary key
                        log.Warn(l => l($"Cannot update unique key {nameof(InfusionGlobalPreference)}[{key}]"));
                        throw new NotSupportedException();
                    }
                }

                var entity = null as InfusionGlobalPreference;
                using (log.Activity(m => m($"Read {nameof(InfusionGlobalPreference)}[{key}]")))
                {
                    entity = context.GlobalPreferences.OfType <InfusionGlobalPreference>().SingleOrDefault(item => item.Name == key);
                    if (entity == null)
                    {
                        log.Warn($"{nameof(GlobalPreference)}[{key}] is not found");
                        throw new KeyNotFoundException();
                    }
                }

                using (log.Activity(m => m("Authorization")))
                {
                    try
                    {
                        security.ValidateUpdate(entity, value);
                    }
                    catch (UnauthorizedAccessException)
                    {
                        log.Warn($"Authorization Denied");
                        throw;
                    }
                    catch (Exception e)
                    {
                        log.Error($"Authorization Error", e);
                        throw;
                    }
                }

                var oldValue = entity.Filter();
                using (log.Activity(m => m("Update Entity")))
                {
                    try
                    {
                        entity.Name = value.Name;
                        //TODO: KB: Do entity by Entity
                        entity.Configurations = value.Configurations;
                        context.SaveChanges();
                    }
                    catch (Exception e) when(e.HasDuplicateKeyNumber())
                    {
                        log.Warn($"Duplicate {nameof(InfusionGlobalPreference.Name)}:\"{value.Name}\"", e);
                        throw new DuplicateKeyException(value.Name);
                    }
                    catch (Exception e)
                    {
                        log.Error($"Update Error", e);
                        throw;
                    }
                }
                var newValue = entity.Filter();

                using (log.Activity(m => m("Emit Event")))
                {
                    try
                    {
                        emitter.OnUpdated(newValue, oldValue);
                    }
                    catch (Exception e)
                    {
                        log.Error($"Emit Event Error", e);
                        throw;
                    }
                }

                log.Info(l => l($"Updated {nameof(InfusionGlobalPreference)}[{key}] by {Thread.CurrentPrincipal?.Identity?.Name}"));
            }
        }