Ejemplo n.º 1
0
        public ActionResult EditPatientLabTest(PatientLabTestEditModel model)
        {
            ViewBag.MenuItem = CurrentMenuItem;

            var returnUrl = (TempData["returnUrl"] ?? string.Empty).ToString();

            ViewBag.ReturnUrl = returnUrl;

            var patientLabTest = unitOfWork.Repository <PatientLabTest>().Queryable().Include(plt => plt.Patient).Include(plt => plt.LabTest).Include(plt => plt.TestUnit).Single(plt => plt.Id == model.PatientLabTestId);

            if (patientLabTest == null)
            {
                ViewBag.Entity = "PatientLabTest";
                return(View("NotFound"));
            }

            if (model.TestDate < patientLabTest.Patient.DateOfBirth)
            {
                ModelState.AddModelError("TestDate", "Test Date should be after Date Of Birth");
            }

            if (ModelState.IsValid)
            {
                try
                {
                    var labTestUnit = unitOfWork.Repository <LabTestUnit>().Queryable().SingleOrDefault(lu => lu.Id == model.LabTestUnitId);

                    patientLabTest.TestDate       = model.TestDate;
                    patientLabTest.TestResult     = model.TestResult;
                    patientLabTest.LabValue       = model.LabValue;
                    patientLabTest.TestUnit       = labTestUnit;
                    patientLabTest.ReferenceLower = model.ReferenceLower;
                    patientLabTest.ReferenceUpper = model.ReferenceUpper;

                    if (model.CustomAttributes != null)
                    {
                        var patientLabTestExtended = (IExtendable)patientLabTest;
                        var customAttributes       = unitOfWork.Repository <CustomAttributeConfiguration>().Queryable().Where(ca => ca.ExtendableTypeName == typeof(PatientLabTest).Name).ToList();

                        for (int i = 0; i < model.CustomAttributes.Length; i++)
                        {
                            var attributeConfig = GetFrameworkCustomAttributeConfig(customAttributes, model.CustomAttributes[i].Name);

                            // If there is not custom attribute configured with this name, ignore.
                            if (attributeConfig == null)
                            {
                                continue;
                            }

                            try
                            {
                                if (attributeConfig.IsRequired && string.IsNullOrWhiteSpace(model.CustomAttributes[i].Value))
                                {
                                    ModelState.AddModelError(string.Format("CustomAttributes[{0}].Value", i), string.Format("{0} is required.", model.CustomAttributes[i].Name));
                                    continue;
                                }

                                switch (model.CustomAttributes[i].Type)
                                {
                                case "Numeric":
                                    decimal number = 0M;
                                    if (decimal.TryParse(model.CustomAttributes[i].Value, out number))
                                    {
                                        patientLabTestExtended.ValidateAndSetAttributeValue(attributeConfig, number, User.Identity.Name);
                                    }
                                    break;

                                case "Selection":
                                    Int32 selection = 0;
                                    if (Int32.TryParse(model.CustomAttributes[i].Value, out selection))
                                    {
                                        patientLabTestExtended.ValidateAndSetAttributeValue(attributeConfig, selection, User.Identity.Name);
                                    }
                                    break;

                                case "DateTime":
                                    DateTime parsedDate = DateTime.MinValue;
                                    if (DateTime.TryParse(model.CustomAttributes[i].Value, out parsedDate))
                                    {
                                        patientLabTestExtended.ValidateAndSetAttributeValue(attributeConfig, parsedDate, User.Identity.Name);
                                    }
                                    break;

                                case "String":
                                default:
                                    patientLabTestExtended.ValidateAndSetAttributeValue(attributeConfig, model.CustomAttributes[i].Value ?? string.Empty, User.Identity.Name);
                                    break;
                                }
                            }
                            catch (CustomAttributeValidationException ve)
                            {
                                ModelState.AddModelError(string.Format("CustomAttributes[{0}].Value", i), ve.Message);
                                continue;
                            }
                        }
                    }

                    if (ModelState.IsValid)
                    {
                        unitOfWork.Repository <PatientLabTest>().Update(patientLabTest);
                        unitOfWork.Complete();

                        HttpCookie cookie = new HttpCookie("PopUpMessage");
                        cookie.Value = "Test and procedure updated successfully";
                        Response.Cookies.Add(cookie);

                        //if (returnUrl != string.Empty)
                        //{
                        //    return Redirect(returnUrl);
                        //}
                        return(Redirect("/Patient/PatientView.aspx?pid=" + patientLabTest.Patient.Id.ToString()));
                    }
                }
                catch (DbEntityValidationException ex)
                {
                    var err = string.Empty;
                    foreach (var eve in ex.EntityValidationErrors)
                    {
                        foreach (var ve in eve.ValidationErrors)
                        {
                            err += String.Format("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage);
                        }
                    }
                    ModelState.AddModelError("PatientLabTestId", string.Format("Unable to update the Patient Condition: {0}", err));
                }
            }

            // Prepare custom attributes
            var cattributes = unitOfWork.Repository <CustomAttributeConfiguration>()
                              .Queryable()
                              .Where(ca => ca.ExtendableTypeName == typeof(PatientLabTest).Name)
                              .ToList();

            model.CustomAttributes = cattributes.Select(c => new CustomAttributeEditModel
            {
                Name            = c.AttributeKey,
                Detail          = c.AttributeDetail == null ? "" : "(" + c.AttributeDetail + ")",
                Type            = c.CustomAttributeType.ToString(),
                IsRequired      = c.IsRequired,
                StringMaxLength = c.StringMaxLength,
                NumericMinValue = c.NumericMinValue,
                NumericMaxValue = c.NumericMaxValue,
                PastDateOnly    = c.PastDateOnly,
                FutureDateOnly  = c.FutureDateOnly
            })
                                     .ToArray();

            var selectiondataRepository = unitOfWork.Repository <SelectionDataItem>();

            if (model.CustomAttributes != null)
            {
                foreach (var selectCustomAttribute in model.CustomAttributes.Where(c => c.Type == VPS.CustomAttributes.CustomAttributeType.Selection.ToString()))
                {
                    ViewData[selectCustomAttribute.Name] = selectiondataRepository
                                                           .Queryable()
                                                           .Where(sd => sd.AttributeKey == selectCustomAttribute.Name)
                                                           .Select(s => new SelectListItem
                    {
                        Value = s.SelectionKey,
                        Text  = s.Value
                    })
                                                           .ToArray();
                }
            }

            ViewBag.LabTests = unitOfWork.Repository <LabTest>()
                               .Queryable()
                               .Select(c => new SelectListItem
            {
                Value = c.Id.ToString(),
                Text  = c.Description
            })
                               .ToArray();

            var labTestUnits = new List <SelectListItem> {
                { new SelectListItem {
                      Value = "", Text = ""
                  } }
            };

            labTestUnits.AddRange(unitOfWork.Repository <LabTestUnit>()
                                  .Queryable()
                                  .OrderBy(c => c.Description)
                                  .Select(c => new SelectListItem
            {
                Value = c.Id.ToString(),
                Text  = c.Description
            })
                                  .ToArray());
            ViewBag.LabTestUnits = labTestUnits;

            var labResults = new List <SelectListItem> {
                { new SelectListItem {
                      Value = "", Text = ""
                  } }
            };

            labResults.AddRange(unitOfWork.Repository <LabResult>()
                                .Queryable()
                                .OrderBy(c => c.Description)
                                .Select(c => new SelectListItem
            {
                Value = c.Description,
                Text  = c.Description
            })
                                .ToArray());
            ViewBag.TestResults = labResults;

            TempData["returnUrl"] = returnUrl;

            return(View(model));
        }
Ejemplo n.º 2
0
        public ActionResult EditPatientLabTest(int id)
        {
            ViewBag.MenuItem = CurrentMenuItem;

            var returnUrl = (Request.UrlReferrer ?? (object)string.Empty).ToString();

            TempData["returnUrl"] = returnUrl;

            ViewBag.ReturnUrl = returnUrl;

            var patientLabTest = unitOfWork.Repository <PatientLabTest>()
                                 .Queryable()
                                 .Include(i => i.Patient)
                                 .Include(i => i.LabTest)
                                 .Include(i => i.TestUnit)
                                 .SingleOrDefault(p => p.Id == id);

            if (patientLabTest == null)
            {
                ViewBag.Entity = "Patient Lab Test";
                return(View("NotFound"));
            }

            var model = new PatientLabTestEditModel
            {
                PatientFullName  = patientLabTest.Patient.FullName,
                PatientLabTestId = patientLabTest.Id,
                LabTest          = patientLabTest.LabTest.Description,
                TestDate         = patientLabTest.TestDate,
                TestResult       = patientLabTest.TestResult,
                LabValue         = patientLabTest.LabValue,
                LabTestUnitId    = patientLabTest.TestUnit != null ?  patientLabTest.TestUnit.Id : 0,
                ReferenceLower   = patientLabTest.ReferenceLower,
                ReferenceUpper   = patientLabTest.ReferenceUpper
            };

            var labTestUnits = new List <SelectListItem> {
                { new SelectListItem {
                      Value = "", Text = ""
                  } }
            };

            labTestUnits.AddRange(unitOfWork.Repository <LabTestUnit>()
                                  .Queryable()
                                  .OrderBy(c => c.Description)
                                  .Select(c => new SelectListItem
            {
                Value = c.Id.ToString(),
                Text  = c.Description
            })
                                  .ToArray());
            ViewBag.LabTestUnits = labTestUnits;

            var customAttributes = unitOfWork.Repository <CustomAttributeConfiguration>()
                                   .Queryable()
                                   .Where(ca => ca.ExtendableTypeName == "PatientLabTest")
                                   .ToList();

            var extendable = (IExtendable)patientLabTest;

            model.CustomAttributes = customAttributes.Select(c => new CustomAttributeEditModel
            {
                Name            = c.AttributeKey,
                Detail          = c.AttributeDetail == null ? "" : "(" + c.AttributeDetail + ")",
                Type            = c.CustomAttributeType.ToString(),
                Value           = GetCustomAttributeVale(extendable, c),
                IsRequired      = c.IsRequired,
                StringMaxLength = c.StringMaxLength,
                NumericMinValue = c.NumericMinValue,
                NumericMaxValue = c.NumericMaxValue,
                PastDateOnly    = c.PastDateOnly,
                FutureDateOnly  = c.FutureDateOnly
            })
                                     .ToArray();

            var selectiondataRepository = unitOfWork.Repository <SelectionDataItem>();

            foreach (var selectCustomAttribute in customAttributes.Where(c => c.CustomAttributeType == VPS.CustomAttributes.CustomAttributeType.Selection))
            {
                ViewData[selectCustomAttribute.AttributeKey] = selectiondataRepository
                                                               .Queryable()
                                                               .Where(sd => sd.AttributeKey == selectCustomAttribute.AttributeKey)
                                                               .Select(s => new SelectListItem
                {
                    Value = s.SelectionKey,
                    Text  = s.Value
                })
                                                               .ToArray();
            }

            var labResults = new List <SelectListItem> {
                { new SelectListItem {
                      Value = "", Text = ""
                  } }
            };

            labResults.AddRange(unitOfWork.Repository <LabResult>()
                                .Queryable()
                                .OrderBy(c => c.Description)
                                .Select(c => new SelectListItem
            {
                Value = c.Description,
                Text  = c.Description
            })
                                .ToArray());
            ViewBag.TestResults = labResults;

            return(View(model));
        }