private void BuildGleasonInterface()
        {
            int patientProtocolId = int.Parse(PatientProtocolId);
            KeyValuePair <Pathology, BusinessObject>?gleasonPair = ProtocolMgmtUtil.GetGleasonPathologyRecord(patientProtocolId);

            if (gleasonPair.HasValue)
            {
                // get pair, determin type of pathology
                Pathology      path          = gleasonPair.Value.Key;
                BusinessObject pathProcedure = gleasonPair.Value.Value;
                // validation
                if (path.IsEmpty)
                {
                    return;
                }

                int    pathologyId = (int)path[Pathology.PathologyId];
                string pathType    = path[Pathology.PathSpecimenType].ToString();
                // manually set Path Fields
                PathDateText.Value     = path[Pathology.PathDateText].ToString();
                PathDate.Value         = path[Pathology.PathDate].ToString();
                PathSpecimenType.Value = pathType;
                // manuall set child records
                if (pathProcedure is ProstatectomyPathology)
                {
                    ProstatectomyPathology prostatectomy = pathProcedure as ProstatectomyPathology;
                    GleasonField1.Text   = prostatectomy[ProstatectomyPathology.PathGG1].ToString();
                    GleasonField2.Text   = prostatectomy[ProstatectomyPathology.PathGG2].ToString();
                    GleasonFieldSum.Text = prostatectomy[ProstatectomyPathology.PathGGS].ToString();
                }
                else if (pathProcedure is BiopsyProstatePathology)
                {
                    BiopsyProstatePathology biopsy = pathProcedure as BiopsyProstatePathology;
                    GleasonField1.Text   = biopsy[BiopsyProstatePathology.PathGG1].ToString();
                    GleasonField2.Text   = biopsy[BiopsyProstatePathology.PathGG2].ToString();
                    GleasonFieldSum.Text = biopsy[BiopsyProstatePathology.PathGGS].ToString();
                }
                else
                {
                    CICHelper.ClearCaisisInputControlValues(GleasonFields);
                }
            }
            else
            {
                CICHelper.ClearCaisisInputControlValues(GleasonFields);
            }
        }
        public override void Populate()
        {
            Procedure diagnosticProcedure   = GetBaselineBiopsy();
            int?      diagnosticProcedureId = null;

            if (diagnosticProcedure != null)
            {
                // set procedure
                diagnosticProcedureId = (int)diagnosticProcedure[Procedure.ProcedureId];
                ProcedureId.Value     = diagnosticProcedureId + "";
                Procedure procedure = new Procedure();
                procedure.Get(diagnosticProcedureId.Value);
                CICHelper.SetFieldValues(DiagnosticBiopsyFields.Controls, procedure);
                // set path stage grade
                PathologyStageGradeId.Value = "";
                Pathology pathology = BusinessObject.GetByFields <Pathology>(new Dictionary <string, object> {
                    { Pathology.PatientId, base.patientId }, { Pathology.ProcedureId, diagnosticProcedureId.Value }
                }).FirstOrDefault();
                if (pathology != null)
                {
                    int pathologyId = (int)pathology[Pathology.PathologyId];
                    PathologyId.Value = pathologyId + "";
                    CICHelper.SetFieldValues(DiagnosticBiopsyFields.Controls, pathology);
                    PathologyStageGrade stageGrade = BusinessObject.GetByParent <PathologyStageGrade>(pathologyId).FirstOrDefault();
                    PathologyStageGradeId.Value = stageGrade != null ? stageGrade[PathologyStageGrade.PathologyStageGradeId].ToString() : "";
                    if (stageGrade != null)
                    {
                        CICHelper.SetFieldValues(StageGradePanel.Controls, stageGrade);
                    }
                }
            }
            else
            {
                PathologyStageGradeId.Value = "";
            }

            // populate
            // static controls
            //            Control clearBtn = e.Item.FindControl("ClearBtn");
            //          Control lockImg = e.Item.FindControl("LockImage");
            //        clearBtn.Visible = false;
            //      lockImg.Visible = false;

            // get procedure
            int?procedureId = null;

            if (!string.IsNullOrEmpty(ProcedureId.Value))
            {
                procedureId = int.Parse(ProcedureId.Value);
            }
            // valida row
            if (procedureId.HasValue)
            {
                Procedure procedure = new Procedure();
                procedure.Get(procedureId.Value);
                CICHelper.SetFieldValues(this.Controls, procedure);
                ProcedureId.Value = procedureId.Value + "";
                // set button states
                bool isLocked = (procedure[Procedure.LockedBy] + "") != "";
                // lockImg.Visible = isLocked;

                // get pathology
                Pathology pathology = BusinessObject.GetByFields <Pathology>(new Dictionary <string, object>
                {
                    { Patient.PatientId, base.patientId },
                    { Pathology.ProcedureId, procedureId.Value }
                }).FirstOrDefault();
                if (pathology != null && pathology.PrimaryKeyHasValue)
                {
                    // populate pathology
                    int pathologyId = (int)pathology[Pathology.PathologyId];
                    CICHelper.SetFieldValues(this.Controls, pathology);
                    PathologyId.Value = pathologyId + "";
                    // populate path
                    BiopsyProstatePathology biopsyPath = BusinessObject.GetByParent <BiopsyProstatePathology>(pathologyId).FirstOrDefault();
                    if (biopsyPath != null && biopsyPath.PrimaryKeyHasValue)
                    {
                        CICHelper.SetFieldValues(this.Controls, biopsyPath);
                    }
                }
            }
            // blank row
            else
            {
                // clearBtn.Visible = true;
            }
            // set script
            string script = string.Format("calculateBxResult('{0}', '{1}', '{2}');", PathGG1.ClientID, PathGG2.ClientID, PathGGS.ClientID);

            PathGG1.Attributes["onchange"] = script;
            PathGG2.Attributes["onchange"] = script;
        }
        public override IEnumerable <BusinessObject> Save()
        {
            // save diag biopsy
            List <BusinessObject>             records          = new List <BusinessObject>();
            IEnumerable <ICaisisInputControl> stageGradeFields = CICHelper.GetCaisisInputControls(StageGradePanel);

            int?procedureId = null;

            if (!string.IsNullOrEmpty(ProcedureId.Value))
            {
                procedureId = int.Parse(ProcedureId.Value);
            }
            int?pathologyId = null;

            if (!string.IsNullOrEmpty(PathologyId.Value))
            {
                pathologyId = int.Parse(PathologyId.Value);
            }

            var inputs           = CICHelper.GetCaisisInputControls(this);
            var procFields       = CICHelper.GetInputControlsByTable <Procedure>(inputs);
            var pathFields       = CICHelper.GetInputControlsByTable <Pathology>(inputs);
            var biopsyPathFields = CICHelper.GetInputControlsByTable <BiopsyProstatePathology>(inputs);

            // validation
            if (CICHelper.InputControlsHaveValue(pathFields))
            {
                // step 1: create procedure
                Procedure procedure = new Procedure();
                if (procedureId.HasValue)
                {
                    procedure.Get(procedureId.Value);
                }
                else
                {
                    procedure[Procedure.PatientId] = base.patientId;
                }
                // copy field values
                procedure[Procedure.ProcName]     = PathSpecimenType.Value;
                procedure[Procedure.ProcDateText] = ProcDateText.Value;
                procedure[Procedure.ProcDate]     = ProcDate.Value;
                procedure[Procedure.ProcSite]     = PathSite.Value;
                // get special stamp
                procedure.AppendToUserName(GetEnteredBySuffix());
                procedure.Save();
                //base.SaveBizo(procedure);
                procedureId       = (int)procedure[Procedure.ProcedureId];
                ProcedureId.Value = procedureId + "";
                records.Add(procedure);

                // step 2: create path
                Pathology pathology = new Pathology();
                if (pathologyId.HasValue)
                {
                    pathology.Get(pathologyId.Value);
                }
                else
                {
                    pathology[Pathology.PatientId]    = base.patientId;
                    pathology[Pathology.ProcedureId]  = procedureId;
                    pathology[Pathology.PathDateText] = ProcDateText.Value;
                    pathology[Pathology.PathDate]     = ProcDate.Value;
                }
                pathology[Pathology.PathSpecimenType] = PathSpecimenType.Value;
                pathology[Pathology.PathSite]         = PathSite.Value;
                CICHelper.SetBOValues(this.Controls, pathology, base.patientId);
                base.SaveBizo(pathology);
                pathologyId       = (int)pathology[Pathology.PathologyId];
                PathologyId.Value = pathologyId + "";
                records.Add(pathology);

                // step 3: create biopsy path
                if (CICHelper.InputControlsHaveValue(biopsyPathFields))
                {
                    BiopsyProstatePathology biopsyPath = BusinessObject.GetByParent <BiopsyProstatePathology>(pathologyId.Value).FirstOrDefault();
                    // new
                    if (biopsyPath == null)
                    {
                        biopsyPath = new BiopsyProstatePathology();
                        biopsyPath[BiopsyProstatePathology.PathologyId] = pathologyId.Value;
                    }
                    CICHelper.SetBOValues(this.Controls, biopsyPath, pathologyId.Value);
                    base.SaveBizo(biopsyPath);
                }

                // step 4: save path stage grade
                if (CICHelper.InputControlsHaveValue(stageGradeFields) && !string.IsNullOrEmpty(PathStageT.Value))
                {
                    int?stageGradeId = null;
                    // load by pri key
                    if (!string.IsNullOrEmpty(PathologyStageGradeId.Value))
                    {
                        stageGradeId = int.Parse(PathologyStageGradeId.Value);
                    }
                    // get par key for save
                    else if (!string.IsNullOrEmpty(PathologyId.Value))
                    {
                        pathologyId = int.Parse(PathologyId.Value);
                    }
                    if (pathologyId.HasValue || stageGradeId.HasValue)
                    {
                        PathologyStageGrade stageGrade = base.SaveRecord <PathologyStageGrade>(StageGradePanel, pathologyId, stageGradeId);
                        PathologyStageGradeId.Value = stageGrade[PathologyStageGrade.PathologyStageGradeId] + "";
                        records.Add(stageGrade);
                    }
                }
            }

            //Populate();
            return(records);
        }
        public override IEnumerable <BusinessObject> Save()
        {
            List <BusinessObject> records       = new List <BusinessObject>();
            IEnumerable <Control> dirtyControls = base.GetDirtyInputControls(BiopsiesRptr);

            foreach (RepeaterItem dirtyRow in dirtyControls)
            {
                // key controls
                HiddenField procIdField = dirtyRow.FindControl("ProcedureId") as HiddenField;
                HiddenField pathIdField = dirtyRow.FindControl("PathologyId") as HiddenField;
                int?        procedureId = null;
                if (!string.IsNullOrEmpty(procIdField.Value))
                {
                    procedureId = int.Parse(procIdField.Value);
                }
                int?pathologyId = null;
                if (!string.IsNullOrEmpty(pathIdField.Value))
                {
                    pathologyId = int.Parse(pathIdField.Value);
                }

                var inputs           = CICHelper.GetCaisisInputControls(dirtyRow);
                var procFields       = CICHelper.GetInputControlsByTable <Procedure>(inputs);
                var pathFields       = CICHelper.GetInputControlsByTable <Pathology>(inputs);
                var biopsyPathFields = CICHelper.GetInputControlsByTable <BiopsyProstatePathology>(inputs);

                // special fields
                ICaisisInputControl pathType     = dirtyRow.FindControl("PathSpecimenType") as ICaisisInputControl;
                ICaisisInputControl procDateText = dirtyRow.FindControl("ProcDateText") as ICaisisInputControl;
                ICaisisInputControl procDate     = dirtyRow.FindControl("ProcDate") as ICaisisInputControl;
                ICaisisInputControl pathSite     = dirtyRow.FindControl("PathSite") as ICaisisInputControl;

                // validation
                if (CICHelper.InputControlsHaveValue(pathFields))
                {
                    // step 1: create procedure
                    Procedure procedure = new Procedure();
                    if (procedureId.HasValue)
                    {
                        procedure.Get(procedureId.Value);
                    }
                    else
                    {
                        procedure[Procedure.PatientId] = base.patientId;
                    }
                    // copy field values
                    procedure[Procedure.ProcName]     = pathType.Value;
                    procedure[Procedure.ProcSite]     = pathSite.Value;
                    procedure[Procedure.ProcDateText] = procDateText.Value;
                    procedure[Procedure.ProcDate]     = procDate.Value;

                    base.SaveBizo(procedure);
                    procedureId       = (int)procedure[Procedure.ProcedureId];
                    procIdField.Value = procedureId + "";
                    // step 2: create path
                    Pathology pathology = new Pathology();
                    if (pathologyId.HasValue)
                    {
                        pathology.Get(pathologyId.Value);
                    }
                    else
                    {
                        pathology[Pathology.PatientId]    = base.patientId;
                        pathology[Pathology.ProcedureId]  = procedureId;
                        pathology[Pathology.PathDateText] = procDateText.Value;
                        pathology[Pathology.PathDate]     = procDate.Value;
                    }
                    pathology[Pathology.PathSpecimenType] = pathType.Value;
                    pathology[Pathology.PathSite]         = pathSite.Value;
                    CICHelper.SetBOValues(dirtyRow.Controls, pathology, base.patientId);
                    base.SaveBizo(pathology);
                    pathologyId       = (int)pathology[Pathology.PathologyId];
                    pathIdField.Value = pathologyId + "";

                    // update biz list
                    records.Add(procedure);
                    records.Add(pathology);
                }
                // step 3: create biopsy path
                if (CICHelper.InputControlsHaveValue(biopsyPathFields) && pathologyId.HasValue)
                {
                    BiopsyProstatePathology biopsyPath = new BiopsyProstatePathology();
                    // load existing
                    biopsyPath.Get(pathologyId.Value);
                    // else create
                    if (biopsyPath.IsEmpty)
                    {
                        biopsyPath[BiopsyProstatePathology.PathologyId] = pathologyId;
                    }
                    CICHelper.SetBOValues(dirtyRow.Controls, biopsyPath, pathologyId.Value);
                    base.SaveBizo(biopsyPath);
                    records.Add(biopsyPath);
                }
            }
            //Populate();
            return(records.ToArray());
        }
        protected void PopulateRowValues(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                // get procedure
                Procedure procedure = e.Item.DataItem as Procedure;

                // key controls
                HiddenField procIdField = e.Item.FindControl("ProcedureId") as HiddenField;
                HiddenField pathIdField = e.Item.FindControl("PathologyId") as HiddenField;
                // static controls
                Control clearBtn = e.Item.FindControl("ClearBtn");
                Control lockImg  = e.Item.FindControl("LockImage");
                clearBtn.Visible = false;
                lockImg.Visible  = false;

                // validate real row
                if (procedure.PrimaryKeyHasValue)
                {
                    int procedureId = (int)procedure[Procedure.ProcedureId];
                    CICHelper.SetFieldValues(e.Item.Controls, procedure);
                    procIdField.Value = procedureId + "";
                    // set button states
                    bool isLocked = (procedure[Procedure.LockedBy] + "") != "";
                    lockImg.Visible = isLocked;

                    // get pathology
                    Pathology pathology = BusinessObject.GetByFields <Pathology>(new Dictionary <string, object>
                    {
                        { Patient.PatientId, base.patientId },
                        { Pathology.ProcedureId, procedureId }
                    }).FirstOrDefault();
                    if (pathology != null && pathology.PrimaryKeyHasValue)
                    {
                        // populate pathology
                        int pathologyId = (int)pathology[Pathology.PathologyId];
                        CICHelper.SetFieldValues(e.Item.Controls, pathology);
                        pathIdField.Value = pathologyId + "";
                        // populate path
                        BiopsyProstatePathology biopsyPath = BusinessObject.GetByParent <BiopsyProstatePathology>(pathologyId).FirstOrDefault();
                        if (biopsyPath != null && biopsyPath.PrimaryKeyHasValue)
                        {
                            CICHelper.SetFieldValues(e.Item.Controls, biopsyPath);
                        }
                    }
                    // no path: hide
                    else
                    {
                        e.Item.Visible = false;
                    }
                }
                // blank row
                else
                {
                    clearBtn.Visible = true;
                }
                // set script
                CaisisTextBox gg1    = e.Item.FindControl("PathGG1") as CaisisTextBox;
                CaisisTextBox gg2    = e.Item.FindControl("PathGG2") as CaisisTextBox;
                CaisisTextBox ggs    = e.Item.FindControl("PathGGS") as CaisisTextBox;
                string        script = string.Format("calculateBxResult('{0}', '{1}', '{2}');", gg1.ClientID, gg2.ClientID, ggs.ClientID);
                gg1.Attributes["onchange"] = script;
                gg2.Attributes["onchange"] = script;
            }
        }
        /// <summary>
        /// Inserts/Updates the relevent labs and gleason calculations
        /// </summary>
        private void SaveDetails()
        {
            // validation
            int             patientId         = int.Parse(BaseDecryptedPatientId);
            int             patientProtocolId = int.Parse(PatientProtocolId);
            PatientProtocol patientProtocol   = new PatientProtocol();

            patientProtocol.Get(patientProtocolId);

            var relatedLabTestIds = GetRelatedLabTestIds(patientProtocolId);

            // LABS (PSA)
            List <KeyValuePair <float, DateTime> > labResults = new List <KeyValuePair <float, DateTime> >();

            foreach (GridViewRow row in LabTestsGrid.Rows)
            {
                LabTest lab = new LabTest();
                // load???
                string currentRowId = LabTestsGrid.DataKeys[row.RowIndex][LabTest.LabTestId].ToString();
                if (!string.IsNullOrEmpty(currentRowId))
                {
                    lab.Get(int.Parse(currentRowId));
                }
                CICHelper.SetBOValues(row.Controls, lab, patientId);
                if (!lab.IsEmpty)
                {
                    lab[LabTest.LabTest_Field] = "PSA";
                    lab.Save();
                    int labTestId = (int)lab[LabTest.LabTestId];

                    string labTest        = lab[LabTest.LabTest_Field].ToString();
                    string labResult      = lab[LabTest.LabResult].ToString();
                    float  labResultValue = 0;
                    if (labTest.Equals("PSA") && float.TryParse(labResult, out labResultValue) && !lab.IsNull(LabTest.LabDate))
                    {
                        DateTime labDate = (DateTime)lab[LabTest.LabDate];
                        labResults.Add(new KeyValuePair <float, DateTime>(labResultValue, labDate));
                    }

                    // RELATED RECORD
                    if (!relatedLabTestIds.Contains(labTestId))
                    {
                        BOL.RelatedRecord relatedRecord = RelatedRecordController.CreateRelatedRecord(lab, patientProtocol);
                    }
                }
            }

            // calculate doubling time
            float?dbl = ProtocolMgmtUtil.GetPatientPSADoublingTime(patientProtocolId);

            if (dbl.HasValue)
            {
                PSADoublingTime.Text = dbl + " Months";
            }
            else
            {
                PSADoublingTime.Text = "N/A";
            }

            // GLEASON SCORE
            Pathology pathology = PatientProtocolController.GetPatientRelatedRecords <Pathology>(patientProtocolId).FirstOrDefault();

            // create new patholgy if needed
            if (!string.IsNullOrEmpty(PathSpecimenType.Value))
            {
                bool   isNewPathology = pathology == null;
                string pathType       = PathSpecimenType.Value;
                if (isNewPathology)
                {
                    pathology = new Pathology();
                    pathology[Pathology.PatientId] = patientId;
                }
                pathology[Pathology.PathSpecimenType] = pathType;
                pathology[Pathology.PathDateText]     = PathDateText.Value;
                pathology[Pathology.PathDate]         = PathDate.Value;
                pathology.Save();
                if (!pathology.IsEmpty)
                {
                    int pathologyId = (int)pathology[Pathology.PathologyId];
                    // create child record
                    if (pathType.Equals("Prostatectomy"))
                    {
                        var prostatectomy = BusinessObject.GetByParent <ProstatectomyPathology>(pathologyId).FirstOrDefault();
                        if (prostatectomy == null)
                        {
                            prostatectomy = new ProstatectomyPathology();
                            prostatectomy[ProstatectomyPathology.PathologyId] = pathologyId;
                        }
                        prostatectomy[ProstatectomyPathology.PathGG1] = GleasonField1.Text;
                        prostatectomy[ProstatectomyPathology.PathGG2] = GleasonField2.Text;
                        prostatectomy[ProstatectomyPathology.PathGGS] = GleasonFieldSum.Text;
                        prostatectomy.Save();
                    }
                    else if (pathType == "Biopsy")
                    {
                        var biopsy = BusinessObject.GetByParent <BiopsyProstatePathology>(pathologyId).FirstOrDefault();
                        if (biopsy == null)
                        {
                            biopsy = new BiopsyProstatePathology();
                            biopsy[BiopsyProstatePathology.PathologyId] = pathologyId;
                        }
                        biopsy[BiopsyProstatePathology.PathGG1] = GleasonField1.Text;
                        biopsy[BiopsyProstatePathology.PathGG2] = GleasonField2.Text;
                        biopsy[BiopsyProstatePathology.PathGGS] = GleasonFieldSum.Text;
                        biopsy.Save();
                    }
                    // create related record if needed
                    if (isNewPathology)
                    {
                        BOL.RelatedRecord relatedPathology = RelatedRecordController.CreateRelatedRecord(pathology, patientProtocol);
                    }
                }
            }

            // rebuild UI
            BuildInterfaces();
        }