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

Code
Comments
Other
Rev Date Author Line
1472 23 Nov 11 martin 1 package net.sf.basedb.reggie.servlet;
1472 23 Nov 11 martin 2
1472 23 Nov 11 martin 3 import java.io.IOException;
1472 23 Nov 11 martin 4 import java.util.Date;
1732 13 Nov 12 olle 5 import java.util.HashMap;
1730 01 Nov 12 olle 6 import java.util.ArrayList;
1730 01 Nov 12 olle 7 import java.util.List;
1732 13 Nov 12 olle 8 import java.util.HashSet;
1732 13 Nov 12 olle 9 import java.util.Set;
1472 23 Nov 11 martin 10
1472 23 Nov 11 martin 11 import javax.servlet.ServletException;
1472 23 Nov 11 martin 12 import javax.servlet.http.HttpServlet;
1472 23 Nov 11 martin 13 import javax.servlet.http.HttpServletRequest;
1472 23 Nov 11 martin 14 import javax.servlet.http.HttpServletResponse;
1472 23 Nov 11 martin 15
1732 13 Nov 12 olle 16 import net.sf.basedb.core.BioMaterial;
1494 16 Dec 11 martin 17 import net.sf.basedb.core.BioMaterialEvent;
1472 23 Nov 11 martin 18 import net.sf.basedb.core.DbControl;
1472 23 Nov 11 martin 19 import net.sf.basedb.core.ItemQuery;
1472 23 Nov 11 martin 20 import net.sf.basedb.core.ItemResultIterator;
1744 27 Nov 12 olle 21 import net.sf.basedb.core.ItemSubtype;
1744 27 Nov 12 olle 22 import net.sf.basedb.core.MeasuredBioMaterial;
3911 02 May 16 nicklas 23 import net.sf.basedb.core.ProgressReporter;
1472 23 Nov 11 martin 24 import net.sf.basedb.core.Sample;
1472 23 Nov 11 martin 25 import net.sf.basedb.core.SessionControl;
3911 02 May 16 nicklas 26 import net.sf.basedb.core.SimpleProgressReporter;
1472 23 Nov 11 martin 27 import net.sf.basedb.core.query.Hql;
1494 16 Dec 11 martin 28 import net.sf.basedb.core.query.Orders;
1732 13 Nov 12 olle 29 import net.sf.basedb.core.query.Restrictions;
1732 13 Nov 12 olle 30 import net.sf.basedb.core.snapshot.SnapshotManager;
2598 22 Aug 14 nicklas 31 import net.sf.basedb.reggie.JsonUtil;
1547 06 Mar 12 nicklas 32 import net.sf.basedb.reggie.Reggie;
1641 09 May 12 nicklas 33 import net.sf.basedb.reggie.Site;
1472 23 Nov 11 martin 34 import net.sf.basedb.reggie.converter.DateToStringConverter;
1732 13 Nov 12 olle 35 import net.sf.basedb.reggie.dao.Annotationtype;
1492 02 Dec 11 nicklas 36 import net.sf.basedb.reggie.dao.Subtype;
1472 23 Nov 11 martin 37 import net.sf.basedb.util.Values;
1472 23 Nov 11 martin 38 import net.sf.basedb.util.error.ThrowableUtil;
1472 23 Nov 11 martin 39
7024 07 Feb 23 nicklas 40 import org.apache.commons.lang3.time.FastDateFormat;
1472 23 Nov 11 martin 41 import org.json.simple.JSONObject;
1472 23 Nov 11 martin 42
1472 23 Nov 11 martin 43 public class SampleReportServlet 
1472 23 Nov 11 martin 44   extends HttpServlet 
1472 23 Nov 11 martin 45 {
1472 23 Nov 11 martin 46   private static final long serialVersionUID = 9211971419478625406L;
1735 16 Nov 12 olle 47
2611 29 Aug 14 nicklas 48   private ReportTableUtil tableUtil;
2024 20 Sep 13 olle 49   private final static String TABLE_ALTERNATIVE_FULL = "full_blood_sample_tables";
2024 20 Sep 13 olle 50   private final static String TABLE_ALTERNATIVE_BLOOD_FILTER = "blood_sample_table_with_filter";
2024 20 Sep 13 olle 51   private final static String BLOOD_FILTER_NONE = "none";
2024 20 Sep 13 olle 52   private final static String BLOOD_FILTER_FOLLOW_UP = "followup";
2024 20 Sep 13 olle 53   private final static String BLOOD_FILTER_NOT_FOLLOW_UP = "notfollowup";
2024 20 Sep 13 olle 54   private final static String BLOOD_FILTER_UNKNOWN = "";
2024 20 Sep 13 olle 55   private final static String BLOOD_FILTER_PRENEO = "PreNeo";
2024 20 Sep 13 olle 56   private final static String BLOOD_FILTER_PREOP = "PreOp";
2024 20 Sep 13 olle 57   private final static String BLOOD_FILTER_FOLLOW_UP_06 = "FollowUp06";
2024 20 Sep 13 olle 58   private final static String BLOOD_FILTER_FOLLOW_UP_12 = "FollowUp12";
2024 20 Sep 13 olle 59   private final static String BLOOD_FILTER_FOLLOW_UP_36 = "FollowUp36";
1813 30 Jan 13 olle 60
1472 23 Nov 11 martin 61   public SampleReportServlet()
1813 30 Jan 13 olle 62   {
1813 30 Jan 13 olle 63     // Create new instance of ReportTableUtilServlet for common report table utilities
2611 29 Aug 14 nicklas 64     tableUtil = new ReportTableUtil();
1813 30 Jan 13 olle 65   }
1472 23 Nov 11 martin 66     
1472 23 Nov 11 martin 67   @Override
1472 23 Nov 11 martin 68   protected void doGet(HttpServletRequest req, HttpServletResponse resp)
1472 23 Nov 11 martin 69     throws ServletException, IOException 
1472 23 Nov 11 martin 70   {  
1472 23 Nov 11 martin 71     String cmd = req.getParameter("cmd");
2598 22 Aug 14 nicklas 72     JsonUtil.setJsonResponseHeaders(resp);
1472 23 Nov 11 martin 73     
1472 23 Nov 11 martin 74     JSONObject json = new JSONObject();
1472 23 Nov 11 martin 75     json.put("status", "ok");
1472 23 Nov 11 martin 76     
3975 26 May 16 nicklas 77     final SessionControl sc = Reggie.getSessionControl(req);
3911 02 May 16 nicklas 78     DbControl dc = null;
3911 02 May 16 nicklas 79     ProgressReporter progress = new SimpleProgressReporter(null);
3911 02 May 16 nicklas 80     
6337 16 Jun 21 nicklas 81     dc = sc.newDbControl(":Sample source report");        
1472 23 Nov 11 martin 82     
1472 23 Nov 11 martin 83     try
1472 23 Nov 11 martin 84     {
3911 02 May 16 nicklas 85       sc.setSessionSetting(cmd+"-progress", progress);
1472 23 Nov 11 martin 86       if ("samplecount".equals(cmd))
1472 23 Nov 11 martin 87       {        
1494 16 Dec 11 martin 88         String startDateParameter = Values.getString(req.getParameter("fdate"), null);
1494 16 Dec 11 martin 89         String endDateParameter = Values.getString(req.getParameter("tdate"), null);
1641 09 May 12 nicklas 90
1641 09 May 12 nicklas 91         Date startDate = Reggie.CONVERTER_STRING_TO_DATE.convert(startDateParameter);
1641 09 May 12 nicklas 92         Date endDate = Reggie.CONVERTER_STRING_TO_DATE.convert(endDateParameter);
1494 16 Dec 11 martin 93         
1729 23 Oct 12 olle 94         if (startDate == null)
1729 23 Oct 12 olle 95         {
1729 23 Oct 12 olle 96           // Get the when the first site started
1729 23 Oct 12 olle 97           for (Site s : Site.getAllSites())
1729 23 Oct 12 olle 98           {
1729 23 Oct 12 olle 99             Date siteDate = Reggie.CONVERTER_STRING_TO_DATE.convert(s.getStartDate().replaceAll("-", ""));
1729 23 Oct 12 olle 100             if (siteDate != null && (startDate == null || startDate.after(siteDate))) 
1729 23 Oct 12 olle 101             {
1729 23 Oct 12 olle 102               startDate = siteDate;            
1729 23 Oct 12 olle 103             }
1729 23 Oct 12 olle 104           }
1729 23 Oct 12 olle 105         }
1729 23 Oct 12 olle 106         if (endDate == null) 
1729 23 Oct 12 olle 107         {
1729 23 Oct 12 olle 108           // Get the date for today
1729 23 Oct 12 olle 109           endDate = new Date();
1729 23 Oct 12 olle 110         }
1729 23 Oct 12 olle 111
1728 23 Oct 12 olle 112         String viewType = null;
1728 23 Oct 12 olle 113         String viewTypeParameter = Values.getString(req.getParameter("vtype"), null);
2611 29 Aug 14 nicklas 114         if (viewTypeParameter != null && !viewTypeParameter.equals(ReportTableUtil.autoView))
1728 23 Oct 12 olle 115         {
1728 23 Oct 12 olle 116           viewType = viewTypeParameter;
1728 23 Oct 12 olle 117         }
1729 23 Oct 12 olle 118         else
1729 23 Oct 12 olle 119         {
1813 30 Jan 13 olle 120           viewType = tableUtil.getViewType(startDate, endDate);
1729 23 Oct 12 olle 121         }
1748 27 Nov 12 olle 122         String sampleType = "specimen";
1748 27 Nov 12 olle 123         String sampleTypeParameter = Values.getString(req.getParameter("stype"), null);
1748 27 Nov 12 olle 124         if (sampleTypeParameter != null)
1748 27 Nov 12 olle 125         {
1748 27 Nov 12 olle 126           sampleType = sampleTypeParameter;
1748 27 Nov 12 olle 127         }
2024 20 Sep 13 olle 128         String tableAlternatives = TABLE_ALTERNATIVE_FULL;
2024 20 Sep 13 olle 129         String tableAlternativesParameter = Values.getString(req.getParameter("tablealternatives"), null);
2024 20 Sep 13 olle 130         if (tableAlternativesParameter != null)
2024 20 Sep 13 olle 131         {
2024 20 Sep 13 olle 132           tableAlternatives = tableAlternativesParameter;
2024 20 Sep 13 olle 133         }
2024 20 Sep 13 olle 134         String bloodSampleFilter = BLOOD_FILTER_NONE;
2024 20 Sep 13 olle 135         String bloodSampleFilterParameter = Values.getString(req.getParameter("bloodsamplefilter"), null);
2024 20 Sep 13 olle 136         if (bloodSampleFilterParameter != null)
2024 20 Sep 13 olle 137         {
2024 20 Sep 13 olle 138           bloodSampleFilter = bloodSampleFilterParameter;
2024 20 Sep 13 olle 139         }
1728 23 Oct 12 olle 140
2024 20 Sep 13 olle 141         json = createSampleCountReport(dc, json, startDate, endDate, viewType, sampleType, tableAlternatives, bloodSampleFilter);
1472 23 Nov 11 martin 142       }
1732 13 Nov 12 olle 143       else if ("consentcount".equals(cmd))
1732 13 Nov 12 olle 144       {        
1738 20 Nov 12 olle 145         String startDateParameter = Values.getString(req.getParameter("fdate"), null);
1738 20 Nov 12 olle 146         String endDateParameter = Values.getString(req.getParameter("tdate"), null);
1738 20 Nov 12 olle 147
1738 20 Nov 12 olle 148         Date startDate = Reggie.CONVERTER_STRING_TO_DATE.convert(startDateParameter);
1738 20 Nov 12 olle 149         Date endDate = Reggie.CONVERTER_STRING_TO_DATE.convert(endDateParameter);
1738 20 Nov 12 olle 150         
1738 20 Nov 12 olle 151         if (startDate == null)
1738 20 Nov 12 olle 152         {
1738 20 Nov 12 olle 153           // Get the when the first site started
1738 20 Nov 12 olle 154           for (Site s : Site.getAllSites())
1738 20 Nov 12 olle 155           {
1738 20 Nov 12 olle 156             Date siteDate = Reggie.CONVERTER_STRING_TO_DATE.convert(s.getStartDate().replaceAll("-", ""));
1738 20 Nov 12 olle 157             if (siteDate != null && (startDate == null || startDate.after(siteDate))) 
1738 20 Nov 12 olle 158             {
1738 20 Nov 12 olle 159               startDate = siteDate;            
1738 20 Nov 12 olle 160             }
1738 20 Nov 12 olle 161           }
1738 20 Nov 12 olle 162         }
1738 20 Nov 12 olle 163         if (endDate == null) 
1738 20 Nov 12 olle 164         {
1738 20 Nov 12 olle 165           // Get the date for today
1738 20 Nov 12 olle 166           endDate = new Date();
1738 20 Nov 12 olle 167         }
1738 20 Nov 12 olle 168
3911 02 May 16 nicklas 169         json = createConsentCountReport(dc, json, startDate, endDate, progress);
1732 13 Nov 12 olle 170       }
1744 27 Nov 12 olle 171       else if ("patientcount".equals(cmd))
1744 27 Nov 12 olle 172       {        
1744 27 Nov 12 olle 173         String startDateParameter = Values.getString(req.getParameter("fdate"), null);
1744 27 Nov 12 olle 174         String endDateParameter = Values.getString(req.getParameter("tdate"), null);
1744 27 Nov 12 olle 175
1744 27 Nov 12 olle 176         Date startDate = Reggie.CONVERTER_STRING_TO_DATE.convert(startDateParameter);
1744 27 Nov 12 olle 177         Date endDate = Reggie.CONVERTER_STRING_TO_DATE.convert(endDateParameter);
1744 27 Nov 12 olle 178         
1744 27 Nov 12 olle 179         if (startDate == null)
1744 27 Nov 12 olle 180         {
1744 27 Nov 12 olle 181           // Get the when the first site started
1744 27 Nov 12 olle 182           for (Site s : Site.getAllSites())
1744 27 Nov 12 olle 183           {
1744 27 Nov 12 olle 184             Date siteDate = Reggie.CONVERTER_STRING_TO_DATE.convert(s.getStartDate().replaceAll("-", ""));
1744 27 Nov 12 olle 185             if (siteDate != null && (startDate == null || startDate.after(siteDate))) 
1744 27 Nov 12 olle 186             {
1744 27 Nov 12 olle 187               startDate = siteDate;            
1744 27 Nov 12 olle 188             }
1744 27 Nov 12 olle 189           }
1744 27 Nov 12 olle 190         }
1744 27 Nov 12 olle 191         if (endDate == null) 
1744 27 Nov 12 olle 192         {
1744 27 Nov 12 olle 193           // Get the date for today
1744 27 Nov 12 olle 194           endDate = new Date();
1744 27 Nov 12 olle 195         }
1744 27 Nov 12 olle 196
1744 27 Nov 12 olle 197         String viewType = null;
1744 27 Nov 12 olle 198         String viewTypeParameter = Values.getString(req.getParameter("vtype"), null);
2611 29 Aug 14 nicklas 199         if (viewTypeParameter != null && !viewTypeParameter.equals(ReportTableUtil.autoView))
1744 27 Nov 12 olle 200         {
1744 27 Nov 12 olle 201           viewType = viewTypeParameter;
1744 27 Nov 12 olle 202         }
1744 27 Nov 12 olle 203         else
1744 27 Nov 12 olle 204         {
1813 30 Jan 13 olle 205           viewType = tableUtil.getViewType(startDate, endDate);
1744 27 Nov 12 olle 206         }
1744 27 Nov 12 olle 207
3911 02 May 16 nicklas 208         json = createPatientCountReport(dc, json, startDate, endDate, viewType, progress);
1744 27 Nov 12 olle 209       }
1765 03 Dec 12 olle 210       else if ("overviewreport".equals(cmd))
1765 03 Dec 12 olle 211       {
1765 03 Dec 12 olle 212         String startDateParameter = Values.getString(req.getParameter("fdate"), null);
1765 03 Dec 12 olle 213         String endDateParameter = Values.getString(req.getParameter("tdate"), null);
1765 03 Dec 12 olle 214
1765 03 Dec 12 olle 215         Date startDate = Reggie.CONVERTER_STRING_TO_DATE.convert(startDateParameter);
1765 03 Dec 12 olle 216         Date endDate = Reggie.CONVERTER_STRING_TO_DATE.convert(endDateParameter);
1765 03 Dec 12 olle 217         
1765 03 Dec 12 olle 218         if (startDate == null)
1765 03 Dec 12 olle 219         {
1765 03 Dec 12 olle 220           // Get the when the first site started
1765 03 Dec 12 olle 221           for (Site s : Site.getAllSites())
1765 03 Dec 12 olle 222           {
1765 03 Dec 12 olle 223             Date siteDate = Reggie.CONVERTER_STRING_TO_DATE.convert(s.getStartDate().replaceAll("-", ""));
1765 03 Dec 12 olle 224             if (siteDate != null && (startDate == null || startDate.after(siteDate))) 
1765 03 Dec 12 olle 225             {
1765 03 Dec 12 olle 226               startDate = siteDate;            
1765 03 Dec 12 olle 227             }
1765 03 Dec 12 olle 228           }
1765 03 Dec 12 olle 229         }
1765 03 Dec 12 olle 230         if (endDate == null) 
1765 03 Dec 12 olle 231         {
1765 03 Dec 12 olle 232           // Get the date for today
1765 03 Dec 12 olle 233           endDate = new Date();
1765 03 Dec 12 olle 234         }
1765 03 Dec 12 olle 235
2024 20 Sep 13 olle 236         String bloodSampleFilter = BLOOD_FILTER_NONE;
2024 20 Sep 13 olle 237         String bloodSampleFilterParameter = Values.getString(req.getParameter("bloodsamplefilter"), null);
2024 20 Sep 13 olle 238         if (bloodSampleFilterParameter != null)
2024 20 Sep 13 olle 239         {
2024 20 Sep 13 olle 240           bloodSampleFilter = bloodSampleFilterParameter;
2024 20 Sep 13 olle 241         }
2024 20 Sep 13 olle 242
3911 02 May 16 nicklas 243         json = createOverviewReport(dc, json, startDate, endDate, bloodSampleFilter, progress);
1765 03 Dec 12 olle 244       }
1772 06 Dec 12 olle 245       else if ("missingsampledatareport".equals(cmd))
1772 06 Dec 12 olle 246       {
1772 06 Dec 12 olle 247         String startDateParameter = Values.getString(req.getParameter("fdate"), null);
1772 06 Dec 12 olle 248         String endDateParameter = Values.getString(req.getParameter("tdate"), null);
1772 06 Dec 12 olle 249
1772 06 Dec 12 olle 250         Date startDate = Reggie.CONVERTER_STRING_TO_DATE.convert(startDateParameter);
1772 06 Dec 12 olle 251         Date endDate = Reggie.CONVERTER_STRING_TO_DATE.convert(endDateParameter);
1772 06 Dec 12 olle 252         
1772 06 Dec 12 olle 253         if (startDate == null)
1772 06 Dec 12 olle 254         {
1772 06 Dec 12 olle 255           // Get the when the first site started
1772 06 Dec 12 olle 256           for (Site s : Site.getAllSites())
1772 06 Dec 12 olle 257           {
1772 06 Dec 12 olle 258             Date siteDate = Reggie.CONVERTER_STRING_TO_DATE.convert(s.getStartDate().replaceAll("-", ""));
1772 06 Dec 12 olle 259             if (siteDate != null && (startDate == null || startDate.after(siteDate))) 
1772 06 Dec 12 olle 260             {
1772 06 Dec 12 olle 261               startDate = siteDate;            
1772 06 Dec 12 olle 262             }
1772 06 Dec 12 olle 263           }
1772 06 Dec 12 olle 264         }
1772 06 Dec 12 olle 265         if (endDate == null) 
1772 06 Dec 12 olle 266         {
1772 06 Dec 12 olle 267           // Get the date for today
1772 06 Dec 12 olle 268           endDate = new Date();
1772 06 Dec 12 olle 269         }
1772 06 Dec 12 olle 270
1772 06 Dec 12 olle 271         String sampleType = "specimen";
1772 06 Dec 12 olle 272         String sampleTypeParameter = Values.getString(req.getParameter("stype"), null);
1772 06 Dec 12 olle 273         if (sampleTypeParameter != null)
1772 06 Dec 12 olle 274         {
1772 06 Dec 12 olle 275           sampleType = sampleTypeParameter;
1772 06 Dec 12 olle 276         }
2024 20 Sep 13 olle 277         String bloodSampleFilter = BLOOD_FILTER_NONE;
2024 20 Sep 13 olle 278         String bloodSampleFilterParameter = Values.getString(req.getParameter("bloodsamplefilter"), null);
2024 20 Sep 13 olle 279         if (bloodSampleFilterParameter != null)
2024 20 Sep 13 olle 280         {
2024 20 Sep 13 olle 281           bloodSampleFilter = bloodSampleFilterParameter;
2024 20 Sep 13 olle 282         }
1772 06 Dec 12 olle 283
3911 02 May 16 nicklas 284         json = createMissingSampleDataReport(dc, json, startDate, endDate, sampleType, bloodSampleFilter, progress);
1772 06 Dec 12 olle 285       }
1472 23 Nov 11 martin 286     }
1472 23 Nov 11 martin 287     catch (Throwable t)
1472 23 Nov 11 martin 288     {
1472 23 Nov 11 martin 289       t.printStackTrace();
1472 23 Nov 11 martin 290       json.clear();
1472 23 Nov 11 martin 291       json.put("status", "error");
1472 23 Nov 11 martin 292       json.put("message", t.getMessage());
1472 23 Nov 11 martin 293       json.put("stacktrace", ThrowableUtil.stackTraceToString(t));
1472 23 Nov 11 martin 294     }
1472 23 Nov 11 martin 295     finally
1472 23 Nov 11 martin 296     {
1472 23 Nov 11 martin 297       if (dc != null) dc.close();
3911 02 May 16 nicklas 298       if (sc != null) sc.setSessionSetting(cmd+"-progress", null);
1472 23 Nov 11 martin 299       json.writeJSONString(resp.getWriter());
1472 23 Nov 11 martin 300     }
1472 23 Nov 11 martin 301   }
1472 23 Nov 11 martin 302   
2024 20 Sep 13 olle 303   private JSONObject createSampleCountReport(DbControl dc, JSONObject json, Date startDate, Date endDate, String viewType, String sampleType, String tableAlternatives, String bloodSampleFilter)
1730 01 Nov 12 olle 304     throws ServletException, IOException 
1730 01 Nov 12 olle 305   {  
1730 01 Nov 12 olle 306     JSONObject jsonReport = new JSONObject();
2611 29 Aug 14 nicklas 307     jsonReport.put("sites", ReportTableUtil.getJSONSites(Site.SORT_BY_NAME));
1730 01 Nov 12 olle 308     
1730 01 Nov 12 olle 309     //List<Restriction> restrictions = new ArrayList<Restriction>();
1730 01 Nov 12 olle 310     ItemQuery<Sample> sampleQuery = Sample.getQuery();
1730 01 Nov 12 olle 311     sampleQuery.joinPermanent(Hql.innerJoin(null, "creationEvent", "ce", true));
1766 04 Dec 12 olle 312     if (sampleType.equals("nospecimen"))
1748 27 Nov 12 olle 313     {
1766 04 Dec 12 olle 314       Subtype.NO_SPECIMEN.addFilter(dc, sampleQuery);
1766 04 Dec 12 olle 315     }
1766 04 Dec 12 olle 316     else if (sampleType.equals("blood"))
1766 04 Dec 12 olle 317     {
1748 27 Nov 12 olle 318       Subtype.BLOOD.addFilter(dc, sampleQuery);
1748 27 Nov 12 olle 319     }
1748 27 Nov 12 olle 320     else
1748 27 Nov 12 olle 321     {
1748 27 Nov 12 olle 322       Subtype.SPECIMEN.addFilter(dc, sampleQuery);
1748 27 Nov 12 olle 323     }
1730 01 Nov 12 olle 324     sampleQuery.setIncludes(Reggie.INCLUDE_IN_CURRENT_PROJECT);
1730 01 Nov 12 olle 325     sampleQuery.order(Orders.asc(Hql.property("name")));
1730 01 Nov 12 olle 326     sampleQuery.order(Orders.asc(Hql.property("ce", "eventDate")));
1730 01 Nov 12 olle 327     sampleQuery.setCacheResult(true);
1730 01 Nov 12 olle 328     ItemResultIterator<Sample> sampleIterator = sampleQuery.iterate(dc);        
2024 20 Sep 13 olle 329     // Use stored annotation snapshots for performance reasons
2024 20 Sep 13 olle 330     SnapshotManager manager = new SnapshotManager();        
1744 27 Nov 12 olle 331     // Store samples that should be processed for report table in list 
2024 20 Sep 13 olle 332     List<Sample> rawSampleList = new ArrayList<Sample>();
1744 27 Nov 12 olle 333     while (sampleIterator.hasNext())
1744 27 Nov 12 olle 334     {
1744 27 Nov 12 olle 335       Sample s = sampleIterator.next();
2024 20 Sep 13 olle 336       rawSampleList.add(s);
1744 27 Nov 12 olle 337     }
2024 20 Sep 13 olle 338     String latestDateStr = null;
2024 20 Sep 13 olle 339     if (!sampleType.equals("blood") || tableAlternatives.equals(TABLE_ALTERNATIVE_BLOOD_FILTER))
2024 20 Sep 13 olle 340     {
2024 20 Sep 13 olle 341       List<Sample> sampleList = createSampleCountSampleList(dc, manager, rawSampleList, bloodSampleFilter);
2024 20 Sep 13 olle 342       
2024 20 Sep 13 olle 343       JSONObject jsonStatistics = tableUtil.createJSONStatistics(sampleList.iterator(), startDate, endDate, viewType);
2024 20 Sep 13 olle 344
2024 20 Sep 13 olle 345       jsonReport.put("statistics", jsonStatistics);
2024 20 Sep 13 olle 346       latestDateStr = (String) jsonStatistics.get("latestDateKey");
2024 20 Sep 13 olle 347     }
2024 20 Sep 13 olle 348     else if (tableAlternatives.equals(TABLE_ALTERNATIVE_FULL))
2024 20 Sep 13 olle 349     {
2024 20 Sep 13 olle 350       // Get sample lists for different blood sample filters
2024 20 Sep 13 olle 351       List<Sample> sampleListNone = createSampleCountSampleList(dc, manager, rawSampleList, BLOOD_FILTER_NONE);
2024 20 Sep 13 olle 352       
2024 20 Sep 13 olle 353       JSONObject jsonStatisticsNone = tableUtil.createJSONStatistics(sampleListNone.iterator(), startDate, endDate, viewType);
2024 20 Sep 13 olle 354
2024 20 Sep 13 olle 355       jsonReport.put("statistics", jsonStatisticsNone);
2024 20 Sep 13 olle 356       latestDateStr = (String) jsonStatisticsNone.get("latestDateKey");
2024 20 Sep 13 olle 357
2024 20 Sep 13 olle 358       // Add extra JSON reports as child items to outer JSON report
2024 20 Sep 13 olle 359       List<Sample> sampleListFollowUp = createSampleCountSampleList(dc, manager, rawSampleList, BLOOD_FILTER_FOLLOW_UP);
2024 20 Sep 13 olle 360       List<Sample> sampleListNotFollowUp = createSampleCountSampleList(dc, manager, rawSampleList, BLOOD_FILTER_NOT_FOLLOW_UP);
2024 20 Sep 13 olle 361       List<Sample> sampleListUnknown = createSampleCountSampleList(dc, manager, rawSampleList, BLOOD_FILTER_UNKNOWN);
2024 20 Sep 13 olle 362       List<Sample> sampleListPreNeo = createSampleCountSampleList(dc, manager, rawSampleList, BLOOD_FILTER_PRENEO);
2024 20 Sep 13 olle 363       List<Sample> sampleListPreOp = createSampleCountSampleList(dc, manager, rawSampleList, BLOOD_FILTER_PREOP);
2024 20 Sep 13 olle 364       List<Sample> sampleListFollow06 = createSampleCountSampleList(dc, manager, rawSampleList, BLOOD_FILTER_FOLLOW_UP_06);
2024 20 Sep 13 olle 365       List<Sample> sampleListFollow12 = createSampleCountSampleList(dc, manager, rawSampleList, BLOOD_FILTER_FOLLOW_UP_12);
2024 20 Sep 13 olle 366       List<Sample> sampleListFollow36 = createSampleCountSampleList(dc, manager, rawSampleList, BLOOD_FILTER_FOLLOW_UP_36);
2024 20 Sep 13 olle 367
2024 20 Sep 13 olle 368       JSONObject jsonReportFollowUp = createSampleCountReport(sampleListFollowUp, startDate, endDate, viewType, sampleType, tableAlternatives, BLOOD_FILTER_FOLLOW_UP);
2024 20 Sep 13 olle 369       JSONObject jsonReportNotFollowUp = createSampleCountReport(sampleListNotFollowUp, startDate, endDate, viewType, sampleType, tableAlternatives, BLOOD_FILTER_NOT_FOLLOW_UP);
2024 20 Sep 13 olle 370       JSONObject jsonReportUnknown = createSampleCountReport(sampleListUnknown, startDate, endDate, viewType, sampleType, tableAlternatives, BLOOD_FILTER_UNKNOWN);
2024 20 Sep 13 olle 371       JSONObject jsonReportPreNeo = createSampleCountReport(sampleListPreNeo, startDate, endDate, viewType, sampleType, tableAlternatives, BLOOD_FILTER_PRENEO);
2024 20 Sep 13 olle 372       JSONObject jsonReportPreOp = createSampleCountReport(sampleListPreOp, startDate, endDate, viewType, sampleType, tableAlternatives, BLOOD_FILTER_PREOP);
2024 20 Sep 13 olle 373       JSONObject jsonReportFollow06 = createSampleCountReport(sampleListFollow06, startDate, endDate, viewType, sampleType, tableAlternatives, BLOOD_FILTER_FOLLOW_UP_06);
2024 20 Sep 13 olle 374       JSONObject jsonReportFollow12 = createSampleCountReport(sampleListFollow12, startDate, endDate, viewType, sampleType, tableAlternatives, BLOOD_FILTER_FOLLOW_UP_12);
2024 20 Sep 13 olle 375       JSONObject jsonReportFollow36 = createSampleCountReport(sampleListFollow36, startDate, endDate, viewType, sampleType, tableAlternatives, BLOOD_FILTER_FOLLOW_UP_36);
2024 20 Sep 13 olle 376
2024 20 Sep 13 olle 377       jsonReport.put("reportFollowUp", jsonReportFollowUp);
2024 20 Sep 13 olle 378       jsonReport.put("reportNotFollowUp", jsonReportNotFollowUp);
2024 20 Sep 13 olle 379       jsonReport.put("reportUnknown", jsonReportUnknown);
2024 20 Sep 13 olle 380       jsonReport.put("reportPreNeo", jsonReportPreNeo);
2024 20 Sep 13 olle 381       jsonReport.put("reportPreOp", jsonReportPreOp);
2024 20 Sep 13 olle 382       jsonReport.put("reportFollowUp06", jsonReportFollow06);
2024 20 Sep 13 olle 383       jsonReport.put("reportFollowUp12", jsonReportFollow12);
2024 20 Sep 13 olle 384       jsonReport.put("reportFollowUp36", jsonReportFollow36);
2024 20 Sep 13 olle 385     }
1730 01 Nov 12 olle 386     if (viewType == null)
1730 01 Nov 12 olle 387     {
1813 30 Jan 13 olle 388       viewType = tableUtil.getViewType(startDate, endDate);
1730 01 Nov 12 olle 389     }
1730 01 Nov 12 olle 390     DateToStringConverter date2StringConverter = Reggie.CONVERTER_DATE_TO_STRING;
1813 30 Jan 13 olle 391     Date periodStartDate = tableUtil.calculatePeriodStartDate(startDate, viewType);
1730 01 Nov 12 olle 392     jsonReport.put("viewType", viewType);
1748 27 Nov 12 olle 393     jsonReport.put("sampleType", sampleType);
2024 20 Sep 13 olle 394     jsonReport.put("tableAlternatives", tableAlternatives);
2024 20 Sep 13 olle 395     jsonReport.put("bloodSampleFilter", bloodSampleFilter);        
1730 01 Nov 12 olle 396     jsonReport.put("beginDate", date2StringConverter.convert(startDate));
1730 01 Nov 12 olle 397     jsonReport.put("endDate", date2StringConverter.convert(endDate));
1730 01 Nov 12 olle 398     jsonReport.put("periodBeginDate", date2StringConverter.convert(periodStartDate));
1730 01 Nov 12 olle 399     // Transfer latest date from jsonStatistics to jsonReport
1730 01 Nov 12 olle 400     jsonReport.put("latestDate", latestDateStr);
1730 01 Nov 12 olle 401     json.put("report", jsonReport);
1730 01 Nov 12 olle 402     return json;
1730 01 Nov 12 olle 403   }
1730 01 Nov 12 olle 404     
2024 20 Sep 13 olle 405   private JSONObject createSampleCountReport(List<Sample> sampleList, Date startDate, Date endDate, String viewType, String sampleType, String tableAlternatives, String bloodSampleFilter)
2024 20 Sep 13 olle 406     throws ServletException, IOException 
2024 20 Sep 13 olle 407   {  
2024 20 Sep 13 olle 408     JSONObject jsonReport = new JSONObject();
2611 29 Aug 14 nicklas 409     jsonReport.put("sites", ReportTableUtil.getJSONSites(Site.SORT_BY_NAME));
2024 20 Sep 13 olle 410     
2024 20 Sep 13 olle 411     String latestDateStr = null;
2024 20 Sep 13 olle 412     JSONObject jsonStatistics = tableUtil.createJSONStatistics(sampleList.iterator(), startDate, endDate, viewType);
2024 20 Sep 13 olle 413     jsonReport.put("statistics", jsonStatistics);
2024 20 Sep 13 olle 414     latestDateStr = (String) jsonStatistics.get("latestDateKey");
2024 20 Sep 13 olle 415     if (viewType == null)
2024 20 Sep 13 olle 416     {
2024 20 Sep 13 olle 417       viewType = tableUtil.getViewType(startDate, endDate);
2024 20 Sep 13 olle 418     }
2024 20 Sep 13 olle 419     DateToStringConverter date2StringConverter = Reggie.CONVERTER_DATE_TO_STRING;
2024 20 Sep 13 olle 420     Date periodStartDate = tableUtil.calculatePeriodStartDate(startDate, viewType);
2024 20 Sep 13 olle 421     jsonReport.put("viewType", viewType);
2024 20 Sep 13 olle 422     jsonReport.put("sampleType", sampleType);
2024 20 Sep 13 olle 423     jsonReport.put("tableAlternatives", tableAlternatives);
2024 20 Sep 13 olle 424     jsonReport.put("bloodSampleFilter", bloodSampleFilter);        
2024 20 Sep 13 olle 425     jsonReport.put("beginDate", date2StringConverter.convert(startDate));
2024 20 Sep 13 olle 426     jsonReport.put("endDate", date2StringConverter.convert(endDate));
2024 20 Sep 13 olle 427     jsonReport.put("periodBeginDate", date2StringConverter.convert(periodStartDate));
2024 20 Sep 13 olle 428     // Transfer latest date from jsonStatistics to jsonReport
2024 20 Sep 13 olle 429     jsonReport.put("latestDate", latestDateStr);
2024 20 Sep 13 olle 430     return jsonReport;
2024 20 Sep 13 olle 431   }
2024 20 Sep 13 olle 432     
2024 20 Sep 13 olle 433   private List<Sample> createSampleCountSampleList(DbControl dc, SnapshotManager manager, List<Sample> rawSampleList, String bloodSampleFilter)
2024 20 Sep 13 olle 434     throws ServletException, IOException 
2024 20 Sep 13 olle 435   {  
2024 20 Sep 13 olle 436     // Store samples that should be processed for report table in list 
2024 20 Sep 13 olle 437     List<Sample> sampleList = new ArrayList<Sample>();
2024 20 Sep 13 olle 438     for (Sample s: rawSampleList)
2024 20 Sep 13 olle 439     {
2203 21 Jan 14 olle 440       // Optional blood sample filter (true if no filter, sample not blood, or blood sample passes filter)
2203 21 Jan 14 olle 441       boolean passedBloodSampleFilter = passBloodSampleFilter(dc, manager, bloodSampleFilter, s);
2203 21 Jan 14 olle 442       if (passedBloodSampleFilter)
2024 20 Sep 13 olle 443       {
2203 21 Jan 14 olle 444         // Add sample to list
2203 21 Jan 14 olle 445         sampleList.add(s);
2203 21 Jan 14 olle 446       }
2024 20 Sep 13 olle 447     }
2024 20 Sep 13 olle 448     return sampleList;
2024 20 Sep 13 olle 449   }
2024 20 Sep 13 olle 450     
3911 02 May 16 nicklas 451   private JSONObject createPatientCountReport(DbControl dc, JSONObject json, Date startDate, Date endDate, String viewType, ProgressReporter progress)
1744 27 Nov 12 olle 452     throws ServletException, IOException 
1744 27 Nov 12 olle 453   {  
1744 27 Nov 12 olle 454     JSONObject jsonReport = new JSONObject();
2611 29 Aug 14 nicklas 455     jsonReport.put("sites", ReportTableUtil.getJSONSites(Site.SORT_BY_NAME));
1744 27 Nov 12 olle 456     
1744 27 Nov 12 olle 457     ItemQuery<Sample> sampleQuery = Sample.getQuery();
1744 27 Nov 12 olle 458     sampleQuery.joinPermanent(Hql.innerJoin(null, "creationEvent", "ce", true));
1744 27 Nov 12 olle 459     // ...only include 'Specimen', 'No Specimen', or 'Blood' items
1744 27 Nov 12 olle 460     sampleQuery.restrict(
1744 27 Nov 12 olle 461       Restrictions.or(
1744 27 Nov 12 olle 462         Subtype.SPECIMEN.restriction(dc, null),
1744 27 Nov 12 olle 463         Subtype.NO_SPECIMEN.restriction(dc, null),
1744 27 Nov 12 olle 464         Subtype.BLOOD.restriction(dc, null)
1744 27 Nov 12 olle 465       ));
1744 27 Nov 12 olle 466     sampleQuery.setIncludes(Reggie.INCLUDE_IN_CURRENT_PROJECT);
1744 27 Nov 12 olle 467     sampleQuery.order(Orders.asc(Hql.property("name")));
1744 27 Nov 12 olle 468     sampleQuery.order(Orders.asc(Hql.property("ce", "eventDate")));
1744 27 Nov 12 olle 469     sampleQuery.setCacheResult(true);
3911 02 May 16 nicklas 470     
3911 02 May 16 nicklas 471     progress.display(1, "Loading samples...");
3911 02 May 16 nicklas 472     long totalCount = sampleQuery.count(dc);
3911 02 May 16 nicklas 473     long count = 0;
3911 02 May 16 nicklas 474     
1744 27 Nov 12 olle 475     ItemResultIterator<Sample> sampleIterator = sampleQuery.iterate(dc);        
1744 27 Nov 12 olle 476                   
1744 27 Nov 12 olle 477     // Store samples that should be processed for report table in list 
1744 27 Nov 12 olle 478     List<Sample> sampleList = new ArrayList<Sample>();
1744 27 Nov 12 olle 479     // Create HashMap to keep track of sample with earliest date for each patient
1744 27 Nov 12 olle 480     HashMap<String, Sample>patientSampleHashMap = new HashMap<String, Sample>();
1744 27 Nov 12 olle 481     // Create HashMap to keep track of sample with unknown date for each patient
1744 27 Nov 12 olle 482     HashMap<String, Sample>patientSampleNoDateHashMap = new HashMap<String, Sample>();
1744 27 Nov 12 olle 483     // Find earliest sample dates for each patient
1744 27 Nov 12 olle 484     ItemSubtype subtypeBlood = Subtype.BLOOD.load(dc);
1744 27 Nov 12 olle 485     while (sampleIterator.hasNext())
1744 27 Nov 12 olle 486     {
3911 02 May 16 nicklas 487       if (count % 100 == 0)
3911 02 May 16 nicklas 488             {
3911 02 May 16 nicklas 489         progress.display(5 + (int)((95 * count) / totalCount), 
3911 02 May 16 nicklas 490             "Processing " + Reggie.formatCount(count) + " of " + Reggie.formatCount(totalCount) + " items");
3911 02 May 16 nicklas 491             }
3911 02 May 16 nicklas 492       count++;
1744 27 Nov 12 olle 493       Sample s = sampleIterator.next();
1744 27 Nov 12 olle 494       BioMaterialEvent creationEvent = s.getCreationEvent();
1744 27 Nov 12 olle 495       Date creationDate = creationEvent.getEventDate();
1744 27 Nov 12 olle 496
1744 27 Nov 12 olle 497       // Find patient id
1744 27 Nov 12 olle 498       BioMaterial patient = null;
1744 27 Nov 12 olle 499       if (s.getItemSubtype().equals(subtypeBlood))
1744 27 Nov 12 olle 500       {
1744 27 Nov 12 olle 501         // The parent is the 'Patient' for blood samples
1744 27 Nov 12 olle 502         patient = s.getParent();
1744 27 Nov 12 olle 503       }
1744 27 Nov 12 olle 504       else
1744 27 Nov 12 olle 505       {
1744 27 Nov 12 olle 506         // The grandparent is the 'Patient' for specimens and no specimens
1744 27 Nov 12 olle 507         MeasuredBioMaterial parent = (MeasuredBioMaterial) s.getParent();
1744 27 Nov 12 olle 508         if (parent != null)
1744 27 Nov 12 olle 509         {
1744 27 Nov 12 olle 510           patient = parent.getParent();
1744 27 Nov 12 olle 511         }
1744 27 Nov 12 olle 512       }
1744 27 Nov 12 olle 513       if (patient != null)
1744 27 Nov 12 olle 514       {
1744 27 Nov 12 olle 515         String patientName = patient.getName();
1744 27 Nov 12 olle 516         if (creationDate != null)
1744 27 Nov 12 olle 517         {
1813 30 Jan 13 olle 518           patientSampleHashMap = tableUtil.updateStringEarliestSampleHashMap(patientSampleHashMap, patientName, s);
1744 27 Nov 12 olle 519         }
1744 27 Nov 12 olle 520         else
1744 27 Nov 12 olle 521         {
1744 27 Nov 12 olle 522           patientSampleNoDateHashMap.put(patientName, s);
1744 27 Nov 12 olle 523         }
1744 27 Nov 12 olle 524       }
1744 27 Nov 12 olle 525     }
1744 27 Nov 12 olle 526     // Create list of samples for report
1744 27 Nov 12 olle 527     // Add sample with earliest date for patient
1744 27 Nov 12 olle 528     for (String key: patientSampleHashMap.keySet())
1744 27 Nov 12 olle 529     {
1744 27 Nov 12 olle 530       if (key != null && !key.equals(""))
1744 27 Nov 12 olle 531       {
1744 27 Nov 12 olle 532         Sample s = patientSampleHashMap.get(key);
1744 27 Nov 12 olle 533         // Include sample in list of samples for report
1744 27 Nov 12 olle 534         sampleList.add(s);
1744 27 Nov 12 olle 535       }
1744 27 Nov 12 olle 536     }
1744 27 Nov 12 olle 537     // Add sample with unknown date if no sample with date exists for patient
1744 27 Nov 12 olle 538     for (String key: patientSampleNoDateHashMap.keySet())
1744 27 Nov 12 olle 539     {
1744 27 Nov 12 olle 540       if (key != null && !key.equals(""))
1744 27 Nov 12 olle 541       {
1744 27 Nov 12 olle 542         // If no sample with date exists for sample, include sample without date
1744 27 Nov 12 olle 543         if (!patientSampleHashMap.containsKey(key))
1744 27 Nov 12 olle 544         {
1744 27 Nov 12 olle 545           Sample s = patientSampleNoDateHashMap.get(key);
1744 27 Nov 12 olle 546           // Include sample in list of samples for report
1744 27 Nov 12 olle 547           sampleList.add(s);
1744 27 Nov 12 olle 548         }
1744 27 Nov 12 olle 549       }
1744 27 Nov 12 olle 550     }
1744 27 Nov 12 olle 551
1813 30 Jan 13 olle 552     JSONObject jsonStatistics = tableUtil.createJSONStatistics(sampleList.iterator(), startDate, endDate, viewType);
1744 27 Nov 12 olle 553     jsonReport.put("statistics", jsonStatistics);        
1744 27 Nov 12 olle 554     if (viewType == null)
1744 27 Nov 12 olle 555     {
1813 30 Jan 13 olle 556       viewType = tableUtil.getViewType(startDate, endDate);
1744 27 Nov 12 olle 557     }
1744 27 Nov 12 olle 558     DateToStringConverter date2StringConverter = Reggie.CONVERTER_DATE_TO_STRING;
1813 30 Jan 13 olle 559     Date periodStartDate = tableUtil.calculatePeriodStartDate(startDate, viewType);
1744 27 Nov 12 olle 560     jsonReport.put("viewType", viewType);
1744 27 Nov 12 olle 561     jsonReport.put("beginDate", date2StringConverter.convert(startDate));
1744 27 Nov 12 olle 562     jsonReport.put("endDate", date2StringConverter.convert(endDate));
1744 27 Nov 12 olle 563     jsonReport.put("periodBeginDate", date2StringConverter.convert(periodStartDate));
1744 27 Nov 12 olle 564     // Transfer latest date from jsonStatistics to jsonReport
1744 27 Nov 12 olle 565     String latestDateStr = (String) jsonStatistics.get("latestDateKey");
1744 27 Nov 12 olle 566     jsonReport.put("latestDate", latestDateStr);
1744 27 Nov 12 olle 567     json.put("report", jsonReport);
3911 02 May 16 nicklas 568     progress.display(100, "Done");
1744 27 Nov 12 olle 569     return json;
1744 27 Nov 12 olle 570   }
1744 27 Nov 12 olle 571     
3911 02 May 16 nicklas 572   private JSONObject createConsentCountReport(DbControl dc, JSONObject json, Date startDate, Date endDate, ProgressReporter progress)
1732 13 Nov 12 olle 573       throws ServletException, IOException 
1732 13 Nov 12 olle 574   {  
1732 13 Nov 12 olle 575     JSONObject jsonReport = new JSONObject();
2611 29 Aug 14 nicklas 576     jsonReport.put("sites", ReportTableUtil.getJSONSites(Site.SORT_BY_NAME));
1732 13 Nov 12 olle 577     
1732 13 Nov 12 olle 578     ItemQuery<Sample> sampleQuery = Sample.getQuery();
1732 13 Nov 12 olle 579     sampleQuery.joinPermanent(Hql.innerJoin(null, "creationEvent", "ce", true));
1732 13 Nov 12 olle 580     // ...only include 'Case' or 'Blood' items
1732 13 Nov 12 olle 581     sampleQuery.restrict(
1732 13 Nov 12 olle 582       Restrictions.or(
1732 13 Nov 12 olle 583         Subtype.CASE.restriction(dc, null),
1732 13 Nov 12 olle 584         Subtype.BLOOD.restriction(dc, null)
1732 13 Nov 12 olle 585       ));
1732 13 Nov 12 olle 586     sampleQuery.setIncludes(Reggie.INCLUDE_IN_CURRENT_PROJECT);
1732 13 Nov 12 olle 587     sampleQuery.order(Orders.asc(Hql.property("name")));
1732 13 Nov 12 olle 588     sampleQuery.order(Orders.asc(Hql.property("ce", "eventDate")));
1732 13 Nov 12 olle 589     sampleQuery.setCacheResult(true);
1732 13 Nov 12 olle 590     //List<Sample> samples = sampleQuery.list(dc);
3911 02 May 16 nicklas 591     
3911 02 May 16 nicklas 592     progress.display(1, "Loading samples...");
3911 02 May 16 nicklas 593     long totalCount = sampleQuery.count(dc);
3911 02 May 16 nicklas 594     long count = 0;
3911 02 May 16 nicklas 595     
1732 13 Nov 12 olle 596     ItemResultIterator<Sample> sampleIterator = sampleQuery.iterate(dc);        
1732 13 Nov 12 olle 597     // Use stored annotation snapshots for performance reasons
1732 13 Nov 12 olle 598     SnapshotManager manager = new SnapshotManager();        
1732 13 Nov 12 olle 599                     
1732 13 Nov 12 olle 600     JSONObject jsonStatistics = new JSONObject();
1732 13 Nov 12 olle 601     JSONObject jsonSitesCombined = new JSONObject();
1732 13 Nov 12 olle 602     String yesKey = "yes";
1926 12 Apr 13 olle 603     String yesPatientExistsKey = "yesPatientExists";
1926 12 Apr 13 olle 604     String yesPatientUnknownKey = "yesPatientUnkown";
1926 12 Apr 13 olle 605     String yesPatientExistsDateExistsKey = "yesPatientExistsDateExists";
1926 12 Apr 13 olle 606     String yesPatientExistsDateUnknownKey = "yesPatientExistsDateUnknown";
1926 12 Apr 13 olle 607     String yesPatientUnknownDateExistsKey = "yesPatientUnknownDateExists";
1926 12 Apr 13 olle 608     String yesPatientUnknownDateUnknownKey = "yesPatientUnknownDateUnknown";
1926 12 Apr 13 olle 609     String yesDateUnknownKey = "yesDateUnknown";
1732 13 Nov 12 olle 610     String noKey = "no";
1926 12 Apr 13 olle 611     String noDateExistsKey = "noDateExists";
1926 12 Apr 13 olle 612     String noDateUnknownKey = "noDateUnknown";
1732 13 Nov 12 olle 613     String notAskedKey = "notAsked";
1926 12 Apr 13 olle 614     String notAskedDateExistsKey = "notAskedDateExists";
1926 12 Apr 13 olle 615     String notAskedDateUnknownKey = "notAskedDateUnknown";
1732 13 Nov 12 olle 616     String otherKey = "other";
1732 13 Nov 12 olle 617     String missingKey = "missing";
1732 13 Nov 12 olle 618     String sitesCombinedKey = "sitesCombinedKey";
1737 16 Nov 12 olle 619     String latestDateKey = "latestDateKey";
1732 13 Nov 12 olle 620     String noDateKey = "noDate";
1784 13 Dec 12 olle 621     String noDateOrMissingKey = "noDateOrMissing";
1926 12 Apr 13 olle 622     String sumKey = "sumKey";
1926 12 Apr 13 olle 623     String sumDateIgnoredKey = "sumDateIgnoredKey";
1926 12 Apr 13 olle 624     String totalKey = "totalKey";
1732 13 Nov 12 olle 625     String duplicateKey = "duplicateKey";
2223 12 Feb 14 olle 626     String permissionDeniedForPatientNameKey = "permissionDeniedForPatientName";
1732 13 Nov 12 olle 627     jsonStatistics.put(sitesCombinedKey, jsonSitesCombined);
1732 13 Nov 12 olle 628     // Initialize site data to 0 for the different keys
1813 30 Jan 13 olle 629     jsonStatistics = tableUtil.initializeJSONSiteData(jsonStatistics, yesKey);
1926 12 Apr 13 olle 630     jsonStatistics = tableUtil.initializeJSONSiteData(jsonStatistics, yesPatientExistsKey);
1926 12 Apr 13 olle 631     jsonStatistics = tableUtil.initializeJSONSiteData(jsonStatistics, yesPatientUnknownKey);
1926 12 Apr 13 olle 632     jsonStatistics = tableUtil.initializeJSONSiteData(jsonStatistics, yesPatientExistsDateExistsKey);
1926 12 Apr 13 olle 633     jsonStatistics = tableUtil.initializeJSONSiteData(jsonStatistics, yesPatientExistsDateUnknownKey);
1926 12 Apr 13 olle 634     jsonStatistics = tableUtil.initializeJSONSiteData(jsonStatistics, yesPatientUnknownDateExistsKey);
1926 12 Apr 13 olle 635     jsonStatistics = tableUtil.initializeJSONSiteData(jsonStatistics, yesPatientUnknownDateUnknownKey);
1926 12 Apr 13 olle 636     jsonStatistics = tableUtil.initializeJSONSiteData(jsonStatistics, yesDateUnknownKey);
1813 30 Jan 13 olle 637     jsonStatistics = tableUtil.initializeJSONSiteData(jsonStatistics, noKey);
1926 12 Apr 13 olle 638     jsonStatistics = tableUtil.initializeJSONSiteData(jsonStatistics, noDateExistsKey);
1926 12 Apr 13 olle 639     jsonStatistics = tableUtil.initializeJSONSiteData(jsonStatistics, noDateUnknownKey);
1813 30 Jan 13 olle 640     jsonStatistics = tableUtil.initializeJSONSiteData(jsonStatistics, notAskedKey);
1926 12 Apr 13 olle 641     jsonStatistics = tableUtil.initializeJSONSiteData(jsonStatistics, notAskedDateExistsKey);
1926 12 Apr 13 olle 642     jsonStatistics = tableUtil.initializeJSONSiteData(jsonStatistics, notAskedDateUnknownKey);
1813 30 Jan 13 olle 643     jsonStatistics = tableUtil.initializeJSONSiteData(jsonStatistics, otherKey);
1813 30 Jan 13 olle 644     jsonStatistics = tableUtil.initializeJSONSiteData(jsonStatistics, missingKey);
1813 30 Jan 13 olle 645     jsonStatistics = tableUtil.initializeJSONSiteData(jsonStatistics, noDateKey);
1813 30 Jan 13 olle 646     jsonStatistics = tableUtil.initializeJSONSiteData(jsonStatistics, noDateOrMissingKey);
1813 30 Jan 13 olle 647     jsonStatistics = tableUtil.initializeJSONSiteData(jsonStatistics, sumKey);
1926 12 Apr 13 olle 648     jsonStatistics = tableUtil.initializeJSONSiteData(jsonStatistics, sumDateIgnoredKey);
1813 30 Jan 13 olle 649     jsonStatistics = tableUtil.initializeJSONSiteData(jsonStatistics, totalKey);
1732 13 Nov 12 olle 650     // Initialize combined data to 0 for the different keys
1732 13 Nov 12 olle 651     jsonSitesCombined.put(yesKey, 0);
1926 12 Apr 13 olle 652     jsonSitesCombined.put(yesPatientExistsKey, 0);
1926 12 Apr 13 olle 653     jsonSitesCombined.put(yesPatientUnknownKey, 0);
1926 12 Apr 13 olle 654     jsonSitesCombined.put(yesPatientExistsDateExistsKey, 0);
1926 12 Apr 13 olle 655     jsonSitesCombined.put(yesPatientExistsDateUnknownKey, 0);
1926 12 Apr 13 olle 656     jsonSitesCombined.put(yesPatientUnknownDateExistsKey, 0);
1926 12 Apr 13 olle 657     jsonSitesCombined.put(yesPatientUnknownDateUnknownKey, 0);
1926 12 Apr 13 olle 658     jsonSitesCombined.put(yesDateUnknownKey, 0);
1732 13 Nov 12 olle 659     jsonSitesCombined.put(noKey, 0);
1926 12 Apr 13 olle 660     jsonSitesCombined.put(noDateExistsKey, 0);
1926 12 Apr 13 olle 661     jsonSitesCombined.put(noDateUnknownKey, 0);
1732 13 Nov 12 olle 662     jsonSitesCombined.put(notAskedKey, 0);
1926 12 Apr 13 olle 663     jsonSitesCombined.put(notAskedDateExistsKey, 0);
1926 12 Apr 13 olle 664     jsonSitesCombined.put(notAskedDateUnknownKey, 0);
1732 13 Nov 12 olle 665     jsonSitesCombined.put(otherKey, 0);
1732 13 Nov 12 olle 666     jsonSitesCombined.put(missingKey, 0);
1738 20 Nov 12 olle 667     jsonSitesCombined.put(noDateKey, 0);
1784 13 Dec 12 olle 668     jsonSitesCombined.put(noDateOrMissingKey, 0);
1732 13 Nov 12 olle 669     jsonSitesCombined.put(sumKey, 0);
1926 12 Apr 13 olle 670     jsonSitesCombined.put(sumDateIgnoredKey, 0);
1732 13 Nov 12 olle 671     jsonSitesCombined.put(totalKey, 0);
1732 13 Nov 12 olle 672     // Initialize other data to 0 for different keys
1732 13 Nov 12 olle 673     jsonStatistics.put(noDateKey, 0);
1784 13 Dec 12 olle 674     jsonStatistics.put(missingKey, 0);
1732 13 Nov 12 olle 675     jsonStatistics.put(duplicateKey, 0);
1738 20 Nov 12 olle 676     Date latestDate = null;
2223 12 Feb 14 olle 677     boolean permissionDeniedForPatientName = false;
2223 12 Feb 14 olle 678     try
2223 12 Feb 14 olle 679     {
2223 12 Feb 14 olle 680       Annotationtype.ALL_FIRST_NAMES.load(dc);
2223 12 Feb 14 olle 681       Annotationtype.FAMILY_NAME.load(dc);
2223 12 Feb 14 olle 682     }
2223 12 Feb 14 olle 683     catch (RuntimeException ex)
2223 12 Feb 14 olle 684     {
2223 12 Feb 14 olle 685       permissionDeniedForPatientName = true;
2223 12 Feb 14 olle 686     }
1732 13 Nov 12 olle 687     // Create HashMap to keep track of processed dates for each patient
1732 13 Nov 12 olle 688     HashMap<String, Set<Date>>patientDateSetHashMap = new HashMap<String, Set<Date>>();
1732 13 Nov 12 olle 689     JSONObject jsonPatientnamePatientid = new JSONObject();
1737 16 Nov 12 olle 690     // Create HashMap to keep track of latest consent date for each site
1737 16 Nov 12 olle 691     HashMap<String, Date> sitePrefixDateHashMap = new HashMap<String, Date>();
1732 13 Nov 12 olle 692     while (sampleIterator.hasNext())
1732 13 Nov 12 olle 693     {
3911 02 May 16 nicklas 694       if (count % 100 == 0)
3911 02 May 16 nicklas 695             {
3911 02 May 16 nicklas 696         progress.display(5 + (int)((95 * count) / totalCount), 
3911 02 May 16 nicklas 697             "Processing " + Reggie.formatCount(count) + " of " + Reggie.formatCount(totalCount) + " items");
3911 02 May 16 nicklas 698             }
3911 02 May 16 nicklas 699       count++;
1732 13 Nov 12 olle 700       Sample s = sampleIterator.next();
1749 28 Nov 12 olle 701 /*
1732 13 Nov 12 olle 702       BioMaterialEvent creationEvent = s.getCreationEvent();
1732 13 Nov 12 olle 703       Date creationDate = creationEvent.getEventDate();
1749 28 Nov 12 olle 704 */
1732 13 Nov 12 olle 705               
1732 13 Nov 12 olle 706       Site site = Site.findByCaseName(s.getName());  
1732 13 Nov 12 olle 707       if (site == Site.UNKNOWN)
1732 13 Nov 12 olle 708       {
1732 13 Nov 12 olle 709         String key = "unknownSite";
1813 30 Jan 13 olle 710         jsonStatistics = tableUtil.updateJSONObjectCounter(jsonStatistics, key);
1732 13 Nov 12 olle 711       }
1732 13 Nov 12 olle 712       else
1732 13 Nov 12 olle 713       {
1732 13 Nov 12 olle 714         // Get site info            
1732 13 Nov 12 olle 715         JSONObject jsonSite = (JSONObject)jsonStatistics.get(site.getPrefix());
1732 13 Nov 12 olle 716         if (jsonSite == null)
1732 13 Nov 12 olle 717         {
1732 13 Nov 12 olle 718           jsonSite = new JSONObject();
1732 13 Nov 12 olle 719           jsonStatistics.put(site.getPrefix(), jsonSite);
1732 13 Nov 12 olle 720         }
1732 13 Nov 12 olle 721
1732 13 Nov 12 olle 722         String consent = (String) Annotationtype.CONSENT.getAnnotationValue(dc, manager, s);
1732 13 Nov 12 olle 723         // Get consent date
1732 13 Nov 12 olle 724         Date consentDate = (Date) Annotationtype.CONSENT_DATE.getAnnotationValue(dc, manager, s);
4177 25 Oct 16 nicklas 725         // Use consent date for filtering if it exists, otherwise the registration date
4177 25 Oct 16 nicklas 726         Date registrationDate = s.getEntryDate();
4177 25 Oct 16 nicklas 727         Date filterDate = consentDate != null ? consentDate : registrationDate;
4177 25 Oct 16 nicklas 728         boolean isIncluded = !(filterDate.before(startDate) || filterDate.after(endDate));
4177 25 Oct 16 nicklas 729         
1926 12 Apr 13 olle 730         // Several key may be of interest, so a list is used to collect them
1926 12 Apr 13 olle 731         List<String> keyList = new ArrayList<String>();
1732 13 Nov 12 olle 732         Boolean duplicate = false;
1732 13 Nov 12 olle 733         if (consent != null)
1732 13 Nov 12 olle 734         {
1738 20 Nov 12 olle 735           if (consentDate != null)
1732 13 Nov 12 olle 736           {
1732 13 Nov 12 olle 737             // Check if a consent already exists for the same patient and date
1738 20 Nov 12 olle 738             // The parent is the 'Patient'
1738 20 Nov 12 olle 739             BioMaterial patient = s.getParent();
1738 20 Nov 12 olle 740             if (patient != null)
1732 13 Nov 12 olle 741             {
1738 20 Nov 12 olle 742               // Store patient ID value
1813 30 Jan 13 olle 743               jsonPatientnamePatientid = tableUtil.updateJSONItemnameItemid(jsonPatientnamePatientid, patient);
1738 20 Nov 12 olle 744               String patientName = patient.getName();
1813 30 Jan 13 olle 745               if (tableUtil.inStringDateSetHashMap(patientDateSetHashMap, patientName, consentDate))
1732 13 Nov 12 olle 746               {
1738 20 Nov 12 olle 747                 duplicate = true;
1738 20 Nov 12 olle 748               }
1738 20 Nov 12 olle 749               else
1738 20 Nov 12 olle 750               {
1813 30 Jan 13 olle 751                 patientDateSetHashMap = tableUtil.updateStringDateSetHashMap(patientDateSetHashMap, patientName, consentDate);
1738 20 Nov 12 olle 752               }
1738 20 Nov 12 olle 753             }
4177 25 Oct 16 nicklas 754             if (isIncluded)
1738 20 Nov 12 olle 755             {
1738 20 Nov 12 olle 756               if (consent.equals("Yes"))
1738 20 Nov 12 olle 757               {
1926 12 Apr 13 olle 758                 keyList.add(yesKey);
1738 20 Nov 12 olle 759                 if (patient != null)
1732 13 Nov 12 olle 760                 {
1926 12 Apr 13 olle 761                   keyList.add(yesPatientExistsKey);
1926 12 Apr 13 olle 762                   keyList.add(yesPatientExistsDateExistsKey);
1732 13 Nov 12 olle 763                 }
1732 13 Nov 12 olle 764                 else
1732 13 Nov 12 olle 765                 {
1926 12 Apr 13 olle 766                   keyList.add(yesPatientUnknownKey);
1926 12 Apr 13 olle 767                   keyList.add(yesPatientUnknownDateExistsKey);
1732 13 Nov 12 olle 768                 }
1732 13 Nov 12 olle 769               }
1738 20 Nov 12 olle 770               else if (consent.equals("No"))
1738 20 Nov 12 olle 771               {
1926 12 Apr 13 olle 772                 keyList.add(noKey);
1926 12 Apr 13 olle 773                 keyList.add(noDateExistsKey);
1738 20 Nov 12 olle 774               }
1738 20 Nov 12 olle 775               else if (consent.equals("Not asked"))
1738 20 Nov 12 olle 776               {
1926 12 Apr 13 olle 777                 keyList.add(notAskedKey);
1926 12 Apr 13 olle 778                 keyList.add(notAskedDateExistsKey);
1738 20 Nov 12 olle 779               }
1732 13 Nov 12 olle 780               else
1732 13 Nov 12 olle 781               {
1926 12 Apr 13 olle 782                 keyList.add(otherKey);
1732 13 Nov 12 olle 783               }
1738 20 Nov 12 olle 784               // Find out if current consent date is the latest so far.
1738 20 Nov 12 olle 785               if (latestDate == null || latestDate.before(consentDate)) 
1738 20 Nov 12 olle 786               {
1738 20 Nov 12 olle 787                 latestDate = consentDate;
1738 20 Nov 12 olle 788               }
1732 13 Nov 12 olle 789             }
1738 20 Nov 12 olle 790           }
4177 25 Oct 16 nicklas 791           else if (isIncluded)
1738 20 Nov 12 olle 792           {
4177 25 Oct 16 nicklas 793             // No ConsentDate; included due to registration date!
1926 12 Apr 13 olle 794             // The parent is the 'Patient'
1926 12 Apr 13 olle 795             BioMaterial patient = s.getParent();
1738 20 Nov 12 olle 796             if (consent.equals("Yes"))
1732 13 Nov 12 olle 797             {
1926 12 Apr 13 olle 798               keyList.add(yesKey);
1926 12 Apr 13 olle 799               keyList.add(yesDateUnknownKey);
1926 12 Apr 13 olle 800               if (patient != null)
1926 12 Apr 13 olle 801               {
1926 12 Apr 13 olle 802                 keyList.add(yesPatientExistsKey);
1926 12 Apr 13 olle 803                 keyList.add(yesPatientExistsDateUnknownKey);
1926 12 Apr 13 olle 804               }
1926 12 Apr 13 olle 805               else
1926 12 Apr 13 olle 806               {
1926 12 Apr 13 olle 807                 keyList.add(yesPatientUnknownKey);
1926 12 Apr 13 olle 808                 keyList.add(yesPatientUnknownDateUnknownKey);
1926 12 Apr 13 olle 809               }
1732 13 Nov 12 olle 810             }
1738 20 Nov 12 olle 811             else if (consent.equals("No"))
1732 13 Nov 12 olle 812             {
1926 12 Apr 13 olle 813               keyList.add(noKey);
1926 12 Apr 13 olle 814               keyList.add(noDateUnknownKey);
1732 13 Nov 12 olle 815             }
1738 20 Nov 12 olle 816             else if (consent.equals("Not asked"))
1732 13 Nov 12 olle 817             {
1926 12 Apr 13 olle 818               keyList.add(notAskedKey);
1926 12 Apr 13 olle 819               keyList.add(notAskedDateUnknownKey);
1732 13 Nov 12 olle 820             }
1738 20 Nov 12 olle 821             else
1738 20 Nov 12 olle 822             {
1926 12 Apr 13 olle 823               keyList.add(otherKey);
1738 20 Nov 12 olle 824             }
1732 13 Nov 12 olle 825           }
1737 16 Nov 12 olle 826           // Keep track of latest consent date for site
1737 16 Nov 12 olle 827           if (consentDate != null)
1737 16 Nov 12 olle 828           {
1737 16 Nov 12 olle 829             Date siteLatestDate = sitePrefixDateHashMap.get(site.getPrefix());
1737 16 Nov 12 olle 830             if (siteLatestDate == null || siteLatestDate.before(consentDate))
1737 16 Nov 12 olle 831             {
1737 16 Nov 12 olle 832               sitePrefixDateHashMap.put(site.getPrefix(), consentDate);
1737 16 Nov 12 olle 833             }
1737 16 Nov 12 olle 834           }
1732 13 Nov 12 olle 835         }
4177 25 Oct 16 nicklas 836         else if (isIncluded)
1738 20 Nov 12 olle 837         {
4177 25 Oct 16 nicklas 838           // No consent; included if due to ConsentDate or registration date
1926 12 Apr 13 olle 839           keyList.add(missingKey);
1738 20 Nov 12 olle 840         }
1732 13 Nov 12 olle 841         // Note that items with duplicate == false include missing consent items
1732 13 Nov 12 olle 842         if (!duplicate)
1732 13 Nov 12 olle 843         {
1926 12 Apr 13 olle 844           if (keyList != null && keyList.size() > 0)
1738 20 Nov 12 olle 845           {
1926 12 Apr 13 olle 846             for (String key: keyList)
1926 12 Apr 13 olle 847             {
1926 12 Apr 13 olle 848               // Update consent counters
1926 12 Apr 13 olle 849               jsonSite = tableUtil.updateJSONObjectCounter(jsonSite, key);
1926 12 Apr 13 olle 850               jsonSitesCombined = tableUtil.updateJSONObjectCounter(jsonSitesCombined, key);
1926 12 Apr 13 olle 851             }
1784 13 Dec 12 olle 852             // Include all consents except missing in summed count
1784 13 Dec 12 olle 853             if (consent != null)
1784 13 Dec 12 olle 854             {
1813 30 Jan 13 olle 855               jsonSite = tableUtil.updateJSONObjectCounter(jsonSite, sumKey);
1813 30 Jan 13 olle 856               jsonSitesCombined = tableUtil.updateJSONObjectCounter(jsonSitesCombined, sumKey);
1926 12 Apr 13 olle 857               jsonSite = tableUtil.updateJSONObjectCounter(jsonSite, sumDateIgnoredKey);
1926 12 Apr 13 olle 858               jsonSitesCombined = tableUtil.updateJSONObjectCounter(jsonSitesCombined, sumDateIgnoredKey);
1784 13 Dec 12 olle 859             }
1738 20 Nov 12 olle 860           }
4177 25 Oct 16 nicklas 861           if (consentDate == null && isIncluded)
1732 13 Nov 12 olle 862           {
1784 13 Dec 12 olle 863             // Include consents with unknown date for site, but not missing consents
1784 13 Dec 12 olle 864             if (consent != null)
1784 13 Dec 12 olle 865             {
1813 30 Jan 13 olle 866               jsonSite = tableUtil.updateJSONObjectCounter(jsonSite, noDateKey);
1813 30 Jan 13 olle 867               jsonSitesCombined = tableUtil.updateJSONObjectCounter(jsonSitesCombined, noDateKey);
1813 30 Jan 13 olle 868               jsonStatistics = tableUtil.updateJSONObjectCounter(jsonStatistics, noDateKey);
1784 13 Dec 12 olle 869             }
1784 13 Dec 12 olle 870             else
1784 13 Dec 12 olle 871             {
1813 30 Jan 13 olle 872               jsonStatistics = tableUtil.updateJSONObjectCounter(jsonStatistics, missingKey);
1784 13 Dec 12 olle 873             }
1813 30 Jan 13 olle 874             jsonSite = tableUtil.updateJSONObjectCounter(jsonSite, noDateOrMissingKey);
1813 30 Jan 13 olle 875             jsonSitesCombined = tableUtil.updateJSONObjectCounter(jsonSitesCombined, noDateOrMissingKey);
1813 30 Jan 13 olle 876             jsonStatistics = tableUtil.updateJSONObjectCounter(jsonStatistics, noDateOrMissingKey);
1732 13 Nov 12 olle 877           }
1784 13 Dec 12 olle 878           // Include all consents except missing in total count
1784 13 Dec 12 olle 879           if (consent != null)
1784 13 Dec 12 olle 880           {
1784 13 Dec 12 olle 881             // Increase the counter for total consents for the site, regardless of creation date
1813 30 Jan 13 olle 882             jsonSite = tableUtil.updateJSONObjectCounter(jsonSite, totalKey);
1738 20 Nov 12 olle 883
1784 13 Dec 12 olle 884             // Increase the counter for total consents for all sites
1813 30 Jan 13 olle 885             jsonSitesCombined = tableUtil.updateJSONObjectCounter(jsonSitesCombined, totalKey);
1784 13 Dec 12 olle 886           }
1732 13 Nov 12 olle 887         }
1732 13 Nov 12 olle 888         else
1732 13 Nov 12 olle 889         {
1813 30 Jan 13 olle 890           jsonStatistics = tableUtil.updateJSONObjectCounter(jsonStatistics, duplicateKey);
1732 13 Nov 12 olle 891         }
1732 13 Nov 12 olle 892       }
1732 13 Nov 12 olle 893     }
1737 16 Nov 12 olle 894     // Add latest consent date for site to site JSON object
1737 16 Nov 12 olle 895     DateToStringConverter date2StringConverter = Reggie.CONVERTER_DATE_TO_STRING;
1737 16 Nov 12 olle 896     for (Site s: Site.getAllSites())
1737 16 Nov 12 olle 897     {
1737 16 Nov 12 olle 898         JSONObject jsonSite = (JSONObject) jsonStatistics.get(s.getPrefix());
1737 16 Nov 12 olle 899         if (jsonSite == null)
1737 16 Nov 12 olle 900         {
1737 16 Nov 12 olle 901           jsonSite = new JSONObject();
1737 16 Nov 12 olle 902           jsonStatistics.put(s.getPrefix(), jsonSite);
1737 16 Nov 12 olle 903         }
1737 16 Nov 12 olle 904         Date siteLatestDate = sitePrefixDateHashMap.get(s.getPrefix());
1737 16 Nov 12 olle 905         jsonSite.put(latestDateKey, date2StringConverter.convert(siteLatestDate));
1737 16 Nov 12 olle 906     }
1738 20 Nov 12 olle 907     // Add latest date for further transfer to other JSON object
1738 20 Nov 12 olle 908     jsonStatistics.put(latestDateKey, date2StringConverter.convert(latestDate));
1732 13 Nov 12 olle 909     // Create JSONObject (patient, dates) for patients with multiple dates
1732 13 Nov 12 olle 910     JSONObject jsonPatientsWithMultipleDates = createJSONPatientsWithMultipleDates(patientDateSetHashMap);
1732 13 Nov 12 olle 911     String patientsWithMultipleDatesKey = "patientsWithMultipleDatesKey";
1732 13 Nov 12 olle 912     jsonStatistics.put(patientsWithMultipleDatesKey, jsonPatientsWithMultipleDates);
1732 13 Nov 12 olle 913     String patientnamePatientidKey = "patientnamePatientidKey";
1732 13 Nov 12 olle 914     jsonStatistics.put(patientnamePatientidKey, jsonPatientnamePatientid);
1732 13 Nov 12 olle 915     String sessionIdKey = "sessionIdKey";
1732 13 Nov 12 olle 916     jsonStatistics.put(sessionIdKey, dc.getSessionControl().getId());
1732 13 Nov 12 olle 917     //
1732 13 Nov 12 olle 918     jsonReport.put("statistics", jsonStatistics);        
1738 20 Nov 12 olle 919     jsonReport.put("beginDate", date2StringConverter.convert(startDate));
1738 20 Nov 12 olle 920     jsonReport.put("endDate", date2StringConverter.convert(endDate));
1738 20 Nov 12 olle 921     // Transfer latest date from jsonStatistics to jsonReport
1738 20 Nov 12 olle 922     String latestDateStr = (String) jsonStatistics.get("latestDateKey");
1738 20 Nov 12 olle 923     jsonReport.put("latestDate", latestDateStr);
2223 12 Feb 14 olle 924     jsonReport.put(permissionDeniedForPatientNameKey, permissionDeniedForPatientName);
1732 13 Nov 12 olle 925     json.put("report", jsonReport);
3911 02 May 16 nicklas 926     progress.display(100, "Done");
1732 13 Nov 12 olle 927     return json;
1732 13 Nov 12 olle 928   }
1732 13 Nov 12 olle 929
3911 02 May 16 nicklas 930   private JSONObject createOverviewReport(DbControl dc, JSONObject json, Date startDate, Date endDate, String bloodSampleFilter, ProgressReporter progress)
1765 03 Dec 12 olle 931       throws ServletException, IOException 
1765 03 Dec 12 olle 932   {  
1765 03 Dec 12 olle 933     JSONObject jsonReport = new JSONObject();
2611 29 Aug 14 nicklas 934     jsonReport.put("sites", ReportTableUtil.getJSONSites(Site.SORT_BY_NAME));
1765 03 Dec 12 olle 935     
1765 03 Dec 12 olle 936     ItemQuery<Sample> sampleQuery = Sample.getQuery();
1765 03 Dec 12 olle 937     sampleQuery.joinPermanent(Hql.innerJoin(null, "creationEvent", "ce", true));
1765 03 Dec 12 olle 938     // ...only include 'Specimen', 'No Specimen', 'Blood', or 'Case' items
1765 03 Dec 12 olle 939     sampleQuery.restrict(
1765 03 Dec 12 olle 940       Restrictions.or(
1765 03 Dec 12 olle 941         Subtype.SPECIMEN.restriction(dc, null),
1765 03 Dec 12 olle 942         Subtype.NO_SPECIMEN.restriction(dc, null),
1765 03 Dec 12 olle 943         Subtype.BLOOD.restriction(dc, null),
1765 03 Dec 12 olle 944         Subtype.CASE.restriction(dc, null)
1765 03 Dec 12 olle 945       ));
1765 03 Dec 12 olle 946     sampleQuery.setIncludes(Reggie.INCLUDE_IN_CURRENT_PROJECT);
1765 03 Dec 12 olle 947     sampleQuery.order(Orders.asc(Hql.property("name")));
1765 03 Dec 12 olle 948     sampleQuery.order(Orders.asc(Hql.property("ce", "eventDate")));
1765 03 Dec 12 olle 949     sampleQuery.setCacheResult(true);
3911 02 May 16 nicklas 950     
3911 02 May 16 nicklas 951     progress.display(1, "Loading samples...");
3911 02 May 16 nicklas 952     long totalCount = sampleQuery.count(dc);
3911 02 May 16 nicklas 953     long count = 0;
3911 02 May 16 nicklas 954     
1765 03 Dec 12 olle 955     ItemResultIterator<Sample> sampleIterator = sampleQuery.iterate(dc);        
1765 03 Dec 12 olle 956     // Use stored annotation snapshots for performance reasons
1765 03 Dec 12 olle 957     SnapshotManager manager = new SnapshotManager();        
1765 03 Dec 12 olle 958
1765 03 Dec 12 olle 959     JSONObject jsonStatistics = new JSONObject();
1765 03 Dec 12 olle 960     JSONObject jsonSitesCombined = new JSONObject();
1765 03 Dec 12 olle 961     String patientKey = "patient";
1765 03 Dec 12 olle 962     String bloodSampleKey = "bloodSample";
2024 20 Sep 13 olle 963     String bloodSampleFollowUpKey = "bloodSampleFollowUp";
1765 03 Dec 12 olle 964     String specimenKey = "specimen";
1765 03 Dec 12 olle 965     String noSpecimenKey = "noSpecimen";
2178 12 Dec 13 olle 966     String patientWithSpecimenKey = "patientWithSpecimen";
2178 12 Dec 13 olle 967     String patientWithNoSpecimenKey = "patientWithNoSpecimen";
2178 12 Dec 13 olle 968     String patientWithBloodSampleKey = "patientWithBloodSample";
2178 12 Dec 13 olle 969     String patientWithFollowUpBloodSampleKey = "patientWithFollowUpBloodSample";
1765 03 Dec 12 olle 970     String patientBloodSampleOnlyKey = "patientBloodSampleOnly";
1765 03 Dec 12 olle 971     String patientSpecimenOnlyKey = "patientSpecimenOnly";
1765 03 Dec 12 olle 972     String patientNoSpecimenOnlyKey = "patientNoSpecimenOnly";
1765 03 Dec 12 olle 973     String patientBloodSampleAndSpecimenKey = "patientBloodSampleAndSpecimen";
1765 03 Dec 12 olle 974     String patientBloodSampleAndNoSpecimenKey = "patientBloodSampleAndNoSpecimen";
1765 03 Dec 12 olle 975     String patientSpecimenAndNoSpecimenKey = "patientSpecimenAndNoSpecimen";
1765 03 Dec 12 olle 976     String patientBloodSampleAndSpecimenAndNoSpecimenKey = "patientBloodSampleAndSpecimenAndNoSpecimen";
1765 03 Dec 12 olle 977     String patientNoSamplesKey = "patientNoSamples";
1765 03 Dec 12 olle 978     String consentYesKey = "consentYes";
1765 03 Dec 12 olle 979     String consentMissingKey = "consentMissing";
1765 03 Dec 12 olle 980     String sitesCombinedKey = "sitesCombinedKey";
1765 03 Dec 12 olle 981     String sumKey = "sumKey";
1765 03 Dec 12 olle 982     String latestDateKey = "latestDateKey";
1765 03 Dec 12 olle 983     String duplicateKey = "duplicateKey";
2183 20 Dec 13 olle 984     String bloodSampleNoDateKey = "bloodSampleNoDate";
2183 20 Dec 13 olle 985     String bloodSampleFollowUpNoDateKey = "bloodSampleFollowUpNoDate";
2183 20 Dec 13 olle 986     String specimenNoDateKey = "specimenNoDate";
2183 20 Dec 13 olle 987     String noSpecimenNoDateKey = "noSpecimenNoDate";
1765 03 Dec 12 olle 988     jsonStatistics.put(sitesCombinedKey, jsonSitesCombined);
1765 03 Dec 12 olle 989     // Initialize site data to 0 for the different keys
1813 30 Jan 13 olle 990     jsonStatistics = tableUtil.initializeJSONSiteData(jsonStatistics, patientKey);
1813 30 Jan 13 olle 991     jsonStatistics = tableUtil.initializeJSONSiteData(jsonStatistics, bloodSampleKey);
2024 20 Sep 13 olle 992     jsonStatistics = tableUtil.initializeJSONSiteData(jsonStatistics, bloodSampleFollowUpKey);
1813 30 Jan 13 olle 993     jsonStatistics = tableUtil.initializeJSONSiteData(jsonStatistics, specimenKey);
1813 30 Jan 13 olle 994     jsonStatistics = tableUtil.initializeJSONSiteData(jsonStatistics, noSpecimenKey);
2178 12 Dec 13 olle 995     jsonStatistics = tableUtil.initializeJSONSiteData(jsonStatistics, patientWithSpecimenKey);
2178 12 Dec 13 olle 996     jsonStatistics = tableUtil.initializeJSONSiteData(jsonStatistics, patientWithNoSpecimenKey);
2178 12 Dec 13 olle 997     jsonStatistics = tableUtil.initializeJSONSiteData(jsonStatistics, patientWithBloodSampleKey);
2178 12 Dec 13 olle 998     jsonStatistics = tableUtil.initializeJSONSiteData(jsonStatistics, patientWithFollowUpBloodSampleKey);
1813 30 Jan 13 olle 999     jsonStatistics = tableUtil.initializeJSONSiteData(jsonStatistics, patientBloodSampleOnlyKey);
1813 30 Jan 13 olle 1000     jsonStatistics = tableUtil.initializeJSONSiteData(jsonStatistics, patientSpecimenOnlyKey);
1813 30 Jan 13 olle 1001     jsonStatistics = tableUtil.initializeJSONSiteData(jsonStatistics, patientNoSpecimenOnlyKey);
1813 30 Jan 13 olle 1002     jsonStatistics = tableUtil.initializeJSONSiteData(jsonStatistics, patientBloodSampleAndSpecimenKey);
1813 30 Jan 13 olle 1003     jsonStatistics = tableUtil.initializeJSONSiteData(jsonStatistics, patientBloodSampleAndNoSpecimenKey);
1813 30 Jan 13 olle 1004     jsonStatistics = tableUtil.initializeJSONSiteData(jsonStatistics, patientSpecimenAndNoSpecimenKey);
1813 30 Jan 13 olle 1005     jsonStatistics = tableUtil.initializeJSONSiteData(jsonStatistics, patientBloodSampleAndSpecimenAndNoSpecimenKey);
1813 30 Jan 13 olle 1006     jsonStatistics = tableUtil.initializeJSONSiteData(jsonStatistics, consentYesKey);
1813 30 Jan 13 olle 1007     jsonStatistics = tableUtil.initializeJSONSiteData(jsonStatistics, consentMissingKey);
2000 29 May 13 olle 1008     jsonStatistics = tableUtil.initializeJSONSiteData(jsonStatistics, patientNoSamplesKey);
2183 20 Dec 13 olle 1009     jsonStatistics = tableUtil.initializeJSONSiteData(jsonStatistics, bloodSampleNoDateKey);
2183 20 Dec 13 olle 1010     jsonStatistics = tableUtil.initializeJSONSiteData(jsonStatistics, bloodSampleFollowUpNoDateKey);
2183 20 Dec 13 olle 1011     jsonStatistics = tableUtil.initializeJSONSiteData(jsonStatistics, specimenNoDateKey);
2183 20 Dec 13 olle 1012     jsonStatistics = tableUtil.initializeJSONSiteData(jsonStatistics, noSpecimenNoDateKey);
2000 29 May 13 olle 1013     jsonStatistics = tableUtil.initializeJSONSiteData(jsonStatistics, sumKey);
1765 03 Dec 12 olle 1014     // Initialize combined data to 0 for the different keys
1765 03 Dec 12 olle 1015     jsonSitesCombined.put(patientKey, 0);
1765 03 Dec 12 olle 1016     jsonSitesCombined.put(bloodSampleKey, 0);
2024 20 Sep 13 olle 1017     jsonSitesCombined.put(bloodSampleFollowUpKey, 0);
1765 03 Dec 12 olle 1018     jsonSitesCombined.put(specimenKey, 0);
1765 03 Dec 12 olle 1019     jsonSitesCombined.put(noSpecimenKey, 0);
2178 12 Dec 13 olle 1020     jsonSitesCombined.put(patientWithSpecimenKey, 0);
2178 12 Dec 13 olle 1021     jsonSitesCombined.put(patientWithNoSpecimenKey, 0);
2178 12 Dec 13 olle 1022     jsonSitesCombined.put(patientWithBloodSampleKey, 0);
2178 12 Dec 13 olle 1023     jsonSitesCombined.put(patientWithFollowUpBloodSampleKey, 0);
1765 03 Dec 12 olle 1024     jsonSitesCombined.put(patientBloodSampleOnlyKey, 0);
1765 03 Dec 12 olle 1025     jsonSitesCombined.put(patientSpecimenOnlyKey, 0);
1765 03 Dec 12 olle 1026     jsonSitesCombined.put(patientNoSpecimenOnlyKey, 0);
1765 03 Dec 12 olle 1027     jsonSitesCombined.put(patientBloodSampleAndSpecimenKey, 0);
1765 03 Dec 12 olle 1028     jsonSitesCombined.put(patientBloodSampleAndNoSpecimenKey, 0);
1765 03 Dec 12 olle 1029     jsonSitesCombined.put(patientSpecimenAndNoSpecimenKey, 0);
1765 03 Dec 12 olle 1030     jsonSitesCombined.put(patientBloodSampleAndSpecimenAndNoSpecimenKey, 0);
1765 03 Dec 12 olle 1031     jsonSitesCombined.put(consentYesKey, 0);
1765 03 Dec 12 olle 1032     jsonSitesCombined.put(consentMissingKey, 0);
1765 03 Dec 12 olle 1033     jsonSitesCombined.put(patientNoSamplesKey, 0);
2183 20 Dec 13 olle 1034     jsonSitesCombined.put(bloodSampleNoDateKey, 0);
2183 20 Dec 13 olle 1035     jsonSitesCombined.put(bloodSampleFollowUpNoDateKey, 0);
2183 20 Dec 13 olle 1036     jsonSitesCombined.put(specimenNoDateKey, 0);
2183 20 Dec 13 olle 1037     jsonSitesCombined.put(noSpecimenNoDateKey, 0);
2000 29 May 13 olle 1038     jsonSitesCombined.put(sumKey, 0);
1765 03 Dec 12 olle 1039     // Initialize other data to 0 for different keys
1765 03 Dec 12 olle 1040     jsonStatistics.put(duplicateKey, 0);
1765 03 Dec 12 olle 1041     Date latestDate = null;
1765 03 Dec 12 olle 1042     // Create HashMap to keep track of processed dates for each patient
1765 03 Dec 12 olle 1043     HashMap<String, Set<Date>>patientDateSetHashMap = new HashMap<String, Set<Date>>();
1765 03 Dec 12 olle 1044     //JSONObject jsonPatientnamePatientid = new JSONObject();
1765 03 Dec 12 olle 1045     // HashMap of patient id's and site prefixes
1765 03 Dec 12 olle 1046     HashMap<String, String> patientHashMap = new HashMap<String, String>();
1765 03 Dec 12 olle 1047     // Set of patient id's for patients with blood samples
1765 03 Dec 12 olle 1048     Set<String> patientsWithBloodSamplesSet = new HashSet<String>();
2178 12 Dec 13 olle 1049     // Set of patient id's for patients with follow-up blood samples
2178 12 Dec 13 olle 1050     Set<String> patientsWithFollowUpBloodSamplesSet = new HashSet<String>();
1765 03 Dec 12 olle 1051     // Set of patient id's for patients with specimens
1765 03 Dec 12 olle 1052     Set<String> patientsWithSpecimensSet = new HashSet<String>();
1765 03 Dec 12 olle 1053     // Set of patient id's for patients with no specimens
1765 03 Dec 12 olle 1054     Set<String> patientsWithNoSpecimensSet = new HashSet<String>();
1765 03 Dec 12 olle 1055     // Create HashMap to keep track of latest item date for each site
1765 03 Dec 12 olle 1056     HashMap<String, Date> sitePrefixDateHashMap = new HashMap<String, Date>();
1765 03 Dec 12 olle 1057     // Get item subtype constants for later use
1765 03 Dec 12 olle 1058     ItemSubtype subtypeSpecimen = Subtype.SPECIMEN.load(dc);
1765 03 Dec 12 olle 1059     ItemSubtype subtypeNoSpecimen = Subtype.NO_SPECIMEN.load(dc);
1765 03 Dec 12 olle 1060     ItemSubtype subtypeBlood = Subtype.BLOOD.load(dc);
1765 03 Dec 12 olle 1061     ItemSubtype subtypeCase = Subtype.CASE.load(dc);
1765 03 Dec 12 olle 1062     //
1765 03 Dec 12 olle 1063     while (sampleIterator != null && sampleIterator.hasNext())
1765 03 Dec 12 olle 1064     {
3911 02 May 16 nicklas 1065       if (count % 100 == 0)
3911 02 May 16 nicklas 1066             {
3911 02 May 16 nicklas 1067         progress.display(5 + (int)((95 * count) / totalCount), 
3911 02 May 16 nicklas 1068             "Processing " + Reggie.formatCount(count) + " of " + Reggie.formatCount(totalCount) + " items");
3911 02 May 16 nicklas 1069             }
3911 02 May 16 nicklas 1070       count++;
1765 03 Dec 12 olle 1071       Sample s = sampleIterator.next();
1765 03 Dec 12 olle 1072       BioMaterialEvent creationEvent = s.getCreationEvent();
1765 03 Dec 12 olle 1073       Date creationDate = creationEvent.getEventDate();
1765 03 Dec 12 olle 1074
2183 20 Dec 13 olle 1075       // Replace unknown creation date with registration date in overview report
2183 20 Dec 13 olle 1076       if (creationDate == null)
2183 20 Dec 13 olle 1077       {
2183 20 Dec 13 olle 1078         creationDate = s.getEntryDate();
2183 20 Dec 13 olle 1079         // Update unknown date counter
2183 20 Dec 13 olle 1080         Site sampleSite = Site.findByCaseName(s.getName());  
2183 20 Dec 13 olle 1081         if (sampleSite != Site.UNKNOWN)
2183 20 Dec 13 olle 1082         {
2183 20 Dec 13 olle 1083           String sampleNoDateKey = null;
2183 20 Dec 13 olle 1084           JSONObject jsonSite = (JSONObject)jsonStatistics.get(sampleSite.getPrefix());
2183 20 Dec 13 olle 1085           if (jsonSite == null)
2183 20 Dec 13 olle 1086           {
2183 20 Dec 13 olle 1087             jsonSite = new JSONObject();
2183 20 Dec 13 olle 1088             jsonStatistics.put(sampleSite.getPrefix(), jsonSite);
2183 20 Dec 13 olle 1089           }
2183 20 Dec 13 olle 1090           if (s.getItemSubtype().equals(subtypeBlood))
2183 20 Dec 13 olle 1091           {
2183 20 Dec 13 olle 1092             sampleNoDateKey = bloodSampleNoDateKey;
2183 20 Dec 13 olle 1093             // Get blood sample annotation
2183 20 Dec 13 olle 1094             String bloodSampleType = (String) Annotationtype.BLOOD_SAMPLE.getAnnotationValue(dc, manager, s);
2183 20 Dec 13 olle 1095             // Follow-up blood sample type (blood sample type == null should not be included)
2183 20 Dec 13 olle 1096             if (bloodSampleType != null &&
2183 20 Dec 13 olle 1097                 (bloodSampleType.equals(BLOOD_FILTER_FOLLOW_UP_06)
2183 20 Dec 13 olle 1098                 || bloodSampleType.equals(BLOOD_FILTER_FOLLOW_UP_12)
2183 20 Dec 13 olle 1099                 || bloodSampleType.equals(BLOOD_FILTER_FOLLOW_UP_36))
2183 20 Dec 13 olle 1100               )
2183 20 Dec 13 olle 1101             {
2183 20 Dec 13 olle 1102               // Update unknown date counter
2183 20 Dec 13 olle 1103               jsonSite = tableUtil.updateJSONObjectCounter(jsonSite, bloodSampleFollowUpNoDateKey);
2183 20 Dec 13 olle 1104               jsonSitesCombined = tableUtil.updateJSONObjectCounter(jsonSitesCombined, bloodSampleFollowUpNoDateKey);
2183 20 Dec 13 olle 1105               jsonStatistics = tableUtil.updateJSONObjectCounter(jsonStatistics, bloodSampleFollowUpNoDateKey);
2183 20 Dec 13 olle 1106             }
2183 20 Dec 13 olle 1107           }
2183 20 Dec 13 olle 1108           else if (s.getItemSubtype().equals(subtypeSpecimen))
2183 20 Dec 13 olle 1109           {
2183 20 Dec 13 olle 1110             sampleNoDateKey = specimenNoDateKey;
2183 20 Dec 13 olle 1111           }
2183 20 Dec 13 olle 1112           else if (s.getItemSubtype().equals(subtypeNoSpecimen))
2183 20 Dec 13 olle 1113           {
2183 20 Dec 13 olle 1114             sampleNoDateKey = noSpecimenNoDateKey;
2183 20 Dec 13 olle 1115           }
2183 20 Dec 13 olle 1116           // Update unknown date counter
2183 20 Dec 13 olle 1117           jsonSite = tableUtil.updateJSONObjectCounter(jsonSite, sampleNoDateKey);
2183 20 Dec 13 olle 1118           jsonSitesCombined = tableUtil.updateJSONObjectCounter(jsonSitesCombined, sampleNoDateKey);
2183 20 Dec 13 olle 1119           jsonStatistics = tableUtil.updateJSONObjectCounter(jsonStatistics, sampleNoDateKey);
2183 20 Dec 13 olle 1120         }
2183 20 Dec 13 olle 1121       }
2183 20 Dec 13 olle 1122       // Skip item if date is outside selected time period
2183 20 Dec 13 olle 1123       if (creationDate.before(startDate) || creationDate.after(endDate))
2183 20 Dec 13 olle 1124       {
2183 20 Dec 13 olle 1125         continue;
2183 20 Dec 13 olle 1126       }
2183 20 Dec 13 olle 1127       
2024 20 Sep 13 olle 1128       // Update follow-up blood sample data before filtering for blood sample type
2024 20 Sep 13 olle 1129       if (s.getItemSubtype().equals(subtypeBlood))
2024 20 Sep 13 olle 1130       {
2024 20 Sep 13 olle 1131         Site bloodSite = Site.findByCaseName(s.getName());  
2024 20 Sep 13 olle 1132         if (bloodSite != Site.UNKNOWN)
2024 20 Sep 13 olle 1133         {
2024 20 Sep 13 olle 1134           // Get blood site info            
2024 20 Sep 13 olle 1135           JSONObject jsonSite = (JSONObject)jsonStatistics.get(bloodSite.getPrefix());
2024 20 Sep 13 olle 1136           if (jsonSite == null)
2024 20 Sep 13 olle 1137           {
2024 20 Sep 13 olle 1138             jsonSite = new JSONObject();
2024 20 Sep 13 olle 1139             jsonStatistics.put(bloodSite.getPrefix(), jsonSite);
2024 20 Sep 13 olle 1140           }
2024 20 Sep 13 olle 1141           // Get blood sample annotation
2024 20 Sep 13 olle 1142           String bloodSampleType = (String) Annotationtype.BLOOD_SAMPLE.getAnnotationValue(dc, manager, s);
2024 20 Sep 13 olle 1143           // Follow-up blood sample type (blood sample type == null should not be included)
2024 20 Sep 13 olle 1144           if (bloodSampleType != null &&
2024 20 Sep 13 olle 1145               (bloodSampleType.equals(BLOOD_FILTER_FOLLOW_UP_06)
2024 20 Sep 13 olle 1146               || bloodSampleType.equals(BLOOD_FILTER_FOLLOW_UP_12)
2024 20 Sep 13 olle 1147               || bloodSampleType.equals(BLOOD_FILTER_FOLLOW_UP_36))
2024 20 Sep 13 olle 1148             )
2024 20 Sep 13 olle 1149           {
2024 20 Sep 13 olle 1150             jsonSite = tableUtil.updateJSONObjectCounter(jsonSite, bloodSampleFollowUpKey);
2024 20 Sep 13 olle 1151             jsonSitesCombined = tableUtil.updateJSONObjectCounter(jsonSitesCombined, bloodSampleFollowUpKey);
2024 20 Sep 13 olle 1152           }
2024 20 Sep 13 olle 1153         }
2024 20 Sep 13 olle 1154       }
2024 20 Sep 13 olle 1155
2203 21 Jan 14 olle 1156       // Optional blood sample filter (true if no filter, sample not blood, or blood sample passes filter)
2203 21 Jan 14 olle 1157       boolean passedBloodSampleFilter = passBloodSampleFilter(dc, manager, bloodSampleFilter, s);
2203 21 Jan 14 olle 1158       if (!passedBloodSampleFilter)
2024 20 Sep 13 olle 1159       {
2203 21 Jan 14 olle 1160         continue;
2024 20 Sep 13 olle 1161       }
2024 20 Sep 13 olle 1162       
1765 03 Dec 12 olle 1163       // Find patient id
1765 03 Dec 12 olle 1164       BioMaterial patient = null;
1765 03 Dec 12 olle 1165       if (s.getItemSubtype().equals(subtypeBlood))
1765 03 Dec 12 olle 1166       {
1765 03 Dec 12 olle 1167         // The parent is the 'Patient' for blood samples and cases
1765 03 Dec 12 olle 1168         patient = s.getParent();
1765 03 Dec 12 olle 1169       }
1765 03 Dec 12 olle 1170       else if (s.getItemSubtype().equals(subtypeSpecimen) || s.getItemSubtype().equals(subtypeNoSpecimen))
1765 03 Dec 12 olle 1171       {
1765 03 Dec 12 olle 1172         // The grandparent is the 'Patient' for specimens and no specimens
1765 03 Dec 12 olle 1173         MeasuredBioMaterial parent = (MeasuredBioMaterial) s.getParent();
1765 03 Dec 12 olle 1174         if (parent != null)
1765 03 Dec 12 olle 1175         {
1765 03 Dec 12 olle 1176           patient = parent.getParent();
1765 03 Dec 12 olle 1177         }
1765 03 Dec 12 olle 1178       }
1765 03 Dec 12 olle 1179
1765 03 Dec 12 olle 1180       // Update statistics for specimens, blood samples, and consents
1765 03 Dec 12 olle 1181       Site site = Site.findByCaseName(s.getName());  
1765 03 Dec 12 olle 1182       if (site == Site.UNKNOWN)
1765 03 Dec 12 olle 1183       {
1765 03 Dec 12 olle 1184         String key = "unknownSite";
1813 30 Jan 13 olle 1185         jsonStatistics = tableUtil.updateJSONObjectCounter(jsonStatistics, key);
1765 03 Dec 12 olle 1186       }
1765 03 Dec 12 olle 1187       else
1765 03 Dec 12 olle 1188       {
1765 03 Dec 12 olle 1189         // Get site info            
1765 03 Dec 12 olle 1190         JSONObject jsonSite = (JSONObject)jsonStatistics.get(site.getPrefix());
1765 03 Dec 12 olle 1191         if (jsonSite == null)
1765 03 Dec 12 olle 1192         {
1765 03 Dec 12 olle 1193           jsonSite = new JSONObject();
1765 03 Dec 12 olle 1194           jsonStatistics.put(site.getPrefix(), jsonSite);
1765 03 Dec 12 olle 1195         }
1765 03 Dec 12 olle 1196
1765 03 Dec 12 olle 1197         // Store patient data for later processing
1765 03 Dec 12 olle 1198         String patientId = null;
1765 03 Dec 12 olle 1199         if (patient != null)
1765 03 Dec 12 olle 1200         {
1765 03 Dec 12 olle 1201           patientId = patient.getName();
1765 03 Dec 12 olle 1202           if (patientId != null && !patientId.equals(""))
1765 03 Dec 12 olle 1203           {
1765 03 Dec 12 olle 1204             // Add patient id to patient HashMap for site
1765 03 Dec 12 olle 1205             patientHashMap.put(patientId, site.getPrefix());
1765 03 Dec 12 olle 1206
1765 03 Dec 12 olle 1207             if (s.getItemSubtype().equals(subtypeSpecimen))
1765 03 Dec 12 olle 1208             {
1765 03 Dec 12 olle 1209               // Add patient to patients with specimens set
1765 03 Dec 12 olle 1210               patientsWithSpecimensSet.add(patientId);
1765 03 Dec 12 olle 1211             }
1765 03 Dec 12 olle 1212
1765 03 Dec 12 olle 1213             if (s.getItemSubtype().equals(subtypeNoSpecimen))
1765 03 Dec 12 olle 1214             {
1765 03 Dec 12 olle 1215               // Add patient to patients with no specimens set
1765 03 Dec 12 olle 1216               patientsWithNoSpecimensSet.add(patientId);
1765 03 Dec 12 olle 1217             }
1765 03 Dec 12 olle 1218
1765 03 Dec 12 olle 1219             if (s.getItemSubtype().equals(subtypeBlood))
1765 03 Dec 12 olle 1220             {
1765 03 Dec 12 olle 1221               // Add patient to patients with blood samples set
1765 03 Dec 12 olle 1222               patientsWithBloodSamplesSet.add(patientId);
2178 12 Dec 13 olle 1223               // Get blood sample annotation
2178 12 Dec 13 olle 1224               String bloodSampleType = (String) Annotationtype.BLOOD_SAMPLE.getAnnotationValue(dc, manager, s);
2178 12 Dec 13 olle 1225               if (bloodSampleType != null &&
2178 12 Dec 13 olle 1226                   (bloodSampleType.equals(BLOOD_FILTER_FOLLOW_UP_06)
2178 12 Dec 13 olle 1227                   || bloodSampleType.equals(BLOOD_FILTER_FOLLOW_UP_12)
2178 12 Dec 13 olle 1228                   || bloodSampleType.equals(BLOOD_FILTER_FOLLOW_UP_36))
2178 12 Dec 13 olle 1229                 )
2178 12 Dec 13 olle 1230               {
2178 12 Dec 13 olle 1231                 patientsWithFollowUpBloodSamplesSet.add(patientId);
2178 12 Dec 13 olle 1232               }
1765 03 Dec 12 olle 1233             }
1765 03 Dec 12 olle 1234           }
1765 03 Dec 12 olle 1235         }
1765 03 Dec 12 olle 1236
1765 03 Dec 12 olle 1237         // Update specimen data
1765 03 Dec 12 olle 1238         if (s.getItemSubtype().equals(subtypeSpecimen))
1765 03 Dec 12 olle 1239         {
1813 30 Jan 13 olle 1240           jsonSite = tableUtil.updateJSONObjectCounter(jsonSite, specimenKey);
1813 30 Jan 13 olle 1241           jsonSitesCombined = tableUtil.updateJSONObjectCounter(jsonSitesCombined, specimenKey);
1765 03 Dec 12 olle 1242         }
1765 03 Dec 12 olle 1243         // Update no specimen data
1765 03 Dec 12 olle 1244         if (s.getItemSubtype().equals(subtypeNoSpecimen))
1765 03 Dec 12 olle 1245         {
1813 30 Jan 13 olle 1246           jsonSite = tableUtil.updateJSONObjectCounter(jsonSite, noSpecimenKey);
1813 30 Jan 13 olle 1247           jsonSitesCombined = tableUtil.updateJSONObjectCounter(jsonSitesCombined, noSpecimenKey);
1765 03 Dec 12 olle 1248         }
1765 03 Dec 12 olle 1249         // Update blood sample data
1765 03 Dec 12 olle 1250         if (s.getItemSubtype().equals(subtypeBlood))
1765 03 Dec 12 olle 1251         {
1813 30 Jan 13 olle 1252           jsonSite = tableUtil.updateJSONObjectCounter(jsonSite, bloodSampleKey);
1813 30 Jan 13 olle 1253           jsonSitesCombined = tableUtil.updateJSONObjectCounter(jsonSitesCombined, bloodSampleKey);
1765 03 Dec 12 olle 1254         }
1765 03 Dec 12 olle 1255
1765 03 Dec 12 olle 1256         // Keep track of latest item date for site
1765 03 Dec 12 olle 1257         if (creationDate != null)
1765 03 Dec 12 olle 1258         {
1765 03 Dec 12 olle 1259           Date siteLatestDate = sitePrefixDateHashMap.get(site.getPrefix());
1765 03 Dec 12 olle 1260           if (siteLatestDate == null || siteLatestDate.before(creationDate))
1765 03 Dec 12 olle 1261           {
1765 03 Dec 12 olle 1262             sitePrefixDateHashMap.put(site.getPrefix(), creationDate);
1765 03 Dec 12 olle 1263           }
1765 03 Dec 12 olle 1264           // Keep track of latest item date so far
1765 03 Dec 12 olle 1265           if (latestDate == null || latestDate.before(creationDate)) 
1765 03 Dec 12 olle 1266           {
1765 03 Dec 12 olle 1267             latestDate = creationDate;
1765 03 Dec 12 olle 1268           }
1765 03 Dec 12 olle 1269         }
1765 03 Dec 12 olle 1270
1765 03 Dec 12 olle 1271         // Process consents
1765 03 Dec 12 olle 1272         String consent = null;
1765 03 Dec 12 olle 1273         Date consentDate = null;
1765 03 Dec 12 olle 1274         if (s.getItemSubtype().equals(subtypeBlood) || s.getItemSubtype().equals(subtypeCase))
1765 03 Dec 12 olle 1275         {
1765 03 Dec 12 olle 1276           // Get consent
1765 03 Dec 12 olle 1277           consent = (String) Annotationtype.CONSENT.getAnnotationValue(dc, manager, s);
1765 03 Dec 12 olle 1278           // Get consent date
1765 03 Dec 12 olle 1279           consentDate = (Date) Annotationtype.CONSENT_DATE.getAnnotationValue(dc, manager, s);
1765 03 Dec 12 olle 1280
1765 03 Dec 12 olle 1281           String key = null;
1765 03 Dec 12 olle 1282           Boolean duplicate = false;
1765 03 Dec 12 olle 1283           if (consent != null)
1765 03 Dec 12 olle 1284           {
1765 03 Dec 12 olle 1285             if (consentDate != null)
1765 03 Dec 12 olle 1286             {
1765 03 Dec 12 olle 1287               // The parent is the 'Patient' for blood samples and cases
1765 03 Dec 12 olle 1288               patient = s.getParent();
1765 03 Dec 12 olle 1289
1765 03 Dec 12 olle 1290               // Check if a consent already exists for the same patient and date
1765 03 Dec 12 olle 1291               if (patient != null)
1765 03 Dec 12 olle 1292               {
1765 03 Dec 12 olle 1293                 patientId = patient.getName();
1765 03 Dec 12 olle 1294                 if (patientId != null && !patientId.equals(""))
1765 03 Dec 12 olle 1295                 {
1813 30 Jan 13 olle 1296                   if (tableUtil.inStringDateSetHashMap(patientDateSetHashMap, patientId, consentDate))
1765 03 Dec 12 olle 1297                   {
1765 03 Dec 12 olle 1298                     duplicate = true;
1765 03 Dec 12 olle 1299                   }
1765 03 Dec 12 olle 1300                   else
1765 03 Dec 12 olle 1301                   {
1813 30 Jan 13 olle 1302                     patientDateSetHashMap = tableUtil.updateStringDateSetHashMap(patientDateSetHashMap, patientId, consentDate);
1765 03 Dec 12 olle 1303                   }
1765 03 Dec 12 olle 1304                 }
1765 03 Dec 12 olle 1305               }
1765 03 Dec 12 olle 1306             }
1765 03 Dec 12 olle 1307             // Count approved consents, including consents without patient id (PAT#) or date
1765 03 Dec 12 olle 1308             if (consent.equals("Yes"))
1765 03 Dec 12 olle 1309             {
1765 03 Dec 12 olle 1310               key = consentYesKey;
1765 03 Dec 12 olle 1311               if (consentDate != null)
1765 03 Dec 12 olle 1312               {
1765 03 Dec 12 olle 1313                 // Keep track of latest item date for site
1765 03 Dec 12 olle 1314                 Date siteLatestDate = sitePrefixDateHashMap.get(site.getPrefix());
1765 03 Dec 12 olle 1315                 if (siteLatestDate == null || siteLatestDate.before(consentDate))
1765 03 Dec 12 olle 1316                 {
1765 03 Dec 12 olle 1317                   sitePrefixDateHashMap.put(site.getPrefix(), consentDate);
1765 03 Dec 12 olle 1318                 }
1765 03 Dec 12 olle 1319                 // Keep track of latest item date so far
1765 03 Dec 12 olle 1320                 if (latestDate == null || latestDate.before(consentDate)) 
1765 03 Dec 12 olle 1321                 {
1765 03 Dec 12 olle 1322                   latestDate = consentDate;
1765 03 Dec 12 olle 1323                 }
1765 03 Dec 12 olle 1324               }
1765 03 Dec 12 olle 1325             }
1765 03 Dec 12 olle 1326           }
1765 03 Dec 12 olle 1327           else
1765 03 Dec 12 olle 1328           {
1765 03 Dec 12 olle 1329             key = consentMissingKey;
1765 03 Dec 12 olle 1330           }
1765 03 Dec 12 olle 1331           // Note that items with duplicate == false include missing consent items
1765 03 Dec 12 olle 1332           if (!duplicate)
1765 03 Dec 12 olle 1333           {
1765 03 Dec 12 olle 1334             if (key != null)
1765 03 Dec 12 olle 1335             {
1765 03 Dec 12 olle 1336               // Update consent counters
1813 30 Jan 13 olle 1337               jsonSite = tableUtil.updateJSONObjectCounter(jsonSite, key);
1813 30 Jan 13 olle 1338               jsonSitesCombined = tableUtil.updateJSONObjectCounter(jsonSitesCombined, key);
1765 03 Dec 12 olle 1339             }
1765 03 Dec 12 olle 1340           }
1765 03 Dec 12 olle 1341           else
1765 03 Dec 12 olle 1342           {
1813 30 Jan 13 olle 1343             jsonStatistics = tableUtil.updateJSONObjectCounter(jsonStatistics, duplicateKey);
1765 03 Dec 12 olle 1344           }
1765 03 Dec 12 olle 1345         }
1765 03 Dec 12 olle 1346       }
1765 03 Dec 12 olle 1347     }
1765 03 Dec 12 olle 1348
1765 03 Dec 12 olle 1349     // Process patients
1765 03 Dec 12 olle 1350     for (String patId: patientHashMap.keySet())
1765 03 Dec 12 olle 1351     {
1765 03 Dec 12 olle 1352       if (patId != null && !patId.equals(""))
1765 03 Dec 12 olle 1353       {
1765 03 Dec 12 olle 1354         // Get site info
1765 03 Dec 12 olle 1355         String sitePrefix = patientHashMap.get(patId);
1765 03 Dec 12 olle 1356         JSONObject jsonSite = (JSONObject)jsonStatistics.get(sitePrefix);
1765 03 Dec 12 olle 1357         if (jsonSite == null)
1765 03 Dec 12 olle 1358         {
1765 03 Dec 12 olle 1359           jsonSite = new JSONObject();
1765 03 Dec 12 olle 1360           jsonStatistics.put(sitePrefix, jsonSite);
1765 03 Dec 12 olle 1361         }
1765 03 Dec 12 olle 1362         // Update patient data
1813 30 Jan 13 olle 1363         jsonSite = tableUtil.updateJSONObjectCounter(jsonSite, patientKey);
1813 30 Jan 13 olle 1364         jsonSitesCombined = tableUtil.updateJSONObjectCounter(jsonSitesCombined, patientKey);
2178 12 Dec 13 olle 1365         // Update data for patients having different kinds of samples
1765 03 Dec 12 olle 1366         if (patientsWithSpecimensSet.contains(patId))
1765 03 Dec 12 olle 1367         {
2178 12 Dec 13 olle 1368           // Patient with at least one specimen
2178 12 Dec 13 olle 1369           jsonSite = tableUtil.updateJSONObjectCounter(jsonSite, patientWithSpecimenKey);
2178 12 Dec 13 olle 1370           jsonSitesCombined = tableUtil.updateJSONObjectCounter(jsonSitesCombined, patientWithSpecimenKey);
2178 12 Dec 13 olle 1371         }
2178 12 Dec 13 olle 1372         if (patientsWithNoSpecimensSet.contains(patId))
2178 12 Dec 13 olle 1373         {
2178 12 Dec 13 olle 1374           // Patient with at least one no specimen
2178 12 Dec 13 olle 1375           jsonSite = tableUtil.updateJSONObjectCounter(jsonSite, patientWithNoSpecimenKey);
2178 12 Dec 13 olle 1376           jsonSitesCombined = tableUtil.updateJSONObjectCounter(jsonSitesCombined, patientWithNoSpecimenKey);
2178 12 Dec 13 olle 1377         }
2178 12 Dec 13 olle 1378         if (patientsWithBloodSamplesSet.contains(patId))
2178 12 Dec 13 olle 1379         {
2178 12 Dec 13 olle 1380           // Patient with at least one blood sample
2178 12 Dec 13 olle 1381           jsonSite = tableUtil.updateJSONObjectCounter(jsonSite, patientWithBloodSampleKey);
2178 12 Dec 13 olle 1382           jsonSitesCombined = tableUtil.updateJSONObjectCounter(jsonSitesCombined, patientWithBloodSampleKey);
2178 12 Dec 13 olle 1383         }
2178 12 Dec 13 olle 1384         if (patientsWithFollowUpBloodSamplesSet.contains(patId))
2178 12 Dec 13 olle 1385         {
2178 12 Dec 13 olle 1386           // Patient with at least one follow-up blood sample
2178 12 Dec 13 olle 1387           jsonSite = tableUtil.updateJSONObjectCounter(jsonSite, patientWithFollowUpBloodSampleKey);
2178 12 Dec 13 olle 1388           jsonSitesCombined = tableUtil.updateJSONObjectCounter(jsonSitesCombined, patientWithFollowUpBloodSampleKey);
2178 12 Dec 13 olle 1389         }
2178 12 Dec 13 olle 1390         // Update data for patient records of different kinds
2178 12 Dec 13 olle 1391         if (patientsWithSpecimensSet.contains(patId))
2178 12 Dec 13 olle 1392         {
1765 03 Dec 12 olle 1393           if (patientsWithNoSpecimensSet.contains(patId))
1765 03 Dec 12 olle 1394           {
1765 03 Dec 12 olle 1395             if (patientsWithBloodSamplesSet.contains(patId))
1765 03 Dec 12 olle 1396             {
1765 03 Dec 12 olle 1397               // Patient with both specimens, no specimens, and blood samples
1813 30 Jan 13 olle 1398               jsonSite = tableUtil.updateJSONObjectCounter(jsonSite, patientBloodSampleAndSpecimenAndNoSpecimenKey);
1813 30 Jan 13 olle 1399               jsonSitesCombined = tableUtil.updateJSONObjectCounter(jsonSitesCombined, patientBloodSampleAndSpecimenAndNoSpecimenKey);
1765 03 Dec 12 olle 1400             }
1765 03 Dec 12 olle 1401             else
1765 03 Dec 12 olle 1402             {
1765 03 Dec 12 olle 1403               // Patient with both specimens and no specimens
1813 30 Jan 13 olle 1404               jsonSite = tableUtil.updateJSONObjectCounter(jsonSite, patientSpecimenAndNoSpecimenKey);
1813 30 Jan 13 olle 1405               jsonSitesCombined = tableUtil.updateJSONObjectCounter(jsonSitesCombined, patientSpecimenAndNoSpecimenKey);
1765 03 Dec 12 olle 1406             }
1765 03 Dec 12 olle 1407           }
1765 03 Dec 12 olle 1408           else
1765 03 Dec 12 olle 1409           {
1765 03 Dec 12 olle 1410             if (patientsWithBloodSamplesSet.contains(patId))
1765 03 Dec 12 olle 1411             {
1765 03 Dec 12 olle 1412               // Patient with both specimens and blood samples
1813 30 Jan 13 olle 1413               jsonSite = tableUtil.updateJSONObjectCounter(jsonSite, patientBloodSampleAndSpecimenKey);
1813 30 Jan 13 olle 1414               jsonSitesCombined = tableUtil.updateJSONObjectCounter(jsonSitesCombined, patientBloodSampleAndSpecimenKey);
1765 03 Dec 12 olle 1415             }
1765 03 Dec 12 olle 1416             else
1765 03 Dec 12 olle 1417             {
1765 03 Dec 12 olle 1418               // Patient with specimens only
1813 30 Jan 13 olle 1419               jsonSite = tableUtil.updateJSONObjectCounter(jsonSite, patientSpecimenOnlyKey);
1813 30 Jan 13 olle 1420               jsonSitesCombined = tableUtil.updateJSONObjectCounter(jsonSitesCombined, patientSpecimenOnlyKey);
1765 03 Dec 12 olle 1421             }
1765 03 Dec 12 olle 1422           }
1765 03 Dec 12 olle 1423         }
1765 03 Dec 12 olle 1424         else
1765 03 Dec 12 olle 1425         {
1765 03 Dec 12 olle 1426           if (patientsWithNoSpecimensSet.contains(patId))
1765 03 Dec 12 olle 1427           {
1765 03 Dec 12 olle 1428             if (patientsWithBloodSamplesSet.contains(patId))
1765 03 Dec 12 olle 1429             {
1765 03 Dec 12 olle 1430               // Patient with both blood samples and no specimens
1813 30 Jan 13 olle 1431               jsonSite = tableUtil.updateJSONObjectCounter(jsonSite, patientBloodSampleAndNoSpecimenKey);
1813 30 Jan 13 olle 1432               jsonSitesCombined = tableUtil.updateJSONObjectCounter(jsonSitesCombined, patientBloodSampleAndNoSpecimenKey);
1765 03 Dec 12 olle 1433             }
1765 03 Dec 12 olle 1434             else
1765 03 Dec 12 olle 1435             {
1765 03 Dec 12 olle 1436               // Patient with no specimens only
1813 30 Jan 13 olle 1437               jsonSite = tableUtil.updateJSONObjectCounter(jsonSite, patientNoSpecimenOnlyKey);
1813 30 Jan 13 olle 1438               jsonSitesCombined = tableUtil.updateJSONObjectCounter(jsonSitesCombined, patientNoSpecimenOnlyKey);
1765 03 Dec 12 olle 1439             }
1765 03 Dec 12 olle 1440           }
1765 03 Dec 12 olle 1441           else
1765 03 Dec 12 olle 1442           {
1765 03 Dec 12 olle 1443             if (patientsWithBloodSamplesSet.contains(patId))
1765 03 Dec 12 olle 1444             {
1765 03 Dec 12 olle 1445               // Patient with blood samples only
1813 30 Jan 13 olle 1446               jsonSite = tableUtil.updateJSONObjectCounter(jsonSite, patientBloodSampleOnlyKey);
1813 30 Jan 13 olle 1447               jsonSitesCombined = tableUtil.updateJSONObjectCounter(jsonSitesCombined, patientBloodSampleOnlyKey);
1765 03 Dec 12 olle 1448             }
1765 03 Dec 12 olle 1449             else
1765 03 Dec 12 olle 1450             {
1765 03 Dec 12 olle 1451               // Patient with no sample
1813 30 Jan 13 olle 1452               jsonSite = tableUtil.updateJSONObjectCounter(jsonSite, patientNoSamplesKey);
1813 30 Jan 13 olle 1453               jsonSitesCombined = tableUtil.updateJSONObjectCounter(jsonSitesCombined, patientNoSamplesKey);
1765 03 Dec 12 olle 1454             }
1765 03 Dec 12 olle 1455           }
1765 03 Dec 12 olle 1456         }
1765 03 Dec 12 olle 1457         // Update summed patient data
1813 30 Jan 13 olle 1458         jsonSite = tableUtil.updateJSONObjectCounter(jsonSite, sumKey);
1813 30 Jan 13 olle 1459         jsonSitesCombined = tableUtil.updateJSONObjectCounter(jsonSitesCombined, sumKey);
1765 03 Dec 12 olle 1460       }
1765 03 Dec 12 olle 1461     }
1765 03 Dec 12 olle 1462
1765 03 Dec 12 olle 1463     // Add latest item date for site to site JSON object
1765 03 Dec 12 olle 1464     DateToStringConverter date2StringConverter = Reggie.CONVERTER_DATE_TO_STRING;
1765 03 Dec 12 olle 1465     for (Site s: Site.getAllSites())
1765 03 Dec 12 olle 1466     {
1765 03 Dec 12 olle 1467       JSONObject jsonSite = (JSONObject) jsonStatistics.get(s.getPrefix());
1765 03 Dec 12 olle 1468       if (jsonSite == null)
1765 03 Dec 12 olle 1469       {
1765 03 Dec 12 olle 1470         jsonSite = new JSONObject();
1765 03 Dec 12 olle 1471         jsonStatistics.put(s.getPrefix(), jsonSite);
1765 03 Dec 12 olle 1472       }
1765 03 Dec 12 olle 1473       Date siteLatestDate = sitePrefixDateHashMap.get(s.getPrefix());
1765 03 Dec 12 olle 1474       jsonSite.put(latestDateKey, date2StringConverter.convert(siteLatestDate));
1765 03 Dec 12 olle 1475     }
1765 03 Dec 12 olle 1476     // Add latest date for further transfer to other JSON object
1765 03 Dec 12 olle 1477     jsonStatistics.put(latestDateKey, date2StringConverter.convert(latestDate));
1765 03 Dec 12 olle 1478     // Add number of patients with no samples
1765 03 Dec 12 olle 1479     Integer numPatientsNoSamples = (Integer) jsonSitesCombined.get(patientNoSamplesKey);
1765 03 Dec 12 olle 1480     if (numPatientsNoSamples == null)
1765 03 Dec 12 olle 1481     {
1765 03 Dec 12 olle 1482       numPatientsNoSamples = 0;
1765 03 Dec 12 olle 1483     }
1765 03 Dec 12 olle 1484     jsonStatistics.put(patientNoSamplesKey, numPatientsNoSamples);
1765 03 Dec 12 olle 1485     // Add number of patients with both specimens and no specimens 
1765 03 Dec 12 olle 1486     Integer numPatientsSpecimensAndNoSpecimens = (Integer) jsonSitesCombined.get(patientSpecimenAndNoSpecimenKey);
1765 03 Dec 12 olle 1487     if (numPatientsSpecimensAndNoSpecimens == null)
1765 03 Dec 12 olle 1488     {
1765 03 Dec 12 olle 1489       numPatientsSpecimensAndNoSpecimens = 0;
1765 03 Dec 12 olle 1490     }
1765 03 Dec 12 olle 1491     jsonStatistics.put(patientSpecimenAndNoSpecimenKey, numPatientsSpecimensAndNoSpecimens);
1765 03 Dec 12 olle 1492     // Add number of patients with both blood samples, specimens, and no specimens
1765 03 Dec 12 olle 1493     Integer numPatientsBloodSamplesAndSpecimensAndNoSpecimens = (Integer) jsonSitesCombined.get(patientBloodSampleAndSpecimenAndNoSpecimenKey);
1765 03 Dec 12 olle 1494     if (numPatientsBloodSamplesAndSpecimensAndNoSpecimens == null)
1765 03 Dec 12 olle 1495     {
1765 03 Dec 12 olle 1496       numPatientsBloodSamplesAndSpecimensAndNoSpecimens = 0;
1765 03 Dec 12 olle 1497     }
1765 03 Dec 12 olle 1498     jsonStatistics.put(patientBloodSampleAndSpecimenAndNoSpecimenKey, numPatientsBloodSamplesAndSpecimensAndNoSpecimens);
1765 03 Dec 12 olle 1499     //
1765 03 Dec 12 olle 1500     jsonReport.put("statistics", jsonStatistics);        
2024 20 Sep 13 olle 1501     jsonReport.put("bloodSampleFilter", bloodSampleFilter);        
1765 03 Dec 12 olle 1502     jsonReport.put("beginDate", date2StringConverter.convert(startDate));
1765 03 Dec 12 olle 1503     jsonReport.put("endDate", date2StringConverter.convert(endDate));
1765 03 Dec 12 olle 1504     // Transfer latest date from jsonStatistics to jsonReport
1765 03 Dec 12 olle 1505     String latestDateStr = (String) jsonStatistics.get("latestDateKey");
1765 03 Dec 12 olle 1506     jsonReport.put("latestDate", latestDateStr);
1765 03 Dec 12 olle 1507     json.put("report", jsonReport);
3911 02 May 16 nicklas 1508     progress.display(100, "Done");
1765 03 Dec 12 olle 1509     return json;
1765 03 Dec 12 olle 1510   }
1765 03 Dec 12 olle 1511
3911 02 May 16 nicklas 1512   private JSONObject createMissingSampleDataReport(DbControl dc, JSONObject json, Date startDate, Date endDate, String sampleType, String bloodSampleFilter, ProgressReporter progress)
1772 06 Dec 12 olle 1513       throws ServletException, IOException 
2203 21 Jan 14 olle 1514   {
3911 02 May 16 nicklas 1515     ItemQuery<Sample> sampleQuery = Sample.getQuery();
3911 02 May 16 nicklas 1516     sampleQuery.joinPermanent(Hql.innerJoin(null, "creationEvent", "ce", true));
3911 02 May 16 nicklas 1517     // Select what sample subtype to include in database search 
3911 02 May 16 nicklas 1518     // Fetch Joining parent CASE and PATIENT should improve performance
3911 02 May 16 nicklas 1519     if (sampleType.equals("nospecimen"))
3911 02 May 16 nicklas 1520     {
3911 02 May 16 nicklas 1521       Subtype.NO_SPECIMEN.addFilter(dc, sampleQuery);
3911 02 May 16 nicklas 1522       sampleQuery.join(Hql.innerJoin(null, "parent", "cse", true));
3911 02 May 16 nicklas 1523       sampleQuery.join(Hql.innerJoin("cse", "parent", "pat", true));
3911 02 May 16 nicklas 1524     }
3911 02 May 16 nicklas 1525     else if (sampleType.equals("blood"))
3911 02 May 16 nicklas 1526     {
3911 02 May 16 nicklas 1527       Subtype.BLOOD.addFilter(dc, sampleQuery);
3911 02 May 16 nicklas 1528       sampleQuery.join(Hql.innerJoin(null, "parent", "pat", true));
3911 02 May 16 nicklas 1529     }
3911 02 May 16 nicklas 1530     else
3911 02 May 16 nicklas 1531     {
3911 02 May 16 nicklas 1532       Subtype.SPECIMEN.addFilter(dc, sampleQuery);
3911 02 May 16 nicklas 1533       sampleQuery.join(Hql.innerJoin(null, "parent", "cse", true));
3911 02 May 16 nicklas 1534       sampleQuery.join(Hql.innerJoin("cse", "parent", "pat", true));
3911 02 May 16 nicklas 1535     }
3911 02 May 16 nicklas 1536     sampleQuery.setIncludes(Reggie.INCLUDE_IN_CURRENT_PROJECT);
3911 02 May 16 nicklas 1537     sampleQuery.order(Orders.asc(Hql.property("name")));
3911 02 May 16 nicklas 1538     sampleQuery.order(Orders.asc(Hql.property("ce", "eventDate")));
3911 02 May 16 nicklas 1539     sampleQuery.setCacheResult(true);
3911 02 May 16 nicklas 1540     
3911 02 May 16 nicklas 1541     progress.display(1, "Loading samples...");
3911 02 May 16 nicklas 1542     long totalCount = sampleQuery.count(dc);
3911 02 May 16 nicklas 1543     long count = 0;
3911 02 May 16 nicklas 1544     
2203 21 Jan 14 olle 1545     JSONObject jsonReport = new JSONObject();
2611 29 Aug 14 nicklas 1546     jsonReport.put("sites", ReportTableUtil.getJSONSites(Site.SORT_BY_NAME));
1772 06 Dec 12 olle 1547     JSONObject jsonStatistics = new JSONObject();
1772 06 Dec 12 olle 1548     JSONObject jsonSitesCombined = new JSONObject();
1772 06 Dec 12 olle 1549     String missingPatientNameKey = "missingPatientName";
1772 06 Dec 12 olle 1550     String missingPadReferenceKey = "missingPadReference";
1772 06 Dec 12 olle 1551     String missingLateralityKey = "missingLaterality";
1772 06 Dec 12 olle 1552     String missingSamplingDateTimeKey = "missingSamplingDateTime";
1772 06 Dec 12 olle 1553     String missingRnaLaterDateTimeKey = "missingRnaLaterDateTime";
1772 06 Dec 12 olle 1554     String missingBloodSamplingDateTimeKey = "missingBloodSamplingDateTime";
1772 06 Dec 12 olle 1555     String missingBloodFreezerDateTimeKey = "missingBloodFreezerDateTime";
1772 06 Dec 12 olle 1556     String permissionDeniedForPatientNameKey = "permissionDeniedForPatientName";
2159 06 Dec 13 nicklas 1557     String permissionDeniedForPadKey = "permissionDeniedForPad";
1772 06 Dec 12 olle 1558     String sitesCombinedKey = "sitesCombinedKey";
1772 06 Dec 12 olle 1559     String latestDateKey = "latestDateKey";
1772 06 Dec 12 olle 1560     String duplicateKey = "duplicateKey";
1772 06 Dec 12 olle 1561     jsonStatistics.put(sitesCombinedKey, jsonSitesCombined);
1772 06 Dec 12 olle 1562     // Initialize site data to 0 for the different keys
1813 30 Jan 13 olle 1563     jsonStatistics = tableUtil.initializeJSONSiteData(jsonStatistics, missingPatientNameKey);
1813 30 Jan 13 olle 1564     jsonStatistics = tableUtil.initializeJSONSiteData(jsonStatistics, missingPadReferenceKey);
1813 30 Jan 13 olle 1565     jsonStatistics = tableUtil.initializeJSONSiteData(jsonStatistics, missingLateralityKey);
1813 30 Jan 13 olle 1566     jsonStatistics = tableUtil.initializeJSONSiteData(jsonStatistics, missingSamplingDateTimeKey);
1813 30 Jan 13 olle 1567     jsonStatistics = tableUtil.initializeJSONSiteData(jsonStatistics, missingRnaLaterDateTimeKey);
1813 30 Jan 13 olle 1568     jsonStatistics = tableUtil.initializeJSONSiteData(jsonStatistics, missingBloodSamplingDateTimeKey);
1813 30 Jan 13 olle 1569     jsonStatistics = tableUtil.initializeJSONSiteData(jsonStatistics, missingBloodFreezerDateTimeKey);
1772 06 Dec 12 olle 1570     // Initialize combined data to 0 for the different keys
1772 06 Dec 12 olle 1571     jsonSitesCombined.put(missingPatientNameKey, 0);
1772 06 Dec 12 olle 1572     jsonSitesCombined.put(missingPadReferenceKey, 0);
1772 06 Dec 12 olle 1573     jsonSitesCombined.put(missingLateralityKey, 0);
1772 06 Dec 12 olle 1574     jsonSitesCombined.put(missingSamplingDateTimeKey, 0);
1772 06 Dec 12 olle 1575     jsonSitesCombined.put(missingRnaLaterDateTimeKey, 0);
1772 06 Dec 12 olle 1576     jsonSitesCombined.put(missingBloodSamplingDateTimeKey, 0);
1772 06 Dec 12 olle 1577     jsonSitesCombined.put(missingBloodFreezerDateTimeKey, 0);
1772 06 Dec 12 olle 1578     // Initialize other data to 0 for different keys
1772 06 Dec 12 olle 1579     jsonStatistics.put(duplicateKey, 0);
1772 06 Dec 12 olle 1580     Date latestDate = null;
1772 06 Dec 12 olle 1581     // Create HashMap to keep track of latest item date for each site
1772 06 Dec 12 olle 1582     HashMap<String, Date> sitePrefixDateHashMap = new HashMap<String, Date>();
1772 06 Dec 12 olle 1583     // Get item subtype constants for later use
1772 06 Dec 12 olle 1584     ItemSubtype subtypeSpecimen = Subtype.SPECIMEN.load(dc);
1772 06 Dec 12 olle 1585     ItemSubtype subtypeNoSpecimen = Subtype.NO_SPECIMEN.load(dc);
1772 06 Dec 12 olle 1586     ItemSubtype subtypeBlood = Subtype.BLOOD.load(dc);
2159 06 Dec 13 nicklas 1587     boolean permissionDeniedForPatientName = false;
2159 06 Dec 13 nicklas 1588     try
2159 06 Dec 13 nicklas 1589     {
2159 06 Dec 13 nicklas 1590       Annotationtype.ALL_FIRST_NAMES.load(dc);
2159 06 Dec 13 nicklas 1591       Annotationtype.FAMILY_NAME.load(dc);
2159 06 Dec 13 nicklas 1592     }
2159 06 Dec 13 nicklas 1593     catch (RuntimeException ex)
2159 06 Dec 13 nicklas 1594     {
2159 06 Dec 13 nicklas 1595       permissionDeniedForPatientName = true;
2159 06 Dec 13 nicklas 1596     }
2159 06 Dec 13 nicklas 1597     boolean permissionDeniedForPad = false;
2159 06 Dec 13 nicklas 1598     try
2159 06 Dec 13 nicklas 1599     {
2159 06 Dec 13 nicklas 1600       Annotationtype.PAD.load(dc);
2159 06 Dec 13 nicklas 1601     }
2159 06 Dec 13 nicklas 1602     catch (RuntimeException ex)
2159 06 Dec 13 nicklas 1603     {
2159 06 Dec 13 nicklas 1604       permissionDeniedForPad = true;
2159 06 Dec 13 nicklas 1605     }
2159 06 Dec 13 nicklas 1606     
3911 02 May 16 nicklas 1607     
3911 02 May 16 nicklas 1608     ItemResultIterator<Sample> sampleIterator = sampleQuery.iterate(dc);
3911 02 May 16 nicklas 1609     
3911 02 May 16 nicklas 1610     // Use stored annotation snapshots for performance reasons
3911 02 May 16 nicklas 1611     SnapshotManager manager = new SnapshotManager();        
3911 02 May 16 nicklas 1612     while (sampleIterator.hasNext())
1772 06 Dec 12 olle 1613     {
3911 02 May 16 nicklas 1614       if (count % 100 == 0)
3911 02 May 16 nicklas 1615             {
3911 02 May 16 nicklas 1616         progress.display(5 + (int)((95 * count) / totalCount), 
3911 02 May 16 nicklas 1617             "Processing " + Reggie.formatCount(count) + " of " + Reggie.formatCount(totalCount) + " items");
3911 02 May 16 nicklas 1618             }
3911 02 May 16 nicklas 1619       count++;
3911 02 May 16 nicklas 1620       Sample s = sampleIterator.next();
3911 02 May 16 nicklas 1621       // Optional blood sample filter (true if no filter, sample not blood, or blood sample passes filter)
3911 02 May 16 nicklas 1622       boolean passedBloodSampleFilter = passBloodSampleFilter(dc, manager, bloodSampleFilter, s);
3911 02 May 16 nicklas 1623       if (passedBloodSampleFilter)
1772 06 Dec 12 olle 1624       {
3911 02 May 16 nicklas 1625         BioMaterialEvent creationEvent = s.getCreationEvent();
3911 02 May 16 nicklas 1626         Date creationDate = creationEvent.getEventDate();
1772 06 Dec 12 olle 1627
3911 02 May 16 nicklas 1628         // Find patient id
3911 02 May 16 nicklas 1629         BioMaterial patient = null;
3911 02 May 16 nicklas 1630         if (s.getItemSubtype().equals(subtypeBlood))
1772 06 Dec 12 olle 1631         {
3911 02 May 16 nicklas 1632           // The parent is the 'Patient' for blood samples and cases
3911 02 May 16 nicklas 1633           patient = s.getParent();
1772 06 Dec 12 olle 1634         }
3911 02 May 16 nicklas 1635         else if (s.getItemSubtype().equals(subtypeSpecimen) || s.getItemSubtype().equals(subtypeNoSpecimen))
2159 06 Dec 13 nicklas 1636         {
3911 02 May 16 nicklas 1637           // The grandparent is the 'Patient' for specimens and no specimens
3911 02 May 16 nicklas 1638           MeasuredBioMaterial parent = (MeasuredBioMaterial) s.getParent();
3911 02 May 16 nicklas 1639           if (parent != null)
3911 02 May 16 nicklas 1640           {
3911 02 May 16 nicklas 1641             patient = parent.getParent();
3911 02 May 16 nicklas 1642           }
2159 06 Dec 13 nicklas 1643         }
1772 06 Dec 12 olle 1644
3911 02 May 16 nicklas 1645         // Initialize sample data
3911 02 May 16 nicklas 1646         String allFirstNames = null;
3911 02 May 16 nicklas 1647         String familyName = null;
3911 02 May 16 nicklas 1648         String pad = null;
3911 02 May 16 nicklas 1649         String laterality = null;
3911 02 May 16 nicklas 1650         Date samplingDate = null;
3911 02 May 16 nicklas 1651         Date rnaLaterDate = null;
3911 02 May 16 nicklas 1652         Date bloodSamplingDate = null;
3911 02 May 16 nicklas 1653         Date bloodFreezerDate = null;
3911 02 May 16 nicklas 1654
3911 02 May 16 nicklas 1655         if (patient != null)
2159 06 Dec 13 nicklas 1656         {
3911 02 May 16 nicklas 1657           if (!permissionDeniedForPatientName)
1772 06 Dec 12 olle 1658           {
3911 02 May 16 nicklas 1659             // Get patient all first names
3911 02 May 16 nicklas 1660             allFirstNames = (String) Annotationtype.ALL_FIRST_NAMES.getAnnotationValue(dc, manager, patient);
3911 02 May 16 nicklas 1661             // Get patient family name
3911 02 May 16 nicklas 1662             familyName = (String) Annotationtype.FAMILY_NAME.getAnnotationValue(dc, manager, patient);
1772 06 Dec 12 olle 1663           }
1772 06 Dec 12 olle 1664         }
1772 06 Dec 12 olle 1665         if (sampleType.equals("specimen"))
1772 06 Dec 12 olle 1666         {
3911 02 May 16 nicklas 1667           // Get PAD reference
2159 06 Dec 13 nicklas 1668           if (!permissionDeniedForPad)
1772 06 Dec 12 olle 1669           {
3911 02 May 16 nicklas 1670             pad = (String) Annotationtype.PAD.getAnnotationValue(dc, manager, s);
1772 06 Dec 12 olle 1671           }
3911 02 May 16 nicklas 1672           // Get laterality
3911 02 May 16 nicklas 1673           laterality = (String) Annotationtype.LATERALITY.getAnnotationValue(dc, manager, s);
3911 02 May 16 nicklas 1674           // Get sampling date
3911 02 May 16 nicklas 1675           samplingDate = (Date) Annotationtype.SAMPLING_DATETIME.getAnnotationValue(dc, manager, s);
3911 02 May 16 nicklas 1676           // Get RNALater date
3911 02 May 16 nicklas 1677           rnaLaterDate = (Date) Annotationtype.RNALATER_DATETIME.getAnnotationValue(dc, manager, s);
1772 06 Dec 12 olle 1678         }
1772 06 Dec 12 olle 1679         else if (sampleType.equals("nospecimen"))
1772 06 Dec 12 olle 1680         {
3911 02 May 16 nicklas 1681           // Get PAD reference
2159 06 Dec 13 nicklas 1682           if (!permissionDeniedForPad)
1923 11 Apr 13 olle 1683           {
3911 02 May 16 nicklas 1684             pad = (String) Annotationtype.PAD.getAnnotationValue(dc, manager, s);
1923 11 Apr 13 olle 1685           }
3911 02 May 16 nicklas 1686           // Get laterality
3911 02 May 16 nicklas 1687           laterality = (String) Annotationtype.LATERALITY.getAnnotationValue(dc, manager, s);
3911 02 May 16 nicklas 1688           // Get sampling date
3911 02 May 16 nicklas 1689           samplingDate = (Date) Annotationtype.SAMPLING_DATETIME.getAnnotationValue(dc, manager, s);
3911 02 May 16 nicklas 1690         }
3911 02 May 16 nicklas 1691         else if (sampleType.equals("blood"))
3911 02 May 16 nicklas 1692         {
3911 02 May 16 nicklas 1693           // Get blood sampling date
3911 02 May 16 nicklas 1694           bloodSamplingDate = (Date) Annotationtype.BLOOD_SAMPLING_DATETIME.getAnnotationValue(dc, manager, s);
3911 02 May 16 nicklas 1695           // Get blood freezer date
3911 02 May 16 nicklas 1696           bloodFreezerDate = (Date) Annotationtype.BLOOD_FREEZER_DATETIME.getAnnotationValue(dc, manager, s);
3911 02 May 16 nicklas 1697         }
3911 02 May 16 nicklas 1698         // Update statistics for sample type
3911 02 May 16 nicklas 1699         Site site = Site.findByCaseName(s.getName());  
3911 02 May 16 nicklas 1700         if (site == Site.UNKNOWN)
3911 02 May 16 nicklas 1701         {
3911 02 May 16 nicklas 1702           String key = "unknownSite";
3911 02 May 16 nicklas 1703           jsonStatistics = tableUtil.updateJSONObjectCounter(jsonStatistics, key);
3911 02 May 16 nicklas 1704         }
3911 02 May 16 nicklas 1705         else
3911 02 May 16 nicklas 1706         {
3911 02 May 16 nicklas 1707           // Get site info            
3911 02 May 16 nicklas 1708           JSONObject jsonSite = (JSONObject)jsonStatistics.get(site.getPrefix());
3911 02 May 16 nicklas 1709           if (jsonSite == null)
1923 11 Apr 13 olle 1710           {
3911 02 May 16 nicklas 1711             jsonSite = new JSONObject();
3911 02 May 16 nicklas 1712             jsonStatistics.put(site.getPrefix(), jsonSite);
1923 11 Apr 13 olle 1713           }
3911 02 May 16 nicklas 1714
3911 02 May 16 nicklas 1715           if (patient != null && !permissionDeniedForPatientName)
1772 06 Dec 12 olle 1716           {
3911 02 May 16 nicklas 1717             // Update missing patient name data, provided the user has permission to check it
3911 02 May 16 nicklas 1718             if (allFirstNames == null || allFirstNames.equals("")
3911 02 May 16 nicklas 1719                 || familyName == null || familyName.equals(""))
3911 02 May 16 nicklas 1720             {
3911 02 May 16 nicklas 1721               jsonSite = tableUtil.updateJSONObjectCounter(jsonSite, missingPatientNameKey);
3911 02 May 16 nicklas 1722               jsonSitesCombined = tableUtil.updateJSONObjectCounter(jsonSitesCombined, missingPatientNameKey);
3911 02 May 16 nicklas 1723             }
1772 06 Dec 12 olle 1724           }
3911 02 May 16 nicklas 1725           if (sampleType.equals("specimen"))
1772 06 Dec 12 olle 1726           {
3911 02 May 16 nicklas 1727             // Update missing PAD reference data
3911 02 May 16 nicklas 1728             if (!permissionDeniedForPad)
3911 02 May 16 nicklas 1729             {
3911 02 May 16 nicklas 1730               if (pad == null || pad.equals(""))
3911 02 May 16 nicklas 1731               {
3911 02 May 16 nicklas 1732                 jsonSite = tableUtil.updateJSONObjectCounter(jsonSite, missingPadReferenceKey);
3911 02 May 16 nicklas 1733                 jsonSitesCombined = tableUtil.updateJSONObjectCounter(jsonSitesCombined, missingPadReferenceKey);
3911 02 May 16 nicklas 1734               }
3911 02 May 16 nicklas 1735             }
3911 02 May 16 nicklas 1736             // Update missing laterality data
3911 02 May 16 nicklas 1737             if (laterality == null || laterality.equals(""))
3911 02 May 16 nicklas 1738             {
3911 02 May 16 nicklas 1739               jsonSite = tableUtil.updateJSONObjectCounter(jsonSite, missingLateralityKey);
3911 02 May 16 nicklas 1740               jsonSitesCombined = tableUtil.updateJSONObjectCounter(jsonSitesCombined, missingLateralityKey);
3911 02 May 16 nicklas 1741             }
3911 02 May 16 nicklas 1742             // Update missing sampling date data
3911 02 May 16 nicklas 1743             if (samplingDate == null)
3911 02 May 16 nicklas 1744             {
3911 02 May 16 nicklas 1745               jsonSite = tableUtil.updateJSONObjectCounter(jsonSite, missingSamplingDateTimeKey);
3911 02 May 16 nicklas 1746               jsonSitesCombined = tableUtil.updateJSONObjectCounter(jsonSitesCombined, missingSamplingDateTimeKey);
3911 02 May 16 nicklas 1747             }
3911 02 May 16 nicklas 1748             // Update missing RNALater date data
3911 02 May 16 nicklas 1749             if (rnaLaterDate == null)
3911 02 May 16 nicklas 1750             {
3911 02 May 16 nicklas 1751               jsonSite = tableUtil.updateJSONObjectCounter(jsonSite, missingRnaLaterDateTimeKey);
3911 02 May 16 nicklas 1752               jsonSitesCombined = tableUtil.updateJSONObjectCounter(jsonSitesCombined, missingRnaLaterDateTimeKey);
3911 02 May 16 nicklas 1753             }
1772 06 Dec 12 olle 1754           }
3911 02 May 16 nicklas 1755           else if (sampleType.equals("nospecimen"))
1772 06 Dec 12 olle 1756           {
3911 02 May 16 nicklas 1757             // Update missing PAD reference data
3911 02 May 16 nicklas 1758             if (!permissionDeniedForPad)
3911 02 May 16 nicklas 1759             {
3911 02 May 16 nicklas 1760               if (pad == null || pad.equals(""))
3911 02 May 16 nicklas 1761               {
3911 02 May 16 nicklas 1762                 jsonSite = tableUtil.updateJSONObjectCounter(jsonSite, missingPadReferenceKey);
3911 02 May 16 nicklas 1763                 jsonSitesCombined = tableUtil.updateJSONObjectCounter(jsonSitesCombined, missingPadReferenceKey);
3911 02 May 16 nicklas 1764               }
3911 02 May 16 nicklas 1765             }
3911 02 May 16 nicklas 1766             // Update missing laterality data
3911 02 May 16 nicklas 1767             if (laterality == null || laterality.equals(""))
3911 02 May 16 nicklas 1768             {
3911 02 May 16 nicklas 1769               jsonSite = tableUtil.updateJSONObjectCounter(jsonSite, missingLateralityKey);
3911 02 May 16 nicklas 1770               jsonSitesCombined = tableUtil.updateJSONObjectCounter(jsonSitesCombined, missingLateralityKey);
3911 02 May 16 nicklas 1771             }
3911 02 May 16 nicklas 1772             // Update missing sampling date data
3911 02 May 16 nicklas 1773             if (samplingDate == null)
3911 02 May 16 nicklas 1774             {
3911 02 May 16 nicklas 1775               jsonSite = tableUtil.updateJSONObjectCounter(jsonSite, missingSamplingDateTimeKey);
3911 02 May 16 nicklas 1776               jsonSitesCombined = tableUtil.updateJSONObjectCounter(jsonSitesCombined, missingSamplingDateTimeKey);
3911 02 May 16 nicklas 1777             }
1772 06 Dec 12 olle 1778           }
3911 02 May 16 nicklas 1779           else if (sampleType.equals("blood"))
1772 06 Dec 12 olle 1780           {
3911 02 May 16 nicklas 1781             // Update missing blood sampling date data
3911 02 May 16 nicklas 1782             if (bloodSamplingDate == null)
3911 02 May 16 nicklas 1783             {
3911 02 May 16 nicklas 1784               jsonSite = tableUtil.updateJSONObjectCounter(jsonSite, missingBloodSamplingDateTimeKey);
3911 02 May 16 nicklas 1785               jsonSitesCombined = tableUtil.updateJSONObjectCounter(jsonSitesCombined, missingBloodSamplingDateTimeKey);
3911 02 May 16 nicklas 1786             }
3911 02 May 16 nicklas 1787             // Update missing blood freezer date data
3911 02 May 16 nicklas 1788             if (bloodFreezerDate == null)
3911 02 May 16 nicklas 1789             {
3911 02 May 16 nicklas 1790               jsonSite = tableUtil.updateJSONObjectCounter(jsonSite, missingBloodFreezerDateTimeKey);
3911 02 May 16 nicklas 1791               jsonSitesCombined = tableUtil.updateJSONObjectCounter(jsonSitesCombined, missingBloodFreezerDateTimeKey);
3911 02 May 16 nicklas 1792             }
1772 06 Dec 12 olle 1793           }
3911 02 May 16 nicklas 1794
3911 02 May 16 nicklas 1795           // Keep track of latest item date for site
3911 02 May 16 nicklas 1796           if (creationDate != null)
1772 06 Dec 12 olle 1797           {
3911 02 May 16 nicklas 1798             Date siteLatestDate = sitePrefixDateHashMap.get(site.getPrefix());
3911 02 May 16 nicklas 1799             if (siteLatestDate == null || siteLatestDate.before(creationDate))
3911 02 May 16 nicklas 1800             {
3911 02 May 16 nicklas 1801               sitePrefixDateHashMap.put(site.getPrefix(), creationDate);
3911 02 May 16 nicklas 1802             }
3911 02 May 16 nicklas 1803             // Keep track of latest item date so far
3911 02 May 16 nicklas 1804             if (latestDate == null || latestDate.before(creationDate)) 
3911 02 May 16 nicklas 1805             {
3911 02 May 16 nicklas 1806               latestDate = creationDate;
3911 02 May 16 nicklas 1807             }
1772 06 Dec 12 olle 1808           }
1772 06 Dec 12 olle 1809         }
3911 02 May 16 nicklas 1810         
1772 06 Dec 12 olle 1811       }
1772 06 Dec 12 olle 1812     }
3911 02 May 16 nicklas 1813
3911 02 May 16 nicklas 1814
1772 06 Dec 12 olle 1815     // Add latest item date for site to site JSON object
1772 06 Dec 12 olle 1816     DateToStringConverter date2StringConverter = Reggie.CONVERTER_DATE_TO_STRING;
1772 06 Dec 12 olle 1817     for (Site s: Site.getAllSites())
1772 06 Dec 12 olle 1818     {
1772 06 Dec 12 olle 1819       JSONObject jsonSite = (JSONObject) jsonStatistics.get(s.getPrefix());
1772 06 Dec 12 olle 1820       if (jsonSite == null)
1772 06 Dec 12 olle 1821       {
1772 06 Dec 12 olle 1822         jsonSite = new JSONObject();
1772 06 Dec 12 olle 1823         jsonStatistics.put(s.getPrefix(), jsonSite);
1772 06 Dec 12 olle 1824       }
1772 06 Dec 12 olle 1825       Date siteLatestDate = sitePrefixDateHashMap.get(s.getPrefix());
1772 06 Dec 12 olle 1826       jsonSite.put(latestDateKey, date2StringConverter.convert(siteLatestDate));
1772 06 Dec 12 olle 1827     }
1772 06 Dec 12 olle 1828     // Add latest date for further transfer to other JSON object
1772 06 Dec 12 olle 1829     jsonStatistics.put(latestDateKey, date2StringConverter.convert(latestDate));
1772 06 Dec 12 olle 1830     //
1772 06 Dec 12 olle 1831     jsonReport.put("statistics", jsonStatistics);        
1772 06 Dec 12 olle 1832     jsonReport.put("sampleType", sampleType);
2024 20 Sep 13 olle 1833     jsonReport.put("bloodSampleFilter", bloodSampleFilter);        
1772 06 Dec 12 olle 1834     jsonReport.put("beginDate", date2StringConverter.convert(startDate));
1772 06 Dec 12 olle 1835     jsonReport.put("endDate", date2StringConverter.convert(endDate));
1772 06 Dec 12 olle 1836     // Transfer latest date from jsonStatistics to jsonReport
1772 06 Dec 12 olle 1837     String latestDateStr = (String) jsonStatistics.get("latestDateKey");
1772 06 Dec 12 olle 1838     jsonReport.put("latestDate", latestDateStr);
2159 06 Dec 13 nicklas 1839     jsonReport.put(permissionDeniedForPatientNameKey, permissionDeniedForPatientName);
2159 06 Dec 13 nicklas 1840     jsonReport.put(permissionDeniedForPadKey, permissionDeniedForPad);
1772 06 Dec 12 olle 1841     json.put("report", jsonReport);
3911 02 May 16 nicklas 1842     progress.display(100, "Done");
1772 06 Dec 12 olle 1843     return json;
1772 06 Dec 12 olle 1844   }
1772 06 Dec 12 olle 1845
2203 21 Jan 14 olle 1846   /**
2203 21 Jan 14 olle 1847    * Optional blood sample filter. Returns 'true' if no filter,
2203 21 Jan 14 olle 1848    * sample not blood, or blood sample passes filter.
2203 21 Jan 14 olle 1849    * 
2203 21 Jan 14 olle 1850    * @param dc DbControl The DbControl to use.
2203 21 Jan 14 olle 1851    * @param manager SnapshotManager The annotation snapshot manager to use.
2203 21 Jan 14 olle 1852    * @param bloodSampleFilter String Optional blood sample filter.
2203 21 Jan 14 olle 1853    * @param s Sample The sample to test.
2203 21 Jan 14 olle 1854    * @return boolean Returns 'true' if no filter, sample not blood, or blood sample passes filter.
2203 21 Jan 14 olle 1855    */
2203 21 Jan 14 olle 1856   private boolean passBloodSampleFilter(DbControl dc, SnapshotManager manager, String bloodSampleFilter, Sample s)
2203 21 Jan 14 olle 1857   {
2203 21 Jan 14 olle 1858     boolean passedFilter = true;
2203 21 Jan 14 olle 1859     // Optional blood sample filter
2203 21 Jan 14 olle 1860     if (bloodSampleFilter != null && !bloodSampleFilter.equals(BLOOD_FILTER_NONE))
2203 21 Jan 14 olle 1861     {
2203 21 Jan 14 olle 1862       ItemSubtype subtypeBlood = Subtype.BLOOD.load(dc);
2203 21 Jan 14 olle 1863       if (s.getItemSubtype().equals(subtypeBlood))
2203 21 Jan 14 olle 1864       {
2203 21 Jan 14 olle 1865         // Get blood sample annotation
2203 21 Jan 14 olle 1866         String bloodSampleType = (String) Annotationtype.BLOOD_SAMPLE.getAnnotationValue(dc, manager, s);
2203 21 Jan 14 olle 1867         if (bloodSampleFilter.equals(BLOOD_FILTER_UNKNOWN))
2203 21 Jan 14 olle 1868         {
2203 21 Jan 14 olle 1869           // Blood sample type unknown (blood sample type == null should be included)
2203 21 Jan 14 olle 1870           if (bloodSampleType != null && !bloodSampleType.equals(bloodSampleFilter))
2203 21 Jan 14 olle 1871           {
2203 21 Jan 14 olle 1872             passedFilter = false;
2203 21 Jan 14 olle 1873           }
2203 21 Jan 14 olle 1874         }
2203 21 Jan 14 olle 1875         else if (bloodSampleFilter.equals(BLOOD_FILTER_FOLLOW_UP))
2203 21 Jan 14 olle 1876         {
2203 21 Jan 14 olle 1877           // Follow-up blood sample type (blood sample type == null should not be included)
2203 21 Jan 14 olle 1878           if (bloodSampleType == null ||
2203 21 Jan 14 olle 1879               !(bloodSampleType.equals(BLOOD_FILTER_FOLLOW_UP_06)
2203 21 Jan 14 olle 1880               || bloodSampleType.equals(BLOOD_FILTER_FOLLOW_UP_12)
2203 21 Jan 14 olle 1881               || bloodSampleType.equals(BLOOD_FILTER_FOLLOW_UP_36))
2203 21 Jan 14 olle 1882             )
2203 21 Jan 14 olle 1883           {
2203 21 Jan 14 olle 1884             passedFilter = false;
2203 21 Jan 14 olle 1885           }
2203 21 Jan 14 olle 1886         }
2203 21 Jan 14 olle 1887         else if (bloodSampleFilter.equals(BLOOD_FILTER_NOT_FOLLOW_UP))
2203 21 Jan 14 olle 1888         {
2203 21 Jan 14 olle 1889           // Non-follow-up blood sample type (blood sample type == null should be included)
2203 21 Jan 14 olle 1890           if (bloodSampleType != null &&
2203 21 Jan 14 olle 1891               (bloodSampleType.equals(BLOOD_FILTER_FOLLOW_UP_06)
2203 21 Jan 14 olle 1892               || bloodSampleType.equals(BLOOD_FILTER_FOLLOW_UP_12)
2203 21 Jan 14 olle 1893               || bloodSampleType.equals(BLOOD_FILTER_FOLLOW_UP_36))
2203 21 Jan 14 olle 1894             )
2203 21 Jan 14 olle 1895           {
2203 21 Jan 14 olle 1896             passedFilter = false;
2203 21 Jan 14 olle 1897           }
2203 21 Jan 14 olle 1898         }
2203 21 Jan 14 olle 1899         else
2203 21 Jan 14 olle 1900         {
2203 21 Jan 14 olle 1901           // Specific blood sample type (blood sample type == null should not be included)
2203 21 Jan 14 olle 1902           if (bloodSampleType == null || !bloodSampleType.equals(bloodSampleFilter))
2203 21 Jan 14 olle 1903           {
2203 21 Jan 14 olle 1904             passedFilter = false;
2203 21 Jan 14 olle 1905           }
2203 21 Jan 14 olle 1906         }
2203 21 Jan 14 olle 1907       }
2203 21 Jan 14 olle 1908     }
2203 21 Jan 14 olle 1909     return passedFilter;
2203 21 Jan 14 olle 1910   }
2203 21 Jan 14 olle 1911
1732 13 Nov 12 olle 1912   private JSONObject createJSONPatientsWithMultipleDates(HashMap<String, Set<Date>> stringDateSetHashMap)
1732 13 Nov 12 olle 1913   {
1732 13 Nov 12 olle 1914     JSONObject jsonPatientsWithMultipleDates = new JSONObject();
1732 13 Nov 12 olle 1915     if (stringDateSetHashMap != null)
1732 13 Nov 12 olle 1916     {
1732 13 Nov 12 olle 1917       Set<String> patientNameSet = stringDateSetHashMap.keySet();
1732 13 Nov 12 olle 1918       if (patientNameSet != null)
1732 13 Nov 12 olle 1919       {
1732 13 Nov 12 olle 1920         for (String patient: patientNameSet)
1732 13 Nov 12 olle 1921         {
1732 13 Nov 12 olle 1922           Set<Date> dateSet = stringDateSetHashMap.get(patient);
1732 13 Nov 12 olle 1923           if (dateSet != null && dateSet.size() > 1)
1732 13 Nov 12 olle 1924           {
1732 13 Nov 12 olle 1925             // More than one date
1732 13 Nov 12 olle 1926             JSONObject jsonMultipleDates = new JSONObject();
1732 13 Nov 12 olle 1927             int dateCounter = 0;
1732 13 Nov 12 olle 1928             for (Date date: dateSet)
1732 13 Nov 12 olle 1929             {
1732 13 Nov 12 olle 1930               if (date != null)
1732 13 Nov 12 olle 1931               {
7024 07 Feb 23 nicklas 1932                 DateToStringConverter converter = new DateToStringConverter(FastDateFormat.getInstance("yyyy-MM-dd"));
1732 13 Nov 12 olle 1933                 String dateStr = converter.convert(date);
5319 28 Feb 19 nicklas 1934                 jsonMultipleDates.put(Integer.toString(dateCounter), dateStr);
1732 13 Nov 12 olle 1935                 dateCounter++;
1732 13 Nov 12 olle 1936               }
1732 13 Nov 12 olle 1937             }
1732 13 Nov 12 olle 1938             jsonPatientsWithMultipleDates.put(patient, jsonMultipleDates);
1732 13 Nov 12 olle 1939           }
1732 13 Nov 12 olle 1940         }
1732 13 Nov 12 olle 1941       }
1732 13 Nov 12 olle 1942     }
1732 13 Nov 12 olle 1943     return jsonPatientsWithMultipleDates;
1732 13 Nov 12 olle 1944   }
1472 23 Nov 11 martin 1945 }