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