protected void PopulateProstatectomyFields(int?procedureId)
        {
            // static controls
            LockImage.Visible = false;

            // valida row
            if (procedureId.HasValue)
            {
                Procedure procedure = new Procedure();
                procedure.Get(procedureId.Value);
                CICHelper.SetFieldValues(ProstatectomyDetailsFields.Controls, procedure);
                ProcedureId.Value = procedureId.Value + "";
                // set button states
                bool isLocked = (procedure[Procedure.LockedBy] + "") != "";
                LockImage.Visible = isLocked;

                // lock fields
                if (isLocked)
                {
                    base.SetControlsLockState(ProstatectomyDetailsFields, true);
                }

                // get pathology
                Pathology pathology = GetPathology(procedureId.Value);
                if (pathology != null && pathology.PrimaryKeyHasValue)
                {
                    // populate pathology
                    int pathologyId = (int)pathology[Pathology.PathologyId];
                    CICHelper.SetFieldValues(ProstatectomyDetailsFields.Controls, pathology);
                    PathologyId.Value = pathologyId + "";
                    // populate path
                    ProstatectomyPathology prostatePath = BusinessObject.GetByParent <ProstatectomyPathology>(pathologyId).FirstOrDefault();
                    if (prostatePath != null && prostatePath.PrimaryKeyHasValue)
                    {
                        CICHelper.SetFieldValues(ProstatectomyDetailsFields.Controls, prostatePath);
                    }
                }
                // no path: hide
                else
                {
                    //e.Item.Visible = false;
                }
            }
            // set script
            string script = string.Format("calculateBxResult('{0}', '{1}', '{2}');", PathGG1.ClientID, PathGG2.ClientID, PathGGS.ClientID);

            PathGG1.Attributes["onchange"] = script;
            PathGG2.Attributes["onchange"] = script;
        }
        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);
            }
        }
        private IEnumerable <BusinessObject> SaveProstatectomy()
        {
            // save prostatectomies
            List <BusinessObject> records       = new List <BusinessObject>();
            IEnumerable <Control> dirtyControls = base.GetDirtyInputControls(ProstatectomyDetailsFields);
            var  inputs  = CICHelper.GetCaisisInputControls(ProstatectomyDetailsFields);
            bool isDirty = dirtyControls.Contains(ProstatectomyDetailsFields);

            if (isDirty)
            {
                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 procFields = CICHelper.GetInputControlsByTable <Procedure>(inputs);
                var pathFields = CICHelper.GetInputControlsByTable <Pathology>(inputs);
                var prostatectomyPathFields = CICHelper.GetInputControlsByTable <ProstatectomyPathology>(inputs);

                // create/update proc
                if (CICHelper.InputControlsHaveValue(procFields))
                {
                    // step 1: create procedure
                    Procedure procedure = new Procedure();
                    if (procedureId.HasValue)
                    {
                        procedure.Get(procedureId.Value);
                    }
                    else
                    {
                        procedure[Procedure.PatientId] = base.patientId;
                    }
                    CICHelper.SetBOValues(ProstatectomyDetailsFields.Controls, procedure, base.patientId);

                    // copy field values
                    procedure[Procedure.ProcName]     = ProcName.Value;
                    procedure[Procedure.ProcDateText] = ProcDateText.Value;
                    procedure[Procedure.ProcDate]     = ProcDate.Value;
                    // special case: special stamp to mark inital proc
                    //base.SaveBizo(procedure);
                    procedure.AppendToUserName(GetEnteredBySuffix());
                    procedure.Save();

                    procedureId       = (int)procedure[Procedure.ProcedureId];
                    ProcedureId.Value = procedureId + "";

                    // update biz list
                    records.Add(procedure);
                }
                // step 2: create/udpate path
                if (pathologyId.HasValue || (CICHelper.InputControlsHaveValue(pathFields) && procedureId.HasValue))
                {
                    Pathology pathology = new Pathology();
                    if (pathologyId.HasValue)
                    {
                        pathology.Get(pathologyId.Value);
                    }
                    else
                    {
                        pathology[Pathology.PatientId]        = base.patientId;
                        pathology[Pathology.ProcedureId]      = procedureId;
                        pathology[Pathology.PathSpecimenType] = ProcName.Value;
                        pathology[Pathology.PathDateText]     = ProcDateText.Value;
                        pathology[Pathology.PathDate]         = ProcDate.Value;
                        pathology[Pathology.PathDisease]      = "Prostate";
                    }
                    CICHelper.SetBOValues(ProstatectomyDetailsFields.Controls, pathology, base.patientId);
                    base.SaveBizo(pathology);
                    pathologyId       = (int)pathology[Pathology.PathologyId];
                    PathologyId.Value = pathologyId + "";
                    records.Add(pathology);
                }
                // step 3: create/udpate Prostatectomy Path
                if (CICHelper.InputControlsHaveValue(prostatectomyPathFields) && pathologyId.HasValue)
                {
                    ProstatectomyPathology prostatePath = new ProstatectomyPathology();
                    // load existing
                    prostatePath.Get(pathologyId.Value);
                    // else create
                    if (prostatePath.IsEmpty)
                    {
                        prostatePath[ProstatectomyPathology.PathologyId] = pathologyId;
                    }
                    CICHelper.SetBOValues(ProstatectomyDetailsFields.Controls, prostatePath, pathologyId.Value);
                    base.SaveBizo(prostatePath);
                    records.Add(prostatePath);
                }
            }
            // save grade details
            if (!string.IsNullOrEmpty(PathologyId.Value))
            {
                int pathologyId = int.Parse(PathologyId.Value);
                records.AddRange(SaveGradeDetails(pathologyId));
            }
            return(records);
        }
        /// <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();
        }