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

Code
Comments
Other
Rev Date Author Line
1283 01 Feb 11 nicklas 1 package net.sf.basedb.reggie.servlet;
1283 01 Feb 11 nicklas 2
1283 01 Feb 11 nicklas 3 import java.io.IOException;
1333 05 Apr 11 nicklas 4 import java.util.Date;
4034 29 Jul 16 nicklas 5 import java.util.HashSet;
1283 01 Feb 11 nicklas 6 import java.util.List;
4034 29 Jul 16 nicklas 7 import java.util.Set;
1283 01 Feb 11 nicklas 8
1283 01 Feb 11 nicklas 9 import javax.servlet.ServletException;
1283 01 Feb 11 nicklas 10 import javax.servlet.http.HttpServlet;
1283 01 Feb 11 nicklas 11 import javax.servlet.http.HttpServletRequest;
1283 01 Feb 11 nicklas 12 import javax.servlet.http.HttpServletResponse;
1283 01 Feb 11 nicklas 13
7024 07 Feb 23 nicklas 14 import org.apache.commons.lang3.time.FastDateFormat;
1283 01 Feb 11 nicklas 15 import org.json.simple.JSONArray;
1283 01 Feb 11 nicklas 16 import org.json.simple.JSONObject;
1283 01 Feb 11 nicklas 17
1286 08 Feb 11 nicklas 18 import net.sf.basedb.core.AnnotationSet;
1283 01 Feb 11 nicklas 19 import net.sf.basedb.core.AnnotationType;
1595 27 Mar 12 nicklas 20 import net.sf.basedb.core.BioMaterialEvent;
1283 01 Feb 11 nicklas 21 import net.sf.basedb.core.BioSource;
1283 01 Feb 11 nicklas 22 import net.sf.basedb.core.DbControl;
1608 20 Apr 12 nicklas 23 import net.sf.basedb.core.InvalidDataException;
6998 20 Jan 23 nicklas 24 import net.sf.basedb.core.Item;
1981 17 May 13 nicklas 25 import net.sf.basedb.core.ItemQuery;
4034 29 Jul 16 nicklas 26 import net.sf.basedb.core.PluginExecutionRequest;
4034 29 Jul 16 nicklas 27 import net.sf.basedb.core.PluginResponse;
1283 01 Feb 11 nicklas 28 import net.sf.basedb.core.Sample;
1283 01 Feb 11 nicklas 29 import net.sf.basedb.core.SessionControl;
4034 29 Jul 16 nicklas 30 import net.sf.basedb.core.SharedItem;
2840 20 Oct 14 nicklas 31 import net.sf.basedb.core.Type;
4034 29 Jul 16 nicklas 32 import net.sf.basedb.core.plugin.Response;
1981 17 May 13 nicklas 33 import net.sf.basedb.core.query.Annotations;
1981 17 May 13 nicklas 34 import net.sf.basedb.core.query.Expressions;
1981 17 May 13 nicklas 35 import net.sf.basedb.core.query.Hql;
6510 03 Dec 21 nicklas 36 import net.sf.basedb.core.query.Orders;
1981 17 May 13 nicklas 37 import net.sf.basedb.core.query.Restrictions;
2598 22 Aug 14 nicklas 38 import net.sf.basedb.reggie.JsonUtil;
1283 01 Feb 11 nicklas 39 import net.sf.basedb.reggie.Reggie;
1614 24 Apr 12 nicklas 40 import net.sf.basedb.reggie.Site;
5387 26 Apr 19 nicklas 41 import net.sf.basedb.reggie.activity.ActivityDef;
1767 04 Dec 12 nicklas 42 import net.sf.basedb.reggie.converter.StringToDateConverter;
3059 19 Dec 14 nicklas 43 import net.sf.basedb.reggie.counter.CounterService;
1610 23 Apr 12 nicklas 44 import net.sf.basedb.reggie.dao.Annotationtype;
1518 23 Jan 12 nicklas 45 import net.sf.basedb.reggie.dao.Blood;
1326 29 Mar 11 nicklas 46 import net.sf.basedb.reggie.dao.Case;
1524 25 Jan 12 nicklas 47 import net.sf.basedb.reggie.dao.Consent;
1608 20 Apr 12 nicklas 48 import net.sf.basedb.reggie.dao.NoSpecimen;
1326 29 Mar 11 nicklas 49 import net.sf.basedb.reggie.dao.Patient;
4460 21 Apr 17 nicklas 50 import net.sf.basedb.reggie.dao.ReferenceDateSource;
4460 21 Apr 17 nicklas 51 import net.sf.basedb.reggie.dao.ReferenceDateWithSource;
4898 10 Jul 18 nicklas 52 import net.sf.basedb.reggie.dao.ReggieItem;
2161 09 Dec 13 nicklas 53 import net.sf.basedb.reggie.dao.ReggieRole;
1326 29 Mar 11 nicklas 54 import net.sf.basedb.reggie.dao.SpecimenTube;
1463 14 Nov 11 martin 55 import net.sf.basedb.reggie.dao.Subtype;
4034 29 Jul 16 nicklas 56 import net.sf.basedb.reggie.plugins.SetPermissionsForDeliveryPlugin;
3726 28 Jan 16 nicklas 57 import net.sf.basedb.util.EqualsHelper;
1283 01 Feb 11 nicklas 58 import net.sf.basedb.util.MD5;
1290 21 Feb 11 nicklas 59 import net.sf.basedb.util.Values;
1283 01 Feb 11 nicklas 60 import net.sf.basedb.util.error.ThrowableUtil;
1283 01 Feb 11 nicklas 61
1283 01 Feb 11 nicklas 62
1286 08 Feb 11 nicklas 63 public class PersonalRegistrationServlet 
1283 01 Feb 11 nicklas 64   extends HttpServlet 
1283 01 Feb 11 nicklas 65 {
1283 01 Feb 11 nicklas 66
1289 14 Feb 11 nicklas 67   private static final long serialVersionUID = 8770173686061080429L;
1523 24 Jan 12 nicklas 68   
1286 08 Feb 11 nicklas 69   public PersonalRegistrationServlet()
1524 25 Jan 12 nicklas 70   {}
1283 01 Feb 11 nicklas 71
1283 01 Feb 11 nicklas 72   @Override
1283 01 Feb 11 nicklas 73   protected void doGet(HttpServletRequest req, HttpServletResponse resp)
1283 01 Feb 11 nicklas 74     throws ServletException, IOException 
1283 01 Feb 11 nicklas 75   {
1283 01 Feb 11 nicklas 76     String cmd = req.getParameter("cmd");
2598 22 Aug 14 nicklas 77     JsonUtil.setJsonResponseHeaders(resp);
1283 01 Feb 11 nicklas 78     
1283 01 Feb 11 nicklas 79     JSONObject json = new JSONObject();
1283 01 Feb 11 nicklas 80     json.put("status", "ok");
1283 01 Feb 11 nicklas 81   
3975 26 May 16 nicklas 82     final SessionControl sc = Reggie.getSessionControl(req);
1283 01 Feb 11 nicklas 83     DbControl dc = null;
1283 01 Feb 11 nicklas 84     try
1283 01 Feb 11 nicklas 85     {
3052 18 Dec 14 nicklas 86       if ("CountAllPatients".equals(cmd))
1283 01 Feb 11 nicklas 87       {
6329 14 Jun 21 nicklas 88         dc = sc.newDbControl(":Pathology referral form registration");
3052 18 Dec 14 nicklas 89         ItemQuery<BioSource> query = BioSource.getQuery();
3052 18 Dec 14 nicklas 90         query.setIncludes(Reggie.INCLUDE_IN_CURRENT_PROJECT);
3052 18 Dec 14 nicklas 91         Subtype.PATIENT.addFilter(dc, query);
3052 18 Dec 14 nicklas 92
3052 18 Dec 14 nicklas 93         query.setCacheResult(true);
3052 18 Dec 14 nicklas 94         long count = query.count(dc);
3052 18 Dec 14 nicklas 95         json.put("count", count);
3052 18 Dec 14 nicklas 96       }
6510 03 Dec 21 nicklas 97       else if ("GetSpecimenWithoutCase".equals(cmd))
6510 03 Dec 21 nicklas 98       {
6510 03 Dec 21 nicklas 99         dc = sc.newDbControl(":Pathology referral form registration");
6510 03 Dec 21 nicklas 100       
6510 03 Dec 21 nicklas 101         ItemQuery<Sample> query = Sample.getQuery();
6510 03 Dec 21 nicklas 102         Subtype.SPECIMEN.addFilter(dc, query);
6510 03 Dec 21 nicklas 103         query.setIncludes(Reggie.INCLUDE_IN_CURRENT_PROJECT);
6510 03 Dec 21 nicklas 104         query.restrict(Restrictions.eq(Hql.property("parent"), null));
6510 03 Dec 21 nicklas 105         // Filter on externalId to get rid of external samples that never get a parent item
6510 03 Dec 21 nicklas 106         query.restrict(Restrictions.like(Hql.property("externalId"), Expressions.string(Subtype.SPECIMEN.getExternalIdPrefix()+"%")));
6510 03 Dec 21 nicklas 107         query.order(Orders.asc(Hql.property("name")));
6510 03 Dec 21 nicklas 108
6510 03 Dec 21 nicklas 109         List<SpecimenTube> specimenTubes = SpecimenTube.toList(query.list(dc));
6510 03 Dec 21 nicklas 110         JSONArray jsonSpecimenTubes = new JSONArray();
6510 03 Dec 21 nicklas 111         String lastCaseName = null;
6510 03 Dec 21 nicklas 112         JSONObject jsonCase = null;
6510 03 Dec 21 nicklas 113         int numSpecimenForLastCase = 0;
6510 03 Dec 21 nicklas 114         for (SpecimenTube tube : specimenTubes)
6510 03 Dec 21 nicklas 115         {
6510 03 Dec 21 nicklas 116           String caseName = tube.getName().substring(0, 7);
6510 03 Dec 21 nicklas 117           String biopsyType = (String)Annotationtype.BIOPSY_TYPE.getAnnotationValue(dc, tube.getItem());
6510 03 Dec 21 nicklas 118           String externalRef = (String)Annotationtype.EXTERNAL_REF.getAnnotationValue(dc, tube.getItem());
6510 03 Dec 21 nicklas 119           if ("SpecimenCoreBiopsy".equals(biopsyType))
6510 03 Dec 21 nicklas 120           {
6510 03 Dec 21 nicklas 121             caseName += "C";
6510 03 Dec 21 nicklas 122           }
6510 03 Dec 21 nicklas 123           else if ("SpecimenCoreBiopsy2nd".equals(biopsyType))
6510 03 Dec 21 nicklas 124           {
6510 03 Dec 21 nicklas 125             caseName += "D";
6510 03 Dec 21 nicklas 126           }
6510 03 Dec 21 nicklas 127           if (!caseName.equals(lastCaseName))
6510 03 Dec 21 nicklas 128           {
6510 03 Dec 21 nicklas 129             numSpecimenForLastCase = 1;
6510 03 Dec 21 nicklas 130             jsonCase = new JSONObject();
6510 03 Dec 21 nicklas 131             jsonCase.put("name", caseName);
6510 03 Dec 21 nicklas 132             lastCaseName = caseName;
6510 03 Dec 21 nicklas 133             jsonSpecimenTubes.add(jsonCase);
6510 03 Dec 21 nicklas 134           }
6510 03 Dec 21 nicklas 135           else
6510 03 Dec 21 nicklas 136           {
6510 03 Dec 21 nicklas 137             numSpecimenForLastCase++;
6510 03 Dec 21 nicklas 138           }
6510 03 Dec 21 nicklas 139           if (externalRef != null) jsonCase.put("externalRef", externalRef);
6510 03 Dec 21 nicklas 140           jsonCase.put("numSpecimen", numSpecimenForLastCase);
6510 03 Dec 21 nicklas 141         }
6510 03 Dec 21 nicklas 142         json.put("specimenTubes", jsonSpecimenTubes);
6510 03 Dec 21 nicklas 143       }
3052 18 Dec 14 nicklas 144       else if ("GetCaseInfo".equals(cmd))
3052 18 Dec 14 nicklas 145       {
1326 29 Mar 11 nicklas 146         /*
1326 29 Mar 11 nicklas 147           Load information about a single case when given the case name/barcode
1326 29 Mar 11 nicklas 148           If a case is found we will load annotations, the patient it is associated with
1326 29 Mar 11 nicklas 149           and specimen tubes for the case. If no case is found we check to see
1326 29 Mar 11 nicklas 150           if there are any specimen tubes that have the case name as prefix and
1326 29 Mar 11 nicklas 151           load information about them.
1326 29 Mar 11 nicklas 152         */
6329 14 Jun 21 nicklas 153         dc = sc.newDbControl(":Pathology referral form registration");
1283 01 Feb 11 nicklas 154         
1326 29 Mar 11 nicklas 155         // Find a case by name
1286 08 Feb 11 nicklas 156         String caseName = req.getParameter("caseName");
1614 24 Apr 12 nicklas 157         Site site = Site.findByCaseName(caseName);
1326 29 Mar 11 nicklas 158         Case theCase = Case.findByName(dc, caseName);
1623 26 Apr 12 nicklas 159         Blood blood = Blood.findByCaseName(dc, caseName, site.useCaseSuffixForPreNeoForms());
2037 03 Oct 13 nicklas 160         if (blood == null && site.useCaseSuffixForPreNeoForms())
2037 03 Oct 13 nicklas 161         {
2037 03 Oct 13 nicklas 162           // Second try without checking 'C' suffix
2037 03 Oct 13 nicklas 163           blood = Blood.findByCaseName(dc, caseName, false);
2037 03 Oct 13 nicklas 164         }
1519 23 Jan 12 nicklas 165         
1326 29 Mar 11 nicklas 166         List<SpecimenTube> specimenTubes = null;
1608 20 Apr 12 nicklas 167         NoSpecimen noSpecimen = null;
1326 29 Mar 11 nicklas 168         JSONObject jsonCase = null;
1519 23 Jan 12 nicklas 169         JSONObject jsonBlood = null;
1283 01 Feb 11 nicklas 170         
1326 29 Mar 11 nicklas 171         if (theCase != null)
1283 01 Feb 11 nicklas 172         {
1513 19 Jan 12 nicklas 173           // Check that the registration is not prohibited
1341 11 Apr 11 nicklas 174           theCase.verifyConsent(dc, null);
1341 11 Apr 11 nicklas 175           
1513 19 Jan 12 nicklas 176           // Load specimen tubes for the case
1623 26 Apr 12 nicklas 177           specimenTubes = SpecimenTube.findByCase(dc, theCase, caseName, site.useCaseSuffixForPreNeoForms());
1513 19 Jan 12 nicklas 178           if (specimenTubes.size() == 0)
1326 29 Mar 11 nicklas 179           {
1513 19 Jan 12 nicklas 180             // No specimen tubes are linked with the case -- see if we can find some unlinked
1623 26 Apr 12 nicklas 181             specimenTubes = SpecimenTube.findByCaseName(dc, caseName, site.useCaseSuffixForPreNeoForms());
1326 29 Mar 11 nicklas 182           }
1608 20 Apr 12 nicklas 183           
1608 20 Apr 12 nicklas 184           if (specimenTubes.size() == 0)
1608 20 Apr 12 nicklas 185           {
1608 20 Apr 12 nicklas 186             // Still not found -- check if there are any 'NoSpecimen' items
1623 26 Apr 12 nicklas 187             List<NoSpecimen> nsList = NoSpecimen.findByCase(dc, theCase, caseName, site.useCaseSuffixForPreNeoForms());
1608 20 Apr 12 nicklas 188             if (nsList.size() > 1)
1608 20 Apr 12 nicklas 189             {
1608 20 Apr 12 nicklas 190               // There should be at most one 'NoSpecimen' item
1608 20 Apr 12 nicklas 191               throw new InvalidDataException(
1608 20 Apr 12 nicklas 192                 "Found " + nsList.size() + " 'NoSpecimen' samples for the case (" + caseName + 
1608 20 Apr 12 nicklas 193                 "). This wizard can't be used until that is corrected.");
1608 20 Apr 12 nicklas 194             }
1608 20 Apr 12 nicklas 195             else if (nsList.size() == 1)
1608 20 Apr 12 nicklas 196             {
1608 20 Apr 12 nicklas 197               noSpecimen = nsList.get(0);
1608 20 Apr 12 nicklas 198             }
1608 20 Apr 12 nicklas 199           }
1981 17 May 13 nicklas 200           
1326 29 Mar 11 nicklas 201           // Continue to load some more info (annotations, etc...)
1326 29 Mar 11 nicklas 202           // ... case annotations
1610 23 Apr 12 nicklas 203           theCase.loadAnnotations(dc, "laterality", Annotationtype.LATERALITY, null);
5277 05 Feb 19 nicklas 204           Consent.loadConsentAnnotations(dc, theCase);
1513 19 Jan 12 nicklas 205
1326 29 Mar 11 nicklas 206           // Wrap what we have so far up into JSON objects
1326 29 Mar 11 nicklas 207           jsonCase = theCase.asJSONObject();
1513 19 Jan 12 nicklas 208
1981 17 May 13 nicklas 209           // If this is a regular (operation) case, Check if there are any core biopsies registered
2840 20 Oct 14 nicklas 210           String suffix = null;
2840 20 Oct 14 nicklas 211           if (caseName.length() > 7) 
1981 17 May 13 nicklas 212           {
2840 20 Oct 14 nicklas 213             suffix = caseName.substring(7, 8);
2840 20 Oct 14 nicklas 214             caseName = caseName.substring(0, 7);
2840 20 Oct 14 nicklas 215           }
2840 20 Oct 14 nicklas 216           if (site.useCaseSuffixForPreNeoForms() && suffix == null)
2840 20 Oct 14 nicklas 217           {
1981 17 May 13 nicklas 218             ItemQuery<Sample> query = theCase.getItem().getChildSamples();
1981 17 May 13 nicklas 219             query.setIncludes(Reggie.INCLUDE_IN_CURRENT_PROJECT);
1981 17 May 13 nicklas 220             query.join(Annotations.innerJoin(null, Annotationtype.BIOPSY_TYPE.load(dc), "bt"));
2840 20 Oct 14 nicklas 221             query.restrict(Restrictions.in(Hql.alias("bt"), Expressions.parameter("btValues")));
2840 20 Oct 14 nicklas 222             query.setParameter("btValues", SpecimenTube.suffix2BiopsyType.values(), Type.STRING);
1981 17 May 13 nicklas 223             
1981 17 May 13 nicklas 224             List<Sample> coreBiopsies = query.list(dc);
1981 17 May 13 nicklas 225             JSONArray jsonCoreBiopsies = new JSONArray();
1981 17 May 13 nicklas 226             for (Sample cb : coreBiopsies)
1981 17 May 13 nicklas 227             {
1981 17 May 13 nicklas 228               JSONObject jsonCb = new JSONObject();
1981 17 May 13 nicklas 229               jsonCb.put("id", cb.getId());
1981 17 May 13 nicklas 230               jsonCb.put("name", cb.getName());
1981 17 May 13 nicklas 231               jsonCb.put("laterality", Annotationtype.LATERALITY.getAnnotationValue(dc, cb));
2840 20 Oct 14 nicklas 232               jsonCb.put("biopsyType", Annotationtype.BIOPSY_TYPE.getAnnotationValue(dc, cb));
1981 17 May 13 nicklas 233               jsonCoreBiopsies.add(jsonCb);
1981 17 May 13 nicklas 234             }
1981 17 May 13 nicklas 235             jsonCase.put("coreBiopsies", jsonCoreBiopsies);
1981 17 May 13 nicklas 236           }
1981 17 May 13 nicklas 237           
1513 19 Jan 12 nicklas 238           // Load the patient associated with the case
1519 23 Jan 12 nicklas 239           Patient patient = Patient.findByCase(dc, theCase);
1519 23 Jan 12 nicklas 240           // The patient can be null if (for example) we have only registered consent=yes so far
1519 23 Jan 12 nicklas 241           if (patient != null) 
1519 23 Jan 12 nicklas 242           {
6575 09 Feb 22 nicklas 243             loadPatientInfo(dc, patient);
1519 23 Jan 12 nicklas 244             jsonCase.put("patient", patient.asJSONObject());
1519 23 Jan 12 nicklas 245           }
1283 01 Feb 11 nicklas 246         }
1326 29 Mar 11 nicklas 247         else
1326 29 Mar 11 nicklas 248         {
1326 29 Mar 11 nicklas 249           // Could not find the registered case -- see if we can find any specimen tubes
1623 26 Apr 12 nicklas 250           specimenTubes = SpecimenTube.findByCaseName(dc, caseName, site.useCaseSuffixForPreNeoForms());
1623 26 Apr 12 nicklas 251           // Get rid of suffix in the case name
1623 26 Apr 12 nicklas 252           if (caseName.length() > 7) caseName = caseName.substring(0, 7);
1326 29 Mar 11 nicklas 253           jsonCase = new JSONObject();
1326 29 Mar 11 nicklas 254           jsonCase.put("name", caseName);
1326 29 Mar 11 nicklas 255         }
1519 23 Jan 12 nicklas 256         
1519 23 Jan 12 nicklas 257         if (blood != null)
1519 23 Jan 12 nicklas 258         {
1519 23 Jan 12 nicklas 259           // Check that the registration is not prohibited
1519 23 Jan 12 nicklas 260           blood.verifyConsent(dc, null);
1610 23 Apr 12 nicklas 261           blood.loadAnnotations(dc, "consent", Annotationtype.CONSENT, null);
1610 23 Apr 12 nicklas 262           blood.loadAnnotations(dc, "consentDate", Annotationtype.CONSENT_DATE, Reggie.CONVERTER_DATE_TO_STRING);
1523 24 Jan 12 nicklas 263           
1519 23 Jan 12 nicklas 264           // Wrap what we have so far up into JSON objects
1519 23 Jan 12 nicklas 265           jsonBlood = blood.asJSONObject();
1519 23 Jan 12 nicklas 266
1519 23 Jan 12 nicklas 267           // Load patient information
1519 23 Jan 12 nicklas 268           Patient patient = Patient.findByBlood(dc, blood);
1519 23 Jan 12 nicklas 269           if (patient != null) 
1519 23 Jan 12 nicklas 270           {
6575 09 Feb 22 nicklas 271             loadPatientInfo(dc, patient);
1519 23 Jan 12 nicklas 272             jsonBlood.put("patient", patient.asJSONObject());
1519 23 Jan 12 nicklas 273           }
1518 23 Jan 12 nicklas 274         }
1518 23 Jan 12 nicklas 275         
1608 20 Apr 12 nicklas 276         // Load 'Specimen'/'NoSpecimen' annotations
1326 29 Mar 11 nicklas 277         JSONArray jsonTubes = new JSONArray();
1326 29 Mar 11 nicklas 278         if (specimenTubes.size() > 0)
1326 29 Mar 11 nicklas 279         {
1326 29 Mar 11 nicklas 280           for (SpecimenTube tube : specimenTubes)
1326 29 Mar 11 nicklas 281           {
5342 02 Apr 19 nicklas 282             tube.loadBioPlateLocation();
1610 23 Apr 12 nicklas 283             tube.loadAnnotations(dc, "pad", Annotationtype.PAD, null);
6991 19 Jan 23 nicklas 284             tube.loadAnnotations(dc, "alternatePad", Annotationtype.ALTERNATE_PAD, null);
6996 20 Jan 23 nicklas 285             tube.loadAnnotations(dc, "externalSpecimenExists", Annotationtype.EXTERNAL_SPECIMEN_EXISTS, null);
6996 20 Jan 23 nicklas 286             tube.loadAnnotations(dc, "externalOperator", Annotationtype.EXTERNAL_OPERATOR, null);
1610 23 Apr 12 nicklas 287             tube.loadAnnotations(dc, "laterality", Annotationtype.LATERALITY, null);
3602 16 Nov 15 nicklas 288             tube.loadAnnotations(dc, "YellowLabel", Annotationtype.YELLOW_LABEL, null);
1610 23 Apr 12 nicklas 289             tube.loadAnnotations(dc, "samplingDate", Annotationtype.SAMPLING_DATETIME, Reggie.CONVERTER_DATETIME_TO_STRING);
1610 23 Apr 12 nicklas 290             tube.loadAnnotations(dc, "rnaLaterDate", Annotationtype.RNALATER_DATETIME, Reggie.CONVERTER_DATETIME_TO_STRING);
6510 03 Dec 21 nicklas 291             tube.loadAnnotations(dc, "biopsyType", Annotationtype.BIOPSY_TYPE, null);
6510 03 Dec 21 nicklas 292             tube.loadAnnotations(dc, "specimenType", Annotationtype.SPECIMEN_TYPE, null);
6510 03 Dec 21 nicklas 293             tube.loadAnnotations(dc, "otherPathNote", Annotationtype.OTHER_PATH_NOTE, null);
6510 03 Dec 21 nicklas 294             tube.loadAnnotations(dc, "operatorDeliveryComment", Annotationtype.OPERATOR_DELIVERY_COMMENT, null);
6998 20 Jan 23 nicklas 295             
6998 20 Jan 23 nicklas 296             tube.loadAnnotations(dc, "externalRef", Annotationtype.EXTERNAL_REF, null);
6998 20 Jan 23 nicklas 297             tube.setAnnotation("importedFrom", JsonUtil.loadLinkedItem(dc, tube.getItem(), "Imported from", Item.FILE, null));
6998 20 Jan 23 nicklas 298             
1326 29 Mar 11 nicklas 299             jsonTubes.add(tube.asJSONObject());
1326 29 Mar 11 nicklas 300           }
1326 29 Mar 11 nicklas 301         }
1326 29 Mar 11 nicklas 302         jsonCase.put("specimen", jsonTubes);
1283 01 Feb 11 nicklas 303         
1608 20 Apr 12 nicklas 304         if (noSpecimen != null)
1608 20 Apr 12 nicklas 305         {
1610 23 Apr 12 nicklas 306           noSpecimen.loadAnnotations(dc, "pad", Annotationtype.PAD, null);
6991 19 Jan 23 nicklas 307           noSpecimen.loadAnnotations(dc, "alternatePad", Annotationtype.ALTERNATE_PAD, null);
1610 23 Apr 12 nicklas 308           noSpecimen.loadAnnotations(dc, "reasonIfNoSpecimen", Annotationtype.REASON_IF_NO_SPECIMEN, null);
6574 08 Feb 22 nicklas 309
6574 08 Feb 22 nicklas 310           noSpecimen.loadAnnotations(dc, "samplingDate", Annotationtype.SAMPLING_DATETIME, Reggie.CONVERTER_DATETIME_TO_STRING);
6574 08 Feb 22 nicklas 311           noSpecimen.loadAnnotations(dc, "rnaLaterDate", Annotationtype.RNALATER_DATETIME, Reggie.CONVERTER_DATETIME_TO_STRING);
6574 08 Feb 22 nicklas 312           noSpecimen.loadAnnotations(dc, "otherPathNote", Annotationtype.OTHER_PATH_NOTE, null);
6574 08 Feb 22 nicklas 313
6574 08 Feb 22 nicklas 314           noSpecimen.loadAnnotations(dc, "externalSpecimenExists", Annotationtype.EXTERNAL_SPECIMEN_EXISTS, null);
6574 08 Feb 22 nicklas 315           noSpecimen.loadAnnotations(dc, "externalOperator", Annotationtype.EXTERNAL_OPERATOR, null);
6574 08 Feb 22 nicklas 316           noSpecimen.loadAnnotations(dc, "liasonComment", Annotationtype.LIASON_COMMENT, null);
1608 20 Apr 12 nicklas 317           jsonCase.put("noSpecimen", noSpecimen.asJSONObject());
1608 20 Apr 12 nicklas 318         }
1608 20 Apr 12 nicklas 319         
1326 29 Mar 11 nicklas 320         // This is what we send back to the browser
1326 29 Mar 11 nicklas 321         json.put("caseInfo", jsonCase);
1519 23 Jan 12 nicklas 322         json.put("bloodInfo", jsonBlood);
1326 29 Mar 11 nicklas 323
1326 29 Mar 11 nicklas 324       }
1326 29 Mar 11 nicklas 325       
1326 29 Mar 11 nicklas 326       else if ("GetPatientInfo".equals(cmd))
1326 29 Mar 11 nicklas 327       {
1326 29 Mar 11 nicklas 328         /*
1326 29 Mar 11 nicklas 329           Load information about a single patient when given a personal number
1326 29 Mar 11 nicklas 330           If a patient is found we will load annotations and the cases associated 
1326 29 Mar 11 nicklas 331           with it. If no patient is found, we populate a new object with some
1326 29 Mar 11 nicklas 332           information (eg. birth date and gender) from the information we have.
1326 29 Mar 11 nicklas 333         */
6329 14 Jun 21 nicklas 334         dc = sc.newDbControl(":Pathology referral form registration");
1326 29 Mar 11 nicklas 335         
1326 29 Mar 11 nicklas 336         // Request parameters
1326 29 Mar 11 nicklas 337         String pnr = req.getParameter("personalNumber");
1326 29 Mar 11 nicklas 338         boolean pnrIsValid = Values.getBoolean(req.getParameter("pnrIsValid"));
1326 29 Mar 11 nicklas 339
1326 29 Mar 11 nicklas 340         // Find a patient by personalNumber
1326 29 Mar 11 nicklas 341         Patient patient = Patient.findByPersonalNumber(dc, pnr);
1326 29 Mar 11 nicklas 342         JSONObject jsonPat = null;
1326 29 Mar 11 nicklas 343         
1326 29 Mar 11 nicklas 344         if (patient != null)
1283 01 Feb 11 nicklas 345         {
6575 09 Feb 22 nicklas 346           loadPatientInfo(dc, patient);
1326 29 Mar 11 nicklas 347           jsonPat = patient.asJSONObject();
1283 01 Feb 11 nicklas 348         }
1283 01 Feb 11 nicklas 349         else
1283 01 Feb 11 nicklas 350         {
1326 29 Mar 11 nicklas 351           // No patient was found -- try to find the highest existing patient number
1326 29 Mar 11 nicklas 352           jsonPat = new JSONObject();
1326 29 Mar 11 nicklas 353           jsonPat.put("personalNumber", pnr);
4898 10 Jul 18 nicklas 354           jsonPat.put("name", Patient.getNextName(dc, Subtype.PATIENT, false));
1319 21 Mar 11 nicklas 355           if (pnrIsValid)
1319 21 Mar 11 nicklas 356           {
1319 21 Mar 11 nicklas 357             // 'Samordningsnummer' have day-in-month + 60
1319 21 Mar 11 nicklas 358             int dayInMonth = Values.getInt(pnr.substring(6,8));
1319 21 Mar 11 nicklas 359             if (dayInMonth > 60) dayInMonth -= 60;
1319 21 Mar 11 nicklas 360             jsonPat.put("dateOfBirth", pnr.substring(0,4) + '-' + pnr.substring(4,6)+'-'+MD5.leftPad(Integer.toString(dayInMonth), '0', 2));
1319 21 Mar 11 nicklas 361             jsonPat.put("gender", Integer.parseInt(pnr.substring(10,11)) % 2 == 0 ? "F" : "M");
1319 21 Mar 11 nicklas 362           }
1283 01 Feb 11 nicklas 363         }
1286 08 Feb 11 nicklas 364
1286 08 Feb 11 nicklas 365         json.put("patientInfo", jsonPat);
1283 01 Feb 11 nicklas 366       }
1283 01 Feb 11 nicklas 367     }
1283 01 Feb 11 nicklas 368     catch (Throwable t)
1283 01 Feb 11 nicklas 369     {
1283 01 Feb 11 nicklas 370       t.printStackTrace();
1283 01 Feb 11 nicklas 371       json.clear();
1283 01 Feb 11 nicklas 372       json.put("status", "error");
1283 01 Feb 11 nicklas 373       json.put("message", t.getMessage());
1283 01 Feb 11 nicklas 374       json.put("stacktrace", ThrowableUtil.stackTraceToString(t));
1283 01 Feb 11 nicklas 375     }
1283 01 Feb 11 nicklas 376     finally
1283 01 Feb 11 nicklas 377     {
1283 01 Feb 11 nicklas 378       if (dc != null) dc.close();
1283 01 Feb 11 nicklas 379       json.writeJSONString(resp.getWriter());
1283 01 Feb 11 nicklas 380     }
1283 01 Feb 11 nicklas 381     
1283 01 Feb 11 nicklas 382     
1283 01 Feb 11 nicklas 383   }
6575 09 Feb 22 nicklas 384   
6575 09 Feb 22 nicklas 385   
6575 09 Feb 22 nicklas 386   private void loadPatientInfo(DbControl dc, Patient patient)
6575 09 Feb 22 nicklas 387   {
6575 09 Feb 22 nicklas 388     // Load patient annotations
6575 09 Feb 22 nicklas 389     patient.loadDefaultAnnotations(dc);
1286 08 Feb 11 nicklas 390
6575 09 Feb 22 nicklas 391     // Load cases associated with this patient
6575 09 Feb 22 nicklas 392     List<Case> cases = Case.findByPatient(dc, patient);
6575 09 Feb 22 nicklas 393     
6575 09 Feb 22 nicklas 394     // Load case annotations
6575 09 Feb 22 nicklas 395     JSONArray jsonCases = new JSONArray();
6575 09 Feb 22 nicklas 396     if (cases.size() > 0)
6575 09 Feb 22 nicklas 397     {
6575 09 Feb 22 nicklas 398       for (Case c : cases)
6575 09 Feb 22 nicklas 399       {
6575 09 Feb 22 nicklas 400         c.loadAnnotations(dc, "laterality", Annotationtype.LATERALITY, null);
6575 09 Feb 22 nicklas 401         Consent.loadConsentAnnotations(dc, c);
6575 09 Feb 22 nicklas 402         jsonCases.add(c.asJSONObject());
6575 09 Feb 22 nicklas 403       }
6575 09 Feb 22 nicklas 404     }
6575 09 Feb 22 nicklas 405     patient.setAnnotation("cases", jsonCases);
6575 09 Feb 22 nicklas 406   }
6575 09 Feb 22 nicklas 407
1286 08 Feb 11 nicklas 408   @Override
1286 08 Feb 11 nicklas 409   protected void doPost(HttpServletRequest req, HttpServletResponse resp)
1286 08 Feb 11 nicklas 410     throws ServletException, IOException 
1286 08 Feb 11 nicklas 411   {
1286 08 Feb 11 nicklas 412     String cmd = req.getParameter("cmd");
2598 22 Aug 14 nicklas 413     JsonUtil.setJsonResponseHeaders(resp);
1286 08 Feb 11 nicklas 414     
1286 08 Feb 11 nicklas 415     JSONObject json = new JSONObject();
1286 08 Feb 11 nicklas 416     json.put("status", "ok");
1290 21 Feb 11 nicklas 417     
1290 21 Feb 11 nicklas 418     JSONArray jsonMessages = new JSONArray();
1283 01 Feb 11 nicklas 419   
3975 26 May 16 nicklas 420     final SessionControl sc = Reggie.getSessionControl(req);
1286 08 Feb 11 nicklas 421     DbControl dc = null;
1286 08 Feb 11 nicklas 422     try
1286 08 Feb 11 nicklas 423     {
1326 29 Mar 11 nicklas 424       if ("CreateCase".equals(cmd))
1286 08 Feb 11 nicklas 425       {
6329 14 Jun 21 nicklas 426         dc = sc.newDbControl(":Pathology referral form registration");
2161 09 Dec 13 nicklas 427
2161 09 Dec 13 nicklas 428         ReggieRole.checkPermission(dc, "'" + cmd + "' wizard", ReggieRole.PATIENT_CURATOR, ReggieRole.ADMINISTRATOR);
2161 09 Dec 13 nicklas 429
3752 17 Feb 16 nicklas 430         JSONObject jsonReq = JsonUtil.parseRequest(req);
1286 08 Feb 11 nicklas 431         JSONObject jsonPat = (JSONObject)jsonReq.get("patientInfo");
1286 08 Feb 11 nicklas 432         JSONObject jsonCase = (JSONObject)jsonReq.get("caseInfo");
1286 08 Feb 11 nicklas 433         
1513 19 Jan 12 nicklas 434         BioSource patient = getOrCreatePatient(dc, jsonPat, jsonMessages);
1286 08 Feb 11 nicklas 435         
1513 19 Jan 12 nicklas 436         // If we already have a case with id it is probably due to an already registered consent=yes
1513 19 Jan 12 nicklas 437         Number preCaseId = (Number)jsonCase.get("id");
1286 08 Feb 11 nicklas 438         
1307 04 Mar 11 nicklas 439         // Get laterality
1307 04 Mar 11 nicklas 440         String laterality = Values.getStringOrNull((String)jsonCase.get("laterality"));
1286 08 Feb 11 nicklas 441         
1307 04 Mar 11 nicklas 442         // Load/create case
1290 21 Feb 11 nicklas 443         String originalCaseName = (String)jsonCase.get("name");
1623 26 Apr 12 nicklas 444         Site site = Site.findByCaseName(originalCaseName);
1623 26 Apr 12 nicklas 445         
4141 30 Sep 16 nicklas 446         Set<SharedItem> itemsToPublish = new HashSet<>();
4141 30 Sep 16 nicklas 447         PluginExecutionRequest permissionChangeRequest = 
4141 30 Sep 16 nicklas 448           SetPermissionsForDeliveryPlugin.createExecutionRequest(dc, "PersonalRegistrationServlet.UpdateCase");
4034 29 Jul 16 nicklas 449
1307 04 Mar 11 nicklas 450         // If merging with an existing case the 'laterality' value holds the id
1307 04 Mar 11 nicklas 451         int caseId = Values.getInt(laterality); 
1290 21 Feb 11 nicklas 452         Sample theCase = null;
4460 21 Apr 17 nicklas 453
4460 21 Apr 17 nicklas 454         // Use the registration date as the starting point for the reference date
4460 21 Apr 17 nicklas 455         ReferenceDateWithSource refDate = new ReferenceDateWithSource(new Date(), ReferenceDateSource.REGISTRATION_DATE);
4460 21 Apr 17 nicklas 456         
1307 04 Mar 11 nicklas 457         if (caseId > 0)
1290 21 Feb 11 nicklas 458         {
1307 04 Mar 11 nicklas 459           // Merge with existing case
1307 04 Mar 11 nicklas 460           theCase = Sample.getById(dc, caseId);
1333 05 Apr 11 nicklas 461           // Load the real laterality from the case
1610 23 Apr 12 nicklas 462           laterality = (String)Annotationtype.LATERALITY.getAnnotationValue(dc, theCase);
1513 19 Jan 12 nicklas 463           
4460 21 Apr 17 nicklas 464           // Load existing reference date from the case and get the winning date
4460 21 Apr 17 nicklas 465           refDate = refDate.getThisOrTheReplacement(ReferenceDateWithSource.getFromAnnotations(dc, theCase));
4460 21 Apr 17 nicklas 466           
1513 19 Jan 12 nicklas 467           if (preCaseId != null)
1513 19 Jan 12 nicklas 468           {
5277 05 Feb 19 nicklas 469             // Remove the pre-registered case but copy consent information first (but do not overwrite)
1513 19 Jan 12 nicklas 470             Sample preCase = Sample.getById(dc, preCaseId.intValue());
5277 05 Feb 19 nicklas 471             boolean copied = Consent.copyConsentAnnotations(dc, preCase, theCase, false);
4460 21 Apr 17 nicklas 472             // In case we have a consent date that replaces the registration date
5277 05 Feb 19 nicklas 473             if (copied)
5277 05 Feb 19 nicklas 474             {
5277 05 Feb 19 nicklas 475               refDate = refDate.getThisOrTheReplacement(ReferenceDateWithSource.getFromAnnotations(dc, preCase));
5277 05 Feb 19 nicklas 476             }
1513 19 Jan 12 nicklas 477             dc.deleteItem(preCase);
1513 19 Jan 12 nicklas 478           }
1513 19 Jan 12 nicklas 479           
1307 04 Mar 11 nicklas 480           jsonMessages.add("Case '" + originalCaseName + "' merged with case '" + theCase.getName() + "' successfully.");
1307 04 Mar 11 nicklas 481         }
1307 04 Mar 11 nicklas 482         else
1307 04 Mar 11 nicklas 483         {
1513 19 Jan 12 nicklas 484           if (preCaseId != null)
1513 19 Jan 12 nicklas 485           {
1513 19 Jan 12 nicklas 486             // Load existing case
1513 19 Jan 12 nicklas 487             theCase = Sample.getById(dc, preCaseId.intValue());
4460 21 Apr 17 nicklas 488             refDate = refDate.getThisOrTheReplacement(ReferenceDateWithSource.getFromAnnotations(dc, theCase));
1513 19 Jan 12 nicklas 489           }
1513 19 Jan 12 nicklas 490           else
1513 19 Jan 12 nicklas 491           {
1513 19 Jan 12 nicklas 492             // Register a new case
1513 19 Jan 12 nicklas 493             theCase = Sample.getNew(dc);
4902 10 Jul 18 nicklas 494             theCase.setExternalId(Case.getNextExternalId(dc));
1513 19 Jan 12 nicklas 495             dc.saveItem(theCase);
4964 03 Sep 18 nicklas 496             ReggieItem.ensureNonExistingItem(dc, Subtype.CASE, originalCaseName);
1513 19 Jan 12 nicklas 497           }
1463 14 Nov 11 martin 498           theCase.setItemSubtype(Subtype.CASE.load(dc));
4964 03 Sep 18 nicklas 499           theCase.setName(originalCaseName);
1307 04 Mar 11 nicklas 500           if (laterality != null)
1307 04 Mar 11 nicklas 501           {
1610 23 Apr 12 nicklas 502             Annotationtype.LATERALITY.setAnnotationValue(dc, theCase, laterality);
1307 04 Mar 11 nicklas 503           }
1463 14 Nov 11 martin 504           theCase.getCreationEvent().setSource(patient);
1523 24 Jan 12 nicklas 505           
1523 24 Jan 12 nicklas 506           Number copyConsentId = (Number)jsonCase.get("copyConsent");
1523 24 Jan 12 nicklas 507           if (copyConsentId != null)
1523 24 Jan 12 nicklas 508           {
1523 24 Jan 12 nicklas 509             Sample consentCase = Sample.getById(dc, copyConsentId.intValue());
5277 05 Feb 19 nicklas 510             Consent.copyConsentAnnotations(dc, consentCase, theCase, true);
4460 21 Apr 17 nicklas 511             refDate = refDate.getThisOrTheReplacement(ReferenceDateWithSource.getFromAnnotations(dc, consentCase));
1523 24 Jan 12 nicklas 512           }
1523 24 Jan 12 nicklas 513           
1290 21 Feb 11 nicklas 514           jsonMessages.add("Case '" + originalCaseName + "' created successfully.");
1290 21 Feb 11 nicklas 515         }
1290 21 Feb 11 nicklas 516         
1286 08 Feb 11 nicklas 517         // Link the case with specimen
1290 21 Feb 11 nicklas 518         AnnotationSet caseAnnotations = theCase.getAnnotationSet();
1289 14 Feb 11 nicklas 519         JSONArray jsonSpecimen = (JSONArray)jsonCase.get("specimen");
1608 20 Apr 12 nicklas 520         
1317 18 Mar 11 nicklas 521         String pad = Values.getStringOrNull((String)jsonCase.get("pad"));
6991 19 Jan 23 nicklas 522         String alternatePad = Values.getStringOrNull((String)jsonCase.get("alternatePad"));
6996 20 Jan 23 nicklas 523         String externalOperator = Values.getStringOrNull((String)jsonCase.get("externalOperator"));
1597 30 Mar 12 nicklas 524         
1289 14 Feb 11 nicklas 525         if (jsonSpecimen != null && jsonSpecimen.size() > 0)
1286 08 Feb 11 nicklas 526         {
1610 23 Apr 12 nicklas 527           AnnotationType rnaLaterType = Annotationtype.RNALATER_DATETIME.load(dc);
1608 20 Apr 12 nicklas 528           Date rnaLaterDateTime = Reggie.CONVERTER_STRING_TO_DATETIME.convert((String)jsonCase.get("rnaLaterDate"));
1600 16 Apr 12 nicklas 529           Date samplingDateTime = Reggie.CONVERTER_STRING_TO_DATETIME.convert((String)jsonCase.get("samplingDate"));
1333 05 Apr 11 nicklas 530
4460 21 Apr 17 nicklas 531           if (samplingDateTime != null)
4460 21 Apr 17 nicklas 532           {
4460 21 Apr 17 nicklas 533             // Create a SAMPLING_DATE reference date and check if it should replace the existing
4460 21 Apr 17 nicklas 534             refDate = refDate.getThisOrTheReplacement(new ReferenceDateWithSource(samplingDateTime, ReferenceDateSource.SAMPLING_DATE));
4460 21 Apr 17 nicklas 535           }
4460 21 Apr 17 nicklas 536           
1289 14 Feb 11 nicklas 537           for (int i = 0; i < jsonSpecimen.size(); ++i)
1286 08 Feb 11 nicklas 538           {
1289 14 Feb 11 nicklas 539             JSONObject jsonSpec = (JSONObject)jsonSpecimen.get(i);
1326 29 Mar 11 nicklas 540             Sample specimen = Sample.getById(dc, ((Number)jsonSpec.get("id")).intValue());
3726 28 Jan 16 nicklas 541             
1595 27 Mar 12 nicklas 542             // Link the specimen with the case 
1595 27 Mar 12 nicklas 543             BioMaterialEvent creationEvent = specimen.getCreationEvent();
1595 27 Mar 12 nicklas 544             creationEvent.setSource(theCase);
1595 27 Mar 12 nicklas 545             // !IMPORTANT - The sampling date must be set here and as an 'SamplingDateTime' annotation
1608 20 Apr 12 nicklas 546             creationEvent.setEventDate(samplingDateTime);
1307 04 Mar 11 nicklas 547             
1333 05 Apr 11 nicklas 548             // Update the specimen annotations
1610 23 Apr 12 nicklas 549             if (laterality != null) Annotationtype.LATERALITY.setAnnotationValue(dc, specimen, laterality);
1610 23 Apr 12 nicklas 550             Annotationtype.PAD.setAnnotationValue(dc, specimen, pad);
6991 19 Jan 23 nicklas 551             Annotationtype.ALTERNATE_PAD.setAnnotationValue(dc, specimen, alternatePad);
6996 20 Jan 23 nicklas 552             Annotationtype.EXTERNAL_OPERATOR.setAnnotationValue(dc, specimen, externalOperator);
1610 23 Apr 12 nicklas 553             if (samplingDateTime != null) Annotationtype.SAMPLING_DATETIME.setAnnotationValue(dc, specimen, samplingDateTime);
1610 23 Apr 12 nicklas 554             if (rnaLaterDateTime != null) Annotationtype.RNALATER_DATETIME.setAnnotationValue(dc, specimen, rnaLaterDateTime);
6510 03 Dec 21 nicklas 555             Annotationtype.OTHER_PATH_NOTE.setAnnotationValue(dc, specimen, Values.getStringOrNull((String)jsonSpec.get("otherPathNote")));
6510 03 Dec 21 nicklas 556             Annotationtype.OPERATOR_DELIVERY_COMMENT.setAnnotationValue(dc, specimen, Values.getStringOrNull((String)jsonSpec.get("operatorDeliveryComment")));
6510 03 Dec 21 nicklas 557             Annotationtype.SPECIMEN_TYPE.setAnnotationValue(dc, specimen, Values.getStringOrNull((String)jsonSpec.get("specimenType")));
6510 03 Dec 21 nicklas 558             Annotationtype.BIOPSY_TYPE.setAnnotationValue(dc, specimen, Values.getStringOrNull((String)jsonSpec.get("biopsyType")));
6510 03 Dec 21 nicklas 559
3726 28 Jan 16 nicklas 560             String yellowLabel = (String)jsonSpec.get("YellowLabel");
3726 28 Jan 16 nicklas 561             String yellowLabelOriginal = (String)jsonSpec.get("YellowLabelOriginal");
3726 28 Jan 16 nicklas 562             if (!EqualsHelper.equals(yellowLabel, yellowLabelOriginal))
3726 28 Jan 16 nicklas 563             {
3726 28 Jan 16 nicklas 564               Annotationtype.YELLOW_LABEL.setAnnotationValue(dc, specimen, yellowLabel);
3726 28 Jan 16 nicklas 565               jsonMessages.add("YellowLabel " +(yellowLabel==null?"removed from":"set on")+" specimen tube '"+ specimen.getName()+"'");
3726 28 Jan 16 nicklas 566             }
4141 30 Sep 16 nicklas 567             
4141 30 Sep 16 nicklas 568             // Share to site (see below)
4141 30 Sep 16 nicklas 569             if (site != Site.UNKNOWN) itemsToPublish.add(specimen);
1286 08 Feb 11 nicklas 570           }
1307 04 Mar 11 nicklas 571           jsonMessages.add(jsonSpecimen.size() + " specimen tube(s) linked with case '" + theCase.getName() + "' successfully.");
1286 08 Feb 11 nicklas 572         }
1286 08 Feb 11 nicklas 573         else
1286 08 Feb 11 nicklas 574         {
1608 20 Apr 12 nicklas 575
6510 03 Dec 21 nicklas 576           // Create new 'NoSpecimen' item
6510 03 Dec 21 nicklas 577           Sample noSpecimen = Sample.getNew(dc);
7000 20 Jan 23 nicklas 578           noSpecimen.setName(SpecimenTube.getNextSpecimenName(dc, originalCaseName, true));
6510 03 Dec 21 nicklas 579           noSpecimen.setExternalId(SpecimenTube.getNextExternalId(dc, Subtype.NO_SPECIMEN));
6510 03 Dec 21 nicklas 580           noSpecimen.setItemSubtype(Subtype.NO_SPECIMEN.load(dc));
6510 03 Dec 21 nicklas 581           dc.saveItem(noSpecimen);
6510 03 Dec 21 nicklas 582           BioMaterialEvent creationEvent = noSpecimen.getCreationEvent();
6151 26 Feb 21 nicklas 583           creationEvent.setSource(theCase);
6151 26 Feb 21 nicklas 584           
6510 03 Dec 21 nicklas 585           if (laterality != null) Annotationtype.LATERALITY.setAnnotationValue(dc, noSpecimen, laterality);
6510 03 Dec 21 nicklas 586           Annotationtype.PAD.setAnnotationValue(dc, noSpecimen, pad);
6991 19 Jan 23 nicklas 587           Annotationtype.ALTERNATE_PAD.setAnnotationValue(dc, noSpecimen, alternatePad);
6510 03 Dec 21 nicklas 588           Annotationtype.BIOPSY_TYPE.setAnnotationValue(dc, noSpecimen, Values.getStringOrNull((String)jsonCase.get("biopsyType")));
6151 26 Feb 21 nicklas 589           
6574 08 Feb 22 nicklas 590           boolean externalSpecimenExists = Boolean.TRUE.equals(jsonCase.get("externalSpecimenExists"));
6574 08 Feb 22 nicklas 591           Annotationtype.EXTERNAL_SPECIMEN_EXISTS.setAnnotationValue(dc, noSpecimen, externalSpecimenExists ? "Yes" : null);
6574 08 Feb 22 nicklas 592           if (externalSpecimenExists)
6574 08 Feb 22 nicklas 593           {
6996 20 Jan 23 nicklas 594             Annotationtype.EXTERNAL_OPERATOR.setAnnotationValue(dc, noSpecimen, externalOperator);
6574 08 Feb 22 nicklas 595             Annotationtype.LIASON_COMMENT.setAnnotationValue(dc, noSpecimen, Values.getStringOrNull((String)jsonCase.get("liasonComment")));
6574 08 Feb 22 nicklas 596             Annotationtype.OTHER_PATH_NOTE.setAnnotationValue(dc, noSpecimen, Values.getStringOrNull((String)jsonCase.get("otherPathNote")));
6574 08 Feb 22 nicklas 597             Date rnaLaterDateTime = Reggie.CONVERTER_STRING_TO_DATETIME.convert((String)jsonCase.get("rnaLaterDate"));
6574 08 Feb 22 nicklas 598             Date samplingDateTime = Reggie.CONVERTER_STRING_TO_DATETIME.convert((String)jsonCase.get("samplingDate"));
6574 08 Feb 22 nicklas 599             Annotationtype.SAMPLING_DATETIME.setAnnotationValue(dc, noSpecimen, samplingDateTime);
6574 08 Feb 22 nicklas 600             Annotationtype.RNALATER_DATETIME.setAnnotationValue(dc, noSpecimen, rnaLaterDateTime);
6574 08 Feb 22 nicklas 601             creationEvent.setEventDate(samplingDateTime);
6574 08 Feb 22 nicklas 602             if (samplingDateTime != null)
6574 08 Feb 22 nicklas 603             {
6574 08 Feb 22 nicklas 604               // Create a SAMPLING_DATE reference date and check if it should replace the existing
6574 08 Feb 22 nicklas 605               refDate = refDate.getThisOrTheReplacement(new ReferenceDateWithSource(samplingDateTime, ReferenceDateSource.SAMPLING_DATE));
6574 08 Feb 22 nicklas 606             }
6574 08 Feb 22 nicklas 607           }
6574 08 Feb 22 nicklas 608           else
6574 08 Feb 22 nicklas 609           {
6574 08 Feb 22 nicklas 610             Annotationtype.REASON_IF_NO_SPECIMEN.setAnnotationValue(dc, noSpecimen, Values.getStringOrNull((String)jsonCase.get("reasonIfNoSpecimen")));
6574 08 Feb 22 nicklas 611             Date samplingDate = Reggie.CONVERTER_STRING_TO_DATE.convert((String)jsonCase.get("samplingDate"));
6574 08 Feb 22 nicklas 612             Annotationtype.SAMPLING_DATETIME.setAnnotationValue(dc, noSpecimen, samplingDate);
6574 08 Feb 22 nicklas 613             creationEvent.setEventDate(samplingDate);
6574 08 Feb 22 nicklas 614             if (samplingDate != null)
6574 08 Feb 22 nicklas 615             {
6574 08 Feb 22 nicklas 616               // Create a SAMPLING_DATE reference date and check if it should replace the existing
6574 08 Feb 22 nicklas 617               refDate = refDate.getThisOrTheReplacement(new ReferenceDateWithSource(samplingDate, ReferenceDateSource.SAMPLING_DATE));
6574 08 Feb 22 nicklas 618             }
6574 08 Feb 22 nicklas 619           }
6574 08 Feb 22 nicklas 620           
6510 03 Dec 21 nicklas 621           jsonMessages.add("NoSpecimen '" + noSpecimen.getName() + "' created successfully.");
1286 08 Feb 11 nicklas 622         }
4460 21 Apr 17 nicklas 623         
4460 21 Apr 17 nicklas 624         // Updates the reference date
4460 21 Apr 17 nicklas 625         if (refDate != null) refDate.updateAnnotations(dc, theCase);
5421 13 May 19 nicklas 626         ActivityDef.REGISTERED_PATHOLOGY_REFERRAL.merge(dc, 1);
1286 08 Feb 11 nicklas 627         dc.commit();
4034 29 Jul 16 nicklas 628         
4034 29 Jul 16 nicklas 629         if (permissionChangeRequest != null)
4034 29 Jul 16 nicklas 630         {
4141 30 Sep 16 nicklas 631           permissionChangeRequest.setParameterValue("items", itemsToPublish);
4034 29 Jul 16 nicklas 632           PluginResponse response = permissionChangeRequest.invoke();
4034 29 Jul 16 nicklas 633           if (response.getStatus() != Response.Status.DONE)
4034 29 Jul 16 nicklas 634           {
4034 29 Jul 16 nicklas 635             jsonMessages.add("[Warning]Could not set permissions for specimen/case/patient to site (see below)");
4034 29 Jul 16 nicklas 636             jsonMessages.add("[Warning]"+response.getMessage());
4034 29 Jul 16 nicklas 637           }
4034 29 Jul 16 nicklas 638         }
4034 29 Jul 16 nicklas 639
1290 21 Feb 11 nicklas 640         json.put("messages", jsonMessages);
1286 08 Feb 11 nicklas 641       }
1326 29 Mar 11 nicklas 642       
1326 29 Mar 11 nicklas 643       else if ("UpdateCase".equals(cmd))
1326 29 Mar 11 nicklas 644       {
6329 14 Jun 21 nicklas 645         dc = sc.newDbControl(":Pathology referral form registration");
2161 09 Dec 13 nicklas 646
2161 09 Dec 13 nicklas 647         ReggieRole.checkPermission(dc, "'" + cmd + "' wizard", ReggieRole.PATIENT_CURATOR, ReggieRole.ADMINISTRATOR);
2161 09 Dec 13 nicklas 648
3752 17 Feb 16 nicklas 649         JSONObject jsonReq = JsonUtil.parseRequest(req);
1326 29 Mar 11 nicklas 650         JSONObject jsonCase = (JSONObject)jsonReq.get("caseInfo");
1572 16 Mar 12 nicklas 651         JSONObject jsonPat = (JSONObject)jsonReq.get("patientInfo");
1326 29 Mar 11 nicklas 652         
1572 16 Mar 12 nicklas 653         BioSource patient = getOrCreatePatient(dc, jsonPat, jsonMessages);
1572 16 Mar 12 nicklas 654       
1326 29 Mar 11 nicklas 655         Number caseId = (Number)jsonCase.get("id");
1326 29 Mar 11 nicklas 656         Sample theCase = Sample.getById(dc, caseId.intValue());
1333 05 Apr 11 nicklas 657         String originalCaseName = (String)jsonCase.get("originalName");
1623 26 Apr 12 nicklas 658         Site site = Site.findByCaseName(originalCaseName);
1326 29 Mar 11 nicklas 659         
4460 21 Apr 17 nicklas 660         // Get the existing reference date
4460 21 Apr 17 nicklas 661         ReferenceDateWithSource refDate = ReferenceDateWithSource.getFromAnnotations(dc, theCase);
4460 21 Apr 17 nicklas 662         
1333 05 Apr 11 nicklas 663         // Get updated case information
1326 29 Mar 11 nicklas 664         String laterality = Values.getStringOrNull((String)jsonCase.get("laterality"));
1326 29 Mar 11 nicklas 665         // If the 'laterality' value is a number, then it hasn't changed
1333 05 Apr 11 nicklas 666         boolean updateLaterality = Values.getInt(laterality, -1) == -1;
1565 16 Mar 12 nicklas 667         String pad = Values.getStringOrNull((String)jsonCase.get("pad"));
6991 19 Jan 23 nicklas 668         String alternatePad = Values.getStringOrNull((String)jsonCase.get("alternatePad"));
6996 20 Jan 23 nicklas 669         String externalOperator = Values.getStringOrNull((String)jsonCase.get("externalOperator"));
1333 05 Apr 11 nicklas 670         JSONArray jsonSpecimen = (JSONArray)jsonCase.get("specimen");
1608 20 Apr 12 nicklas 671         JSONObject jsonNoSpecimen = (JSONObject)jsonCase.get("noSpecimen");
1333 05 Apr 11 nicklas 672       
4141 30 Sep 16 nicklas 673         Set<SharedItem> itemsToPublish = new HashSet<>();
4141 30 Sep 16 nicklas 674         PluginExecutionRequest permissionChangeRequest = 
4141 30 Sep 16 nicklas 675           SetPermissionsForDeliveryPlugin.createExecutionRequest(dc, "PersonalRegistrationServlet.UpdateCase");
4034 29 Jul 16 nicklas 676         
1333 05 Apr 11 nicklas 677         // Update laterality and PAD for the case
1333 05 Apr 11 nicklas 678         if (updateLaterality)
1326 29 Mar 11 nicklas 679         {
1610 23 Apr 12 nicklas 680           Annotationtype.LATERALITY.setAnnotationValue(dc, theCase, laterality);
1981 17 May 13 nicklas 681           // We must ALWAYS change the laterality on Specimen and NoSpecimen child items (eg. core biopises, etc)
1981 17 May 13 nicklas 682           ItemQuery<Sample> childQuery = theCase.getChildSamples();
1981 17 May 13 nicklas 683           childQuery.setIncludes(Reggie.INCLUDE_IN_CURRENT_PROJECT);
6510 03 Dec 21 nicklas 684           childQuery.restrict(Restrictions.or(Subtype.SPECIMEN.restriction(dc, null), Subtype.NO_SPECIMEN.restriction(dc, null)));
1981 17 May 13 nicklas 685           
6510 03 Dec 21 nicklas 686           int numUpdated = 0;
1981 17 May 13 nicklas 687           for (Sample child : childQuery.list(dc))
1981 17 May 13 nicklas 688           {
6510 03 Dec 21 nicklas 689             if (Annotationtype.LATERALITY.setAnnotationValue(dc, child, laterality))
6510 03 Dec 21 nicklas 690             {
6510 03 Dec 21 nicklas 691               numUpdated++;
6510 03 Dec 21 nicklas 692             }
1981 17 May 13 nicklas 693           }
6510 03 Dec 21 nicklas 694           if (numUpdated > 0)
1981 17 May 13 nicklas 695           {
6510 03 Dec 21 nicklas 696             jsonMessages.add("Updated laterality on " + numUpdated + " child Specimen/NoSpecimen items.");
1981 17 May 13 nicklas 697           }
1333 05 Apr 11 nicklas 698         }
1608 20 Apr 12 nicklas 699       
1333 05 Apr 11 nicklas 700         jsonMessages.add("Case '" + originalCaseName + "' updated successfully.");
1333 05 Apr 11 nicklas 701         
1333 05 Apr 11 nicklas 702         // Update the specimen tubes
1333 05 Apr 11 nicklas 703         if (jsonSpecimen != null && jsonSpecimen.size() > 0)
1981 17 May 13 nicklas 704         {
1600 16 Apr 12 nicklas 705           Date samplingDateTime = Reggie.CONVERTER_STRING_TO_DATETIME.convert((String)jsonCase.get("samplingDate"));
1600 16 Apr 12 nicklas 706           Date rnaLaterDateTime = Reggie.CONVERTER_STRING_TO_DATETIME.convert((String)jsonCase.get("rnaLaterDate"));
6510 03 Dec 21 nicklas 707           String otherPathNote = (String)jsonCase.get("otherPathNote");
1333 05 Apr 11 nicklas 708           
4460 21 Apr 17 nicklas 709           if (samplingDateTime != null)
4460 21 Apr 17 nicklas 710           {
4460 21 Apr 17 nicklas 711             refDate = refDate.getThisOrTheReplacement(new ReferenceDateWithSource(samplingDateTime, ReferenceDateSource.SAMPLING_DATE));
4460 21 Apr 17 nicklas 712           }
4460 21 Apr 17 nicklas 713           
1333 05 Apr 11 nicklas 714           for (int i = 0; i < jsonSpecimen.size(); ++i)
1326 29 Mar 11 nicklas 715           {
1333 05 Apr 11 nicklas 716             JSONObject jsonSpec = (JSONObject)jsonSpecimen.get(i);
1333 05 Apr 11 nicklas 717             Sample specimen = Sample.getById(dc, ((Number)jsonSpec.get("id")).intValue());
1595 27 Mar 12 nicklas 718             // !IMPORTANT - The sampling date must be set here and as an 'SamplingDateTime' annotation
2600 26 Aug 14 nicklas 719             BioMaterialEvent creationEvent = specimen.getCreationEvent();
2600 26 Aug 14 nicklas 720             creationEvent.setEventDate(samplingDateTime);
2600 26 Aug 14 nicklas 721   
2600 26 Aug 14 nicklas 722             // Ensure the specimen is linked with the case 
2600 26 Aug 14 nicklas 723             if (specimen.getParentType() == null)
2600 26 Aug 14 nicklas 724             {
2600 26 Aug 14 nicklas 725               creationEvent.setSource(theCase);
2600 26 Aug 14 nicklas 726             }
1333 05 Apr 11 nicklas 727             
1333 05 Apr 11 nicklas 728             // Update the specimen annotations
1610 23 Apr 12 nicklas 729             Annotationtype.PAD.setAnnotationValue(dc, specimen, pad);
6991 19 Jan 23 nicklas 730             Annotationtype.ALTERNATE_PAD.setAnnotationValue(dc, specimen, alternatePad);
6996 20 Jan 23 nicklas 731             Annotationtype.EXTERNAL_OPERATOR.setAnnotationValue(dc, specimen, externalOperator);
1610 23 Apr 12 nicklas 732             if (samplingDateTime != null) Annotationtype.SAMPLING_DATETIME.setAnnotationValue(dc, specimen, samplingDateTime);
1610 23 Apr 12 nicklas 733             if (rnaLaterDateTime != null) Annotationtype.RNALATER_DATETIME.setAnnotationValue(dc, specimen, rnaLaterDateTime);
6510 03 Dec 21 nicklas 734             Annotationtype.OTHER_PATH_NOTE.setAnnotationValue(dc, specimen, Values.getStringOrNull((String)jsonSpec.get("otherPathNote")));
6510 03 Dec 21 nicklas 735             Annotationtype.OPERATOR_DELIVERY_COMMENT.setAnnotationValue(dc, specimen, Values.getStringOrNull((String)jsonSpec.get("operatorDeliveryComment")));
6510 03 Dec 21 nicklas 736             Annotationtype.SPECIMEN_TYPE.setAnnotationValue(dc, specimen, Values.getStringOrNull((String)jsonSpec.get("specimenType")));
6510 03 Dec 21 nicklas 737             Annotationtype.BIOPSY_TYPE.setAnnotationValue(dc, specimen, Values.getStringOrNull((String)jsonSpec.get("biopsyType")));
3726 28 Jan 16 nicklas 738             String yellowLabel = (String)jsonSpec.get("YellowLabel");
3726 28 Jan 16 nicklas 739             String yellowLabelOriginal = (String)jsonSpec.get("YellowLabelOriginal");
3726 28 Jan 16 nicklas 740             if (!EqualsHelper.equals(yellowLabel, yellowLabelOriginal))
3726 28 Jan 16 nicklas 741             {
3726 28 Jan 16 nicklas 742               Annotationtype.YELLOW_LABEL.setAnnotationValue(dc, specimen, yellowLabel);
3726 28 Jan 16 nicklas 743               jsonMessages.add("YellowLabel " +(yellowLabel==null?"removed from":"set on")+" specimen tube '"+ specimen.getName()+"'");
4034 29 Jul 16 nicklas 744               
4141 30 Sep 16 nicklas 745               // Share to site (see below)
4141 30 Sep 16 nicklas 746               if (site != Site.UNKNOWN) itemsToPublish.add(specimen);
3726 28 Jan 16 nicklas 747             }
1326 29 Mar 11 nicklas 748           }
1333 05 Apr 11 nicklas 749           jsonMessages.add(jsonSpecimen.size() + " specimen tube(s) updated.");
1333 05 Apr 11 nicklas 750
1326 29 Mar 11 nicklas 751         }
1623 26 Apr 12 nicklas 752         else
1333 05 Apr 11 nicklas 753         {
6510 03 Dec 21 nicklas 754           Sample noSpecimen = null;
6510 03 Dec 21 nicklas 755           if (jsonNoSpecimen != null)
1623 26 Apr 12 nicklas 756           {
6510 03 Dec 21 nicklas 757             noSpecimen = Sample.getById(dc, ((Number)jsonNoSpecimen.get("id")).intValue());
1623 26 Apr 12 nicklas 758           }
1623 26 Apr 12 nicklas 759           else
1623 26 Apr 12 nicklas 760           {
6510 03 Dec 21 nicklas 761             noSpecimen = Sample.getNew(dc);
7000 20 Jan 23 nicklas 762             noSpecimen.setName(SpecimenTube.getNextSpecimenName(dc, originalCaseName, true));
6510 03 Dec 21 nicklas 763             noSpecimen.setExternalId(SpecimenTube.getNextExternalId(dc, Subtype.NO_SPECIMEN));
6510 03 Dec 21 nicklas 764             noSpecimen.setItemSubtype(Subtype.NO_SPECIMEN.load(dc));
6510 03 Dec 21 nicklas 765             noSpecimen.getCreationEvent().setSource(theCase);
6510 03 Dec 21 nicklas 766             Annotationtype.LATERALITY.copyAnnotationValues(dc, theCase, noSpecimen, true);
6510 03 Dec 21 nicklas 767             dc.saveItem(noSpecimen);
6510 03 Dec 21 nicklas 768             jsonMessages.add("NoSpecimen '" + noSpecimen.getName() + "' created successfully.");
1623 26 Apr 12 nicklas 769           }
6510 03 Dec 21 nicklas 770           Annotationtype.PAD.setAnnotationValue(dc, noSpecimen, pad);
6991 19 Jan 23 nicklas 771           Annotationtype.ALTERNATE_PAD.setAnnotationValue(dc, noSpecimen, alternatePad);
6510 03 Dec 21 nicklas 772           Annotationtype.BIOPSY_TYPE.setAnnotationValue(dc, noSpecimen, Values.getStringOrNull((String)jsonCase.get("biopsyType")));
6574 08 Feb 22 nicklas 773           
6574 08 Feb 22 nicklas 774           boolean externalSpecimenExists = Boolean.TRUE.equals(jsonCase.get("externalSpecimenExists"));
6574 08 Feb 22 nicklas 775           Annotationtype.EXTERNAL_SPECIMEN_EXISTS.setAnnotationValue(dc, noSpecimen, externalSpecimenExists ? "Yes" : null);
6574 08 Feb 22 nicklas 776           BioMaterialEvent creationEvent = noSpecimen.getCreationEvent();
6574 08 Feb 22 nicklas 777           if (externalSpecimenExists)
6574 08 Feb 22 nicklas 778           {
6996 20 Jan 23 nicklas 779             Annotationtype.EXTERNAL_OPERATOR.setAnnotationValue(dc, noSpecimen, externalOperator);
6574 08 Feb 22 nicklas 780             Annotationtype.LIASON_COMMENT.setAnnotationValue(dc, noSpecimen, Values.getStringOrNull((String)jsonCase.get("liasonComment")));
6574 08 Feb 22 nicklas 781             Annotationtype.OTHER_PATH_NOTE.setAnnotationValue(dc, noSpecimen, Values.getStringOrNull((String)jsonCase.get("otherPathNote")));
6574 08 Feb 22 nicklas 782             Date rnaLaterDateTime = Reggie.CONVERTER_STRING_TO_DATETIME.convert((String)jsonCase.get("rnaLaterDate"));
6574 08 Feb 22 nicklas 783             Date samplingDateTime = Reggie.CONVERTER_STRING_TO_DATETIME.convert((String)jsonCase.get("samplingDate"));
6574 08 Feb 22 nicklas 784             Annotationtype.SAMPLING_DATETIME.setAnnotationValue(dc, noSpecimen, samplingDateTime);
6574 08 Feb 22 nicklas 785             Annotationtype.RNALATER_DATETIME.setAnnotationValue(dc, noSpecimen, rnaLaterDateTime);
6574 08 Feb 22 nicklas 786             creationEvent.setEventDate(samplingDateTime);
6574 08 Feb 22 nicklas 787             if (samplingDateTime != null)
6574 08 Feb 22 nicklas 788             {
6574 08 Feb 22 nicklas 789               // Create a SAMPLING_DATE reference date and check if it should replace the existing
6574 08 Feb 22 nicklas 790               refDate = refDate.getThisOrTheReplacement(new ReferenceDateWithSource(samplingDateTime, ReferenceDateSource.SAMPLING_DATE));
6574 08 Feb 22 nicklas 791             }
6574 08 Feb 22 nicklas 792           }
6574 08 Feb 22 nicklas 793           else
6574 08 Feb 22 nicklas 794           {
6574 08 Feb 22 nicklas 795             Annotationtype.REASON_IF_NO_SPECIMEN.setAnnotationValue(dc, noSpecimen, Values.getStringOrNull((String)jsonCase.get("reasonIfNoSpecimen")));
6574 08 Feb 22 nicklas 796             Date samplingDate = Reggie.CONVERTER_STRING_TO_DATE.convert((String)jsonCase.get("samplingDate"));
6574 08 Feb 22 nicklas 797             Annotationtype.SAMPLING_DATETIME.setAnnotationValue(dc, noSpecimen, samplingDate);
6574 08 Feb 22 nicklas 798             creationEvent.setEventDate(samplingDate);
6574 08 Feb 22 nicklas 799             if (samplingDate != null)
6574 08 Feb 22 nicklas 800             {
6574 08 Feb 22 nicklas 801               // Create a SAMPLING_DATE reference date and check if it should replace the existing
6574 08 Feb 22 nicklas 802               refDate = refDate.getThisOrTheReplacement(new ReferenceDateWithSource(samplingDate, ReferenceDateSource.SAMPLING_DATE));
6574 08 Feb 22 nicklas 803             }
6574 08 Feb 22 nicklas 804           }
1333 05 Apr 11 nicklas 805         }
1333 05 Apr 11 nicklas 806         
4460 21 Apr 17 nicklas 807         if (refDate != null) refDate.updateAnnotations(dc, theCase);
4460 21 Apr 17 nicklas 808         
1326 29 Mar 11 nicklas 809         dc.commit();
4034 29 Jul 16 nicklas 810         
4141 30 Sep 16 nicklas 811         if (itemsToPublish.size() > 0)
4034 29 Jul 16 nicklas 812         {
4141 30 Sep 16 nicklas 813           permissionChangeRequest.setParameterValue("items", itemsToPublish);
4034 29 Jul 16 nicklas 814           PluginResponse response = permissionChangeRequest.invoke();
4034 29 Jul 16 nicklas 815           if (response.getStatus() != Response.Status.DONE)
4034 29 Jul 16 nicklas 816           {
4034 29 Jul 16 nicklas 817             jsonMessages.add("[Warning]Could not set permissions for specimen/case/patient to site (see below)");
4034 29 Jul 16 nicklas 818             jsonMessages.add("[Warning]"+response.getMessage());
4034 29 Jul 16 nicklas 819           }
4034 29 Jul 16 nicklas 820         }
1326 29 Mar 11 nicklas 821       }
3059 19 Dec 14 nicklas 822       json.put("messages", jsonMessages);
3059 19 Dec 14 nicklas 823       CounterService.getInstance().setForceCount();
1286 08 Feb 11 nicklas 824     }
1286 08 Feb 11 nicklas 825     catch (Throwable t)
1286 08 Feb 11 nicklas 826     {
1286 08 Feb 11 nicklas 827       t.printStackTrace();
1286 08 Feb 11 nicklas 828       json.clear();
1286 08 Feb 11 nicklas 829       json.put("status", "error");
1286 08 Feb 11 nicklas 830       json.put("message", t.getMessage());
1286 08 Feb 11 nicklas 831       json.put("stacktrace", ThrowableUtil.stackTraceToString(t));
1286 08 Feb 11 nicklas 832     }
1286 08 Feb 11 nicklas 833     finally
1286 08 Feb 11 nicklas 834     {
1286 08 Feb 11 nicklas 835       if (dc != null) dc.close();
1286 08 Feb 11 nicklas 836       json.writeJSONString(resp.getWriter());
1286 08 Feb 11 nicklas 837     }
1286 08 Feb 11 nicklas 838     
1286 08 Feb 11 nicklas 839   }
1283 01 Feb 11 nicklas 840   
1513 19 Jan 12 nicklas 841   private BioSource getOrCreatePatient(DbControl dc, JSONObject jsonPat, JSONArray jsonMessages)
1513 19 Jan 12 nicklas 842     throws Exception
1513 19 Jan 12 nicklas 843   {
1513 19 Jan 12 nicklas 844     Number patientId = (Number)jsonPat.get("id");
1513 19 Jan 12 nicklas 845     BioSource patient = null;
1767 04 Dec 12 nicklas 846     String familyName = Values.getStringOrNull((String)jsonPat.get("familyName"));
1767 04 Dec 12 nicklas 847     String allFirstNames = Values.getStringOrNull((String)jsonPat.get("allFirstNames"));
1513 19 Jan 12 nicklas 848     if (patientId != null)
1513 19 Jan 12 nicklas 849     {
1513 19 Jan 12 nicklas 850       patient = BioSource.getById(dc, patientId.intValue());
1572 16 Mar 12 nicklas 851       // Update names
1767 04 Dec 12 nicklas 852       Annotationtype.FAMILY_NAME.setAnnotationValue(dc, patient, familyName);
1767 04 Dec 12 nicklas 853       Annotationtype.ALL_FIRST_NAMES.setAnnotationValue(dc, patient, allFirstNames);
1513 19 Jan 12 nicklas 854     }
1513 19 Jan 12 nicklas 855     else
1513 19 Jan 12 nicklas 856     {
2613 29 Aug 14 nicklas 857       String pnr = (String)jsonPat.get("personalNumber");
2613 29 Aug 14 nicklas 858       String patName = (String)jsonPat.get("name");
2613 29 Aug 14 nicklas 859       Patient.ensureNotExistingPatient(dc, pnr, patName);
2613 29 Aug 14 nicklas 860
1513 19 Jan 12 nicklas 861       // Create new patient
1513 19 Jan 12 nicklas 862       patient = BioSource.getNew(dc);
1513 19 Jan 12 nicklas 863       patient.setItemSubtype(Subtype.PATIENT.load(dc));
4898 10 Jul 18 nicklas 864       patient.setName(patName);
5088 13 Nov 18 nicklas 865       patient.setExternalId(Patient.getNextExternalId(dc, Subtype.PATIENT));
4073 02 Sep 16 nicklas 866
7024 07 Feb 23 nicklas 867       StringToDateConverter dateConverter = new StringToDateConverter(FastDateFormat.getInstance("yyyy-MM-dd"));
1767 04 Dec 12 nicklas 868       Date dateOfBirth = dateConverter.convert((String)jsonPat.get("dateOfBirth"));
1767 04 Dec 12 nicklas 869       String gender = Values.getStringOrNull((String)jsonPat.get("gender"));
1513 19 Jan 12 nicklas 870       
1767 04 Dec 12 nicklas 871       Annotationtype.PERSONAL_NUMBER.setAnnotationValue(dc, patient, pnr);
1767 04 Dec 12 nicklas 872       Annotationtype.FAMILY_NAME.setAnnotationValue(dc, patient, familyName);
1767 04 Dec 12 nicklas 873       Annotationtype.ALL_FIRST_NAMES.setAnnotationValue(dc, patient, allFirstNames);
1767 04 Dec 12 nicklas 874       Annotationtype.GENDER.setAnnotationValue(dc, patient, gender);
1767 04 Dec 12 nicklas 875       Annotationtype.DATE_OF_BIRTH.setAnnotationValue(dc, patient, dateOfBirth);
1513 19 Jan 12 nicklas 876       
1513 19 Jan 12 nicklas 877       if (gender == null || dateOfBirth == null)
1513 19 Jan 12 nicklas 878       {
1513 19 Jan 12 nicklas 879         patient.setDescription("This patient was recorded with a non-standard PersonalNumber. " +
1513 19 Jan 12 nicklas 880             "DateOfBirth and Gender annotations could not be automatically generated.");
1513 19 Jan 12 nicklas 881       }
1513 19 Jan 12 nicklas 882       dc.saveItem(patient);
1513 19 Jan 12 nicklas 883       
5421 13 May 19 nicklas 884       ActivityDef.REGISTERED_PATIENT.merge(dc, 1);
5387 26 Apr 19 nicklas 885
1513 19 Jan 12 nicklas 886       jsonMessages.add("Patient '" + patient.getName() + "' created successfully.");
1513 19 Jan 12 nicklas 887     }
1513 19 Jan 12 nicklas 888     return patient;
1513 19 Jan 12 nicklas 889
1513 19 Jan 12 nicklas 890   }
1283 01 Feb 11 nicklas 891   
1283 01 Feb 11 nicklas 892 }