Exemple #1
0
        private static void Save(IBusinessObject b, XmlServiceSettings settings)
        {
            if ((b is Procedure || b is OperatingRoomDetail) && !settings.EnableDuplicateRecords.Contains(b.TableName))
            {
                Dictionary <string, object> searchCriteria;

                if (b is Procedure)
                {
                    searchCriteria =
                        new Dictionary <string, object>()
                    {
                        { Procedure.PatientId, b[Procedure.PatientId] },
                        { Procedure.ProcSurgeon, b[Procedure.ProcSurgeon] },
                        { Procedure.ProcName, b[Procedure.ProcName] },
                        { Procedure.ProcDate, b[Procedure.ProcDate] },
                        { Procedure.ProcCPT_Code, b[Procedure.ProcCPT_Code] }
                    };
                }
                else
                {
                    searchCriteria =
                        new Dictionary <string, object>()
                    {
                        { OperatingRoomDetail.PatientId, b[OperatingRoomDetail.PatientId] },
                        { OperatingRoomDetail.OpDate, b[OperatingRoomDetail.OpDate] }
                    };
                }

                IEnumerable <IBusinessObject> existing = BusinessObject.GetByFields(b.TableName, searchCriteria);

                if (existing.Count() == 1)                 // if there's more than one, just insert
                {
                    IBusinessObject ex = existing.First();

                    foreach (string f in ex.FieldNames)
                    {
                        if (ExcludeField(b, f) || IsEmpty(b[f]))
                        {
                            b[f] = ex[f];
                        }
                    }
                }
            }

            b.Save();
        }
