Changeset 1727


Ignore:
Timestamp:
Oct 23, 2012, 8:18:20 AM (9 years ago)
Author:
olle
Message:

Fixes #421. Bug fixes for problems with different period views.

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

Legend:

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

    r1726 r1727  
    187187    var sdString = report.beginDate;
    188188    var edString = report.endDate;
     189    var psdString = report.periodBeginDate;
    189190    var ldString = report.latestDate;
    190191    var startDate = new Date();
     
    196197    endDate.setMonth(edString.substr(4,2)-1);
    197198    endDate.setDate(edString.substr(6));
     199    var periodStartDate = new Date();
     200    periodStartDate.setYear(psdString.substr(0,4));
     201    periodStartDate.setMonth(psdString.substr(4,2)-1);
     202    periodStartDate.setDate(psdString.substr(6));
    198203    var latestDate;
    199204    if (ldString != null)
     
    214219    if (viewType == 'WEEK')
    215220    {
    216       var startWeek = getWeekByDate(startDate);
    217       var endWeek = getWeekByDate(endDate);
    218       var tempDate = new Date(startDate.getFullYear(), startDate.getMonth(), startDate.getDate()-startDate.getDay()+1);
     221      var startWeek = getISOWeekNumber(periodStartDate);
     222      var endWeek = getISOWeekNumber(endDate);
     223      var tempDate = new Date(periodStartDate.getFullYear(), periodStartDate.getMonth(), periodStartDate.getDate()-periodStartDate.getDay()+1);
    219224      numCols = 0;
    220225      while (tempDate < endDate)
     
    227232    else if (viewType == 'MONTH')
    228233    {
    229       numCols = (endDate.getFullYear()-startDate.getFullYear())*12 + endDate.getMonth()-startDate.getMonth()+1;
     234      numCols = (endDate.getFullYear()-periodStartDate.getFullYear())*12 + endDate.getMonth()-periodStartDate.getMonth()+1;
    230235      headerText += 'month';
    231236    }
     
    233238    {
    234239      //First year
    235       numCols = 4 - Math.floor(startDate.getMonth()/3);
     240      numCols = 4 - Math.floor(periodStartDate.getMonth()/3);
    236241      numCols += Math.floor(endDate.getMonth()/3)+1;
    237       if((endDate.getFullYear() - startDate.getFullYear()) > 1) numCols += 4 * (endDate.getFullYear() - startDate.getFullYear()-1);
     242      if((endDate.getFullYear() - periodStartDate.getFullYear()) > 1)
     243      {
     244        numCols += 4 * (endDate.getFullYear() - periodStartDate.getFullYear()-1);
     245      }
    238246      headerText += 'quarter';
    239247    }
    240248    else
    241249    {
    242       numCols = endDate.getFullYear() - startDate.getFullYear() + 1;
     250      numCols = endDate.getFullYear() - periodStartDate.getFullYear() + 1;
    243251      headerText += 'year';
    244252    }
     
    247255    // Set table header
    248256    headerText += ' (between ' + startDate.getFullYear()+'-';
    249     if ((startDate.getMonth()+1) < 10) headerText += '0' + (startDate.getMonth()+1);
    250     else headerText += (startDate.getMonth()+1);
     257    if ((startDate.getMonth()+1) < 10)
     258    {
     259      headerText += '0' + (startDate.getMonth()+1);
     260    }
     261    else
     262    {
     263      headerText += (startDate.getMonth()+1);
     264    }
    251265    headerText += '-';
    252     if (startDate.getDate() < 10) headerText += '0' + startDate.getDate();
    253     else headerText += startDate.getDate();
     266    if (startDate.getDate() < 10)
     267    {
     268      headerText += '0' + startDate.getDate();
     269    }
     270    else
     271    {
     272      headerText += startDate.getDate();
     273    }
    254274    headerText += ' and ';
    255275    headerText += endDate.getFullYear()+'-';
    256     if ((endDate.getMonth()+1) < 10) headerText += '0' + (endDate.getMonth()+1);
    257     else headerText += (endDate.getMonth()+1);
     276    if ((endDate.getMonth()+1) < 10)
     277    {
     278      headerText += '0' + (endDate.getMonth()+1);
     279    }
     280    else
     281    {
     282      headerText += (endDate.getMonth()+1);
     283    }
    258284    headerText += '-';
    259     if (endDate.getDate() < 10) headerText += '0' + endDate.getDate();
    260     else headerText += endDate.getDate();
     285    if (endDate.getDate() < 10)
     286    {
     287      headerText += '0' + endDate.getDate();
     288    }
     289    else
     290    {
     291      headerText += endDate.getDate();
     292    }
    261293    headerText += ')';
    262294    if (latestDate != null)
     
    264296      headerText += '\nLast registration ' + latestDate.getFullYear();
    265297      headerText += '-';
    266       if ((latestDate.getMonth()+1) < 10) headerText += '0';
     298      if ((latestDate.getMonth()+1) < 10)
     299      {
     300        headerText += '0';
     301      }
    267302      headerText += (latestDate.getMonth()+1);
    268303      headerText += '-';
    269       if (latestDate.getDate()<10) headerText += '0';
     304      if (latestDate.getDate()<10)
     305      {
     306        headerText += '0';
     307      }
    270308      headerText += latestDate.getDate();
    271309    }   
     
    277315    {
    278316      subHeaderRowYear.appendChild(getTableCellElement('', 'reportsubheader', 2));
    279       addYearSubHeaders(startDate, endDate, subHeaderRowYear, viewType);     
    280       subHeaderRowYear.appendChild(getTableCellElement('', 'reportsubheader'));
     317      addYearSubHeaders(periodStartDate, endDate, subHeaderRowYear, viewType);     
     318      subHeaderRowYear.appendChild(getTableCellElement('', 'reportsubheader', 2));
    281319    }   
    282320         
     
    287325    columnHeaderRow.appendChild(startDateHeader);
    288326   
    289     if (viewType == 'MONTH') addMonthColumnHeaders(columnHeaderRow, startDate);
    290     else if (viewType == 'WEEK') addWeekColumnHeaders(columnHeaderRow, startDate);
    291     else if (viewType == 'QUARTER') addQuarterColumnHeaders(columnHeaderRow, startDate);
    292     else if (viewType == 'YEAR') addYearSubHeaders(startDate, endDate, columnHeaderRow, viewType);
     327    if (viewType == 'MONTH') addMonthColumnHeaders(columnHeaderRow, periodStartDate);
     328    else if (viewType == 'WEEK') addWeekColumnHeaders(columnHeaderRow, periodStartDate);
     329    else if (viewType == 'QUARTER') addQuarterColumnHeaders(columnHeaderRow, periodStartDate);
     330    else if (viewType == 'YEAR') addYearSubHeaders(periodStartDate, endDate, columnHeaderRow, viewType);
    293331   
    294332   
     
    313351      if (sitesCombined[key] != '') gt += sitesCombined[key];
    314352    }
    315     sitesCombinedRow.appendChild(getTableCellElement(gt, 'colsummary'));
     353    sitesCombinedRow.appendChild(getTableCellElement(gt,'colsummary'));
    316354    reportTable.appendChild(sitesCombinedRow); 
     355
    317356    return reportTable;
    318357  }
     
    339378    for (var i=0;i<numCols;i++)
    340379    {     
    341       var weekNum = getWeekByDate(currentDate);
     380      var weekNum = getISOWeekNumber(currentDate);
    342381      var columnText = weekNum;
    343382      if (weekNum < 10) columnText = '0'+columnText;     
     
    390429        else if (currentYear == startDate.getFullYear())
    391430        {
    392           var startWeek = getWeekByDate(startDate);
     431          var startWeek = getISOWeekNumber(startDate);
    393432          var lastDay = new Date(startDate.getFullYear(), 11, 31);
    394           var endWeek = getWeekByDate(lastDay);
    395           if (endWeek == 1) endWeek = getWeekByDate(lastDay.setDate(lastDay.getDate()-7));
     433          var endWeek = getISOWeekNumber(lastDay);
     434          if (endWeek == 1)
     435          {
     436            lastDay.setDate(lastDay.getDate()-7);
     437            endWeek = getISOWeekNumber(lastDay);
     438          }
    396439          colspan = endWeek - startWeek +1;
    397440        }
    398441        else if (currentYear == endDate.getFullYear())
    399442        {
    400           var endWeek = getWeekByDate(endDate);
     443          var endWeek = getISOWeekNumber(endDate);
    401444          colspan = endWeek;
    402445        }
     
    404447        {
    405448          var dateInLastWeek = new Date(currentYear, 11, 31);
    406           var endWeek = getWeekByDate(dateInLastWeek);
    407           if (endWeek == 1) endWeek = getWeekByDate(dateInLastWeek.setDate(dateInLastWeek.getDate()-7));
     449          var endWeek = getISOWeekNumber(dateInLastWeek);
     450          if (endWeek == 1)
     451          {
     452            dateInLastWeek.setDate(dateInLastWeek.getDate()-7);
     453            endWeek = getISOWeekNumber(dateInLastWeek);
     454          }
    408455          colspan = endWeek;
    409456        }               
     
    444491    var fdString = report.beginDate;
    445492    var ldString = report.endDate;
     493    var psdString = report.periodBeginDate;
    446494    var startDate = new Date();
    447495    startDate.setYear(fdString.substr(0,4));
     
    452500    endDate.setMonth(ldString.substr(4,2)-1);
    453501    endDate.setDate(ldString.substr(6));
     502    var periodStartDate = new Date();
     503    periodStartDate.setYear(psdString.substr(0,4));
     504    periodStartDate.setMonth(psdString.substr(4,2)-1);
     505    periodStartDate.setDate(psdString.substr(6));
    454506    var viewType = report.viewType;   
    455507    var sites = report.sites;   
     
    472524      tableRow.appendChild(tableCol);
    473525      tableRow.appendChild(getTableCellElement(year+'-'+(month)+'-'+date, 'reportdata'));
    474       var currentDate = new Date(startDate.getFullYear(), startDate.getMonth(), startDate.getDate());    
    475       var grandTotal = 0;
     526      var grandTotal = 0;   
     527      var currentDate = new Date(periodStartDate.getFullYear(), periodStartDate.getMonth(), periodStartDate.getDate());     
    476528      var columnCounter = 0;
    477529      do
     
    489541          }
    490542          grandTotal += data;         
    491           if ( (currentDate.getFullYear() <= siteStartDate.getFullYear()))
     543          if ( (currentDate.getFullYear() < siteStartDate.getFullYear()))
    492544          {
    493545            data = '';
     
    540592        else if (viewType == 'WEEK')
    541593        {
    542           var weekIndex = getWeekByDate(currentDate);
     594          var weekIndex = getISOWeekNumber(currentDate);
    543595          keyIndex = yearIndex + '' + weekIndex;
    544596          if (siteData != null)
     
    547599          }
    548600          grandTotal += data;         
    549           if (currentDate.getFullYear() <= siteStartDate.getFullYear() && getWeekByDate(currentDate) < getWeekByDate(siteStartDate))
     601          if (currentDate.getFullYear() <= siteStartDate.getFullYear() && getISOWeekNumber(currentDate) < getISOWeekNumber(siteStartDate))
    550602          {
    551603            data = '';
     
    559611        tableRow.appendChild(getTableCellElement(data, 'reportdata'));
    560612        columnCounter++;       
    561       }while (columnCounter< numCols)     
     613      } while (columnCounter < numCols)
    562614      tableRow.appendChild(getTableCellElement(grandTotal, 'rowsummary'));
    563615      reportTable.appendChild(tableRow);
     
    604656    return listElement;
    605657  }
     658
     659  /*
     660   * Get the ISO week number for a given date
     661   *
     662   * Based on code at http://stackoverflow.com/questions/6117814/get-week-of-year-in-javascript-like-in-php
     663   */
     664  function getISOWeekNumber(date)
     665  {
     666    tmpDate = new Date(date);
     667    tmpDate.setHours(0,0,0);
     668    // Set to nearest Thursday: current date + 4 - current day number
     669    // Make Sunday day number 7
     670    tmpDate.setDate(tmpDate.getDate() + 4 - (tmpDate.getDay() || 7));
     671    // Get first day of year
     672    var yearStart = new Date(tmpDate.getFullYear(), 0, 1);
     673    // Calculate full weeks to nearast Thursday (86400000 = 24*60*60*1000 number of ms in a day)
     674    var weekNo = Math.ceil(( ( (tmpDate - yearStart) / 86400000) + 1)/7);
     675    return weekNo;
     676  }
     677
    606678  </script>
    607679 
     
    683755                </ul>                 
    684756              </td>
    685             </tr>           
     757            </tr>
    686758            </table>
    687759          </td>
  • extensions/net.sf.basedb.reggie/trunk/src/net/sf/basedb/reggie/servlet/SampleReportServlet.java

    r1710 r1727  
    173173          }
    174174        }
     175
    175176        jsonReport.put("statistics", jsonStatistics);       
    176177        if (viewType == null) viewType = getViewType(startDate, endDate);
     178        Date periodStartDate = calculatePeriodStartDate(startDate, viewType);
    177179        jsonReport.put("viewType", viewType);
    178180        jsonReport.put("beginDate", date2StringConverter.convert(startDate));
    179181        jsonReport.put("endDate", date2StringConverter.convert(endDate));
     182        jsonReport.put("periodBeginDate", date2StringConverter.convert(periodStartDate));
    180183        jsonReport.put("latestDate", date2StringConverter.convert(latestDate));
    181184        json.put("report", jsonReport);
     
    225228      }
    226229    }
     230    else if (viewType.equals(weekView))
     231    {
     232      // Get year number
     233      DateToStringConverter converter = new DateToStringConverter(new SimpleDateFormat("yyyy"));
     234      currentPeriod = converter.convert(currentDate);
     235      // Use Calendar to get ISO week number
     236      Calendar cal = GregorianCalendar.getInstance();
     237      cal.setFirstDayOfWeek(Calendar.MONDAY);
     238      cal.setMinimalDaysInFirstWeek(4);
     239      cal.setTime(currentDate);
     240      int week = cal.get(Calendar.WEEK_OF_YEAR);
     241      // Add week number to year number to get week in "yyyyw" format
     242      currentPeriod += Integer.toString(week);
     243    }
    227244    else
    228245    {     
    229246      String dateFormat = null;
    230       if (viewType.equals(weekView))
    231       {
    232         dateFormat = "yyyyw";
    233       }
    234       else if (viewType.equals(monthView))
     247      if (viewType.equals(monthView))
    235248      {
    236249        dateFormat = "yyyyMM";
     
    291304  }
    292305
     306  Date calculatePeriodStartDate(Date date, String viewType)
     307  {
     308    Date tmpDate = null;
     309    if (viewType.equals(weekView))
     310    {
     311      tmpDate = mondayInISOWeek(date);
     312    }
     313    else if (viewType.equals(monthView))
     314    {
     315      tmpDate = firstDayInMonth(date);
     316    }
     317    else if (viewType.equals(quarterView))
     318    {
     319      tmpDate = firstDayInQuarter(date);
     320    }
     321    else if (viewType.equals(yearView))
     322    {
     323      tmpDate = firstDayInYear(date);
     324    }
     325    Date periodDate = adjustDayTime(tmpDate, 0, 0, 0);
     326    return periodDate;
     327  }
     328
     329  Date mondayInISOWeek(Date date)
     330  {
     331    // Get weekday number; 1 => Sunday, 2 => Monday, ... , 7 => Saturday
     332    Calendar cal = GregorianCalendar.getInstance();
     333    cal.setTime(date);
     334    int weekdayNumber = cal.get(Calendar.DAY_OF_WEEK);
     335    // Get weekday offset from Monday; 6 => Sunday, 0 => Monday, ... , 5 => Saturday
     336    int offset = weekdayNumber - 2;
     337    if (offset < 0)
     338    {
     339      offset = offset + 7;
     340    }
     341    // Get date for Monday in ISO week
     342    long msSince19700101 = date.getTime() - offset*24*60*60*1000;
     343    Date dateMondayInWeek = new Date(msSince19700101);
     344    return dateMondayInWeek;
     345  }
     346
     347  Date firstDayInMonth(Date date)
     348  {
     349    // Get year and month;
     350    Calendar cal = GregorianCalendar.getInstance();
     351    cal.setTime(date);
     352    int year = cal.get(Calendar.YEAR);
     353    int month = cal.get(Calendar.MONTH);
     354    // Get date for first day in month
     355    cal.set(year, month, 1);
     356    Date dateFirstDayInMonth = cal.getTime();
     357    return dateFirstDayInMonth;
     358  }
     359
     360  Date firstDayInQuarter(Date date)
     361  {
     362    // Get year and month;
     363    Calendar cal = GregorianCalendar.getInstance();
     364    cal.setTime(date);
     365    int year = cal.get(Calendar.YEAR);
     366    int month = cal.get(Calendar.MONTH);
     367    // Get first month of quarter;  0 => January, 1 => February, ... , 11 => December
     368    if (month < 3)
     369    {
     370      month = 0;
     371    }
     372    else if (month < 6)
     373    {
     374      month = 3;
     375    }
     376    else if (month < 9)
     377    {
     378      month = 6;
     379    }
     380    else
     381    {
     382      month = 9;
     383    }
     384    // Get date for first day in quarter
     385    cal.set(year, month, 1);
     386    Date dateFirstDayInQuarter = cal.getTime();
     387    return dateFirstDayInQuarter;
     388  }
     389
     390  Date firstDayInYear(Date date)
     391  {
     392    // Get year;
     393    Calendar cal = GregorianCalendar.getInstance();
     394    cal.setTime(date);
     395    int year = cal.get(Calendar.YEAR);
     396    // Get date for first day in year
     397    cal.set(year, 1, 1);
     398    Date dateFirstDayInYear = cal.getTime();
     399    return dateFirstDayInYear;
     400  }
     401
     402  Date adjustDayTime(Date date, int hour, int min, int sec)
     403  {
     404    // Get year, month, and date
     405    Calendar cal = GregorianCalendar.getInstance();
     406    cal.setTime(date);
     407    int year = cal.get(Calendar.YEAR);
     408    int month = cal.get(Calendar.MONTH);
     409    int dateInMonth = cal.get(Calendar.DATE);
     410    // Adjust time of day for date
     411    cal.clear();
     412    cal.set(year, month, dateInMonth, hour, min, sec);
     413    Date adjustedDate = cal.getTime();
     414    return adjustedDate;
     415  }
     416
    293417  @SuppressWarnings("unchecked")
    294418  private static JSONObject getJSONSites()
Note: See TracChangeset for help on using the changeset viewer.