View Javadoc

1   package net.sf.statcvs.pages;
2   
3   import java.util.ArrayList;
4   import java.util.Calendar;
5   import java.util.Collection;
6   import java.util.Collections;
7   import java.util.Date;
8   import java.util.GregorianCalendar;
9   import java.util.HashSet;
10  import java.util.Iterator;
11  import java.util.List;
12  import java.util.Set;
13  
14  import net.sf.statcvs.model.Commit;
15  import net.sf.statcvs.model.SymbolicName;
16  import net.sf.statcvs.output.ReportConfig;
17  
18  public class LogPageMaker {
19      private final static String[] MONTH_TWO_CHARACTERS = { "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12" };
20      private final static String[] MONTH_NAME = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November",
21              "December" };
22  
23      public static String getAnchor(final SymbolicName tag) {
24          return "tag-" + tag.getName().replace('.', '_');
25      }
26  
27      public static String getURL(final Date date) {
28          final Calendar calendar = new GregorianCalendar();
29          calendar.setTime(date);
30          return getFileName(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH)) + ".html";
31      }
32  
33      private static String getFileName(final int year, final int month) {
34          return year + "-" + MONTH_TWO_CHARACTERS[month];
35      }
36  
37      private final ReportConfig config;
38      private final int year;
39      private final int month;
40      private final List commits = new ArrayList();
41  
42      /**
43       * Creates a new LogPageMaker.
44       * @param year The log page's year
45       * @param month The log page's month (0 for January)
46       * @param commits A list of commits; those not in the
47       * 		right month will be ignored
48       * @param outputFormat
49       */
50      public LogPageMaker(final ReportConfig config, final int year, final int month, final Collection commits) {
51          this.config = config;
52          this.year = year;
53          this.month = month;
54          final Calendar calendar = new GregorianCalendar();
55          final Iterator it = commits.iterator();
56          while (it.hasNext()) {
57              final Commit commit = (Commit) it.next();
58              calendar.setTime(commit.getDate());
59              if (calendar.get(Calendar.YEAR) != year || calendar.get(Calendar.MONTH) != month) {
60                  continue;
61              }
62              this.commits.add(commit);
63          }
64      }
65  
66      public NavigationNode toFile() {
67          final Page result = this.config.createPage(getFileName(), getTitle(), getTitle() + " Commit Log");
68          result.addAttribute("Number of Commits", this.commits.size());
69          result.addAttribute("Number of Active Developers", countActiveDevelopers());
70          if (!this.commits.isEmpty()) {
71              result.addRawContent(new CommitListFormatter(this.config, this.commits, getTags(), true).render());
72          }
73          return result;
74      }
75  
76      private String getFileName() {
77          return getFileName(this.year, this.month);
78      }
79  
80      private String getTitle() {
81          return MONTH_NAME[this.month] + " " + year;
82      }
83  
84      private int countActiveDevelopers() {
85          final Set developers = new HashSet();
86          final Iterator it = this.commits.iterator();
87          while (it.hasNext()) {
88              final Commit commit = (Commit) it.next();
89              developers.add(commit.getAuthor());
90          }
91          return developers.size();
92      }
93  
94      private List getTags() {
95          final List tags = new ArrayList();
96          final Calendar calendar = new GregorianCalendar();
97          final Iterator it = this.config.getRepository().getSymbolicNames().iterator();
98          while (it.hasNext()) {
99              final SymbolicName tag = (SymbolicName) it.next();
100             calendar.setTime(tag.getDate());
101             if (calendar.get(Calendar.YEAR) == this.year && calendar.get(Calendar.MONTH) == this.month) {
102                 tags.add(tag);
103             }
104         }
105         Collections.reverse(tags);
106         return tags;
107     }
108 }