public Hashtable[] Patients() { conciergeEntities conciergeEntities; using (conciergeEntities = new conciergeEntities()) { conciergeEntities.Connection.Open(); Hashtable[] result = new Hashtable[(from p in conciergeEntities.patient select p).Count()]; int i = 0; foreach (var p in (from p in conciergeEntities.patient select p)) { result[i] = new Hashtable(); result[i]["first"] = p.first; result[i]["last"] = p.last; result[i]["dob"] = p.dob; result[i]["gender"] = p.gender; result[i]["emercency_contact"] = p.emergency_contact; i++; } conciergeEntities.SaveChanges(); return result; } }
public Hashtable UpdateDoctor(Hashtable values) { Hashtable result = new Hashtable(); List<Hashtable> doctorList = new List<Hashtable>(); result["status"] = "ok"; try { if(values["id"] == null) throw new Exception("Missing doctor id"); using (conciergeEntities ent = new conciergeEntities()) { ent.Connection.Open(); int id = (int)values["id"]; var query = from d in ent.doctor where d.id == id select d; if (query.Count() != 1) throw new Exception("Bad doctor id"); doctor doctor = query.First(); doctor.address1 = values["address1"] as string; doctor.address2 = values["address2"] as string; doctor.address3 = values["address3"] as string; doctor.city = values["city"] as string; doctor.contact_person = values["contact_person"] as string; doctor.country = values["country"] as string; doctor.email = values["email"] as string; doctor.fax = values["fax"] as string; doctor.firstname = values["firstname"] as string; doctor.lastname = values["lastname"] as string; doctor.locality1 = values["locality1"] as string; doctor.locality2 = values["locality2"] as string; doctor.postal_code = values["postal_code"] as string; doctor.telephone = values["telephone"] as string; ent.SaveChanges(); } } catch (Exception ex) { result["status"] = "error"; result["reason"] = Explain(ex); } return result; }
Hashtable RemoveFile_(Hashtable values,conciergeEntities ent) { Hashtable result = new Hashtable(); result["status"] = "ok"; try { int document_id = (int)result["document_id"]; var query = from doc in ent.document where doc.id == document_id select doc; if(query.Count() < 1) throw new Exception(string.Format("Trying to remove unknown file id {0}",document_id)); var query2 = from ds in ent.document_segment where ds.document_id == document_id select ds; foreach(document_segment ds in query2) { ent.document_segment.DeleteObject(ds); } ent.SaveChanges(); ent.document.DeleteObject(query.First()); ent.SaveChanges(); } catch (Exception ex) { result["status"] = "error"; result["reason"] = Explain(ex); } return result; }
// No, these paramters are not redundant. The s private Hashtable AddFile(Stream stream, conciergeEntities ent, string source) { Hashtable result = new Hashtable(); Hashtable result2; result["status"] = "ok"; try { string hash = FileHash(stream); result2 = GetServerDate(ent); if (result2["status"] as string != "ok") return result2; document doc = ent.document.CreateObject(); doc.path = source; doc.checksum = FileHash(stream); result2 = GetServerDate(ent); if (result2["status"] as string != "ok") return result2; DateTime dt = (DateTime)result2["date"]; doc.added_date = dt; ent.document.AddObject(doc); ent.SaveChanges(); int id = (from d in ent.document select d.id).Max(); result["file_id"] = id; doc = (from d in ent.document where d.id == id select d).First(); stream.Position = 0; int fullSegments = (int)(stream.Length / MaxDocumentSegmentSize); int leftOver = (int)(stream.Length % MaxDocumentSegmentSize); for (int iSegment = 0; iSegment < fullSegments; iSegment++) { byte[] buffer = new byte[MaxDocumentSegmentSize]; int position = iSegment * MaxDocumentSegmentSize; stream.Read(buffer, 0, MaxDocumentSegmentSize); document_segment segment = ent.document_segment.CreateObject(); segment.document_id = id; segment.data = buffer; segment.position = iSegment; ent.document_segment.AddObject(segment); } ent.SaveChanges(); if (leftOver > 0) { byte[] buffer = new byte[leftOver]; int position = (int)(stream.Length - leftOver); stream.Read(buffer, 0, leftOver); document_segment segment = ent.document_segment.CreateObject(); segment.document_id = id; segment.data = buffer; segment.position = fullSegments; ent.document_segment.AddObject(segment); } ent.SaveChanges(); } catch (Exception ex) { result["reason"] = Explain(ex); result["status"] = "error"; } return result; }
public Hashtable AddFile(Hashtable values) { Hashtable result = new Hashtable(); result["status"] = "ok"; try { using (conciergeEntities ent = new conciergeEntities()) { ent.Connection.Open(); using (DbTransaction txn = ent.Connection.BeginTransaction()) { /* if (values["name"] as string != "" && values["patient_id"] == null) { throw new Exception("name but not patient specified when adding file"); } if (values["name"] as string == "" && values["patient_id"] != null) { throw new Exception("patient but not name specified when adding file"); }*/ result = AddFile(values, ent); ent.SaveChanges(); if (result["status"] as string != "ok") { txn.Rollback(); return result; } int document_id = (int)result["file_id"]; /* if (values["patient_id"] != null) { int patient_id = (int)values["patient_id"]; var query = from p in ent.patient where p.id == patient_id select p; if (query.Count() == 0) throw new Exception("patient not found while adding file"); patient p2 = query.First(); if (p2.signature_document_id != null) throw new Exception("patient already has a signature"); p2.signature_document_id = (int)result["document_id"]; ent.SaveChanges(); } * */ string name = values["name"] as string ; if (name != "") { var query = from sd in ent.special_document where sd.name.Trim().ToLower() == name.Trim().ToLower() select sd; if (query.Count() > 0) throw new Exception("Special file already exists"); special_document sd2 = ent.special_document.CreateObject(); sd2.name = name; sd2.document_id = document_id; ent.special_document.AddObject(sd2); ent.SaveChanges(); } ent.SaveChanges(); txn.Commit(); } } } catch (Exception ex) { result["status"] = "error"; result["reason"] = Explain(ex); } return result; /* Hashtable result = new Hashtable(); result["status"] = "ok"; try { string name = ((values["name"] as string) ?? "").Trim(); string path = ((values["path"] as string) ?? "").Trim(); using (conciergeEntities ent = new conciergeEntities()) { ent.Connection.Open(); using (DbTransaction txn = ent.Connection.BeginTransaction()) { FileInfo fi = new FileInfo(path); if (fi == null) throw new Exception("Bad path"); int i = AddFile_(fi, ent); ent.SaveChanges(); if (name != string.Empty) { special_document sd = ent.special_document.CreateObject(); sd.document_id = i; sd.name = name; ent.special_document.AddObject(sd); } ent.SaveChanges(); txn.Commit(); } } } catch (Exception ex) { result["status"] = "error"; result["reason"] = Explain(ex); } return result;*/ }
public Hashtable AddActivities(List<Hashtable> items) { Hashtable result = new Hashtable(); result["status"] = "ok"; List<Hashtable> dataResults = new List<Hashtable>(); result["data"] = dataResults; string fileName = string.Empty; conciergeEntities conciergeEntities; using (conciergeEntities = new conciergeEntities()) { conciergeEntities.Connection.Open(); using (DbTransaction transaction = conciergeEntities.Connection.BeginTransaction()) { foreach (var item in items) { string specialty = (item["specialty"] as string).Trim().ToLower(); string target_name = (item["target_name"] as string).Trim().ToLower(); int patient_id = (int)item["patient_id"]; string fullFileName = item["path"] as string; FileInfo fileInfo = new FileInfo(fullFileName); fileName = fileInfo.Name; int doctor_id = (int)item["doctor_id"]; DateTime dt; Match m; dt = (DateTime)item["procedureDate"]; string procedure_name = item["procedure_name"] as string; string location_name = item["location_name"] as string; ; var dpQuery0 = from dp in conciergeEntities.doctor_patient where dp.patient_id == patient_id && dp.doctor_id == doctor_id select dp; doctor_patient doctor_patient; if (dpQuery0.Count() == 0) { doctor_patient = conciergeEntities.doctor_patient.CreateObject(); doctor_patient.doctor_id = doctor_id; doctor_patient.patient_id = patient_id; conciergeEntities.doctor_patient.AddObject(doctor_patient); conciergeEntities.SaveChanges(); } doctor_patient = dpQuery0.First(); int doctor_patient_id = doctor_patient.id; //int file_id = AddFile_(fileInfo, conciergeEntities); Hashtable r = AddFile(fileInfo, conciergeEntities); int file_id = (int)r["file_id"]; conciergeEntities.SaveChanges(); var dpQuery = from dp in conciergeEntities.doctor_patient where dp.doctor_id == doctor_id && dp.patient_id == patient_id select dp; var dsQuery = from ds in conciergeEntities.doctor_specialty where ds.doctor_id == doctor_id && ds.specialty_name.Trim().ToLower() == specialty select ds; doctor_specialty dr_spe; switch (dsQuery.Count()) { case 0: { dr_spe = conciergeEntities.CreateObject<doctor_specialty>(); dr_spe.specialty_name = specialty; dr_spe.doctor_id = doctor_id; conciergeEntities.doctor_specialty.AddObject(dr_spe); conciergeEntities.SaveChanges(); break; } case 1: { dr_spe = dsQuery.First(); break; } default: { throw new Exception("More than one doctor specialty relationship exists in the database"); } } int procedure_target_id; var ptQuery = from pt in conciergeEntities.procedure_target where pt.procedure_name.Trim().ToLower() == procedure_name && pt.target_name.Trim().ToLower() == target_name select pt; procedure_target_id = ptQuery.First().id; activity activity2 = conciergeEntities.activity.CreateObject(); activity2.procedure_specialty_id = (int)item["procedure_specialty_id"]; activity2.procedure_target_id = procedure_target_id; activity2.doctor_specialty_id = dr_spe.id; activity2.date = dt; activity2.doctor_patient_id = doctor_patient_id; activity2.location_name = location_name; activity2.document_id = file_id; //activity activity2 = activity.Createactivity(specialty_id,doctor_id,dt,patient_id,procedure_name,location_name,file_id); var foo = (from d in conciergeEntities.doctor where d.id == doctor_id select d).First(); //activity2.doctorReference.Attach(foo); //activity2.doctorReference.Load(); conciergeEntities.activity.AddObject(activity2); conciergeEntities.SaveChanges(); note note = conciergeEntities.note.CreateObject(); note.user = WindowsIdentity.GetCurrent().Name; note.text = "Added file from concierge directory\n"; foreach (string key in item.Keys) { note.text += string.Format("{0}: {1}\n", key, item[key]); } note.when = DateTime.Now; conciergeEntities.note.AddObject(note); conciergeEntities.SaveChanges(); if ((from doc_pat in conciergeEntities.doctor_patient where doc_pat.doctor_id == doctor_id && doc_pat.patient_id == patient_id select doc_pat).Count() < 1) { doctor_patient doc_pat = conciergeEntities.doctor_patient.CreateObject(); doc_pat.doctor_id = doctor_id; doc_pat.patient_id = patient_id; conciergeEntities.doctor_patient.AddObject(doc_pat); conciergeEntities.SaveChanges(); } try { conciergeEntities.SaveChanges(); conciergeEntities.Refresh(System.Data.Objects.RefreshMode.StoreWins, conciergeEntities.activity); } catch (Exception ) { throw; } Hashtable dataResultItem = new Hashtable(); dataResultItem["file_id"] = file_id; dataResults.Add(dataResultItem); } transaction.Commit(); } } return result; }
public Hashtable AddLocation(Hashtable values) { Hashtable result = new Hashtable(); result["status"] = "ok"; try { using (conciergeEntities ent = new conciergeEntities()) { location location = ent.location.CreateObject(); location.name = values["name"] as string; location.address1 = values["address1"] as string; location.address2 = values["address2"] as string; location.address3 = values["address3"] as string; location.city = values["city"] as string; location.contact_person = values["contact_person"] as string; location.country = values["country"] as string; location.email = values["email"] as string; location.fax = values["fax"] as string; location.locality1 = values["locality1"] as string; location.locality2 = values["locality2"] as string; location.postal_code = values["postal_code"] as string; location.telephone = values["telephone"] as string; ent.location.AddObject(location); ent.SaveChanges(); } } catch (Exception ex) { result["status"] = "error"; result["reason"] = Explain(ex); } return result; }
public Hashtable AddStamp(Hashtable values) { Hashtable result = new Hashtable(); result["status"] = "ok"; try { if (values["image"] == null) throw new Exception("image not specified when calling AddStamp"); Image image = values["image"] as Image; if (values["patient_id"] == null) throw new Exception("patient_id not specified when calling AddStamp"); int patient_id = (int)values["patient_id"]; using (conciergeEntities ent = new conciergeEntities()) { ent.Connection.Open(); using (DbTransaction txn = ent.Connection.BeginTransaction()) { //Hashtable values = new Hashtable(); MemoryStream ms = new MemoryStream(); image.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp); ms.Position = 0; result = AddFile(ms, ent, "SIGNATURE"); if (result["status"] as string != "ok") { txn.Rollback(); return result; } ent.SaveChanges(); int file_id = (int)result["file_id"]; patient_signature ps = ent.patient_signature.CreateObject(); ps.document_id = file_id; ps.patient_id = patient_id; ent.patient_signature.AddObject(ps); ent.SaveChanges(); txn.Commit(); } } } catch (Exception ex) { result["status"] = "error"; result["reason"] = Explain(ex); } return result; }
public void DeleteDocument(int id) { conciergeEntities conciergeEntities; using (conciergeEntities = new conciergeEntities()) { conciergeEntities.Connection.Open(); using (DbTransaction transaction = conciergeEntities.Connection.BeginTransaction()) { if ((from docu in conciergeEntities.document where docu.id == id select docu).Count() < 1) { throw new Exception("Document not found"); } var query = from docu in conciergeEntities.document join segm in conciergeEntities.document_segment on docu.id equals segm.document_id where docu.id == id select segm; foreach (document_segment segm in query) { conciergeEntities.document_segment.DeleteObject(segm); } conciergeEntities.SaveChanges(); var query2 = from acti in conciergeEntities.activity where acti.document_id == id select acti; foreach (activity acti in query2) { conciergeEntities.activity.DeleteObject(acti); } conciergeEntities.SaveChanges(); document doc = (from docu in conciergeEntities.document where docu.id == id select docu).First(); conciergeEntities.document.DeleteObject(doc); conciergeEntities.SaveChanges(); transaction.Commit(); } } }
// called by AddPatient(XElement) public void AddPatient(string firstName, string lastName,string dateOfBirth,string gender,string emergencyContact) { conciergeEntities conciergeEntities; using (conciergeEntities = new conciergeEntities()) { conciergeEntities.Connection.Open(); if ((from patient in conciergeEntities.patient where patient.first == firstName && patient.last == lastName select patient).Count() > 0) { throw new Exception("Patient already exists"); } patient p = conciergeEntities.patient.CreateObject(); p.first = firstName; p.last = lastName; p.dob = DateTime.Parse(dateOfBirth); p.gender = gender; p.emergency_contact = emergencyContact; conciergeEntities.patient.AddObject(p); conciergeEntities.SaveChanges(); } }