public static bool Put(DicomPatientData person) { string key = person.PatientKey; bool ok = true; using (var outStream = new MemoryStream()) { Serializer.Serialize <DicomPatientData>(outStream, person); ok = dbClientDicomPatients.Update(key, outStream.ToArray()); } if (ok) { dbClientUpdates1.InsertAutoIncrement(DB_DICOMPATIENTS, StringToolkit.GetBytes(key)); byte[] byteGps = dbClientGpsFiles.Get(key); if ((byteGps == null) || (byteGps.Length == 0)) { // it doesn't exist, insert it PatientGPSFileList gps = new PatientGPSFileList { EntryTimestamp = DateTime.Now.Ticks }; using (var outStream = new MemoryStream()) { Serializer.Serialize <PatientGPSFileList>(outStream, gps); ok &= dbClientGpsFiles.Create(key, outStream.ToArray()); } if (ok) { dbClientUpdates1.InsertAutoIncrement(DB_GPSFILES, StringToolkit.GetBytes(key)); } } return(ok); } return(false); }
public void ParseResponse(string findscu, string modality) { bool runFindscu = false; if (String.IsNullOrWhiteSpace(AET)) { // before trying the default FINDSCU, find out if there are AE titles in the DB IEnumerator <KeyVal> aetitles = dbClientDeviceAETitles.GetEnumerator(); while (aetitles.MoveNext()) { DicomDeviceAETitle aetitle = GetAETitle(Encoding.ASCII.GetString(aetitles.Current.Key)); if (aetitle != null && !String.IsNullOrWhiteSpace(aetitle.DeviceAETitle)) { AET = aetitle.DeviceAETitle; runFindscu |= RunFindscu(findscu, modality); if (runFindscu) { break; } } } } else { // call it with the given AE title runFindscu |= RunFindscu(findscu, modality); } if (!runFindscu) { // no AE titles in DB or the call to findscu simply failed // try the default AE title AET = CALLING_DEFAULT; runFindscu = RunFindscu(findscu, modality); } if (!runFindscu) { // if there is still an error with the default AE title Logger.WriteMsg(LogSeverity.Error, "Failed calling findscu with default AE title."); return; } List <DicomPatientData> persons = ParseXml("out.xml", out List <Patient> patients); Logger.WriteMsg(LogSeverity.Info, $"findscu retrieved {persons.Count} patients"); for (int i = 0; i < patients.Count; i++) { DicomPatientData p = persons[i]; Put(p); Put(patients[i]); } /* Read back * foreach (DicomPatientData p in persons) { * DicomPatientData person = (DicomPatientData)Get(p.PatientKey, false); * } * foreach (Patient p in patients) { * Patient patient = (Patient)Get(p.GetFallbackKey().ToUpper(), true); * } */ }
private List <DicomPatientData> ParseXml(string xmlfile, out List <Patient> patients) { var doc = new XmlDocument(); doc.Load(xmlfile); var dicomPatients = new List <DicomPatientData>(); patients = new List <Patient>(); XmlNodeList nodes = doc.DocumentElement.SelectNodes("/responses/data-set"); foreach (XmlNode node in nodes) { string patientFirstName = null, patientLastName = null, patientId = null, patientBirthDate = null, patientSex = null, characterSet = null; string transliteratedPatientFirstName = null, transliteratedPatientLastName = null; TimeParameter tp = null; TRICS.Planner.Model.Common.Gender gender = TRICS.Planner.Model.Common.Gender.Unknown; bool transliterationOk = false; foreach (XmlNode elem in node.SelectNodes("element")) { var el = elem.Attributes.GetNamedItem("name"); if (el.InnerText.StartsWith("PatientName")) { var patientName = elem.InnerText; var group1 = patientName.Split('=')[0]; string converted = GetConvertedString(group1); if (converted != null) { transliterationOk = true; var namePieces = group1.Split('^'); if (namePieces.Length == 1) { // just the last name is given patientLastName = namePieces[0]; } if (namePieces.Length >= 2) { patientLastName = namePieces[0]; patientFirstName = namePieces[1]; } var transliteratedPieces = converted.Split('^'); if (transliteratedPieces.Length == 1) { // just the last name is given transliteratedPatientLastName = transliteratedPieces[0]; } if (transliteratedPieces.Length >= 2) { transliteratedPatientLastName = transliteratedPieces[0]; transliteratedPatientFirstName = transliteratedPieces[1]; } } } else if (el.InnerText.StartsWith("PatientID")) { patientId = elem.InnerText; } else if (el.InnerText.StartsWith("PatientBirthDate")) { if (elem.InnerText.Length >= 8) { tp = new TimeParameter { Year = Int32.Parse(elem.InnerText.Substring(0, 4)), Month = Int32.Parse(elem.InnerText.Substring(4, 2)), Day = Int32.Parse(elem.InnerText.Substring(6, 2)) }; var year = Int32.Parse(elem.InnerText.Substring(0, 4)); var month = Int32.Parse(elem.InnerText.Substring(4, 2)); var day = Int32.Parse(elem.InnerText.Substring(6, 2)); patientBirthDate = day + "/" + month + "/" + year; } } else if (el.InnerText.StartsWith("PatientSex")) { if (String.IsNullOrWhiteSpace(elem.InnerText)) { gender = TRICS.Planner.Model.Common.Gender.Unknown; } else { if (elem.InnerText.StartsWith("M")) { gender = TRICS.Planner.Model.Common.Gender.Male; } else if (elem.InnerText.StartsWith("F")) { gender = TRICS.Planner.Model.Common.Gender.Female; } else { gender = TRICS.Planner.Model.Common.Gender.Any; } } patientSex = elem.InnerText; } else if (el.InnerText.StartsWith("SpecificCharacterSet")) { characterSet = elem.InnerText; } } if (transliterationOk) { // even if transliteration succeeds, check the character set if (!String.IsNullOrWhiteSpace(characterSet)) { // check if we support the first alphabet given string alphabet = characterSet.Split('\\')[0].Trim(); if (alphabet.Count() > 0) { if (!supportedAplhabets.Contains(alphabet)) { // alphabet not supported, so we reject this patient altogheter transliterationOk = false; } } // else it is the default ASCII } if (transliterationOk) { Patient patient = new Patient { ChartNumber = patientId, LastName = transliteratedPatientLastName, FirstName = transliteratedPatientFirstName, Birthday = tp, Gender = gender }; patient.Birthday.UserFormat = "dd/MM/yyyy"; patients.Add(patient); if (String.IsNullOrWhiteSpace(characterSet)) { // in case we don't have a SpecificCharacterSet from findscu // and the transliterated names don't coincide with the original ones, // we need to specify UTF8 as character set, since the default ASCII won't do if ( ( (transliteratedPatientFirstName != null) && !transliteratedPatientFirstName.Equals(patientFirstName, StringComparison.InvariantCultureIgnoreCase) ) || ( (transliteratedPatientLastName != null) && !transliteratedPatientLastName.Equals(patientLastName, StringComparison.InvariantCultureIgnoreCase) ) ) { characterSet = SupportedCharacterSets.UTF8; } } DicomPatientData person = new DicomPatientData { Identity = new DicomWorkItem { PatientLastName = patientLastName, PatientFirstName = patientFirstName, PatientID = patientId, PatientBirthDate = patientBirthDate, PatientSex = patientSex, SpecificCharacterSet = characterSet }, PatientKey = patient.GetFallbackKey().ToUpper() }; dicomPatients.Add(person); } else { Logger.WriteMsg(LogSeverity.Warning, $"Given character set {characterSet} not supported"); } } else { Logger.WriteMsg(LogSeverity.Warning, $"Can't transliterate patient with ID {patientId}"); } } return(dicomPatients); }