extensions/net.sf.basedb.meludi/trunk/src/net/sf/basedb/meludi/servlet/ConsentFormServlet.java

Code
Comments
Other
Rev Date Author Line
4785 27 Apr 18 olle 1 package net.sf.basedb.meludi.servlet;
4785 27 Apr 18 olle 2
4785 27 Apr 18 olle 3 import java.io.IOException;
4860 19 Jun 18 olle 4 import java.text.SimpleDateFormat;
4785 27 Apr 18 olle 5 import java.util.Date;
4811 16 May 18 olle 6 import java.util.HashMap;
4785 27 Apr 18 olle 7 import java.util.List;
4785 27 Apr 18 olle 8
4785 27 Apr 18 olle 9 import javax.servlet.ServletException;
4785 27 Apr 18 olle 10 import javax.servlet.http.HttpServlet;
4785 27 Apr 18 olle 11 import javax.servlet.http.HttpServletRequest;
4785 27 Apr 18 olle 12 import javax.servlet.http.HttpServletResponse;
4785 27 Apr 18 olle 13
4785 27 Apr 18 olle 14 import org.json.simple.JSONArray;
4785 27 Apr 18 olle 15 import org.json.simple.JSONObject;
4785 27 Apr 18 olle 16
4785 27 Apr 18 olle 17 import net.sf.basedb.core.Application;
4860 19 Jun 18 olle 18 import net.sf.basedb.core.BioPlate;
4785 27 Apr 18 olle 19 import net.sf.basedb.core.BioSource;
4785 27 Apr 18 olle 20 import net.sf.basedb.core.DbControl;
4785 27 Apr 18 olle 21 import net.sf.basedb.core.InvalidDataException;
4860 19 Jun 18 olle 22 import net.sf.basedb.core.ItemQuery;
4785 27 Apr 18 olle 23 import net.sf.basedb.core.Sample;
4785 27 Apr 18 olle 24 import net.sf.basedb.core.SessionControl;
4860 19 Jun 18 olle 25 import net.sf.basedb.core.query.Expressions;
4860 19 Jun 18 olle 26 import net.sf.basedb.core.query.Hql;
4860 19 Jun 18 olle 27 import net.sf.basedb.core.query.Orders;
4860 19 Jun 18 olle 28 import net.sf.basedb.core.query.Restrictions;
4785 27 Apr 18 olle 29 import net.sf.basedb.core.snapshot.SnapshotManager;
4785 27 Apr 18 olle 30 import net.sf.basedb.meludi.JsonUtil;
4785 27 Apr 18 olle 31 import net.sf.basedb.meludi.Meludi;
4785 27 Apr 18 olle 32 import net.sf.basedb.meludi.Site;
4860 19 Jun 18 olle 33 import net.sf.basedb.meludi.converter.StringToDateConverter;
4785 27 Apr 18 olle 34 import net.sf.basedb.meludi.counter.CounterService;
4785 27 Apr 18 olle 35 import net.sf.basedb.meludi.dao.Annotationtype;
4785 27 Apr 18 olle 36 //import net.sf.basedb.meludi.dao.Blood;
4785 27 Apr 18 olle 37 import net.sf.basedb.meludi.dao.Case;
4785 27 Apr 18 olle 38 import net.sf.basedb.meludi.dao.Consent;
4785 27 Apr 18 olle 39 import net.sf.basedb.meludi.dao.Patient;
4785 27 Apr 18 olle 40 import net.sf.basedb.meludi.dao.MeludiRole;
4785 27 Apr 18 olle 41 import net.sf.basedb.meludi.dao.SpecimenTube;
4785 27 Apr 18 olle 42 import net.sf.basedb.meludi.dao.Subtype;
4860 19 Jun 18 olle 43 import net.sf.basedb.util.MD5;
4785 27 Apr 18 olle 44 //import net.sf.basedb.meludi.projectarchive.ProjectArchiveService;
4785 27 Apr 18 olle 45 import net.sf.basedb.util.Values;
4785 27 Apr 18 olle 46 import net.sf.basedb.util.error.ThrowableUtil;
4785 27 Apr 18 olle 47
4785 27 Apr 18 olle 48
4785 27 Apr 18 olle 49 public class ConsentFormServlet 
4785 27 Apr 18 olle 50   extends HttpServlet 
4785 27 Apr 18 olle 51 {
4785 27 Apr 18 olle 52   private static final long serialVersionUID = 28308664025515890L;
4785 27 Apr 18 olle 53
4785 27 Apr 18 olle 54   public ConsentFormServlet()
4785 27 Apr 18 olle 55   {}
4785 27 Apr 18 olle 56
4785 27 Apr 18 olle 57   @SuppressWarnings("unchecked")
4785 27 Apr 18 olle 58   @Override
4785 27 Apr 18 olle 59   protected void doGet(HttpServletRequest req, HttpServletResponse resp)
4785 27 Apr 18 olle 60     throws ServletException, IOException 
4785 27 Apr 18 olle 61   {
4785 27 Apr 18 olle 62     System.out.println("ConsentFormServlet::doGet(): Start");
4785 27 Apr 18 olle 63     String ID = req.getParameter("ID");
4785 27 Apr 18 olle 64     String cmd = req.getParameter("cmd");
4785 27 Apr 18 olle 65     JsonUtil.setJsonResponseHeaders(resp);
4785 27 Apr 18 olle 66     
4785 27 Apr 18 olle 67     JSONObject json = new JSONObject();
4785 27 Apr 18 olle 68     json.put("status", "ok");
4785 27 Apr 18 olle 69   
4785 27 Apr 18 olle 70     //final SessionControl sc = Meludi.getSessionControl(req);
5468 04 Jun 19 olle 71     //final SessionControl sc = Application.getSessionControl(ID, req.getRemoteAddr());
5744 20 Nov 19 olle 72     //final SessionControl sc  = Application.getSessionControl(ID, "", req.getRemoteAddr(), true);
5744 20 Nov 19 olle 73     final SessionControl sc  = Application.getSessionControl(ID, null, req.getRemoteAddr(), true);
4785 27 Apr 18 olle 74     DbControl dc = null;
4785 27 Apr 18 olle 75     System.out.println("ConsentFormServlet::doGet(): cmd = \"" + cmd + "\" Start");
4785 27 Apr 18 olle 76     try
4785 27 Apr 18 olle 77     {
4860 19 Jun 18 olle 78       if ("CountCases".equals(cmd))
4785 27 Apr 18 olle 79       {
4860 19 Jun 18 olle 80         dc = sc.newDbControl();
4860 19 Jun 18 olle 81         ItemQuery<Sample> query = Sample.getQuery();
4860 19 Jun 18 olle 82         Subtype.CASE.addFilter(dc, query);
4860 19 Jun 18 olle 83         query.setIncludes(Meludi.INCLUDE_IN_CURRENT_PROJECT);
4860 19 Jun 18 olle 84         query.setCacheResult(true);
4860 19 Jun 18 olle 85         long count = query.count(dc);
4860 19 Jun 18 olle 86         json.put("count", count);
4860 19 Jun 18 olle 87         json.put("sampleItemPrefix", Meludi.fetchSampleItemPrefix(sc.getActiveProjectId()));
4860 19 Jun 18 olle 88         json.put("sampleItemNumDigits", Meludi.fetchSampleItemNumDigits(sc.getActiveProjectId()));
4860 19 Jun 18 olle 89         json.put("referralPrefix", Meludi.fetchReferralPrefix(sc.getActiveProjectId()));
4860 19 Jun 18 olle 90         json.put("referralNumDigits", Meludi.fetchReferralNumDigits(sc.getActiveProjectId()));
4860 19 Jun 18 olle 91       }
4860 19 Jun 18 olle 92       else if ("GetCaseInfo".equals(cmd))
4860 19 Jun 18 olle 93       {
4785 27 Apr 18 olle 94         /*
4785 27 Apr 18 olle 95           Load consent-related information about a case. Basically there are 4
4785 27 Apr 18 olle 96           outcomes:
4785 27 Apr 18 olle 97           1. No info exists for the given case --> Allow registration of free-floating case (Yes, No, Not asked)
4785 27 Apr 18 olle 98           2. Only specimen tubes exists --> Only allow registration of Yes as a free-floating case
4785 27 Apr 18 olle 99           3. Free-floating case exists (with Yes, No or Not asked) --> No changes are allowed
4785 27 Apr 18 olle 100           4. A real case or blood sample exists which connects to a patient --> List all cases + blood samples
4785 27 Apr 18 olle 101              for the patient with current consent status and allow Yes + date to be registered for those
4785 27 Apr 18 olle 102              do not have a consent yet.
4785 27 Apr 18 olle 103         */
4785 27 Apr 18 olle 104         dc = sc.newDbControl();
4785 27 Apr 18 olle 105         
4860 19 Jun 18 olle 106         String referralId = req.getParameter("referralId");
5044 19 Oct 18 olle 107         String personalNumber = req.getParameter("personalNumber");
4785 27 Apr 18 olle 108         String caseName = req.getParameter("caseName");
4860 19 Jun 18 olle 109         System.out.println("ConsentFormServlet::doGet(): cmd = \"" + cmd + "\" Start referralId = " + referralId + " caseName = " + caseName);
5044 19 Oct 18 olle 110         System.out.println("ConsentFormServlet::doGet(): cmd = \"" + cmd + "\" Start personalNumber = " + personalNumber + " caseName = " + caseName);
4785 27 Apr 18 olle 111
4860 19 Jun 18 olle 112         // If no case name is given, try to get it from referral ID
4860 19 Jun 18 olle 113         if (caseName == null || caseName.equals(""))
4860 19 Jun 18 olle 114         {
4860 19 Jun 18 olle 115           // If referral ID exists, get name for corresponding case item
4860 19 Jun 18 olle 116           if (referralId != null && !referralId.equals(""))
4860 19 Jun 18 olle 117           {
4860 19 Jun 18 olle 118             // Check if existing case exists for the referral ID
4860 19 Jun 18 olle 119             Case aCase = Case.findByReferralId(dc, referralId);
4860 19 Jun 18 olle 120             if (aCase != null)
4860 19 Jun 18 olle 121             {
4860 19 Jun 18 olle 122               caseName = aCase.getName();
4860 19 Jun 18 olle 123             }
4860 19 Jun 18 olle 124             else
4860 19 Jun 18 olle 125             {
4860 19 Jun 18 olle 126               // Get next case name
4860 19 Jun 18 olle 127               caseName = fetchNewCaseName(dc);
4860 19 Jun 18 olle 128             }
4860 19 Jun 18 olle 129           }
5044 19 Oct 18 olle 130           // If personal Number exists, get name for corresponding case item
5044 19 Oct 18 olle 131           else if (personalNumber != null && !personalNumber.equals(""))
5044 19 Oct 18 olle 132           {
5044 19 Oct 18 olle 133             // Find patient from personal number
5044 19 Oct 18 olle 134             Patient patient = Patient.findByPersonalNumber(dc, personalNumber);
5044 19 Oct 18 olle 135             if (patient != null)
5044 19 Oct 18 olle 136             {
5044 19 Oct 18 olle 137               List<Case> caseList = Case.findByPatient(dc, patient);
5044 19 Oct 18 olle 138               if (caseList != null && caseList.size() > 0)
5044 19 Oct 18 olle 139               {
5044 19 Oct 18 olle 140                 // Get first case item for patient
5044 19 Oct 18 olle 141                 Case aCase = (Case) caseList.get(0);
5044 19 Oct 18 olle 142                 if (aCase != null)
5044 19 Oct 18 olle 143                 {
5044 19 Oct 18 olle 144                   caseName = aCase.getName();
5044 19 Oct 18 olle 145                 }
5044 19 Oct 18 olle 146               }
5044 19 Oct 18 olle 147             }
5044 19 Oct 18 olle 148           }
4860 19 Jun 18 olle 149         }
4860 19 Jun 18 olle 150         System.out.println("ConsentFormServlet::doGet(): cmd = \"" + cmd + "\" (2) referralId = " + referralId + " caseName = " + caseName);
4860 19 Jun 18 olle 151
4785 27 Apr 18 olle 152         //Site site = Site.findByCaseName(caseName);
4785 27 Apr 18 olle 153         Site site = Site.UNKNOWN;
4785 27 Apr 18 olle 154         Patient patient = null;
4785 27 Apr 18 olle 155         
4785 27 Apr 18 olle 156         // First try to locate a case + patient
4785 27 Apr 18 olle 157         JSONObject jsonCase = null;
4785 27 Apr 18 olle 158         Case theCase = Case.findByName(dc, caseName);
4785 27 Apr 18 olle 159         
4789 04 May 18 olle 160         System.out.println("ConsentFormServlet::doGet(): cmd = \"" + cmd + "\" caseName = " + caseName + " theCase = " + theCase);
4785 27 Apr 18 olle 161
4785 27 Apr 18 olle 162         if (theCase != null) 
4785 27 Apr 18 olle 163         {
4879 27 Jun 18 olle 164           theCase.loadAnnotations(dc, "diagnosisConfirmed", Annotationtype.DIAGNOSIS_CONFIRMED, null);
4785 27 Apr 18 olle 165           System.out.println("ConsentFormServlet::doGet(): cmd = \"" + cmd + "\" caseName = " + caseName + " theCase.asJSONObject() = " + theCase.asJSONObject());
4785 27 Apr 18 olle 166           // Use stored annotation snapshots for performance reasons
4785 27 Apr 18 olle 167           SnapshotManager manager = new SnapshotManager();        
4785 27 Apr 18 olle 168           site = fetchSite(dc, manager, theCase.getSample());
4785 27 Apr 18 olle 169           jsonCase = theCase.asJSONObject();
4785 27 Apr 18 olle 170
4785 27 Apr 18 olle 171           patient = Patient.findByCase(dc, theCase);
4860 19 Jun 18 olle 172           if (patient != null)
4860 19 Jun 18 olle 173           {
4860 19 Jun 18 olle 174             patient.loadDefaultAnnotations(dc);
4860 19 Jun 18 olle 175           }
4785 27 Apr 18 olle 176           // We do not want the 'virtual patients' used for statistics
4785 27 Apr 18 olle 177           //if (patient != null && !patient.getName().startsWith("PAT")) patient = null;
4785 27 Apr 18 olle 178         }
4860 19 Jun 18 olle 179         else
4860 19 Jun 18 olle 180         {
4860 19 Jun 18 olle 181           jsonCase = new JSONObject();
4860 19 Jun 18 olle 182           jsonCase.put("name", caseName);
4860 19 Jun 18 olle 183           json.put("caseInfo", jsonCase);
4860 19 Jun 18 olle 184         }
4785 27 Apr 18 olle 185
4785 27 Apr 18 olle 186 /*
4785 27 Apr 18 olle 187         // Then, check for a blood sample + patient
4785 27 Apr 18 olle 188         Blood blood = Blood.findByCaseName(dc, caseName, site.useCaseSuffixForPreNeoForms());
4785 27 Apr 18 olle 189         if (blood == null && site.useCaseSuffixForPreNeoForms())
4785 27 Apr 18 olle 190         {
4785 27 Apr 18 olle 191           // Second try without checking 'C' suffix
4785 27 Apr 18 olle 192           blood = Blood.findByCaseName(dc, caseName, false);
4785 27 Apr 18 olle 193         }
4785 27 Apr 18 olle 194         if (blood != null) 
4785 27 Apr 18 olle 195         {
4785 27 Apr 18 olle 196           Patient bloodPatient = Patient.findByBlood(dc, blood);
4785 27 Apr 18 olle 197           if (bloodPatient != null)
4785 27 Apr 18 olle 198           {
4785 27 Apr 18 olle 199             // Verify that both samples have the same patient
4785 27 Apr 18 olle 200             if (patient != null && !bloodPatient.equals(patient))
4785 27 Apr 18 olle 201             {
4785 27 Apr 18 olle 202               throw new InvalidDataException("Found case '" + theCase.getName() + "' and blood '" + blood.getName() + 
4785 27 Apr 18 olle 203                 "' but they have not the same patient. This wizard can't continue until that is corrected.");  
4785 27 Apr 18 olle 204             }
4785 27 Apr 18 olle 205             patient = bloodPatient;
4785 27 Apr 18 olle 206           }
4785 27 Apr 18 olle 207         }
4785 27 Apr 18 olle 208 */
4785 27 Apr 18 olle 209         
4785 27 Apr 18 olle 210         if (patient != null)
4785 27 Apr 18 olle 211         {
4785 27 Apr 18 olle 212           // This is case 4 as described above.
4785 27 Apr 18 olle 213           JSONObject jsonPatient = patient.asJSONObject();
4785 27 Apr 18 olle 214           
4785 27 Apr 18 olle 215           // Load all cases for the patient
4785 27 Apr 18 olle 216           List<Case> allCases = Case.findByPatient(dc, patient);
4785 27 Apr 18 olle 217           JSONArray jsonAll = new JSONArray();
4785 27 Apr 18 olle 218           for (Case c : allCases)
4785 27 Apr 18 olle 219           {
4785 27 Apr 18 olle 220             Consent.loadConsentAnnotations(dc, c);
4785 27 Apr 18 olle 221             //c.loadAnnotations(dc, "laterality", Annotationtype.LATERALITY, null);
4785 27 Apr 18 olle 222             jsonAll.add(c.asJSONObject());
4785 27 Apr 18 olle 223           }
4785 27 Apr 18 olle 224           jsonPatient.put("allCases", jsonAll);
4785 27 Apr 18 olle 225           
4785 27 Apr 18 olle 226           // Load all blood samples for the patient
4785 27 Apr 18 olle 227 /*
4785 27 Apr 18 olle 228           List<Blood> allBlood = Blood.findByPatient(dc, patient);
4785 27 Apr 18 olle 229           jsonAll = new JSONArray();
4785 27 Apr 18 olle 230           for (Blood b : allBlood)
4785 27 Apr 18 olle 231           {
4785 27 Apr 18 olle 232             Consent.loadConsentAnnotations(dc, b);
4785 27 Apr 18 olle 233             jsonAll.add(b.asJSONObject());
4785 27 Apr 18 olle 234           }
4785 27 Apr 18 olle 235           jsonPatient.put("allBlood", jsonAll);
4785 27 Apr 18 olle 236 */
4785 27 Apr 18 olle 237           
4785 27 Apr 18 olle 238           json.put("patientInfo", jsonPatient);
4860 19 Jun 18 olle 239           json.put("caseInfo", theCase.asJSONObject());
4785 27 Apr 18 olle 240         }
4785 27 Apr 18 olle 241         else if (theCase != null)
4785 27 Apr 18 olle 242         {
4785 27 Apr 18 olle 243           // This is case 3 as described above.
4785 27 Apr 18 olle 244           Consent.loadConsentAnnotations(dc, theCase);
4879 27 Jun 18 olle 245           theCase.loadAnnotations(dc, "diagnosisConfirmed", Annotationtype.DIAGNOSIS_CONFIRMED, null);
4785 27 Apr 18 olle 246           json.put("caseInfo", theCase.asJSONObject());
4785 27 Apr 18 olle 247         }
4785 27 Apr 18 olle 248         else
4785 27 Apr 18 olle 249         {
4785 27 Apr 18 olle 250           // Check if specimen tubes have been registered
4785 27 Apr 18 olle 251           //List<SpecimenTube> specimenTubes = SpecimenTube.findByCaseName(dc, caseName, false);
4785 27 Apr 18 olle 252           List<SpecimenTube> specimenTubes = SpecimenTube.findByCaseName(dc, caseName);
4785 27 Apr 18 olle 253           if (specimenTubes != null && specimenTubes.size() > 0)
4785 27 Apr 18 olle 254           {
4785 27 Apr 18 olle 255             //  This is case 2 as described above.
4785 27 Apr 18 olle 256             JSONArray jsonTubes = new JSONArray();
4785 27 Apr 18 olle 257             for (SpecimenTube tube : specimenTubes)
4785 27 Apr 18 olle 258             {
4785 27 Apr 18 olle 259               jsonTubes.add(tube.asJSONObject());
4785 27 Apr 18 olle 260             }
4785 27 Apr 18 olle 261             json.put("specimenInfo", jsonTubes);
4785 27 Apr 18 olle 262           }
4785 27 Apr 18 olle 263           else
4785 27 Apr 18 olle 264           {
4785 27 Apr 18 olle 265             // This is case 1 as described above -- no info is returned
4785 27 Apr 18 olle 266           }
4785 27 Apr 18 olle 267         }
4785 27 Apr 18 olle 268         System.out.println("ConsentFormServlet::doGet(): cmd = \"" + cmd + "\" json = " + json);
4785 27 Apr 18 olle 269       }
4860 19 Jun 18 olle 270       else if ("GetPatientInfo".equals(cmd))
4860 19 Jun 18 olle 271       {
4860 19 Jun 18 olle 272         /*
4860 19 Jun 18 olle 273           Load information about a single patient when given a personal number
4860 19 Jun 18 olle 274           If a patient is found we will load annotations and the cases associated 
4860 19 Jun 18 olle 275           with it. If no patient is found, we populate a new object with some
4860 19 Jun 18 olle 276           information (eg. birth date and gender) from the information we have.
4860 19 Jun 18 olle 277         */
4860 19 Jun 18 olle 278         dc = sc.newDbControl();
4860 19 Jun 18 olle 279         
4860 19 Jun 18 olle 280         // Request parameters
4860 19 Jun 18 olle 281         String pnr = req.getParameter("personalNumber");
4860 19 Jun 18 olle 282         boolean pnrIsValid = Values.getBoolean(req.getParameter("pnrIsValid"));
4860 19 Jun 18 olle 283
4860 19 Jun 18 olle 284         // Find a patient by personalNumber
4860 19 Jun 18 olle 285         Patient patient = Patient.findByPersonalNumber(dc, pnr);
4860 19 Jun 18 olle 286         JSONObject jsonPat = null;
4860 19 Jun 18 olle 287         
4860 19 Jun 18 olle 288         if (patient != null)
4860 19 Jun 18 olle 289         {
4860 19 Jun 18 olle 290           // Load patient annotations
4860 19 Jun 18 olle 291           patient.loadDefaultAnnotations(dc);
4860 19 Jun 18 olle 292
4860 19 Jun 18 olle 293           // Load cases associated with this patient
4860 19 Jun 18 olle 294           List<Case> cases = Case.findByPatient(dc, patient);
4860 19 Jun 18 olle 295           
4860 19 Jun 18 olle 296           // Load case annotations
4860 19 Jun 18 olle 297           JSONArray jsonCases = new JSONArray();
4860 19 Jun 18 olle 298           if (cases.size() > 0)
4860 19 Jun 18 olle 299           {
4860 19 Jun 18 olle 300             for (Case c : cases)
4860 19 Jun 18 olle 301             {
4860 19 Jun 18 olle 302               jsonCases.add(c.asJSONObject());
4860 19 Jun 18 olle 303             }
4860 19 Jun 18 olle 304           }
4860 19 Jun 18 olle 305           
4860 19 Jun 18 olle 306           jsonPat = patient.asJSONObject();
4860 19 Jun 18 olle 307           jsonPat.put("cases", jsonCases);
4860 19 Jun 18 olle 308
4860 19 Jun 18 olle 309         }
4860 19 Jun 18 olle 310         else
4860 19 Jun 18 olle 311         {
4860 19 Jun 18 olle 312           // No patient was found -- try to find the highest existing patient number
4860 19 Jun 18 olle 313           String patientItemPrefix = Meludi.fetchPatientItemPrefix(sc.getActiveProjectId());
4860 19 Jun 18 olle 314           jsonPat = new JSONObject();
4860 19 Jun 18 olle 315           jsonPat.put("personalNumber", pnr);
4860 19 Jun 18 olle 316           jsonPat.put("name", Patient.generateNextName(dc, patientItemPrefix, Subtype.PATIENT));
4860 19 Jun 18 olle 317           if (pnrIsValid)
4860 19 Jun 18 olle 318           {
4860 19 Jun 18 olle 319             // 'Samordningsnummer' have day-in-month + 60
4860 19 Jun 18 olle 320             int dayInMonth = Values.getInt(pnr.substring(6,8));
4860 19 Jun 18 olle 321             if (dayInMonth > 60) dayInMonth -= 60;
4860 19 Jun 18 olle 322             jsonPat.put("dateOfBirth", pnr.substring(0,4) + '-' + pnr.substring(4,6)+'-'+MD5.leftPad(Integer.toString(dayInMonth), '0', 2));
4860 19 Jun 18 olle 323             jsonPat.put("gender", Integer.parseInt(pnr.substring(10,11)) % 2 == 0 ? "F" : "M");
4860 19 Jun 18 olle 324           }
4860 19 Jun 18 olle 325         }
4860 19 Jun 18 olle 326
4860 19 Jun 18 olle 327         json.put("patientInfo", jsonPat);
4860 19 Jun 18 olle 328       }
4785 27 Apr 18 olle 329     }
4785 27 Apr 18 olle 330     catch (Throwable t)
4785 27 Apr 18 olle 331     {
4785 27 Apr 18 olle 332       t.printStackTrace();
4785 27 Apr 18 olle 333       json.clear();
4785 27 Apr 18 olle 334       json.put("status", "error");
4785 27 Apr 18 olle 335       json.put("message", t.getMessage());
4785 27 Apr 18 olle 336       json.put("stacktrace", ThrowableUtil.stackTraceToString(t));
4785 27 Apr 18 olle 337     }
4785 27 Apr 18 olle 338     finally
4785 27 Apr 18 olle 339     {
4785 27 Apr 18 olle 340       if (dc != null) dc.close();
4785 27 Apr 18 olle 341       json.writeJSONString(resp.getWriter());
4785 27 Apr 18 olle 342     }
4785 27 Apr 18 olle 343   }
4785 27 Apr 18 olle 344
4860 19 Jun 18 olle 345   /**
4860 19 Jun 18 olle 346    * Fetches new case name for the active project,
4860 19 Jun 18 olle 347    * equal to the one following the currently
4860 19 Jun 18 olle 348    * highest case name.
4860 19 Jun 18 olle 349    * 
4860 19 Jun 18 olle 350    * @param dc DbControl The DbControl object to use.
4860 19 Jun 18 olle 351    * @return String The new case name.
4860 19 Jun 18 olle 352    */
4860 19 Jun 18 olle 353   private String fetchNewCaseName(DbControl dc)
4860 19 Jun 18 olle 354   {
4860 19 Jun 18 olle 355     SessionControl sc = dc.getSessionControl();
4860 19 Jun 18 olle 356     String sampleItemPrefix = Meludi.fetchSampleItemPrefix(sc.getActiveProjectId());
4860 19 Jun 18 olle 357     int sampleItemNumDigits = Meludi.fetchSampleItemNumDigits(sc.getActiveProjectId());
4860 19 Jun 18 olle 358     // Get highest current case number
4860 19 Jun 18 olle 359     int highestCaseNumber = 0;
4860 19 Jun 18 olle 360     String highestCaseName = fetchHighestCaseName(dc);
4860 19 Jun 18 olle 361     if (highestCaseName != null)
4860 19 Jun 18 olle 362     {
4860 19 Jun 18 olle 363       // Remove sample item prefix
4860 19 Jun 18 olle 364       int prefixLength = sampleItemPrefix.length();
4860 19 Jun 18 olle 365       String highestCaseNumString = highestCaseName.substring(prefixLength);
4860 19 Jun 18 olle 366       // Remove leading zeroes in case number
4860 19 Jun 18 olle 367       while (highestCaseNumString.indexOf("0") == 0)
4860 19 Jun 18 olle 368       {
4860 19 Jun 18 olle 369         highestCaseNumString = highestCaseNumString.substring(1);
4860 19 Jun 18 olle 370       }
4860 19 Jun 18 olle 371       // Get highest current case number as integer
4860 19 Jun 18 olle 372       highestCaseNumber = Integer.parseUnsignedInt(highestCaseNumString);
4860 19 Jun 18 olle 373     }
4860 19 Jun 18 olle 374     // Get new case number
4860 19 Jun 18 olle 375     int newCaseNumber = highestCaseNumber + 1;
4860 19 Jun 18 olle 376     // Get new case number string with leading zeroes
4860 19 Jun 18 olle 377     String newCaseNumString = "" + newCaseNumber;
4860 19 Jun 18 olle 378     while (newCaseNumString.length() < sampleItemNumDigits)
4860 19 Jun 18 olle 379     {
4860 19 Jun 18 olle 380       newCaseNumString = "0" + newCaseNumString;
4860 19 Jun 18 olle 381     }
4860 19 Jun 18 olle 382     // Get new case name
4860 19 Jun 18 olle 383     String newCaseName = sampleItemPrefix + newCaseNumString;
4860 19 Jun 18 olle 384     return newCaseName;
4860 19 Jun 18 olle 385   }
4860 19 Jun 18 olle 386
4860 19 Jun 18 olle 387   /**
4860 19 Jun 18 olle 388    * Fetches the highest case name for the active project,
4860 19 Jun 18 olle 389    * or `null`, if no cases exist.
4860 19 Jun 18 olle 390    * 
4860 19 Jun 18 olle 391    * @param dc DbControl The DbControl object to use.
4860 19 Jun 18 olle 392    * @return String The highest case name, or `null` if no cases exist.
4860 19 Jun 18 olle 393    */
4860 19 Jun 18 olle 394   private String fetchHighestCaseName(DbControl dc)
4860 19 Jun 18 olle 395   {
4860 19 Jun 18 olle 396     String highestCaseName = null;
4860 19 Jun 18 olle 397     ItemQuery<Sample> query = Sample.getQuery();
4860 19 Jun 18 olle 398     Subtype.CASE.addFilter(dc, query);
4860 19 Jun 18 olle 399     query.setIncludes(Meludi.INCLUDE_IN_CURRENT_PROJECT);
4860 19 Jun 18 olle 400     query.setCacheResult(true);
4860 19 Jun 18 olle 401     query.order(Orders.desc(Hql.property("name")));
4860 19 Jun 18 olle 402     List<Sample> caseNames = query.list(dc);
4860 19 Jun 18 olle 403     if (caseNames != null && caseNames.size() > 0)
4860 19 Jun 18 olle 404     {
4860 19 Jun 18 olle 405       Sample highestCase = (Sample) caseNames.get(0);
4860 19 Jun 18 olle 406       highestCaseName = highestCase.getName();
4860 19 Jun 18 olle 407     }
4860 19 Jun 18 olle 408     return highestCaseName;
4860 19 Jun 18 olle 409   }
4860 19 Jun 18 olle 410
4785 27 Apr 18 olle 411   @SuppressWarnings("unchecked")
4785 27 Apr 18 olle 412   @Override
4785 27 Apr 18 olle 413   protected void doPost(HttpServletRequest req, HttpServletResponse resp)
4785 27 Apr 18 olle 414     throws ServletException, IOException 
4785 27 Apr 18 olle 415   {
4785 27 Apr 18 olle 416     String ID = req.getParameter("ID");
4785 27 Apr 18 olle 417     String cmd = req.getParameter("cmd");
4785 27 Apr 18 olle 418     JsonUtil.setJsonResponseHeaders(resp);
4785 27 Apr 18 olle 419     
4785 27 Apr 18 olle 420     JSONObject json = new JSONObject();
4785 27 Apr 18 olle 421     json.put("status", "ok");
4785 27 Apr 18 olle 422     
4785 27 Apr 18 olle 423     JSONArray jsonMessages = new JSONArray();
4785 27 Apr 18 olle 424   
4785 27 Apr 18 olle 425     //final SessionControl sc = Meludi.getSessionControl(req);
5468 04 Jun 19 olle 426     //final SessionControl sc = Application.getSessionControl(ID, req.getRemoteAddr());
5744 20 Nov 19 olle 427     //final SessionControl sc  = Application.getSessionControl(ID, "", req.getRemoteAddr(), true);
5744 20 Nov 19 olle 428     final SessionControl sc  = Application.getSessionControl(ID, null, req.getRemoteAddr(), true);
4785 27 Apr 18 olle 429     DbControl dc = null;
4785 27 Apr 18 olle 430     System.out.println("ConsentFormServlet::doPost(): cmd = \"" + cmd + "\" Start");
4785 27 Apr 18 olle 431     try
4785 27 Apr 18 olle 432     {
4785 27 Apr 18 olle 433       if ("RegisterConsent".equals(cmd))
4785 27 Apr 18 olle 434       {
4785 27 Apr 18 olle 435         dc = sc.newDbControl();
4785 27 Apr 18 olle 436
4785 27 Apr 18 olle 437         MeludiRole.checkPermission(dc, "'" + cmd + "' wizard", MeludiRole.PATIENT_CURATOR, MeludiRole.ADMINISTRATOR);
4785 27 Apr 18 olle 438
4785 27 Apr 18 olle 439         JSONObject jsonReq = JsonUtil.parseRequest(req);
4785 27 Apr 18 olle 440         JSONObject jsonConsent = (JSONObject)jsonReq.get("consentInfo");
4860 19 Jun 18 olle 441         JSONObject jsonPat = (JSONObject)jsonReq.get("patientInfo");
4860 19 Jun 18 olle 442
4860 19 Jun 18 olle 443         BioSource patient = getOrCreatePatient(dc, jsonPat, jsonMessages);
4860 19 Jun 18 olle 444
4860 19 Jun 18 olle 445         String referralId = (String)jsonConsent.get("referralId");
4785 27 Apr 18 olle 446         String caseName = (String)jsonConsent.get("caseName");
4785 27 Apr 18 olle 447         String consent = Values.getStringOrNull((String)jsonConsent.get("consent"));
4785 27 Apr 18 olle 448         Date consentDate = Meludi.CONVERTER_STRING_TO_DATE.convert((String)jsonConsent.get("consentDate"));
4879 27 Jun 18 olle 449         Boolean diagnosisConfirmed = Values.getBoolean((String)jsonConsent.get("diagnosisConfirmed"), false);
4785 27 Apr 18 olle 450         // ID of samples can be both case and blood samples
4785 27 Apr 18 olle 451         List<Number> selectedSamples = (List<Number>)jsonConsent.get("selectedSamples");
5013 08 Oct 18 olle 452         //System.out.println("ConsentFormServlet::doPost(): cmd = \"" + cmd + "\" referralId = " + referralId + " caseName = " + caseName + " consent = " + consent + " consentDate = " + consentDate + " diagnosisConfirmed = " + diagnosisConfirmed + " selectedSamples = " + selectedSamples);
4785 27 Apr 18 olle 453
4789 04 May 18 olle 454         //Sample theCase = null;
4789 04 May 18 olle 455         Case thisCase = Case.findByName(dc, caseName);
4811 16 May 18 olle 456         Sample theCase = null;
4811 16 May 18 olle 457         if (thisCase != null)
4811 16 May 18 olle 458         {
4811 16 May 18 olle 459           theCase = Sample.getById(dc, thisCase.getSample().getId());
4811 16 May 18 olle 460         }
4789 04 May 18 olle 461
4789 04 May 18 olle 462         if (theCase != null)
4785 27 Apr 18 olle 463         {
5013 08 Oct 18 olle 464           // Update case with consent info
4789 04 May 18 olle 465           Annotationtype.CONSENT.setAnnotationValue(dc, theCase, consent);
4789 04 May 18 olle 466           if (consentDate != null) Annotationtype.CONSENT_DATE.setAnnotationValue(dc, theCase, consentDate);
4879 27 Jun 18 olle 467           Annotationtype.DIAGNOSIS_CONFIRMED.setAnnotationValue(dc, theCase, diagnosisConfirmed);          
4789 04 May 18 olle 468           jsonMessages.add("Consent for case '" + theCase.getName() + "' set to: " + consent);
4789 04 May 18 olle 469         }
4860 19 Jun 18 olle 470         else
4789 04 May 18 olle 471         {
4785 27 Apr 18 olle 472           // Create new case
4785 27 Apr 18 olle 473           theCase = Sample.getNew(dc);
4785 27 Apr 18 olle 474           theCase.setItemSubtype(Subtype.CASE.load(dc));
4785 27 Apr 18 olle 475           theCase.setName(caseName);
4860 19 Jun 18 olle 476           theCase.getCreationEvent().setSource(patient);
4811 16 May 18 olle 477
4811 16 May 18 olle 478           // Check if site should be found from site prefix
4811 16 May 18 olle 479           String siteDefault = null;
4811 16 May 18 olle 480           Boolean usesSitePrefix = Meludi.fetchUsesSitePrefix(sc.getActiveProjectId());
4811 16 May 18 olle 481           if (usesSitePrefix)
4811 16 May 18 olle 482           {
4860 19 Jun 18 olle 483             String rootItemDigits = "" + referralId;
4860 19 Jun 18 olle 484             String referralPrefix = Meludi.fetchReferralPrefix(sc.getActiveProjectId());
4860 19 Jun 18 olle 485             int referralPrefixLength = referralPrefix.length();
4860 19 Jun 18 olle 486             if (rootItemDigits.length() > referralPrefixLength)
4860 19 Jun 18 olle 487             {
4860 19 Jun 18 olle 488               rootItemDigits = rootItemDigits.substring(referralPrefixLength, rootItemDigits.length());
4860 19 Jun 18 olle 489             }
4860 19 Jun 18 olle 490             String sitePrefix = rootItemDigits.substring(0, 2);
4811 16 May 18 olle 491             HashMap<String,String> sitePrefixSiteKeyHashMap = Meludi.fetchSitePrefixSiteKeyHashMap(sc.getActiveProjectId());
4811 16 May 18 olle 492             if (sitePrefixSiteKeyHashMap != null)
4811 16 May 18 olle 493             {
4811 16 May 18 olle 494               siteDefault = sitePrefixSiteKeyHashMap.get(sitePrefix);
4811 16 May 18 olle 495               if (siteDefault == null)
4811 16 May 18 olle 496               {
4811 16 May 18 olle 497                 siteDefault = "";
4811 16 May 18 olle 498               }
4811 16 May 18 olle 499             }
4811 16 May 18 olle 500             Annotationtype.SITE.setAnnotationValue(dc, theCase, siteDefault);
4811 16 May 18 olle 501           }
4785 27 Apr 18 olle 502           
4785 27 Apr 18 olle 503           if ("Not asked".equals(consent))
4785 27 Apr 18 olle 504           {
4785 27 Apr 18 olle 505             theCase.setDescription((String)jsonConsent.get("reasonIfNotAsked"));
4785 27 Apr 18 olle 506           }
4785 27 Apr 18 olle 507           if (!"Yes".equals(consent))
4785 27 Apr 18 olle 508           {
4785 27 Apr 18 olle 509             // Create virtual patient
4785 27 Apr 18 olle 510             BioSource vPat = BioSource.getNew(dc);
4785 27 Apr 18 olle 511             Subtype virtualSubtype = "No".equals(consent) ? Subtype.NO : Subtype.NOT_ASKED;
4785 27 Apr 18 olle 512             vPat.setItemSubtype(virtualSubtype.load(dc));
4785 27 Apr 18 olle 513             vPat.setName(Patient.generateNextName(dc, consent, virtualSubtype));
4785 27 Apr 18 olle 514             theCase.getCreationEvent().setSource(vPat);
4785 27 Apr 18 olle 515             dc.saveItem(vPat);
4785 27 Apr 18 olle 516             jsonMessages.add("Virtual patient '" + vPat.getName() + "' created.");
4785 27 Apr 18 olle 517           }
4785 27 Apr 18 olle 518           else
4785 27 Apr 18 olle 519           {
4785 27 Apr 18 olle 520             // Generate External ID
4785 27 Apr 18 olle 521             //theCase.setExternalId(Case.generateNextExternalId(dc, "C"));
4785 27 Apr 18 olle 522           }
4785 27 Apr 18 olle 523           Annotationtype.CONSENT.setAnnotationValue(dc, theCase, consent);
4785 27 Apr 18 olle 524           if (consentDate != null) Annotationtype.CONSENT_DATE.setAnnotationValue(dc, theCase, consentDate);
4860 19 Jun 18 olle 525           Annotationtype.REFERRAL_ID.setAnnotationValue(dc, theCase, referralId);
4879 27 Jun 18 olle 526           Annotationtype.DIAGNOSIS_CONFIRMED.setAnnotationValue(dc, theCase, diagnosisConfirmed);          
4860 19 Jun 18 olle 527           jsonMessages.add("Case '" + caseName + "' created successfully with consent: " + consent);
4785 27 Apr 18 olle 528           
4785 27 Apr 18 olle 529           dc.saveItem(theCase);
4860 19 Jun 18 olle 530           //jsonMessages.add("Case '" + caseName + "' created successfully with consent: " + consent);
4785 27 Apr 18 olle 531         }
4860 19 Jun 18 olle 532 /*
4785 27 Apr 18 olle 533         else
4785 27 Apr 18 olle 534         {
4785 27 Apr 18 olle 535           for (Number id : selectedSamples)
4785 27 Apr 18 olle 536           {
4785 27 Apr 18 olle 537             theCase = Sample.getById(dc, id.intValue());
4785 27 Apr 18 olle 538             //boolean isBlood = Subtype.BLOOD.load(dc).equals(theCase.getItemSubtype());
4785 27 Apr 18 olle 539             boolean isBlood = false;
4785 27 Apr 18 olle 540             Annotationtype.CONSENT.setAnnotationValue(dc, theCase, consent);
4785 27 Apr 18 olle 541             if (consentDate != null) Annotationtype.CONSENT_DATE.setAnnotationValue(dc, theCase, consentDate);
4879 27 Jun 18 olle 542             Annotationtype.DIAGNOSIS_CONFIRMED.setAnnotationValue(dc, theCase, diagnosisConfirmed);          
4785 27 Apr 18 olle 543             if (isBlood)
4785 27 Apr 18 olle 544             {
4785 27 Apr 18 olle 545               jsonMessages.add("Consent for blood '" + theCase.getName() + "' set to: " + consent);
4785 27 Apr 18 olle 546             }
4785 27 Apr 18 olle 547             else
4785 27 Apr 18 olle 548             {
4785 27 Apr 18 olle 549               jsonMessages.add("Consent for case '" + theCase.getName() + "' set to: " + consent);
4785 27 Apr 18 olle 550             }
4785 27 Apr 18 olle 551           }
4785 27 Apr 18 olle 552         }
4860 19 Jun 18 olle 553 */
4785 27 Apr 18 olle 554       
4785 27 Apr 18 olle 555         dc.commit();
4785 27 Apr 18 olle 556         json.put("messages", jsonMessages);
4785 27 Apr 18 olle 557       }
4785 27 Apr 18 olle 558       CounterService.getInstance().setForceCount();
4785 27 Apr 18 olle 559       //ProjectArchiveService.getInstance().setForceCheck();
4785 27 Apr 18 olle 560     }
4785 27 Apr 18 olle 561     catch (Throwable t)
4785 27 Apr 18 olle 562     {
4785 27 Apr 18 olle 563       t.printStackTrace();
4785 27 Apr 18 olle 564       json.clear();
4785 27 Apr 18 olle 565       json.put("status", "error");
4785 27 Apr 18 olle 566       json.put("message", t.getMessage());
4785 27 Apr 18 olle 567       json.put("stacktrace", ThrowableUtil.stackTraceToString(t));
4785 27 Apr 18 olle 568     }
4785 27 Apr 18 olle 569     finally
4785 27 Apr 18 olle 570     {
4785 27 Apr 18 olle 571       if (dc != null) dc.close();
4785 27 Apr 18 olle 572       json.writeJSONString(resp.getWriter());
4785 27 Apr 18 olle 573     }
4785 27 Apr 18 olle 574     
4785 27 Apr 18 olle 575   }
4785 27 Apr 18 olle 576
4860 19 Jun 18 olle 577   @SuppressWarnings("unchecked")
4860 19 Jun 18 olle 578   private BioSource getOrCreatePatient(DbControl dc, JSONObject jsonPat, JSONArray jsonMessages)
4860 19 Jun 18 olle 579     throws Exception
4860 19 Jun 18 olle 580   {
4860 19 Jun 18 olle 581     Number patientId = (Number)jsonPat.get("id");
4860 19 Jun 18 olle 582     BioSource patient = null;
4860 19 Jun 18 olle 583     String familyName = Values.getStringOrNull((String)jsonPat.get("familyName"));
4860 19 Jun 18 olle 584     String allFirstNames = Values.getStringOrNull((String)jsonPat.get("allFirstNames"));
4860 19 Jun 18 olle 585     if (patientId != null)
4860 19 Jun 18 olle 586     {
4860 19 Jun 18 olle 587       patient = BioSource.getById(dc, patientId.intValue());
4860 19 Jun 18 olle 588       // Update names
4860 19 Jun 18 olle 589       Annotationtype.FAMILY_NAME.setAnnotationValue(dc, patient, familyName);
4860 19 Jun 18 olle 590       Annotationtype.ALL_FIRST_NAMES.setAnnotationValue(dc, patient, allFirstNames);
4860 19 Jun 18 olle 591     }
4860 19 Jun 18 olle 592     else
4860 19 Jun 18 olle 593     {
4860 19 Jun 18 olle 594       String pnr = (String)jsonPat.get("personalNumber");
4860 19 Jun 18 olle 595       String patName = (String)jsonPat.get("name");
4860 19 Jun 18 olle 596       Patient.ensureNotExistingPatient(dc, pnr, patName);
4860 19 Jun 18 olle 597
4860 19 Jun 18 olle 598       // Create new patient
4860 19 Jun 18 olle 599       patient = BioSource.getNew(dc);
4860 19 Jun 18 olle 600       patient.setItemSubtype(Subtype.PATIENT.load(dc));
4860 19 Jun 18 olle 601       patient.setName((String)jsonPat.get("name"));
4860 19 Jun 18 olle 602       StringToDateConverter dateConverter = new StringToDateConverter(new SimpleDateFormat("yyyy-MM-dd"));
4860 19 Jun 18 olle 603       Date dateOfBirth = dateConverter.convert((String)jsonPat.get("dateOfBirth"));
4860 19 Jun 18 olle 604       String gender = Values.getStringOrNull((String)jsonPat.get("gender"));
4860 19 Jun 18 olle 605       
4860 19 Jun 18 olle 606       Annotationtype.PERSONAL_NUMBER.setAnnotationValue(dc, patient, pnr);
4860 19 Jun 18 olle 607       Annotationtype.FAMILY_NAME.setAnnotationValue(dc, patient, familyName);
4860 19 Jun 18 olle 608       Annotationtype.ALL_FIRST_NAMES.setAnnotationValue(dc, patient, allFirstNames);
4860 19 Jun 18 olle 609       Annotationtype.GENDER.setAnnotationValue(dc, patient, gender);
4860 19 Jun 18 olle 610       Annotationtype.DATE_OF_BIRTH.setAnnotationValue(dc, patient, dateOfBirth);
4860 19 Jun 18 olle 611       
4860 19 Jun 18 olle 612       if (gender == null || dateOfBirth == null)
4860 19 Jun 18 olle 613       {
4860 19 Jun 18 olle 614         patient.setDescription("This patient was recorded with a non-standard PersonalNumber. " +
4860 19 Jun 18 olle 615             "DateOfBirth and Gender annotations could not be automatically generated.");
4860 19 Jun 18 olle 616       }
4860 19 Jun 18 olle 617       dc.saveItem(patient);
4860 19 Jun 18 olle 618       
4860 19 Jun 18 olle 619       jsonMessages.add("Patient '" + patient.getName() + "' created successfully.");
4860 19 Jun 18 olle 620     }
4860 19 Jun 18 olle 621     return patient;
4860 19 Jun 18 olle 622
4860 19 Jun 18 olle 623   }
4860 19 Jun 18 olle 624   
4785 27 Apr 18 olle 625   /**
4785 27 Apr 18 olle 626    * Returns the site for the sample, based on the latter's site annotation.
4785 27 Apr 18 olle 627    * 
4785 27 Apr 18 olle 628    * @param dc DbControl The DbControl to use.
4785 27 Apr 18 olle 629    * @param manager SnapshotManager The SnapshotManager to use for speeding up annotation retrieval.
4785 27 Apr 18 olle 630    * @param s Sample The sample to find the site for.
4785 27 Apr 18 olle 631    * @return Site The site object found for the sample or UNKNOWN.
4785 27 Apr 18 olle 632    */
4785 27 Apr 18 olle 633   private Site fetchSite(DbControl dc, SnapshotManager manager, Sample s)
4785 27 Apr 18 olle 634   {
4785 27 Apr 18 olle 635     Site site = Site.UNKNOWN;
4785 27 Apr 18 olle 636     String prefix = (String) Annotationtype.SITE.getAnnotationValue(dc, manager, s);
4785 27 Apr 18 olle 637     if (prefix != null)
4785 27 Apr 18 olle 638     {
4785 27 Apr 18 olle 639       site = Site.findByPrefix(prefix);
4785 27 Apr 18 olle 640     }
4785 27 Apr 18 olle 641     return site;
4785 27 Apr 18 olle 642   }  
4785 27 Apr 18 olle 643
4785 27 Apr 18 olle 644 }