extensions/net.sf.basedb.reggie/trunk/src/net/sf/basedb/reggie/servlet/BloodRegistrationServlet.java

Code
Comments
Other
Rev Date Author Line
1518 23 Jan 12 nicklas 1 package net.sf.basedb.reggie.servlet;
1518 23 Jan 12 nicklas 2
1518 23 Jan 12 nicklas 3 import java.io.IOException;
1518 23 Jan 12 nicklas 4 import java.util.Date;
1518 23 Jan 12 nicklas 5
1518 23 Jan 12 nicklas 6 import javax.servlet.ServletException;
1518 23 Jan 12 nicklas 7 import javax.servlet.http.HttpServlet;
1518 23 Jan 12 nicklas 8 import javax.servlet.http.HttpServletRequest;
1518 23 Jan 12 nicklas 9 import javax.servlet.http.HttpServletResponse;
1518 23 Jan 12 nicklas 10
7024 07 Feb 23 nicklas 11 import org.apache.commons.lang3.time.FastDateFormat;
1518 23 Jan 12 nicklas 12 import org.json.simple.JSONArray;
1518 23 Jan 12 nicklas 13 import org.json.simple.JSONObject;
1518 23 Jan 12 nicklas 14
1736 16 Nov 12 nicklas 15 import net.sf.basedb.core.BioMaterialEvent;
1518 23 Jan 12 nicklas 16 import net.sf.basedb.core.BioSource;
1518 23 Jan 12 nicklas 17 import net.sf.basedb.core.DbControl;
1518 23 Jan 12 nicklas 18 import net.sf.basedb.core.Sample;
1518 23 Jan 12 nicklas 19 import net.sf.basedb.core.SessionControl;
2598 22 Aug 14 nicklas 20 import net.sf.basedb.reggie.JsonUtil;
1518 23 Jan 12 nicklas 21 import net.sf.basedb.reggie.Reggie;
1614 24 Apr 12 nicklas 22 import net.sf.basedb.reggie.Site;
5387 26 Apr 19 nicklas 23 import net.sf.basedb.reggie.activity.ActivityDef;
1767 04 Dec 12 nicklas 24 import net.sf.basedb.reggie.converter.StringToDateConverter;
3059 19 Dec 14 nicklas 25 import net.sf.basedb.reggie.counter.CounterService;
1610 23 Apr 12 nicklas 26 import net.sf.basedb.reggie.dao.Annotationtype;
1518 23 Jan 12 nicklas 27 import net.sf.basedb.reggie.dao.Blood;
1518 23 Jan 12 nicklas 28 import net.sf.basedb.reggie.dao.Case;
1524 25 Jan 12 nicklas 29 import net.sf.basedb.reggie.dao.Consent;
1518 23 Jan 12 nicklas 30 import net.sf.basedb.reggie.dao.Patient;
4900 10 Jul 18 nicklas 31 import net.sf.basedb.reggie.dao.ReggieItem;
2161 09 Dec 13 nicklas 32 import net.sf.basedb.reggie.dao.ReggieRole;
1518 23 Jan 12 nicklas 33 import net.sf.basedb.reggie.dao.Subtype;
1614 24 Apr 12 nicklas 34 import net.sf.basedb.util.Values;
1518 23 Jan 12 nicklas 35 import net.sf.basedb.util.error.ThrowableUtil;
1518 23 Jan 12 nicklas 36
1518 23 Jan 12 nicklas 37
1518 23 Jan 12 nicklas 38 public class BloodRegistrationServlet 
1518 23 Jan 12 nicklas 39   extends HttpServlet 
1518 23 Jan 12 nicklas 40 {
1518 23 Jan 12 nicklas 41
1518 23 Jan 12 nicklas 42   private static final long serialVersionUID = 8770173686061080429L;
1518 23 Jan 12 nicklas 43
1518 23 Jan 12 nicklas 44   public BloodRegistrationServlet()
1518 23 Jan 12 nicklas 45   {}
1518 23 Jan 12 nicklas 46
1518 23 Jan 12 nicklas 47   @Override
1518 23 Jan 12 nicklas 48   protected void doGet(HttpServletRequest req, HttpServletResponse resp)
1518 23 Jan 12 nicklas 49     throws ServletException, IOException 
1518 23 Jan 12 nicklas 50   {
1518 23 Jan 12 nicklas 51     String cmd = req.getParameter("cmd");
2598 22 Aug 14 nicklas 52     JsonUtil.setJsonResponseHeaders(resp);
1518 23 Jan 12 nicklas 53     
1518 23 Jan 12 nicklas 54     JSONObject json = new JSONObject();
1518 23 Jan 12 nicklas 55     json.put("status", "ok");
1518 23 Jan 12 nicklas 56   
3975 26 May 16 nicklas 57     final SessionControl sc = Reggie.getSessionControl(req);
1518 23 Jan 12 nicklas 58     DbControl dc = null;
1518 23 Jan 12 nicklas 59     try
1518 23 Jan 12 nicklas 60     {
1518 23 Jan 12 nicklas 61       if ("GetBloodInfo".equals(cmd))
1518 23 Jan 12 nicklas 62       {
1518 23 Jan 12 nicklas 63         /*
1518 23 Jan 12 nicklas 64           Load information about a single blood case when given the case name/barcode
1518 23 Jan 12 nicklas 65           If a case is found we will load annotations, and information about the patient 
1518 23 Jan 12 nicklas 66           it is associated with.
1518 23 Jan 12 nicklas 67         */
6329 14 Jun 21 nicklas 68         dc = sc.newDbControl(":Blood referral form registration");
1518 23 Jan 12 nicklas 69         
1524 25 Jan 12 nicklas 70         // Find blood + case by case name (blood name = case name + '.b' suffix)
6277 03 Jun 21 nicklas 71         String originalCaseName = req.getParameter("caseName");
6277 03 Jun 21 nicklas 72         String caseName = stripPrefix(originalCaseName, "K");
1614 24 Apr 12 nicklas 73         Site site = Site.findByCaseName(caseName);
2162 09 Dec 13 nicklas 74         json.put("siteInfo", site.asJSONObject());
2162 09 Dec 13 nicklas 75         
1623 26 Apr 12 nicklas 76         Blood blood = Blood.findByCaseName(dc, caseName, site.useCaseSuffixForPreNeoForms());
1518 23 Jan 12 nicklas 77         Patient patient = null;
1518 23 Jan 12 nicklas 78         JSONObject jsonBlood = null;
1518 23 Jan 12 nicklas 79         
1518 23 Jan 12 nicklas 80         if (blood != null)
1518 23 Jan 12 nicklas 81         {
1518 23 Jan 12 nicklas 82           // Check that the registration is not prohibited
1518 23 Jan 12 nicklas 83           blood.verifyConsent(dc, null);
1518 23 Jan 12 nicklas 84           
1518 23 Jan 12 nicklas 85           // ... blood annotations
1610 23 Apr 12 nicklas 86           blood.loadAnnotations(dc, "samplingDate", Annotationtype.BLOOD_SAMPLING_DATETIME, Reggie.CONVERTER_DATETIME_TO_STRING);
1610 23 Apr 12 nicklas 87           blood.loadAnnotations(dc, "freezerDate", Annotationtype.BLOOD_FREEZER_DATETIME, Reggie.CONVERTER_DATETIME_TO_STRING);
1610 23 Apr 12 nicklas 88           blood.loadAnnotations(dc, "serum", Annotationtype.BLOOD_SERUM, null);
1614 24 Apr 12 nicklas 89           blood.loadAnnotations(dc, "bloodSample", Annotationtype.BLOOD_SAMPLE, null);
1775 11 Dec 12 olle 90           blood.loadAnnotations(dc, "otherPathNote", Annotationtype.OTHER_PATH_NOTE, null);
1518 23 Jan 12 nicklas 91
1518 23 Jan 12 nicklas 92           // Wrap what we have so far up into JSON objects
1518 23 Jan 12 nicklas 93           jsonBlood = blood.asJSONObject();
1518 23 Jan 12 nicklas 94
1518 23 Jan 12 nicklas 95           // Load the patient associated with the case
1518 23 Jan 12 nicklas 96           patient = Patient.findByBlood(dc, blood);
1518 23 Jan 12 nicklas 97         }
1518 23 Jan 12 nicklas 98         else
1518 23 Jan 12 nicklas 99         {
1614 24 Apr 12 nicklas 100           // Could not find blood -- generate next name after stripping suffix
1614 24 Apr 12 nicklas 101           if (caseName.length() > 7) caseName = caseName.substring(0, 7);
4900 10 Jul 18 nicklas 102           String bloodName = Blood.getNextBloodName(dc, caseName);
1614 24 Apr 12 nicklas 103           
1622 25 Apr 12 nicklas 104           // Try to find other items related to this case so that we
1622 25 Apr 12 nicklas 105           // may find a patient and consent-related information
1622 25 Apr 12 nicklas 106
1622 25 Apr 12 nicklas 107           // First, check if a case can be found
1622 25 Apr 12 nicklas 108           Case theCase = Case.findByName(dc, caseName);
1622 25 Apr 12 nicklas 109           if (theCase != null)
1622 25 Apr 12 nicklas 110           {
1622 25 Apr 12 nicklas 111             // Check that the registration is not prohibited
1622 25 Apr 12 nicklas 112             theCase.verifyConsent(dc, null);
1622 25 Apr 12 nicklas 113             patient = Patient.findByCase(dc, theCase);
1622 25 Apr 12 nicklas 114             Consent.loadConsentAnnotations(dc, theCase);
1622 25 Apr 12 nicklas 115             json.put("consentInfo", theCase.asJSONObject());
1622 25 Apr 12 nicklas 116           }
1622 25 Apr 12 nicklas 117           
1622 25 Apr 12 nicklas 118           // Second, check if another blood sample can be found
1614 24 Apr 12 nicklas 119           if (!bloodName.endsWith("b"))
1614 24 Apr 12 nicklas 120           {
1622 25 Apr 12 nicklas 121             // If the auto-generated blood name has a numeric suffix 
1622 25 Apr 12 nicklas 122             // we should be able to find a patient by looking for the '.b' blood sample
1622 25 Apr 12 nicklas 123             Blood otherBlood = Blood.getByName(dc, caseName + ".b");
1622 25 Apr 12 nicklas 124             if (otherBlood != null) 
1622 25 Apr 12 nicklas 125             {
1622 25 Apr 12 nicklas 126               if (patient == null) patient = Patient.findByBlood(dc, otherBlood);
1622 25 Apr 12 nicklas 127               if (!json.containsKey("consentInfo"))
1622 25 Apr 12 nicklas 128               {
1622 25 Apr 12 nicklas 129                 Consent.loadConsentAnnotations(dc, otherBlood);
1622 25 Apr 12 nicklas 130                 json.put("consentInfo", otherBlood.asJSONObject());
1622 25 Apr 12 nicklas 131               }
1622 25 Apr 12 nicklas 132             }
1614 24 Apr 12 nicklas 133           }
1614 24 Apr 12 nicklas 134           
1518 23 Jan 12 nicklas 135           jsonBlood = new JSONObject();
1614 24 Apr 12 nicklas 136           jsonBlood.put("name", bloodName);
1524 25 Jan 12 nicklas 137           
1524 25 Jan 12 nicklas 138         }
6277 03 Jun 21 nicklas 139         jsonBlood.put("originalCaseName", originalCaseName);
1524 25 Jan 12 nicklas 140         json.put("bloodInfo", jsonBlood);
1524 25 Jan 12 nicklas 141         
1518 23 Jan 12 nicklas 142         // Connect the patient information if we have any
1518 23 Jan 12 nicklas 143         if (patient != null)
1518 23 Jan 12 nicklas 144         {
1518 23 Jan 12 nicklas 145           patient.loadDefaultAnnotations(dc);
1524 25 Jan 12 nicklas 146           json.put("patientInfo", patient.asJSONObject());
1518 23 Jan 12 nicklas 147         }
1518 23 Jan 12 nicklas 148         
1518 23 Jan 12 nicklas 149       }
1518 23 Jan 12 nicklas 150     }
1518 23 Jan 12 nicklas 151     catch (Throwable t)
1518 23 Jan 12 nicklas 152     {
1518 23 Jan 12 nicklas 153       t.printStackTrace();
1518 23 Jan 12 nicklas 154       json.clear();
1518 23 Jan 12 nicklas 155       json.put("status", "error");
1518 23 Jan 12 nicklas 156       json.put("message", t.getMessage());
1518 23 Jan 12 nicklas 157       json.put("stacktrace", ThrowableUtil.stackTraceToString(t));
1518 23 Jan 12 nicklas 158     }
1518 23 Jan 12 nicklas 159     finally
1518 23 Jan 12 nicklas 160     {
1518 23 Jan 12 nicklas 161       if (dc != null) dc.close();
1518 23 Jan 12 nicklas 162       json.writeJSONString(resp.getWriter());
1518 23 Jan 12 nicklas 163     }
1518 23 Jan 12 nicklas 164     
1518 23 Jan 12 nicklas 165     
1518 23 Jan 12 nicklas 166   }
1518 23 Jan 12 nicklas 167
1518 23 Jan 12 nicklas 168   @Override
1518 23 Jan 12 nicklas 169   protected void doPost(HttpServletRequest req, HttpServletResponse resp)
1518 23 Jan 12 nicklas 170     throws ServletException, IOException 
1518 23 Jan 12 nicklas 171   {
1518 23 Jan 12 nicklas 172     String ID = req.getParameter("ID");
1518 23 Jan 12 nicklas 173     String cmd = req.getParameter("cmd");
2598 22 Aug 14 nicklas 174     JsonUtil.setJsonResponseHeaders(resp);
1518 23 Jan 12 nicklas 175     
1518 23 Jan 12 nicklas 176     JSONObject json = new JSONObject();
1518 23 Jan 12 nicklas 177     json.put("status", "ok");
1518 23 Jan 12 nicklas 178     
1518 23 Jan 12 nicklas 179     JSONArray jsonMessages = new JSONArray();
1518 23 Jan 12 nicklas 180   
3975 26 May 16 nicklas 181     final SessionControl sc = Reggie.getSessionControl(req);
1518 23 Jan 12 nicklas 182     DbControl dc = null;
1518 23 Jan 12 nicklas 183     try
1518 23 Jan 12 nicklas 184     {
1518 23 Jan 12 nicklas 185       if ("CreateBlood".equals(cmd))
1518 23 Jan 12 nicklas 186       {
6329 14 Jun 21 nicklas 187         dc = sc.newDbControl(":Blood referral form registration");
2161 09 Dec 13 nicklas 188
2161 09 Dec 13 nicklas 189         ReggieRole.checkPermission(dc, "'" + cmd + "' wizard", ReggieRole.PATIENT_CURATOR, ReggieRole.ADMINISTRATOR);
2161 09 Dec 13 nicklas 190
3752 17 Feb 16 nicklas 191         JSONObject jsonReq = JsonUtil.parseRequest(req);
1518 23 Jan 12 nicklas 192         JSONObject jsonPat = (JSONObject)jsonReq.get("patientInfo");
1518 23 Jan 12 nicklas 193         JSONObject jsonBlood = (JSONObject)jsonReq.get("bloodInfo");
1518 23 Jan 12 nicklas 194         
1518 23 Jan 12 nicklas 195         BioSource patient = getOrCreatePatient(dc, jsonPat, jsonMessages);
1524 25 Jan 12 nicklas 196                   
1524 25 Jan 12 nicklas 197         // Load/create blood
1518 23 Jan 12 nicklas 198         String bloodName = (String)jsonBlood.get("name");
1524 25 Jan 12 nicklas 199         Sample blood = Sample.getNew(dc);
1518 23 Jan 12 nicklas 200         blood.setItemSubtype(Subtype.BLOOD.load(dc));
4900 10 Jul 18 nicklas 201         blood.setName(ReggieItem.ensureNonExistingItem(dc, Subtype.BLOOD, bloodName));
4902 10 Jul 18 nicklas 202         blood.setExternalId(Blood.getNextExternalId(dc));
1736 16 Nov 12 nicklas 203         BioMaterialEvent creationEvent = blood.getCreationEvent();
1736 16 Nov 12 nicklas 204         creationEvent.setSource(patient);
1524 25 Jan 12 nicklas 205         dc.saveItem(blood);
1518 23 Jan 12 nicklas 206
1518 23 Jan 12 nicklas 207         // Annotations
1524 25 Jan 12 nicklas 208         Date samplingDate = Reggie.CONVERTER_STRING_TO_DATETIME.convert((String)jsonBlood.get("samplingDate"));
1524 25 Jan 12 nicklas 209         Date freezerDate = Reggie.CONVERTER_STRING_TO_DATETIME.convert((String)jsonBlood.get("freezerDate"));
1614 24 Apr 12 nicklas 210         String bloodSample = Values.getStringOrNull((String)jsonBlood.get("bloodSample"));
1775 11 Dec 12 olle 211         String otherPathNote = Values.getStringOrNull((String)jsonBlood.get("otherPathNote"));
1610 23 Apr 12 nicklas 212         
1736 16 Nov 12 nicklas 213         creationEvent.setEventDate(samplingDate);
1736 16 Nov 12 nicklas 214         Annotationtype.BLOOD_SAMPLING_DATETIME.setAnnotationValue(dc, blood, samplingDate);
1736 16 Nov 12 nicklas 215         Annotationtype.BLOOD_FREEZER_DATETIME.setAnnotationValue(dc, blood, freezerDate);
1610 23 Apr 12 nicklas 216         Annotationtype.BLOOD_SERUM.setAnnotationValue(dc, blood, jsonBlood.get("serum"));
1614 24 Apr 12 nicklas 217         Annotationtype.BLOOD_SAMPLE.setAnnotationValue(dc, blood, bloodSample);
1775 11 Dec 12 olle 218         Annotationtype.OTHER_PATH_NOTE.setAnnotationValue(dc, blood, otherPathNote);
6277 03 Jun 21 nicklas 219         Annotationtype.BD47_ID.setAnnotationValue(dc, blood, jsonBlood.get("originalCaseName"));
1518 23 Jan 12 nicklas 220
1524 25 Jan 12 nicklas 221         Number copyConsentId = (Number)jsonBlood.get("copyConsent");
1524 25 Jan 12 nicklas 222         if (copyConsentId != null)
1524 25 Jan 12 nicklas 223         {
1524 25 Jan 12 nicklas 224           Sample consentCase = Sample.getById(dc, copyConsentId.intValue());
5277 05 Feb 19 nicklas 225           Consent.copyConsentAnnotations(dc, consentCase, blood, true);
1788 14 Dec 12 nicklas 226           if (consentCase.getParentType() == null)
1788 14 Dec 12 nicklas 227           {
1788 14 Dec 12 nicklas 228             // Temporary case for holding consent information that should deleted
1788 14 Dec 12 nicklas 229             dc.deleteItem(consentCase);
1788 14 Dec 12 nicklas 230           }
1788 14 Dec 12 nicklas 231           jsonMessages.add("Consent copied from case '" + consentCase.getName() + "'");
1524 25 Jan 12 nicklas 232         }
1524 25 Jan 12 nicklas 233         
1518 23 Jan 12 nicklas 234         jsonMessages.add("Blood '" + bloodName + "' created successfully.");
5421 13 May 19 nicklas 235         ActivityDef.REGISTERED_BLOOD.merge(dc, 1);
1518 23 Jan 12 nicklas 236         dc.commit();
1518 23 Jan 12 nicklas 237         json.put("messages", jsonMessages);
1518 23 Jan 12 nicklas 238         
1518 23 Jan 12 nicklas 239       }
1518 23 Jan 12 nicklas 240       
1518 23 Jan 12 nicklas 241       else if ("UpdateBlood".equals(cmd))
1518 23 Jan 12 nicklas 242       {
6329 14 Jun 21 nicklas 243         dc = sc.newDbControl(":Blood referral form registration");
2161 09 Dec 13 nicklas 244
2161 09 Dec 13 nicklas 245         ReggieRole.checkPermission(dc, "'" + cmd + "' wizard", ReggieRole.PATIENT_CURATOR, ReggieRole.ADMINISTRATOR);
2161 09 Dec 13 nicklas 246
3752 17 Feb 16 nicklas 247         JSONObject jsonReq = JsonUtil.parseRequest(req);
1518 23 Jan 12 nicklas 248         JSONObject jsonBlood = (JSONObject)jsonReq.get("bloodInfo");
1518 23 Jan 12 nicklas 249         
1518 23 Jan 12 nicklas 250         Number bloodId = (Number)jsonBlood.get("id");
1518 23 Jan 12 nicklas 251         Sample blood = Sample.getById(dc, bloodId.intValue());
1518 23 Jan 12 nicklas 252         
1518 23 Jan 12 nicklas 253         // Annotations
1524 25 Jan 12 nicklas 254         Date samplingDate = Reggie.CONVERTER_STRING_TO_DATETIME.convert((String)jsonBlood.get("samplingDate"));
1524 25 Jan 12 nicklas 255         Date freezerDate = Reggie.CONVERTER_STRING_TO_DATETIME.convert((String)jsonBlood.get("freezerDate"));
1614 24 Apr 12 nicklas 256         String bloodSample = Values.getStringOrNull((String)jsonBlood.get("bloodSample"));
1775 11 Dec 12 olle 257         String otherPathNote = Values.getStringOrNull((String)jsonBlood.get("otherPathNote"));
1610 23 Apr 12 nicklas 258         
1736 16 Nov 12 nicklas 259         blood.getCreationEvent().setEventDate(samplingDate);
1736 16 Nov 12 nicklas 260         
1736 16 Nov 12 nicklas 261         Annotationtype.BLOOD_SAMPLING_DATETIME.setAnnotationValue(dc, blood, samplingDate);
1736 16 Nov 12 nicklas 262         Annotationtype.BLOOD_FREEZER_DATETIME.setAnnotationValue(dc, blood, freezerDate);
1610 23 Apr 12 nicklas 263         Annotationtype.BLOOD_SERUM.setAnnotationValue(dc, blood, jsonBlood.get("serum"));
1614 24 Apr 12 nicklas 264         Annotationtype.BLOOD_SAMPLE.setAnnotationValue(dc, blood, bloodSample);
1775 11 Dec 12 olle 265         Annotationtype.OTHER_PATH_NOTE.setAnnotationValue(dc, blood, otherPathNote);
1518 23 Jan 12 nicklas 266
1518 23 Jan 12 nicklas 267         jsonMessages.add("Blood '" + blood.getName() + "' updated successfully.");
1518 23 Jan 12 nicklas 268                 
1518 23 Jan 12 nicklas 269         dc.commit();
1518 23 Jan 12 nicklas 270         json.put("messages", jsonMessages);
1518 23 Jan 12 nicklas 271       }
3059 19 Dec 14 nicklas 272       CounterService.getInstance().setForceCount();
1518 23 Jan 12 nicklas 273     }
1518 23 Jan 12 nicklas 274     catch (Throwable t)
1518 23 Jan 12 nicklas 275     {
1518 23 Jan 12 nicklas 276       t.printStackTrace();
1518 23 Jan 12 nicklas 277       json.clear();
1518 23 Jan 12 nicklas 278       json.put("status", "error");
1518 23 Jan 12 nicklas 279       json.put("message", t.getMessage());
1518 23 Jan 12 nicklas 280       json.put("stacktrace", ThrowableUtil.stackTraceToString(t));
1518 23 Jan 12 nicklas 281     }
1518 23 Jan 12 nicklas 282     finally
1518 23 Jan 12 nicklas 283     {
1518 23 Jan 12 nicklas 284       if (dc != null) dc.close();
1518 23 Jan 12 nicklas 285       json.writeJSONString(resp.getWriter());
1518 23 Jan 12 nicklas 286     }
1518 23 Jan 12 nicklas 287     
1518 23 Jan 12 nicklas 288   }
1518 23 Jan 12 nicklas 289   
6277 03 Jun 21 nicklas 290   private String stripPrefix(String s, String prefix)
6277 03 Jun 21 nicklas 291   {
6277 03 Jun 21 nicklas 292     if (s != null && prefix != null && s.startsWith(prefix))
6277 03 Jun 21 nicklas 293     {
6277 03 Jun 21 nicklas 294       s = s.substring(prefix.length());
6277 03 Jun 21 nicklas 295     }
6277 03 Jun 21 nicklas 296     return s;
6277 03 Jun 21 nicklas 297   }
6277 03 Jun 21 nicklas 298   
6277 03 Jun 21 nicklas 299   
1518 23 Jan 12 nicklas 300   private BioSource getOrCreatePatient(DbControl dc, JSONObject jsonPat, JSONArray jsonMessages)
1518 23 Jan 12 nicklas 301     throws Exception
1518 23 Jan 12 nicklas 302   {
1518 23 Jan 12 nicklas 303     Number patientId = (Number)jsonPat.get("id");
1518 23 Jan 12 nicklas 304     BioSource patient = null;
1518 23 Jan 12 nicklas 305     
1518 23 Jan 12 nicklas 306     if (patientId != null)
1518 23 Jan 12 nicklas 307     {
1518 23 Jan 12 nicklas 308       patient = BioSource.getById(dc, patientId.intValue());
1518 23 Jan 12 nicklas 309     }
1518 23 Jan 12 nicklas 310     else
1518 23 Jan 12 nicklas 311     {
2613 29 Aug 14 nicklas 312       String pnr = (String)jsonPat.get("personalNumber");
2613 29 Aug 14 nicklas 313       String patName = (String)jsonPat.get("name");
2613 29 Aug 14 nicklas 314       Patient.ensureNotExistingPatient(dc, pnr, patName);
2613 29 Aug 14 nicklas 315       
1518 23 Jan 12 nicklas 316       // Create new patient
1518 23 Jan 12 nicklas 317       patient = BioSource.getNew(dc);
1518 23 Jan 12 nicklas 318       patient.setItemSubtype(Subtype.PATIENT.load(dc));
4900 10 Jul 18 nicklas 319       patient.setName(patName);
5088 13 Nov 18 nicklas 320       patient.setExternalId(Patient.getNextExternalId(dc, Subtype.PATIENT));
7024 07 Feb 23 nicklas 321       StringToDateConverter dateConverter = new StringToDateConverter(FastDateFormat.getInstance("yyyy-MM-dd"));
1767 04 Dec 12 nicklas 322       Date dateOfBirth = dateConverter.convert((String)jsonPat.get("dateOfBirth"));
1767 04 Dec 12 nicklas 323       String gender = Values.getStringOrNull((String)jsonPat.get("gender"));
1767 04 Dec 12 nicklas 324       String familyName = Values.getStringOrNull((String)jsonPat.get("familyName"));
1767 04 Dec 12 nicklas 325       String allFirstNames = Values.getStringOrNull((String)jsonPat.get("allFirstNames"));
1767 04 Dec 12 nicklas 326
1767 04 Dec 12 nicklas 327       Annotationtype.PERSONAL_NUMBER.setAnnotationValue(dc, patient, pnr);
1767 04 Dec 12 nicklas 328       Annotationtype.FAMILY_NAME.setAnnotationValue(dc, patient, familyName);
1767 04 Dec 12 nicklas 329       Annotationtype.ALL_FIRST_NAMES.setAnnotationValue(dc, patient, allFirstNames);
1767 04 Dec 12 nicklas 330       Annotationtype.GENDER.setAnnotationValue(dc, patient, gender);
1767 04 Dec 12 nicklas 331       Annotationtype.DATE_OF_BIRTH.setAnnotationValue(dc, patient, dateOfBirth);
1767 04 Dec 12 nicklas 332     
1518 23 Jan 12 nicklas 333       if (gender == null || dateOfBirth == null)
1518 23 Jan 12 nicklas 334       {
1518 23 Jan 12 nicklas 335         patient.setDescription("This patient was recorded with a non-standard PersonalNumber. " +
1518 23 Jan 12 nicklas 336             "DateOfBirth and Gender annotations could not be automatically generated.");
1518 23 Jan 12 nicklas 337       }
1518 23 Jan 12 nicklas 338       dc.saveItem(patient);
5421 13 May 19 nicklas 339       ActivityDef.REGISTERED_PATIENT.merge(dc, 1);
5387 26 Apr 19 nicklas 340
1518 23 Jan 12 nicklas 341       jsonMessages.add("Patient '" + patient.getName() + "' created successfully.");
1518 23 Jan 12 nicklas 342     }
1518 23 Jan 12 nicklas 343     return patient;
1518 23 Jan 12 nicklas 344
1518 23 Jan 12 nicklas 345   }
1518 23 Jan 12 nicklas 346   
1518 23 Jan 12 nicklas 347   
1518 23 Jan 12 nicklas 348 }