Exemple #1
0
        private void SaveParameterBound(string range, string type, decimal?minValue, decimal?maxValue, AdhocParameter adhocParameter)
        {
            ParameterBound APB = adhocParameter.ParameterBounds.FirstOrDefault();

            if (APB == null)
            {
                APB                = new ParameterBound();
                APB.IsEnforced     = true;
                APB.MaxValue       = maxValue;
                APB.MinValue       = minValue;
                APB.Type           = type;
                APB.Range          = range;
                APB.AdhocParameter = adhocParameter;
                adhocParameter.ParameterBounds.Add(APB);
            }
            else
            {
                APB.IsEnforced     = true;
                APB.MaxValue       = maxValue;
                APB.MinValue       = minValue;
                APB.Type           = type;
                APB.Range          = range;
                APB.AdhocParameter = adhocParameter;
            }
        }
        public ActionResult CreatePost(SystemCreateModel Model)
        {
            var trimmedName = Model.Name.Trim().ToLower();
            var nameExists  = context.SystemTypes.Any(t => t.Name.Trim().ToLower() == trimmedName);

            if (ModelState.IsValid && !nameExists)
            {
                var newSystem = new SystemType {
                    Name = Model.Name, IsActive = Model.IsActive
                };

                foreach (var p in Model.Parameters)
                {
                    var newParameter = new Parameter
                    {
                        Name      = p.Name,
                        Frequency = p.Frequency,
                        Type      = context.ParameterTypes.First(x => x.Id == p.ParameterTypeId),
                        Unit      = p.Unit,
                        Source    = p.Source,
                        Link      = p.Link,
                        Use       = p.Use,
                    };

                    foreach (var b in p.Bounds)
                    {
                        var site = context.Sites.FirstOrDefault(x => x.Id == b.SiteId);

                        var newBound = new ParameterBound
                        {
                            Type           = b.Type,
                            Range          = b.Range,
                            MinValue       = b.MinValue,
                            MaxValue       = b.MaxValue,
                            MinDescription = b.MinDescription,
                            MaxDescription = b.MaxDescription,
                            IsEnforced     = b.IsEnforced
//                            Site = site
                        };
                        newParameter.ParameterBounds.Add(newBound);
                    }

                    newSystem.Parameters.Add(newParameter);
                }

                context.SystemTypes.Add(newSystem);
                context.SaveChanges();

                TempData["CreateSuccess"] = Model.Name + " was created successfully";
                return(RedirectToAction("Index"));
            }

            if (nameExists)
            {
                ModelState.AddModelError("", String.Format("A system type with the name {0} already exists.  Please choose a unique site name.", Model.Name));
                ViewBag.Data           = Model;
                ViewBag.ParameterTypes = context.ParameterTypes.Select(x => new { Id = x.Id, Name = x.Name });
                ViewBag.Sites          = context.Sites.Select(x => new { Id = x.Id, Name = x.Name });
            }

            ViewBag.ParameterTypes = context.ParameterTypes.Select(x => new { Id = x.Id, Name = x.Name });
            return(View(Model));
        }
        public ActionResult EditPost(SystemCreateModel Model)
        {
            var trimmedName = Model.Name.Trim().ToLower();
            var nameExists  = context.SystemTypes.Any(t => t.Id != Model.Id && t.Name.Trim().ToLower() == trimmedName);

            if (ModelState.IsValid && !nameExists)
            {
                var systemType = context.SystemTypes.FirstOrDefault(x => x.Id == Model.Id);

                if (systemType == null)
                {
                    TempData["Error"] = "Invalid Id";
                    return(RedirectToAction("Index"));
                }

                systemType.Name     = Model.Name;
                systemType.IsActive = Model.IsActive;

                var ToRemove = systemType.Parameters.Where(x => !Model.Parameters.Any(y => y.Id == x.Id)).ToList();
                var ToUpdate = systemType.Parameters.Where(x => Model.Parameters.Any(y => y.Id == x.Id)).ToList();
                var ToAdd    = Model.Parameters.Where(x => !systemType.Parameters.Any(y => y.Id == x.Id)).ToList();

                //Remove parameters that no longer exist
                foreach (var p in ToRemove)
                {
                    systemType.Parameters.Remove(p);
                }

                //Update any existing parameters
                foreach (var p in ToUpdate)
                {
                    var NewData = Model.Parameters.First(x => x.Id == p.Id);

                    p.Name      = NewData.Name;
                    p.Frequency = NewData.Frequency;
                    p.Type      = context.ParameterTypes.First(x => x.Id == NewData.ParameterTypeId);
                    p.Unit      = NewData.Unit;
                    p.Source    = NewData.Source;
                    p.Link      = NewData.Link;
                    p.Use       = NewData.Use;

                    var ToRemoveBounds = p.ParameterBounds.Where(x => !NewData.Bounds.Any(y => y.Id == x.Id)).ToList();
                    var ToUpdateBounds = p.ParameterBounds.Where(x => NewData.Bounds.Any(y => y.Id == x.Id)).ToList();
                    var ToAddBounds    = NewData.Bounds.Where(x => !p.ParameterBounds.Any(y => y.Id == x.Id)).ToList();

                    //Remove bounds that no longer exist
                    foreach (var b in ToRemoveBounds)
                    {
                        p.ParameterBounds.Remove(b);
                    }

                    //Update any existing parameter bounds
                    foreach (var b in ToUpdateBounds)
                    {
                        var NewBoundData = NewData.Bounds.First(x => x.Id == b.Id);

                        var site = context.Sites.FirstOrDefault(x => x.Id == NewBoundData.SiteId);

                        b.Type           = NewBoundData.Type;
                        b.Range          = NewBoundData.Range;
                        b.MinValue       = NewBoundData.MinValue;
                        b.MaxValue       = NewBoundData.MaxValue;
                        b.MinDescription = NewBoundData.MinDescription;
                        b.MaxDescription = NewBoundData.MaxDescription;
                        b.IsEnforced     = NewBoundData.IsEnforced;

                        //var forceLoad = b.Site;
                        //b.Site = site;
                    }

                    //Add any new bounds
                    foreach (var b in ToAddBounds)
                    {
                        var site     = context.Sites.FirstOrDefault(x => x.Id == b.SiteId);
                        var newBound = new ParameterBound
                        {
                            Type           = b.Type,
                            Range          = b.Range,
                            MinValue       = b.MinValue,
                            MaxValue       = b.MaxValue,
                            MinDescription = b.MinDescription,
                            MaxDescription = b.MaxDescription,
                            IsEnforced     = b.IsEnforced
//                            Site = site
                        };
                        p.ParameterBounds.Add(newBound);
                    }
                }

                //Add any new parameters
                foreach (var p in ToAdd)
                {
                    var newParameter = new Parameter
                    {
                        Name      = p.Name,
                        Frequency = p.Frequency,
                        Type      = context.ParameterTypes.First(x => x.Id == p.ParameterTypeId),
                        Unit      = p.Unit,
                        Source    = p.Source,
                        Link      = p.Link,
                        Use       = p.Use
                    };

                    foreach (var b in p.Bounds)
                    {
                        var site     = context.Sites.FirstOrDefault(x => x.Id == b.SiteId);
                        var newBound = new ParameterBound
                        {
                            Type           = b.Type,
                            Range          = b.Range,
                            MinValue       = b.MinValue,
                            MaxValue       = b.MaxValue,
                            MinDescription = b.MinDescription,
                            MaxDescription = b.MaxDescription,
                            IsEnforced     = b.IsEnforced
//                            Site = site
                        };
                        newParameter.ParameterBounds.Add(newBound);
                    }

                    systemType.Parameters.Add(newParameter);
                }

                context.SaveChanges();

                TempData["CreateSuccess"] = Model.Name + " was updated successfully";
                return(RedirectToAction("Index"));
            }

            if (nameExists)
            {
                ModelState.AddModelError("", String.Format("A system type with the name {0} already exists.  Please choose a unique site name.", Model.Name));
                ViewBag.Data           = Model;
                ViewBag.ParameterTypes = context.ParameterTypes.Select(x => new { Id = x.Id, Name = x.Name });
                ViewBag.Sites          = context.Sites.Select(x => new { Id = x.Id, Name = x.Name });
                return(View());
            }

            return(Edit(Model.Id));
        }