Example #1
0
        private int?SaveForm(int?specimenAccessionId)
        {
            bool isNewAccession = !specimenAccessionId.HasValue;
            int  patientId      = int.Parse(BaseDecryptedPatientId);
            // udpate SpecimenAccession
            SpecimenAccession sa = new SpecimenAccession();

            // load
            if (specimenAccessionId.HasValue)
            {
                sa.Get(specimenAccessionId.Value);
            }
            CICHelper.SetBOValues(this.Controls, sa, patientId);
            // special case: needle
            PopulateNeedleBizField(sa);
            sa.Save();
            // validate accession data
            if (!sa.PrimaryKeyHasValue)
            {
                return(null);
            }
            specimenAccessionId = (int)sa[SpecimenAccession.SpecimenAccessionId];

            // special case: diagnostics for tissue
            if (QuerySpecimenType == QUERY_TISSUE && !string.IsNullOrEmpty(AccessionProcName.Text) && AccessionProcName.Text.StartsWith("Image-Guided", StringComparison.OrdinalIgnoreCase))
            {
                SaveDiagnostic(sa);
            }
            else if (specimenAccessionId.HasValue && !string.IsNullOrEmpty(DiagnosticIdField.Value))
            {
                int diagnosticId = int.Parse(DiagnosticIdField.Value);
                DeleteRelatedDiagnostic(specimenAccessionId.Value, diagnosticId);
            }

            /*
             *  SPECIMENS: for new accessions and single field udpates, force save/update
             */
            string selectedTissueSubType = Tissue_SpecimenSubType.Value;
            Dictionary <string, List <int?> > validation = new Dictionary <string, List <int?> >();

            foreach (GridViewRow row in SpecimensGrid.Rows)
            {
                int rowIndex = row.RowIndex;
                ICaisisInputControl specimenIdField = row.FindControl(Specimen.SpecimenId) as ICaisisInputControl;
                ICaisisInputControl refNumField     = row.FindControl(Specimen.SpecimenReferenceNumber) as ICaisisInputControl;
                ICaisisInputControl subTypeField    = row.FindControl(Specimen.SpecimenSubType) as ICaisisInputControl;

                if (QuerySpecimenType == QUERY_TISSUE)
                {
                    subTypeField.Value = selectedTissueSubType;
                }
                if (isNewAccession || forceSpecimenUpdate)
                {
                    // only for rows with a reference #
                    if (!string.IsNullOrEmpty(refNumField.Value) && !SpecimensGrid.DirtyRows.Contains(rowIndex))
                    {
                        SpecimensGrid.DirtyRows.Add(rowIndex);
                    }
                }
                // validation
                int?   specimenId = null;
                string specimenReferenceNumber = refNumField.Value;
                if (!string.IsNullOrEmpty(specimenReferenceNumber))
                {
                    if (!string.IsNullOrEmpty(specimenIdField.Value))
                    {
                        specimenId = int.Parse(specimenIdField.Value);
                    }
                    validation.Adjoin(specimenReferenceNumber, new List <int?>());
                    validation[specimenReferenceNumber].Add(specimenId);
                }
            }
            List <string> currentDuplicates  = new List <string>();
            List <string> externalDuplicates = new List <string>();

            // TODO: refactor
            Caisis.Data.Database db = new Data.Database();
            string db_table         = new Specimen().TableName;

            if (validation.Count() > 0)
            {
                // validate against siblings
                currentDuplicates.AddRange(validation.Where(a => a.Value.Count() > 1).Select(a => a.Key));
                if (currentDuplicates.Count() == 0)
                {
                    // validate against db
                    foreach (var pair in validation)
                    {
                        bool   specimenValid           = true;
                        string specimenReferenceNumber = pair.Key;
                        foreach (int?specimenId in pair.Value)
                        {
                            Dictionary <string, object> db_query = new Dictionary <string, object>()
                            {
                                { Specimen.SpecimenReferenceNumber, specimenReferenceNumber }
                            };
                            var specimens = Caisis.BOL.BusinessObject.GetByFields <Specimen>(db_query);
                            var found     = new List <int>();
                            db.Select(db_table, Specimen.SpecimenId, new string[] { Specimen.SpecimenId }, new string[0], db_query, (r) =>
                            {
                                int rowSpecimenId = r.GetInt32(r.GetOrdinal(Specimen.SpecimenId));
                                found.Add(rowSpecimenId);
                                return(true);
                            });
                            // OK, if doesn't exist in db or if current specimen already assigned to #
                            specimenValid = found.Count() == 0 || (specimenId.HasValue && found.Contains(specimenId.Value));
                        }
                        if (!specimenValid)
                        {
                            externalDuplicates.Add(specimenReferenceNumber);
                        }
                    }
                }
            }
            if (currentDuplicates.Count() > 0)
            {
                string message = "Unable to update the form. The following Specimen IDs were entered more than once. [" + string.Join(",", currentDuplicates.ToArray()) + "]";
                throw new ClientException(message);
            }
            else if (externalDuplicates.Count() > 0)
            {
                string message = "Unable to update the form. The following Specimen IDs already exist is the system. [" + string.Join(",", externalDuplicates.ToArray()) + "]";
                throw new ClientException(message);
            }
            // OK
            else
            {
                // save specimens
                SpecimensGrid.Save(specimenAccessionId.Value);

                // populate form
                this.PopulateForm(specimenAccessionId.Value);
            }

            return(specimenAccessionId);
        }