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

Code
Comments
Other
Rev Date Author Line
1354 28 Apr 11 nicklas 1 package net.sf.basedb.reggie.servlet;
1354 28 Apr 11 nicklas 2
1354 28 Apr 11 nicklas 3 import java.io.IOException;
1354 28 Apr 11 nicklas 4 import java.util.Date;
1354 28 Apr 11 nicklas 5 import java.util.List;
1354 28 Apr 11 nicklas 6
1354 28 Apr 11 nicklas 7 import javax.servlet.ServletException;
1354 28 Apr 11 nicklas 8 import javax.servlet.http.HttpServlet;
1354 28 Apr 11 nicklas 9 import javax.servlet.http.HttpServletRequest;
1354 28 Apr 11 nicklas 10 import javax.servlet.http.HttpServletResponse;
1354 28 Apr 11 nicklas 11
1354 28 Apr 11 nicklas 12 import org.json.simple.JSONArray;
1354 28 Apr 11 nicklas 13 import org.json.simple.JSONObject;
1354 28 Apr 11 nicklas 14
1504 18 Jan 12 nicklas 15 import net.sf.basedb.core.BioSource;
1354 28 Apr 11 nicklas 16 import net.sf.basedb.core.DbControl;
1521 24 Jan 12 nicklas 17 import net.sf.basedb.core.InvalidDataException;
1354 28 Apr 11 nicklas 18 import net.sf.basedb.core.Sample;
1354 28 Apr 11 nicklas 19 import net.sf.basedb.core.SessionControl;
2598 22 Aug 14 nicklas 20 import net.sf.basedb.reggie.JsonUtil;
1354 28 Apr 11 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;
3059 19 Dec 14 nicklas 24 import net.sf.basedb.reggie.counter.CounterService;
1610 23 Apr 12 nicklas 25 import net.sf.basedb.reggie.dao.Annotationtype;
1519 23 Jan 12 nicklas 26 import net.sf.basedb.reggie.dao.Blood;
1354 28 Apr 11 nicklas 27 import net.sf.basedb.reggie.dao.Case;
1524 25 Jan 12 nicklas 28 import net.sf.basedb.reggie.dao.Consent;
1504 18 Jan 12 nicklas 29 import net.sf.basedb.reggie.dao.Patient;
4460 21 Apr 17 nicklas 30 import net.sf.basedb.reggie.dao.ReferenceDateSource;
4460 21 Apr 17 nicklas 31 import net.sf.basedb.reggie.dao.ReferenceDateWithSource;
5423 13 May 19 nicklas 32 import net.sf.basedb.reggie.dao.ReggieItem;
2161 09 Dec 13 nicklas 33 import net.sf.basedb.reggie.dao.ReggieRole;
1354 28 Apr 11 nicklas 34 import net.sf.basedb.reggie.dao.SpecimenTube;
1463 14 Nov 11 martin 35 import net.sf.basedb.reggie.dao.Subtype;
3845 14 Apr 16 nicklas 36 import net.sf.basedb.reggie.projectarchive.ProjectArchiveService;
1354 28 Apr 11 nicklas 37 import net.sf.basedb.util.Values;
1354 28 Apr 11 nicklas 38 import net.sf.basedb.util.error.ThrowableUtil;
1354 28 Apr 11 nicklas 39
1354 28 Apr 11 nicklas 40
1354 28 Apr 11 nicklas 41 public class ConsentFormServlet 
1354 28 Apr 11 nicklas 42   extends HttpServlet 
1354 28 Apr 11 nicklas 43 {
1354 28 Apr 11 nicklas 44
1354 28 Apr 11 nicklas 45   private static final long serialVersionUID = 28308664025515890L;
1354 28 Apr 11 nicklas 46
1354 28 Apr 11 nicklas 47   public ConsentFormServlet()
1524 25 Jan 12 nicklas 48   {}
1354 28 Apr 11 nicklas 49
1354 28 Apr 11 nicklas 50   @Override
1354 28 Apr 11 nicklas 51   protected void doGet(HttpServletRequest req, HttpServletResponse resp)
1354 28 Apr 11 nicklas 52     throws ServletException, IOException 
1354 28 Apr 11 nicklas 53   {
1354 28 Apr 11 nicklas 54     String cmd = req.getParameter("cmd");
2598 22 Aug 14 nicklas 55     JsonUtil.setJsonResponseHeaders(resp);
1354 28 Apr 11 nicklas 56     
1354 28 Apr 11 nicklas 57     JSONObject json = new JSONObject();
1354 28 Apr 11 nicklas 58     json.put("status", "ok");
1354 28 Apr 11 nicklas 59   
3975 26 May 16 nicklas 60     final SessionControl sc = Reggie.getSessionControl(req);
1354 28 Apr 11 nicklas 61     DbControl dc = null;
1354 28 Apr 11 nicklas 62     try
1354 28 Apr 11 nicklas 63     {
1354 28 Apr 11 nicklas 64       if ("GetCaseInfo".equals(cmd))
1354 28 Apr 11 nicklas 65       {
1354 28 Apr 11 nicklas 66         /*
1521 24 Jan 12 nicklas 67           Load consent-related information about a case. Basically there are 4
1521 24 Jan 12 nicklas 68           outcomes:
1521 24 Jan 12 nicklas 69           1. No info exists for the given case --> Allow registration of free-floating case (Yes, No, Not asked)
1521 24 Jan 12 nicklas 70           2. Only specimen tubes exists --> Only allow registration of Yes as a free-floating case
1521 24 Jan 12 nicklas 71           3. Free-floating case exists (with Yes, No or Not asked) --> No changes are allowed
1521 24 Jan 12 nicklas 72           4. A real case or blood sample exists which connects to a patient --> List all cases + blood samples
1521 24 Jan 12 nicklas 73              for the patient with current consent status and allow Yes + date to be registered for those
5058 29 Oct 18 nicklas 74              do not have a consent yet. If the current consent is 'No' (could happen if a patient has
5058 29 Oct 18 nicklas 75              retracted the consent but the retraction process has not been completed yet), no changes are allowed.
1354 28 Apr 11 nicklas 76         */
6329 14 Jun 21 nicklas 77         dc = sc.newDbControl(":Consent form registration");
1354 28 Apr 11 nicklas 78         
1354 28 Apr 11 nicklas 79         String caseName = req.getParameter("caseName");
1614 24 Apr 12 nicklas 80         Site site = Site.findByCaseName(caseName);
1520 23 Jan 12 nicklas 81         Patient patient = null;
1519 23 Jan 12 nicklas 82         
1521 24 Jan 12 nicklas 83         // First try to locate a case + patient
1521 24 Jan 12 nicklas 84         Case theCase = Case.findByName(dc, caseName);
1521 24 Jan 12 nicklas 85         if (theCase != null) 
1354 28 Apr 11 nicklas 86         {
1520 23 Jan 12 nicklas 87           patient = Patient.findByCase(dc, theCase);
1521 24 Jan 12 nicklas 88           // We do not want the 'virtual patients' used for statisicts
1521 24 Jan 12 nicklas 89           if (patient != null && !patient.getName().startsWith("PAT")) patient = null;
1354 28 Apr 11 nicklas 90         }
1521 24 Jan 12 nicklas 91         
1521 24 Jan 12 nicklas 92         // Then, check for a blood sample + patient
1623 26 Apr 12 nicklas 93         Blood blood = Blood.findByCaseName(dc, caseName, site.useCaseSuffixForPreNeoForms());
1623 26 Apr 12 nicklas 94         if (blood == null && site.useCaseSuffixForPreNeoForms())
1622 25 Apr 12 nicklas 95         {
1622 25 Apr 12 nicklas 96           // Second try without checking 'C' suffix
1622 25 Apr 12 nicklas 97           blood = Blood.findByCaseName(dc, caseName, false);
1622 25 Apr 12 nicklas 98         }
1521 24 Jan 12 nicklas 99         if (blood != null) 
1354 28 Apr 11 nicklas 100         {
1521 24 Jan 12 nicklas 101           Patient bloodPatient = Patient.findByBlood(dc, blood);
1521 24 Jan 12 nicklas 102           if (bloodPatient != null)
1354 28 Apr 11 nicklas 103           {
1521 24 Jan 12 nicklas 104             // Verify that both samples have the same patient
1521 24 Jan 12 nicklas 105             if (patient != null && !bloodPatient.equals(patient))
1354 28 Apr 11 nicklas 106             {
1521 24 Jan 12 nicklas 107               throw new InvalidDataException("Found case '" + theCase.getName() + "' and blood '" + blood.getName() + 
1521 24 Jan 12 nicklas 108                 "' but they have not the same patient. This wizard can't continue until that is corrected.");  
1354 28 Apr 11 nicklas 109             }
1521 24 Jan 12 nicklas 110             patient = bloodPatient;
1354 28 Apr 11 nicklas 111           }
1354 28 Apr 11 nicklas 112         }
1354 28 Apr 11 nicklas 113         
1520 23 Jan 12 nicklas 114         if (patient != null)
1520 23 Jan 12 nicklas 115         {
1521 24 Jan 12 nicklas 116           // This is case 4 as described above.
1520 23 Jan 12 nicklas 117           JSONObject jsonPatient = patient.asJSONObject();
1520 23 Jan 12 nicklas 118           
1520 23 Jan 12 nicklas 119           // Load all cases for the patient
1520 23 Jan 12 nicklas 120           List<Case> allCases = Case.findByPatient(dc, patient);
1520 23 Jan 12 nicklas 121           JSONArray jsonAll = new JSONArray();
1521 24 Jan 12 nicklas 122           for (Case c : allCases)
1520 23 Jan 12 nicklas 123           {
1524 25 Jan 12 nicklas 124             Consent.loadConsentAnnotations(dc, c);
1610 23 Apr 12 nicklas 125             c.loadAnnotations(dc, "laterality", Annotationtype.LATERALITY, null);
1521 24 Jan 12 nicklas 126             jsonAll.add(c.asJSONObject());
1520 23 Jan 12 nicklas 127           }
1520 23 Jan 12 nicklas 128           jsonPatient.put("allCases", jsonAll);
1520 23 Jan 12 nicklas 129           
1521 24 Jan 12 nicklas 130           // Load all blood samples for the patient
1520 23 Jan 12 nicklas 131           List<Blood> allBlood = Blood.findByPatient(dc, patient);
1520 23 Jan 12 nicklas 132           jsonAll = new JSONArray();
1521 24 Jan 12 nicklas 133           for (Blood b : allBlood)
1520 23 Jan 12 nicklas 134           {
1524 25 Jan 12 nicklas 135             Consent.loadConsentAnnotations(dc, b);
1521 24 Jan 12 nicklas 136             jsonAll.add(b.asJSONObject());
1520 23 Jan 12 nicklas 137           }
1520 23 Jan 12 nicklas 138           jsonPatient.put("allBlood", jsonAll);
1521 24 Jan 12 nicklas 139           
1520 23 Jan 12 nicklas 140           json.put("patientInfo", jsonPatient);
1520 23 Jan 12 nicklas 141         }
1521 24 Jan 12 nicklas 142         else if (theCase != null)
1521 24 Jan 12 nicklas 143         {
1521 24 Jan 12 nicklas 144           // This is case 3 as described above.
1524 25 Jan 12 nicklas 145           Consent.loadConsentAnnotations(dc, theCase);
1521 24 Jan 12 nicklas 146           json.put("caseInfo", theCase.asJSONObject());
1521 24 Jan 12 nicklas 147         }
1521 24 Jan 12 nicklas 148         else
1521 24 Jan 12 nicklas 149         {
1521 24 Jan 12 nicklas 150           // Check if specimen tubes have been registered
1623 26 Apr 12 nicklas 151           List<SpecimenTube> specimenTubes = SpecimenTube.findByCaseName(dc, caseName, false);
1521 24 Jan 12 nicklas 152           if (specimenTubes != null && specimenTubes.size() > 0)
1521 24 Jan 12 nicklas 153           {
1521 24 Jan 12 nicklas 154             //  This is case 2 as described above.
1521 24 Jan 12 nicklas 155             JSONArray jsonTubes = new JSONArray();
1521 24 Jan 12 nicklas 156             for (SpecimenTube tube : specimenTubes)
1521 24 Jan 12 nicklas 157             {
1521 24 Jan 12 nicklas 158               jsonTubes.add(tube.asJSONObject());
1521 24 Jan 12 nicklas 159             }
1521 24 Jan 12 nicklas 160             json.put("specimenInfo", jsonTubes);
1521 24 Jan 12 nicklas 161           }
1521 24 Jan 12 nicklas 162           else
1521 24 Jan 12 nicklas 163           {
1521 24 Jan 12 nicklas 164             // This is case 1 as described above -- no info is returned
1521 24 Jan 12 nicklas 165           }
1521 24 Jan 12 nicklas 166         }
1354 28 Apr 11 nicklas 167       }
1354 28 Apr 11 nicklas 168     }
1354 28 Apr 11 nicklas 169     catch (Throwable t)
1354 28 Apr 11 nicklas 170     {
1354 28 Apr 11 nicklas 171       t.printStackTrace();
1354 28 Apr 11 nicklas 172       json.clear();
1354 28 Apr 11 nicklas 173       json.put("status", "error");
1354 28 Apr 11 nicklas 174       json.put("message", t.getMessage());
1354 28 Apr 11 nicklas 175       json.put("stacktrace", ThrowableUtil.stackTraceToString(t));
1354 28 Apr 11 nicklas 176     }
1354 28 Apr 11 nicklas 177     finally
1354 28 Apr 11 nicklas 178     {
1354 28 Apr 11 nicklas 179       if (dc != null) dc.close();
1354 28 Apr 11 nicklas 180       json.writeJSONString(resp.getWriter());
1354 28 Apr 11 nicklas 181     }
1354 28 Apr 11 nicklas 182   }
1354 28 Apr 11 nicklas 183
1354 28 Apr 11 nicklas 184   @SuppressWarnings("unchecked")
1354 28 Apr 11 nicklas 185   @Override
1354 28 Apr 11 nicklas 186   protected void doPost(HttpServletRequest req, HttpServletResponse resp)
1354 28 Apr 11 nicklas 187     throws ServletException, IOException 
1354 28 Apr 11 nicklas 188   {
1354 28 Apr 11 nicklas 189     String cmd = req.getParameter("cmd");
2598 22 Aug 14 nicklas 190     JsonUtil.setJsonResponseHeaders(resp);
1354 28 Apr 11 nicklas 191     
1354 28 Apr 11 nicklas 192     JSONObject json = new JSONObject();
1354 28 Apr 11 nicklas 193     json.put("status", "ok");
1354 28 Apr 11 nicklas 194     
1354 28 Apr 11 nicklas 195     JSONArray jsonMessages = new JSONArray();
1354 28 Apr 11 nicklas 196   
3975 26 May 16 nicklas 197     final SessionControl sc = Reggie.getSessionControl(req);
1354 28 Apr 11 nicklas 198     DbControl dc = null;
1354 28 Apr 11 nicklas 199     try
1354 28 Apr 11 nicklas 200     {
1354 28 Apr 11 nicklas 201       if ("RegisterConsent".equals(cmd))
1354 28 Apr 11 nicklas 202       {
6329 14 Jun 21 nicklas 203         dc = sc.newDbControl(":Consent form registration");
2161 09 Dec 13 nicklas 204
2161 09 Dec 13 nicklas 205         ReggieRole.checkPermission(dc, "'" + cmd + "' wizard", ReggieRole.PATIENT_CURATOR, ReggieRole.ADMINISTRATOR);
2161 09 Dec 13 nicklas 206
3752 17 Feb 16 nicklas 207         JSONObject jsonReq = JsonUtil.parseRequest(req);
1521 24 Jan 12 nicklas 208         JSONObject jsonConsent = (JSONObject)jsonReq.get("consentInfo");
4460 21 Apr 17 nicklas 209
4460 21 Apr 17 nicklas 210         // Use the registration date as the starting point for the reference date
4460 21 Apr 17 nicklas 211         ReferenceDateWithSource refDate = new ReferenceDateWithSource(new Date(), ReferenceDateSource.REGISTRATION_DATE);
1354 28 Apr 11 nicklas 212         
1521 24 Jan 12 nicklas 213         String caseName = (String)jsonConsent.get("caseName");
1521 24 Jan 12 nicklas 214         String consent = Values.getStringOrNull((String)jsonConsent.get("consent"));
1524 25 Jan 12 nicklas 215         Date consentDate = Reggie.CONVERTER_STRING_TO_DATE.convert((String)jsonConsent.get("consentDate"));
1521 24 Jan 12 nicklas 216         // ID of samples can be both case and blood samples
1521 24 Jan 12 nicklas 217         List<Number> selectedSamples = (List<Number>)jsonConsent.get("selectedSamples");
1521 24 Jan 12 nicklas 218
4460 21 Apr 17 nicklas 219         if (consentDate != null)
4460 21 Apr 17 nicklas 220         {
4460 21 Apr 17 nicklas 221           refDate = refDate.getThisOrTheReplacement(new ReferenceDateWithSource(consentDate, ReferenceDateSource.CONSENT_DATE));
4460 21 Apr 17 nicklas 222         }
4460 21 Apr 17 nicklas 223         
1354 28 Apr 11 nicklas 224         Sample theCase = null;
1521 24 Jan 12 nicklas 225         if (selectedSamples == null)
1354 28 Apr 11 nicklas 226         {
1354 28 Apr 11 nicklas 227           // Create new case
1354 28 Apr 11 nicklas 228           theCase = Sample.getNew(dc);
1463 14 Nov 11 martin 229           theCase.setItemSubtype(Subtype.CASE.load(dc));
1354 28 Apr 11 nicklas 230           theCase.setName(caseName);
5423 13 May 19 nicklas 231           ReggieItem.ensureNonExistingItem(dc, Subtype.CASE, caseName);
1354 28 Apr 11 nicklas 232           if ("Not asked".equals(consent))
1354 28 Apr 11 nicklas 233           {
1521 24 Jan 12 nicklas 234             theCase.setDescription((String)jsonConsent.get("reasonIfNotAsked"));
1354 28 Apr 11 nicklas 235           }
1504 18 Jan 12 nicklas 236           if (!"Yes".equals(consent))
1504 18 Jan 12 nicklas 237           {
1504 18 Jan 12 nicklas 238             // Create virtual patient
1504 18 Jan 12 nicklas 239             BioSource vPat = BioSource.getNew(dc);
1526 25 Jan 12 nicklas 240             Subtype virtualSubtype = "No".equals(consent) ? Subtype.NO : Subtype.NOT_ASKED;
1526 25 Jan 12 nicklas 241             vPat.setItemSubtype(virtualSubtype.load(dc));
4894 09 Jul 18 nicklas 242             vPat.setName(Patient.getNextName(dc, virtualSubtype, true));
5088 13 Nov 18 nicklas 243             vPat.setExternalId(Patient.getNextExternalId(dc, virtualSubtype));
1504 18 Jan 12 nicklas 244             theCase.getCreationEvent().setSource(vPat);
1504 18 Jan 12 nicklas 245             dc.saveItem(vPat);
1521 24 Jan 12 nicklas 246             jsonMessages.add("Virtual patient '" + vPat.getName() + "' created.");
1504 18 Jan 12 nicklas 247           }
4069 02 Sep 16 nicklas 248           else
4069 02 Sep 16 nicklas 249           {
4069 02 Sep 16 nicklas 250             // Generate External ID
4902 10 Jul 18 nicklas 251             theCase.setExternalId(Case.getNextExternalId(dc));
4069 02 Sep 16 nicklas 252           }
1610 23 Apr 12 nicklas 253           Annotationtype.CONSENT.setAnnotationValue(dc, theCase, consent);
1610 23 Apr 12 nicklas 254           if (consentDate != null) Annotationtype.CONSENT_DATE.setAnnotationValue(dc, theCase, consentDate);
1504 18 Jan 12 nicklas 255           
4460 21 Apr 17 nicklas 256           if (refDate != null) refDate.updateAnnotations(dc, theCase);
4460 21 Apr 17 nicklas 257           
1354 28 Apr 11 nicklas 258           dc.saveItem(theCase);
1354 28 Apr 11 nicklas 259           jsonMessages.add("Case '" + caseName + "' created successfully with consent: " + consent);
1354 28 Apr 11 nicklas 260         }
1354 28 Apr 11 nicklas 261         else
1354 28 Apr 11 nicklas 262         {
1521 24 Jan 12 nicklas 263           for (Number id : selectedSamples)
1520 23 Jan 12 nicklas 264           {
1521 24 Jan 12 nicklas 265             theCase = Sample.getById(dc, id.intValue());
1521 24 Jan 12 nicklas 266             boolean isBlood = Subtype.BLOOD.load(dc).equals(theCase.getItemSubtype());
1610 23 Apr 12 nicklas 267             Annotationtype.CONSENT.setAnnotationValue(dc, theCase, consent);
1610 23 Apr 12 nicklas 268             if (consentDate != null) Annotationtype.CONSENT_DATE.setAnnotationValue(dc, theCase, consentDate);
1521 24 Jan 12 nicklas 269             if (isBlood)
1520 23 Jan 12 nicklas 270             {
1521 24 Jan 12 nicklas 271               jsonMessages.add("Consent for blood '" + theCase.getName() + "' set to: " + consent);
1521 24 Jan 12 nicklas 272             }
1521 24 Jan 12 nicklas 273             else
1521 24 Jan 12 nicklas 274             {
4460 21 Apr 17 nicklas 275               ReferenceDateWithSource refDateForThisCase = refDate.getThisOrTheReplacement(ReferenceDateWithSource.getFromAnnotations(dc, theCase));
4460 21 Apr 17 nicklas 276               refDateForThisCase.updateAnnotations(dc, theCase);
4460 21 Apr 17 nicklas 277               
1520 23 Jan 12 nicklas 278               jsonMessages.add("Consent for case '" + theCase.getName() + "' set to: " + consent);
1520 23 Jan 12 nicklas 279             }
1520 23 Jan 12 nicklas 280           }
1354 28 Apr 11 nicklas 281         }
5421 13 May 19 nicklas 282         ActivityDef.REGISTERED_CONSENT.merge(dc, 1);
1354 28 Apr 11 nicklas 283         dc.commit();
1354 28 Apr 11 nicklas 284         json.put("messages", jsonMessages);
1354 28 Apr 11 nicklas 285       }
3845 14 Apr 16 nicklas 286       CounterService.getInstance().setForceCount();
3845 14 Apr 16 nicklas 287       ProjectArchiveService.getInstance().setForceCheck();
1354 28 Apr 11 nicklas 288     }
1354 28 Apr 11 nicklas 289     catch (Throwable t)
1354 28 Apr 11 nicklas 290     {
1354 28 Apr 11 nicklas 291       t.printStackTrace();
1354 28 Apr 11 nicklas 292       json.clear();
1354 28 Apr 11 nicklas 293       json.put("status", "error");
1354 28 Apr 11 nicklas 294       json.put("message", t.getMessage());
1354 28 Apr 11 nicklas 295       json.put("stacktrace", ThrowableUtil.stackTraceToString(t));
1354 28 Apr 11 nicklas 296     }
1354 28 Apr 11 nicklas 297     finally
1354 28 Apr 11 nicklas 298     {
1354 28 Apr 11 nicklas 299       if (dc != null) dc.close();
1354 28 Apr 11 nicklas 300       json.writeJSONString(resp.getWriter());
1354 28 Apr 11 nicklas 301     }
1354 28 Apr 11 nicklas 302     
1354 28 Apr 11 nicklas 303   }
1524 25 Jan 12 nicklas 304
1354 28 Apr 11 nicklas 305   
1524 25 Jan 12 nicklas 306
1354 28 Apr 11 nicklas 307 }