Changeset 1744


Ignore:
Timestamp:
Nov 27, 2012, 12:46:18 PM (9 years ago)
Author:
olle
Message:

Fixes #433. First version of the patient count report generator added.

Location:
extensions/net.sf.basedb.reggie/trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • extensions/net.sf.basedb.reggie/trunk/resources/index.jsp

    r1732 r1744  
    271271            <li>Sample count report</li>
    272272            <li>Consent count report</li>
     273            <li>Patient count report</li>
    273274            </ul>
    274275          </dd>
  • extensions/net.sf.basedb.reggie/trunk/resources/samplereportgenerator.jsp

    r1743 r1744  
    6767      // Set item name for report period text
    6868      document.getElementById("items01").innerHTML="tubes";
     69      // Set item name for report time step text
     70      document.getElementById("items02").innerHTML="specimens";
     71      document.getElementById("items03").innerHTML="specimens";
     72      document.getElementById("items04").innerHTML="specimens";
     73      document.getElementById("items05").innerHTML="specimens";
    6974      // Show view type pop-up menu
    7075      document.getElementById("viewTypeSubSection01").style.display = 'block';
     
    8085      document.getElementById("viewTypeSubSection02").style.display = 'none';
    8186      document.getElementById("viewTypeSubSection04").style.display = 'none';
     87    }
     88    else if (frm.reporttype[frm.reporttype.selectedIndex].value == 'patientcount')
     89    {
     90      // Set item name for report period text
     91      document.getElementById("items01").innerHTML="patients";
     92      // Set item name for report time step text
     93      document.getElementById("items02").innerHTML="patients";
     94      document.getElementById("items03").innerHTML="patients";
     95      document.getElementById("items04").innerHTML="patients";
     96      document.getElementById("items05").innerHTML="patients";
     97      // Show view type pop-up menu
     98      document.getElementById("viewTypeSubSection01").style.display = 'block';
     99      document.getElementById("viewTypeSubSection02").style.display = 'block';
     100      document.getElementById("viewTypeSubSection04").style.display = 'block';
    82101    }
    83102    Main.show('itemCountSection');
     
    150169      frm.viewtype.disabled = true;
    151170    }
     171    if (reportType == 'patientcount')
     172    {
     173      frm.viewtype.disabled = true;
     174    }
    152175    Main.hide('gocreate');
    153176    Main.show('reportSection');
     
    160183      if (frm.viewtype.value != null) url += '&vtype='+frm.viewtype.value;
    161184    }
     185    if (reportType == 'patientcount')
     186    {     
     187      if (frm.viewtype.value != null) url += '&vtype='+frm.viewtype.value;
     188    }
    162189   
    163190    var request = Ajax.getXmlHttpRequest();
     
    179206      if ('samplecount' == reportType)
    180207      {     
    181         reportTable = createSampleCountReport(report);
     208        reportTable = createItemCountReport(report, reportType);
    182209      }
    183210      else if ('consentcount' == reportType)
    184211      {     
    185212        reportTable = createConsentCountReport(report);
     213      }
     214      if ('patientcount' == reportType)
     215      {     
     216        reportTable = createItemCountReport(report, reportType);
    186217      }
    187218    }
     
    240271      summaryList.appendChild(getListElement(patientsWithMultipleDatesText));
    241272    }
     273    else if ('patientcount' == reportType)
     274    {     
     275      summaryList.appendChild(getListElement(unknownSite + ' patients registered to unknown sites.'));
     276      summaryList.appendChild(getListElement(unknownCreation + ' patients without creation date. These are included in the \'Total\' column.'));
     277    }
    242278    cellElement.appendChild(summaryList);
    243279    if ('consentcount' == reportType)
     
    250286  }
    251287 
    252   function createSampleCountReport(report)
     288  function createItemCountReport(report, reportType)
    253289  {
    254290    var reportTable = getReportTable();
     
    284320    var columnHeaderRow = document.createElement('tr');
    285321   
    286     var headerText = '# Specimen by ';
     322    var headerText = '# Item by ';
     323    if ('samplecount' == reportType)
     324    {
     325      headerText = '# Specimen by ';
     326    }
     327    else if ('patientcount' == reportType)
     328    {
     329      headerText = '# Patients by ';
     330    }
    287331    if (viewType == 'WEEK')
    288332    {
     
    435479    }
    436480    // Get total number of samples for site for the selected time period
    437     var sum = statistics.sumKey;
     481    var sum = getJSONData(statistics, 'sumKey', 0);
    438482    // Add entry with total number of samples for site for the selected time period
    439483    sitesCombinedRow.appendChild(getTableCellElement(sum, 'colsummary'));
    440484    // Get total number of samples for all sites, regardless of creation date
    441     var total = statistics.totalKey;
     485    var total = getJSONData(statistics, 'totalKey', 0);
    442486    // Add entry with total number of samples for all sites, regardless of creation date
    443487    sitesCombinedRow.appendChild(getTableCellElement(total, 'colsummary'));
     
    557601    }
    558602    // Add entry with total number of consents with chosen restrictions
    559     data = getJSONData(sitesCombined, sumKey);
     603    data = getJSONData(sitesCombined, sumKey, 0);
    560604    sitesCombinedRow.appendChild(getTableCellElement(data, 'colsummary'));
    561605    // Add entry with total number of consents, regardless of restrictions
    562606    var totalKey = 'totalKey';
    563     data = getJSONData(sitesCombined, totalKey);
     607    data = getJSONData(sitesCombined, totalKey, 0);
    564608    sitesCombinedRow.appendChild(getTableCellElement(data, 'colsummary'));
    565609    reportTable.appendChild(sitesCombinedRow); 
     
    633677    // Add entry with total number of consents with chosen restrictions
    634678    var noDateKey = 'noDate';
    635     data = getJSONData(sitesCombined, noDateKey);
     679    data = getJSONData(sitesCombined, noDateKey, 0);
    636680    sitesCombinedRow.appendChild(getTableCellElement(data, 'colsummary'));
    637681    // Add entry with total number of consents, regardless of restrictions
    638682    var totalKey = 'totalKey';
    639     data = getJSONData(sitesCombined, totalKey);
     683    data = getJSONData(sitesCombined, totalKey, 0);
    640684    sitesCombinedRow.appendChild(getTableCellElement(data, 'colsummary'));
    641685    reportTable.appendChild(sitesCombinedRow); 
     
    10591103      var siteData = statistics[namePrefix];
    10601104      var siteLatestDate = siteData['latestDateKey'];
     1105      var siteLatestDateStr = '????-??-??';
     1106      if (siteLatestDate != null)
     1107      {
     1108        var siteLatestDateYear = siteLatestDate.substr(0,4);
     1109        var siteLatestDateMonth = siteLatestDate.substr(4,2);
     1110        var siteLatestDateDate = siteLatestDate.substr(6,2);
     1111        siteLatestDateStr = siteLatestDateYear + '-' + siteLatestDateMonth + '-' + siteLatestDateDate;
     1112      }
     1113/*
    10611114      var siteLatestDateYear = siteLatestDate.substr(0,4);
    10621115      var siteLatestDateMonth = siteLatestDate.substr(4,2);
    10631116      var siteLatestDateDate = siteLatestDate.substr(6,2);
    10641117      var siteLatestDateStr = siteLatestDateYear + '-' + siteLatestDateMonth + '-' + siteLatestDateDate;
     1118*/
    10651119      var tableRow = document.createElement('tr');
    10661120      var tableCol = getTableCellElement(siteName, 'rowtitle');
     
    13081362                <option value="samplecount" selected="yes">Sample count report</option>
    13091363                <option value="consentcount">Consent count report</option>
     1364                <option value="patientcount">Patient count report</option>
    13101365              </select>
    13111366            </td>
     
    13701425                  Auto will adjust the report depending on the size of the period.
    13711426                  <ul>
    1372                     <li>Period shorter then 3 months - specimens per week</li>
    1373                     <li>Period shorter then 13 months - specimens per month</li>
    1374                     <li>Period shorter then 3 year - specimens per quarter</li>
    1375                     <li>Period is 3 year or greater - specimens per year</li>
     1427                    <li>Period shorter then 3 months - <text id="items02">items</text> per week</li>
     1428                    <li>Period shorter then 13 months - <text id="items03">items</text> per month</li>
     1429                    <li>Period shorter then 3 year - <text id="items04">items</text> per quarter</li>
     1430                    <li>Period is 3 year or greater - <text id="items05">items</text> per year</li>
    13761431                  </ul>                 
    13771432                </div>
  • extensions/net.sf.basedb.reggie/trunk/src/net/sf/basedb/reggie/servlet/SampleReportServlet.java

    r1738 r1744  
    1010import java.util.List;
    1111import java.util.HashSet;
     12import java.util.Iterator;
    1213import java.util.Set;
    1314import java.util.TreeMap;
     
    2425import net.sf.basedb.core.ItemQuery;
    2526import net.sf.basedb.core.ItemResultIterator;
     27import net.sf.basedb.core.ItemSubtype;
     28import net.sf.basedb.core.MeasuredBioMaterial;
    2629import net.sf.basedb.core.Sample;
    2730import net.sf.basedb.core.SessionControl;
     
    148151        json = createConsentCountReport(dc, json, startDate, endDate);
    149152      }
     153      else if ("patientcount".equals(cmd))
     154      {       
     155        String startDateParameter = Values.getString(req.getParameter("fdate"), null);
     156        String endDateParameter = Values.getString(req.getParameter("tdate"), null);
     157
     158        Date startDate = Reggie.CONVERTER_STRING_TO_DATE.convert(startDateParameter);
     159        Date endDate = Reggie.CONVERTER_STRING_TO_DATE.convert(endDateParameter);
     160       
     161        if (startDate == null)
     162        {
     163          // Get the when the first site started
     164          for (Site s : Site.getAllSites())
     165          {
     166            Date siteDate = Reggie.CONVERTER_STRING_TO_DATE.convert(s.getStartDate().replaceAll("-", ""));
     167            if (siteDate != null && (startDate == null || startDate.after(siteDate)))
     168            {
     169              startDate = siteDate;           
     170            }
     171          }
     172        }
     173        if (endDate == null)
     174        {
     175          // Get the date for today
     176          endDate = new Date();
     177        }
     178
     179        String viewType = null;
     180        String viewTypeParameter = Values.getString(req.getParameter("vtype"), null);
     181        if (viewTypeParameter != null && !viewTypeParameter.equals("AUTO"))
     182        {
     183          viewType = viewTypeParameter;
     184        }
     185        else
     186        {
     187          viewType = getViewType(startDate, endDate);
     188        }
     189
     190        json = createPatientCountReport(dc, json, startDate, endDate, viewType);
     191      }
    150192    }
    151193    catch (Throwable t)
     
    179221    sampleQuery.setCacheResult(true);
    180222    ItemResultIterator<Sample> sampleIterator = sampleQuery.iterate(dc);       
     223    // Store samples that should be processed for report table in list
     224    List<Sample> sampleList = new ArrayList<Sample>();
     225    while (sampleIterator.hasNext())
     226    {
     227      Sample s = sampleIterator.next();
     228      sampleList.add(s);
     229    }
    181230                 
    182     JSONObject jsonStatistics = createJSONStatistics(sampleIterator, startDate, endDate, viewType);
     231    JSONObject jsonStatistics = createJSONStatistics(sampleList.iterator(), startDate, endDate, viewType);
     232    jsonReport.put("statistics", jsonStatistics);       
     233    if (viewType == null)
     234    {
     235      viewType = getViewType(startDate, endDate);
     236    }
     237    DateToStringConverter date2StringConverter = Reggie.CONVERTER_DATE_TO_STRING;
     238    Date periodStartDate = calculatePeriodStartDate(startDate, viewType);
     239    jsonReport.put("viewType", viewType);
     240    jsonReport.put("beginDate", date2StringConverter.convert(startDate));
     241    jsonReport.put("endDate", date2StringConverter.convert(endDate));
     242    jsonReport.put("periodBeginDate", date2StringConverter.convert(periodStartDate));
     243    // Transfer latest date from jsonStatistics to jsonReport
     244    String latestDateStr = (String) jsonStatistics.get("latestDateKey");
     245    jsonReport.put("latestDate", latestDateStr);
     246    String siteOrder = alphabeticalOrder;
     247    JSONObject jsonSiteOrderList = createJSONSiteOrderList(siteOrder);
     248    jsonReport.put("siteOrderListKey", jsonSiteOrderList);
     249    json.put("report", jsonReport);
     250    return json;
     251  }
     252   
     253  private JSONObject createPatientCountReport(DbControl dc, JSONObject json, Date startDate, Date endDate, String viewType)
     254    throws ServletException, IOException
     255  {
     256    JSONObject jsonReport = new JSONObject();
     257    jsonReport.put("sites", getJSONSites());
     258   
     259    ItemQuery<Sample> sampleQuery = Sample.getQuery();
     260    sampleQuery.joinPermanent(Hql.innerJoin(null, "creationEvent", "ce", true));
     261    // ...only include 'Specimen', 'No Specimen', or 'Blood' items
     262    sampleQuery.restrict(
     263      Restrictions.or(
     264        Subtype.SPECIMEN.restriction(dc, null),
     265        Subtype.NO_SPECIMEN.restriction(dc, null),
     266        Subtype.BLOOD.restriction(dc, null)
     267      ));
     268    sampleQuery.setIncludes(Reggie.INCLUDE_IN_CURRENT_PROJECT);
     269    sampleQuery.order(Orders.asc(Hql.property("name")));
     270    sampleQuery.order(Orders.asc(Hql.property("ce", "eventDate")));
     271    sampleQuery.setCacheResult(true);
     272    ItemResultIterator<Sample> sampleIterator = sampleQuery.iterate(dc);       
     273                 
     274    // Store samples that should be processed for report table in list
     275    List<Sample> sampleList = new ArrayList<Sample>();
     276    // Create HashMap to keep track of sample with earliest date for each patient
     277    HashMap<String, Sample>patientSampleHashMap = new HashMap<String, Sample>();
     278    // Create HashMap to keep track of sample with unknown date for each patient
     279    HashMap<String, Sample>patientSampleNoDateHashMap = new HashMap<String, Sample>();
     280    // Find earliest sample dates for each patient
     281    ItemSubtype subtypeBlood = Subtype.BLOOD.load(dc);
     282    while (sampleIterator.hasNext())
     283    {
     284      Sample s = sampleIterator.next();
     285      BioMaterialEvent creationEvent = s.getCreationEvent();
     286      Date creationDate = creationEvent.getEventDate();
     287
     288      // Find patient id
     289      BioMaterial patient = null;
     290      if (s.getItemSubtype().equals(subtypeBlood))
     291      {
     292        // The parent is the 'Patient' for blood samples
     293        patient = s.getParent();
     294      }
     295      else
     296      {
     297        // The grandparent is the 'Patient' for specimens and no specimens
     298        MeasuredBioMaterial parent = (MeasuredBioMaterial) s.getParent();
     299        if (parent != null)
     300        {
     301          patient = parent.getParent();
     302        }
     303      }
     304      if (patient != null)
     305      {
     306        String patientName = patient.getName();
     307        if (creationDate != null)
     308        {
     309          patientSampleHashMap = updateStringEarliestSampleHashMap(patientSampleHashMap, patientName, s);
     310        }
     311        else
     312        {
     313          patientSampleNoDateHashMap.put(patientName, s);
     314        }
     315      }
     316    }
     317    // Create list of samples for report
     318    // Add sample with earliest date for patient
     319    for (String key: patientSampleHashMap.keySet())
     320    {
     321      if (key != null && !key.equals(""))
     322      {
     323        Sample s = patientSampleHashMap.get(key);
     324        // Include sample in list of samples for report
     325        sampleList.add(s);
     326      }
     327    }
     328    // Add sample with unknown date if no sample with date exists for patient
     329    for (String key: patientSampleNoDateHashMap.keySet())
     330    {
     331      if (key != null && !key.equals(""))
     332      {
     333        // If no sample with date exists for sample, include sample without date
     334        if (!patientSampleHashMap.containsKey(key))
     335        {
     336          Sample s = patientSampleNoDateHashMap.get(key);
     337          // Include sample in list of samples for report
     338          sampleList.add(s);
     339        }
     340      }
     341    }
     342
     343    JSONObject jsonStatistics = createJSONStatistics(sampleList.iterator(), startDate, endDate, viewType);
    183344    jsonReport.put("statistics", jsonStatistics);       
    184345    if (viewType == null)
     
    534695  }
    535696
    536   private int numDatesInStringDateSetHashMap(HashMap<String, Set<Date>> stringDateSetHashMap)
    537   {
    538     int numDates = 0;
    539     if (stringDateSetHashMap != null)
    540     {
    541       Set<String> stringSet = stringDateSetHashMap.keySet();
    542       if (stringSet != null)
    543       {
    544         for (String string: stringSet)
    545         {
    546           Set<Date> dateSet = stringDateSetHashMap.get(string);
    547           if (dateSet != null)
    548           {
    549             numDates += dateSet.size();
    550           }
    551         }
    552       }
    553     }
    554     return numDates;
    555   }
    556 
    557   private Set<String> keysWithMultipleDates(HashMap<String, Set<Date>> stringDateSetHashMap)
    558   {
    559     Set<String> keysWithMultipleDatesSet = new HashSet<String>();
    560     if (stringDateSetHashMap != null)
    561     {
    562       Set<String> stringSet = stringDateSetHashMap.keySet();
    563       if (stringSet != null)
    564       {
    565         for (String string: stringSet)
    566         {
    567           Set<Date> dateSet = stringDateSetHashMap.get(string);
    568           if (dateSet != null && dateSet.size() > 1)
    569           {
    570             keysWithMultipleDatesSet.add(string);
    571           }
    572         }
    573       }
    574     }
    575     return keysWithMultipleDatesSet;
     697  private HashMap<String, Sample> updateStringEarliestSampleHashMap(HashMap<String, Sample> stringSampleHashMap, String string, Sample sample)
     698  {
     699    if (stringSampleHashMap == null)
     700    {
     701      stringSampleHashMap = new HashMap<String, Sample>();
     702    }
     703    if (string != null && !string.equals(""))
     704    {
     705      // Store the sample with the earliest date for the item defined by string
     706      Sample storedSample = stringSampleHashMap.get(string);
     707      Date sampleDate = null;
     708      Date storedSampleDate = null;
     709      if (sample != null)
     710      {
     711        BioMaterialEvent creationEvent = sample.getCreationEvent();
     712        sampleDate = creationEvent.getEventDate();
     713      }
     714      if (storedSample != null)
     715      {
     716        BioMaterialEvent creationEvent = storedSample.getCreationEvent();
     717        storedSampleDate = creationEvent.getEventDate();
     718      }
     719      if (sampleDate != null)
     720      {
     721        if (storedSampleDate != null)
     722        {
     723          if (sampleDate.before(storedSampleDate))
     724          {
     725            // New sample date earlier, store that sample for patient
     726            stringSampleHashMap.put(string, sample);
     727          }
     728        }
     729        else
     730        {
     731          // No stored sample; store new sample for patient
     732          stringSampleHashMap.put(string, sample);
     733        }
     734      }
     735    }
     736    return stringSampleHashMap;
    576737  }
    577738
     
    610771  }
    611772
    612   private JSONObject createJSONStatistics(ItemResultIterator<Sample> sampleIterator, Date startDate, Date endDate, String viewType)
     773  private JSONObject createJSONStatistics(Iterator<Sample> sampleIterator, Date startDate, Date endDate, String viewType)
    613774    throws ServletException, IOException
    614775  {
Note: See TracChangeset for help on using the changeset viewer.