Exemple #2
0
        /// <summary>
        /// Migrates an eform, saving each record.
        /// </summary>
        /// <param name="patientId"></param>
        /// <param name="xml"></param>
        /// <param name="migrationErrorLog"></param>
        /// <param name="settings"></param>
        /// <returns>True on success, false if there were migration errors, in which case the migrationErrorLog will be non-null.</returns>
        public static bool MigrateEformXml(int patientId, string xml, out string migrationErrorLog, XmlServiceSettings settings)
        {
            int?     encounterid        = null; // will read it after saving encounter and propagate it to dependant objects
            int?     opdetailid         = null;
            int?     currentProcedureId = null; // used to handle pathology records; we can have multiple procedures
            DateTime?opdate             = null;
            string   opdatetext         = null;
            string   notes;
            string   notesTable;
            string   notesField;

            bool migrationErrors = false;

            migrationErrorLog = null;
            StringBuilder migrationErrorLogBuffer = new StringBuilder();

            List <IBusinessObject> ancestors = new List <IBusinessObject>();

            foreach (IBusinessObject bizo in ReadXml(xml, out notes, out notesTable, out notesField))
            {
                try
                {
                    bool childErrors = false;

                    if (bizo.HasField(Patient.PatientId))
                    {
                        bizo[Patient.PatientId] = patientId;
                    }

                    // allow more than one Encounter
                    if (!(bizo is Encounter) && bizo.HasField(Encounter.EncounterId) && encounterid != null)
                    {
                        bizo[Encounter.EncounterId] = encounterid;
                    }

                    if (bizo is Procedure && opdetailid != null)
                    {
                        bizo[Procedure.OperatingRoomDetailId] = opdetailid;

                        if (opdate != null && IsEmpty(bizo[Procedure.ProcDate]))
                        {
                            bizo[Procedure.ProcDate] = opdate.Value;
                        }

                        if (opdatetext != null && opdatetext != "" && IsEmpty(bizo[Procedure.ProcDateText]))
                        {
                            bizo[Procedure.ProcDateText] = opdatetext;
                        }
                    }

                    if (!(bizo is Procedure) &&
                        bizo.HasField(Procedure.ProcedureId) &&
                        currentProcedureId.HasValue
                        // only set if not already populated
                        && bizo.IsNull(Procedure.ProcedureId))
                    {
                        bizo[Procedure.ProcedureId] = currentProcedureId;
                    }

                    if (!(bizo is Procedure || bizo is OperatingRoomDetail || bizo is MedicalTherapy) &&
                        bizo.HasField(OperatingRoomDetail.OperatingRoomDetailId) &&
                        opdetailid.HasValue)
                    {
                        bizo[OperatingRoomDetail.OperatingRoomDetailId] = opdetailid;
                    }

                    if (bizo.TableName == notesTable)
                    {
                        //if (notesField != null && bizo.HasColumn(notesField) && notes != null)
                        if (notesField != null && bizo.HasField(notesField) && notes != null)
                        {
                            bizo[notesField] += notes;
                        }
                    }

                    //if (BOFactory.HasParentTable(bizo.Tablename) && bizo.ParentTablename != "Patients" && ancestors.Count > 0)
                    if (BusinessObject.HasParentTable(bizo.TableName) && bizo.ParentTableName != "Patients" && ancestors.Count > 0)
                    {
                        // parent key set: do nothing
                        if (!bizo.IsNull(bizo.ParentKeyName))
                        {
                            // do nothhing
                        }
                        else
                        {
                            int?parentKey = GetParentKey(ancestors, bizo);
                            if (parentKey.HasValue)
                            {
                                bizo[bizo.ParentKeyName] = parentKey.Value;
                            }
                            else
                            {
                                // migration error
                                migrationErrors = true;
                                childErrors     = true;
                                migrationErrorLogBuffer.AppendFormat("Did not attempt to insert {0} record because of a problem with the parent record.", bizo.TableName);
                                migrationErrorLogBuffer.AppendLine();
                            }
                        }
                    }

                    //BizObject _b = bizo as BizObject;
                    BusinessObject _b = bizo as BusinessObject;

                    if (_b != null)
                    {
                        //foreach (DataColumn c in _b.table.Columns)
                        //{
                        //    if (c.ColumnName.EndsWith("DataSource"))
                        //    {
                        //        bizo[c.ColumnName] = "EForm";
                        //    }
                        //}

                        foreach (string fieldname in _b.FieldNames)
                        {
                            if (fieldname.EndsWith("DataSource"))
                            {
                                bizo[fieldname] = "EForm";
                            }
                        }

                        _b.InsideEForm = true;
                    }

                    // Don't allow errors to stop the migration process. Append errors to the log and send to the admin.

                    try
                    {
                        if (!childErrors)
                        {
                            // bizo.Save();
                            Save(bizo, settings);
                        }
                    }
                    catch (Exception e)
                    {
                        migrationErrors = true;

                        migrationErrorLogBuffer.AppendFormat("Error inserting into {0} table.", bizo.TableName);
                        migrationErrorLogBuffer.AppendLine();
                        migrationErrorLogBuffer.AppendLine("Attempted to insert the following fields:");
                        DumpRecord(migrationErrorLogBuffer, _b);
                        migrationErrorLogBuffer.AppendLine();
                        migrationErrorLogBuffer.AppendLine("Error detail follows:");
                        migrationErrorLogBuffer.AppendLine(e.Message);
                        migrationErrorLogBuffer.AppendLine(e.StackTrace);
                    }

                    if (bizo is Encounter)
                    {
                        object oEncounterId = bizo[Encounter.EncounterId];
                        if (oEncounterId != null && oEncounterId != DBNull.Value)
                        {
                            encounterid = (int)oEncounterId;
                        }
                        else
                        {
                            migrationErrorLogBuffer.AppendLine("Could not insert the encounter. See above for details.");
                        }
                    }

                    if (bizo is OperatingRoomDetail)
                    {
                        object oOpDetailId = bizo[OperatingRoomDetail.OperatingRoomDetailId];

                        if (oOpDetailId != null && oOpDetailId != DBNull.Value)
                        {
                            opdetailid = (int)oOpDetailId;

                            object oDate     = bizo[OperatingRoomDetail.OpDate];
                            object oDateText = bizo[OperatingRoomDetail.OpDateText];

                            if (oDate != null && oDate != DBNull.Value)
                            {
                                opdate = (DateTime)oDate;
                            }

                            if (oDateText != null)
                            {
                                opdatetext = oDateText.ToString();
                            }
                        }
                        else
                        {
                            migrationErrorLogBuffer.AppendLine("Could not insert the operating room detail. See above for details.");
                        }
                    }

                    if (bizo is Procedure)
                    {
                        object oCurrentProcedureId = bizo[Procedure.ProcedureId];

                        if (oCurrentProcedureId != null && oCurrentProcedureId != DBNull.Value)
                        {
                            currentProcedureId = (int)oCurrentProcedureId;
                        }
                        else
                        {
                            migrationErrorLogBuffer.AppendLine("Could not insert the procedure. See above for details.");
                        }
                    }

                    Percolate(ancestors, bizo);
                }
                catch (Exception outerE)
                {
                    migrationErrors = true;

                    migrationErrorLogBuffer.AppendFormat("General error caught while migrating data for {0} table.", bizo.TableName);
                    migrationErrorLogBuffer.AppendLine();
                    migrationErrorLogBuffer.AppendLine("Error message follows:");
                    migrationErrorLogBuffer.AppendLine(outerE.Message);
                    migrationErrorLogBuffer.AppendLine(outerE.StackTrace);
                }
            }

            if (migrationErrors)
            {
                migrationErrorLogBuffer.AppendLine("see appended xml:");
                migrationErrorLogBuffer.AppendLine(xml);
                migrationErrorLog = migrationErrorLogBuffer.ToString();
                return(false);
            }
            else
            {
                return(true);
            }
        }