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 boolean firstLogPage; 41 private final List commits = new ArrayList(); 42 43 /** 44 * Creates a new LogPageMaker. 45 * @param year The log page's year 46 * @param month The log page's month (0 for January) 47 * @param commits A list of commits; those not in the 48 * right month will be ignored 49 * @param outputFormat 50 */ 51 public LogPageMaker(final ReportConfig config, final int year, final int month, final Collection commits, final boolean firstLogPage) { 52 this.config = config; 53 this.year = year; 54 this.month = month; 55 this.firstLogPage = firstLogPage; 56 final Calendar calendar = new GregorianCalendar(); 57 final Iterator it = commits.iterator(); 58 while (it.hasNext()) { 59 final Commit commit = (Commit) it.next(); 60 calendar.setTime(commit.getDate()); 61 if (calendar.get(Calendar.YEAR) != year || calendar.get(Calendar.MONTH) != month) { 62 continue; 63 } 64 this.commits.add(commit); 65 } 66 } 67 68 public NavigationNode toFile() { 69 final Page result = this.config.createPage(getFileName(), getTitle(), getTitle() + " Commit Log"); 70 result.addAttribute("Number of Commits", this.commits.size()); 71 result.addAttribute("Number of Active Developers", countActiveDevelopers()); 72 if (!this.commits.isEmpty()) { 73 result.addRawContent(new CommitListFormatter(this.config, this.commits, getTags(), true).render()); 74 } 75 return result; 76 } 77 78 private String getFileName() { 79 if (!firstLogPage) { 80 return getFileName(this.year, this.month); 81 } 82 return "commitlog"; 83 } 84 85 private String getTitle() { 86 return MONTH_NAME[this.month] + " " + year; 87 } 88 89 private int countActiveDevelopers() { 90 final Set developers = new HashSet(); 91 final Iterator it = this.commits.iterator(); 92 while (it.hasNext()) { 93 final Commit commit = (Commit) it.next(); 94 developers.add(commit.getAuthor()); 95 } 96 return developers.size(); 97 } 98 99 private List getTags() { 100 final List tags = new ArrayList(); 101 final Calendar calendar = new GregorianCalendar(); 102 final Iterator it = this.config.getRepository().getSymbolicNames().iterator(); 103 while (it.hasNext()) { 104 final SymbolicName tag = (SymbolicName) it.next(); 105 calendar.setTime(tag.getDate()); 106 if (calendar.get(Calendar.YEAR) == this.year && calendar.get(Calendar.MONTH) == this.month) { 107 tags.add(tag); 108 } 109 } 110 Collections.reverse(tags); 111 return tags; 112 } 113 }