1 package net.sf.statcvs.pages;
2
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.HashMap;
6 import java.util.Iterator;
7 import java.util.List;
8 import java.util.Map;
9
10 import net.sf.statcvs.Messages;
11 import net.sf.statcvs.charts.ChartImage;
12 import net.sf.statcvs.charts.LOCChartMaker.DirectoryLOCChartMaker;
13 import net.sf.statcvs.model.Commit;
14 import net.sf.statcvs.model.Directory;
15 import net.sf.statcvs.model.Repository;
16 import net.sf.statcvs.model.Revision;
17 import net.sf.statcvs.model.VersionedFile;
18 import net.sf.statcvs.output.ReportConfig;
19 import net.sf.statcvs.output.WebRepositoryIntegration;
20 import net.sf.statcvs.reports.AuthorsForDirectoryTableReport;
21
22
23
24
25
26
27 public class DirectoryPageMaker {
28 private final static int RECENT_COMMITS_LENGTH = 20;
29
30 public static String getURL(final Directory directory) {
31 return getFilename(directory) + ".html";
32 }
33
34 private static String getFilename(final Directory directory) {
35 return "dir" + HTML.escapeDirectoryName(directory.getPath());
36 }
37
38 private final ReportConfig config;
39 private final Repository repository;
40 private final Directory directory;
41
42 public DirectoryPageMaker(final ReportConfig config, final Directory directory) {
43 this.config = config;
44 this.repository = config.getRepository();
45 this.directory = directory;
46 }
47
48 public Page toFile() {
49 final ChartImage chart = new DirectoryLOCChartMaker(this.config, this.directory).toFile();
50
51 final String title = "Directory " + (this.directory.isRoot() ? "[root]" : this.directory.getPath());
52 final Page page = this.config.createPage(getFilename(this.directory), title, title);
53 if (!this.directory.getRevisions().isEmpty() && isInitiallyEmpty()) {
54 page.addAttribute("Directory Created", ((Revision) this.directory.getRevisions().first()).getDate());
55 }
56 if (!this.directory.getRevisions().isEmpty() && getCurrentFileCount() == 0) {
57 page.addAttribute("Directory Deleted", ((Revision) this.directory.getRevisions().last()).getDate());
58 }
59 page.addAttribute("Total Files", getCurrentFileCount());
60 page.addAttribute("Deleted Files", getDeadFileCount());
61 page.addAttribute("Lines of Code", getCurrentLOC());
62 if (this.config.getWebRepository() != null) {
63 final WebRepositoryIntegration rep = this.config.getWebRepository();
64 final String text = Messages.getString("BROWSE_WEB_REPOSITORY") + " " + rep.getName();
65 page.addLink(rep.getDirectoryUrl(this.directory), text);
66 }
67 page.add(this.directory, true);
68 ;
69 if (chart != null) {
70 page.addSection(Messages.getString("LOC_TITLE"));
71 page.add(chart);
72 }
73 if (!this.directory.getRevisions().isEmpty()) {
74 page.addSection("Developers");
75 page.add(new AuthorsForDirectoryTableReport(this.config, this.directory));
76 }
77 final List dirCommits = getCommitsInDirectory();
78 if (!dirCommits.isEmpty()) {
79 page.addSection(Messages.getString("MOST_RECENT_COMMITS"));
80 final CommitListFormatter renderer = new CommitListFormatter(this.config, dirCommits, Collections.EMPTY_LIST, RECENT_COMMITS_LENGTH, false);
81 page.addRawContent(renderer.render());
82 }
83 return page;
84 }
85
86 private Commit getCommit(final Revision rev) {
87 final Iterator it = this.repository.getCommits().iterator();
88 while (it.hasNext()) {
89 final Commit commit = (Commit) it.next();
90 if (commit.getRevisions().contains(rev)) {
91 return commit;
92 }
93 }
94 return null;
95 }
96
97 private List getCommitsInDirectory() {
98 final Map commitsToFilteredCommits = new HashMap();
99 final Iterator it = this.directory.getRevisions().iterator();
100 while (it.hasNext()) {
101 final Revision rev = (Revision) it.next();
102 final Commit commit = getCommit(rev);
103 if (commit == null) {
104 continue;
105 }
106 if (commitsToFilteredCommits.containsKey(commit)) {
107 final Commit filteredCommit = (Commit) commitsToFilteredCommits.get(commit);
108 filteredCommit.addRevision(rev);
109 } else {
110 final Commit filteredCommit = new Commit(rev);
111 commitsToFilteredCommits.put(commit, filteredCommit);
112 }
113 }
114 final List commits = new ArrayList(commitsToFilteredCommits.values());
115 Collections.sort(commits);
116 return commits;
117 }
118
119 private int getCurrentLOC() {
120 int result = 0;
121 final Iterator it = this.directory.getFiles().iterator();
122 while (it.hasNext()) {
123 final VersionedFile file = (VersionedFile) it.next();
124 result += file.getCurrentLinesOfCode();
125 }
126 return result;
127 }
128
129 private int getCurrentFileCount() {
130 int result = 0;
131 final Iterator it = this.directory.getFiles().iterator();
132 while (it.hasNext()) {
133 final VersionedFile file = (VersionedFile) it.next();
134 if (!file.isDead()) {
135 result++;
136 }
137 }
138 return result;
139 }
140
141 private int getDeadFileCount() {
142 int result = 0;
143 final Iterator it = this.directory.getFiles().iterator();
144 while (it.hasNext()) {
145 final VersionedFile file = (VersionedFile) it.next();
146 if (file.isDead()) {
147 result++;
148 }
149 }
150 return result;
151 }
152
153 private boolean isInitiallyEmpty() {
154 final Iterator it = this.directory.getFiles().iterator();
155 while (it.hasNext()) {
156 final VersionedFile file = (VersionedFile) it.next();
157 if (!file.getInitialRevision().isInitialRevision()) {
158 return false;
159 }
160 }
161 return true;
162 }
163 }