Coverage Report - net.sf.statcvs.reports.AbstractRollingLocTableReport
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractRollingLocTableReport
0%
0/112
0%
0/62
3.833
 
 1  
 /*
 2  
     StatCvs - CVS statistics generation 
 3  
     Copyright (C) 2002  Lukasz Pekacki <lukasz@pekacki.de>
 4  
     http://statcvs.sf.net/
 5  
     
 6  
     This library is free software; you can redistribute it and/or
 7  
     modify it under the terms of the GNU Lesser General Public
 8  
     License as published by the Free Software Foundation; either
 9  
     version 2.1 of the License, or (at your option) any later version.
 10  
 
 11  
     This library is distributed in the hope that it will be useful,
 12  
     but WITHOUT ANY WARRANTY; without even the implied warranty of
 13  
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 14  
     Lesser General Public License for more details.
 15  
 
 16  
     You should have received a copy of the GNU Lesser General Public
 17  
     License along with this library; if not, write to the Free Software
 18  
     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 19  
     
 20  
         $RCSfile: AbstractRollingLocTableReport.java,v $ 
 21  
         Created on $Date: 2009/03/09 22:21:36 $ 
 22  
 */
 23  
 package net.sf.statcvs.reports;
 24  
 
 25  
 import java.util.ArrayList;
 26  
 import java.util.Calendar;
 27  
 import java.util.Collection;
 28  
 import java.util.Date;
 29  
 import java.util.Iterator;
 30  
 import java.util.List;
 31  
 
 32  
 import net.sf.statcvs.Messages;
 33  
 import net.sf.statcvs.model.Author;
 34  
 import net.sf.statcvs.model.Repository;
 35  
 import net.sf.statcvs.model.Revision;
 36  
 import net.sf.statcvs.output.ReportConfig;
 37  
 import net.sf.statcvs.reportmodel.GenericColumn;
 38  
 import net.sf.statcvs.reportmodel.IntegerColumn;
 39  
 import net.sf.statcvs.reportmodel.Table;
 40  
 import net.sf.statcvs.util.IntegerMap;
 41  
 
 42  
 /**
 43  
  * Convenience superclass for table reports related to last n months for authors and directories.
 44  
  * Contains methods to calculate some common stuff for these tables.
 45  
  * @author Lukasz Pekacki
 46  
  * @author Benoit Xhenseval
 47  
  * @version $Id: AbstractRollingLocTableReport.java,v 1.3 2009/03/09 22:21:36 benoitx Exp $
 48  
  */
 49  
 public abstract class AbstractRollingLocTableReport {
 50  
     private static final int NUMBER_OF_MONTHS = 12;
 51  
     /**
 52  
      * Sort the authors table by name
 53  
      * */
 54  
     public static final int SORT_BY_NAME = 0;
 55  
 
 56  
     /**
 57  
      * Sort the authors table by lines of code
 58  
      * */
 59  
     public static final int SORT_BY_LINES = 1;
 60  
 
 61  
     private final ReportConfig config;
 62  
     private final Repository content;
 63  
 
 64  
     private final List changesMaps;
 65  
     private final List linesMaps;
 66  0
     private final IntegerMap authors = new IntegerMap();
 67  
     private Date cutOff;
 68  
 
 69  
     /**
 70  
      * Constructor
 71  
      * @param content render table on specified content
 72  
      */
 73  0
     public AbstractRollingLocTableReport(final ReportConfig config) {
 74  0
         this.config = config;
 75  0
         this.content = config.getRepository();
 76  0
         changesMaps = new ArrayList(NUMBER_OF_MONTHS + 1);
 77  0
         linesMaps = new ArrayList(NUMBER_OF_MONTHS + 1);
 78  0
         for (int i = 0; i < NUMBER_OF_MONTHS + 1; i++) {
 79  0
             changesMaps.add(new IntegerMap());
 80  0
             linesMaps.add(new IntegerMap());
 81  
         }
 82  0
     }
 83  
 
 84  
     protected void calculateChangesAndLinesPerDeveloper(final Collection revs) {
 85  0
         final Iterator it = revs.iterator();
 86  0
         final Date last = content.getLastDate();
 87  0
         final Calendar cal = Calendar.getInstance();
 88  0
         cal.setTime(last);
 89  0
         cal.add(Calendar.MONTH, -NUMBER_OF_MONTHS + 1);
 90  0
         cal.set(Calendar.DAY_OF_MONTH, 1);
 91  0
         cal.add(Calendar.DAY_OF_MONTH, -1);
 92  0
         cal.set(Calendar.HOUR_OF_DAY, 23);
 93  0
         cal.set(Calendar.MINUTE, 59);
 94  0
         cal.set(Calendar.SECOND, 59);
 95  0
         cal.set(Calendar.MILLISECOND, 999);
 96  0
         cutOff = cal.getTime();
 97  
         //        System.out.println("LAST DATE " + last);
 98  0
         //        System.out.println("CUTO DATE " + cutOff);
 99  0
         while (it.hasNext()) {
 100  0
             final Revision rev = (Revision) it.next();
 101  0
             if (rev.getAuthor() == null || !this.config.isDeveloper(rev.getAuthor())) {
 102  0
                 continue;
 103  
             }
 104  0
 
 105  0
             final IntegerMap changesMap = findMap(cal, rev.getDate(), cutOff, changesMaps);
 106  0
             final IntegerMap linesMap = findMap(cal, rev.getDate(), cutOff, linesMaps);
 107  0
 
 108  0
             changesMap.addInt(rev.getAuthor(), 1);
 109  0
             linesMap.addInt(rev.getAuthor(), rev.getNewLines());
 110  0
             authors.addInt(rev.getAuthor(), rev.getNewLines());
 111  0
         }
 112  0
     }
 113  
 
 114  0
     private IntegerMap findMap(final Calendar cal, final Date date, final Date cutOff, final List listOfMaps) {
 115  0
         if (date.compareTo(cutOff) < 0) {
 116  0
             return (IntegerMap) listOfMaps.get(0);
 117  
         }
 118  0
 
 119  0
         cal.setTime(cutOff);
 120  0
         final int cutOffMonth = cal.get(Calendar.MONTH) + 1;
 121  0
         cal.setTime(date);
 122  0
         final int monthRev = cal.get(Calendar.MONTH) + 1;
 123  0
 
 124  0
         int idx = monthRev - cutOffMonth;
 125  
 
 126  0
         if (idx < 0) {
 127  0
             idx += 12;
 128  0
         }
 129  
 
 130  
         //        System.out.println(" Date " + date + " / " + cutOffMonth + " vs " + monthRev + " Diff " + (monthRev + cutOffMonth) + " idx " + (idx));
 131  0
 
 132  0
         if (idx == 0) {
 133  0
             idx = 12;
 134  
         }
 135  0
 
 136  0
         return (IntegerMap) listOfMaps.get(idx);
 137  0
     }
 138  0
 
 139  0
     protected Table createChangesAndLinesTable(final GenericColumn keys, final GenericColumn keys2, final String summary) {
 140  0
         final Table result = new Table(summary);
 141  0
         keys.setTotal(Messages.getString("TOTALS"));
 142  0
         result.addColumn(keys);
 143  0
         if (keys2 != null) {
 144  0
             keys.setTotal("");
 145  0
             keys2.setTotal(Messages.getString("TOTALS"));
 146  0
             result.addColumn(keys2);
 147  0
         }
 148  0
         result.setKeysInFirstColumn(true);
 149  0
 
 150  0
         final Calendar cal = Calendar.getInstance();
 151  0
         cal.setTime(cutOff);
 152  0
         final List columns = new ArrayList();
 153  0
         for (int i = 0; i < NUMBER_OF_MONTHS + 1; i++) {
 154  0
             String title = null;
 155  0
             if (i == 0) {
 156  0
                 title = "Up to " + (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.YEAR);
 157  0
             }
 158  0
             if (i != 0) {
 159  0
                 cal.add(Calendar.MONTH, 1);
 160  0
                 title = "" + (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.YEAR);
 161  
             }
 162  0
             final IntegerColumn column = new IntegerColumn(title);
 163  0
             columns.add(column);
 164  0
             result.addColumn(column);
 165  0
         }
 166  0
 
 167  0
         final Iterator it = authors.iteratorSortedByValueReverse();
 168  0
 
 169  0
         while (it.hasNext()) {
 170  0
             final Object key = it.next();
 171  0
             keys.addValue(key);
 172  0
             if (keys2 != null) {
 173  0
                 keys2.addValue(key);
 174  
             }
 175  0
             //            System.out.println("Author " + key);
 176  0
             for (int i = 0; i < NUMBER_OF_MONTHS + 1; i++) {
 177  0
                 final IntegerMap linesMap = (IntegerMap) linesMaps.get(i);
 178  0
                 ((IntegerColumn) columns.get(i)).addValue(linesMap.get(key));
 179  
             }
 180  0
         }
 181  
 
 182  0
         if (result.getRowCount() > 1) {
 183  0
             result.setShowTotals(true);
 184  0
         }
 185  0
         return result;
 186  
     }
 187  
 
 188  0
     protected Repository getContent() {
 189  0
         return content;
 190  0
     }
 191  0
 
 192  0
     public int getDeveloperCount() {
 193  0
         int result = 0;
 194  0
         final Iterator it = getContent().getAuthors().iterator();
 195  0
         while (it.hasNext()) {
 196  0
             final Author author = (Author) it.next();
 197  0
             if (this.config.isDeveloper(author)) {
 198  0
                 result++;
 199  
             }
 200  0
         }
 201  0
         return result;
 202  
     }
 203  
 }