lemval
2012-01-31 1c30dad2115fc513791d8a5b292ad0f7d7b85749
commit | author | age
f13c4c 1 /*
JM 2  * Copyright 2011 gitblit.com.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
5fe7df 16 package com.gitblit.wicket.pages;
JM 17
45c0d6 18 import java.awt.Color;
fc8426 19 import java.awt.Dimension;
45c0d6 20 import java.text.MessageFormat;
a1ea87 21 import java.text.ParseException;
8a2e9c 22 import java.util.ArrayList;
5fe7df 23 import java.util.List;
8a2e9c 24
5fe7df 25 import org.apache.wicket.PageParameters;
JM 26 import org.apache.wicket.markup.html.basic.Label;
1fa5e8 27 import org.apache.wicket.markup.html.link.BookmarkablePageLink;
5fe7df 28 import org.eclipse.jgit.lib.Repository;
a1ea87 29 import org.eclipse.jgit.revwalk.RevCommit;
531cd2 30 import org.wicketstuff.googlecharts.Chart;
JM 31 import org.wicketstuff.googlecharts.ChartAxis;
32 import org.wicketstuff.googlecharts.ChartAxisType;
33 import org.wicketstuff.googlecharts.ChartProvider;
34 import org.wicketstuff.googlecharts.ChartType;
35 import org.wicketstuff.googlecharts.IChartData;
36 import org.wicketstuff.googlecharts.LineStyle;
37 import org.wicketstuff.googlecharts.MarkerType;
38 import org.wicketstuff.googlecharts.ShapeMarker;
f5d0ad 39
d25c59 40 import com.gitblit.Constants.AccessRestrictionType;
87cc1e 41 import com.gitblit.GitBlit;
155bf7 42 import com.gitblit.Keys;
1f9dae 43 import com.gitblit.models.Metric;
a1ea87 44 import com.gitblit.models.PathModel;
fe3262 45 import com.gitblit.models.RepositoryModel;
e21181 46 import com.gitblit.utils.ArrayUtils;
5fe7df 47 import com.gitblit.utils.JGitUtils;
a1ea87 48 import com.gitblit.utils.MarkdownUtils;
8a2e9c 49 import com.gitblit.utils.StringUtils;
45c0d6 50 import com.gitblit.utils.TimeUtils;
bc10f9 51 import com.gitblit.wicket.WicketUtils;
698678 52 import com.gitblit.wicket.panels.BranchesPanel;
JM 53 import com.gitblit.wicket.panels.LogPanel;
82df52 54 import com.gitblit.wicket.panels.RepositoryUrlPanel;
698678 55 import com.gitblit.wicket.panels.TagsPanel;
5fe7df 56
JM 57 public class SummaryPage extends RepositoryPage {
58
59     public SummaryPage(PageParameters params) {
cebf45 60         super(params);
155bf7 61
85c2e6 62         int numberCommits = GitBlit.getInteger(Keys.web.summaryCommitCount, 20);
fb01c9 63         if (numberCommits <= 0) {
85c2e6 64             numberCommits = 20;
fb01c9 65         }
85c2e6 66         int numberRefs = GitBlit.getInteger(Keys.web.summaryRefsCount, 5);
155bf7 67
JM 68         Repository r = getRepository();
fe3262 69         RepositoryModel model = getRepositoryModel();
82df52 70
45c0d6 71         List<Metric> metrics = null;
JM 72         Metric metricsTotal = null;
fe3262 73         if (!model.skipSummaryMetrics && GitBlit.getBoolean(Keys.web.generateActivityGraph, true)) {
JM 74             metrics = GitBlit.self().getRepositoryDefaultMetrics(model, r);
45c0d6 75             metricsTotal = metrics.remove(0);
ef5c58 76         }
85c2e6 77
8c9a20 78         addSyndicationDiscoveryLink();
5fe7df 79
JM 80         // repository description
f97bf0 81         add(new Label("repositoryDescription", getRepositoryModel().description));
JM 82         add(new Label("repositoryOwner", getRepositoryModel().owner));
155bf7 83
88598b 84         add(WicketUtils.createTimestampLabel("repositoryLastChange",
JM 85                 JGitUtils.getLastChange(r, null), getTimeZone()));
45c0d6 86         if (metricsTotal == null) {
008322 87             add(new Label("branchStats", ""));
45c0d6 88         } else {
008322 89             add(new Label("branchStats",
JM 90                     MessageFormat.format("{0} commits and {1} tags in {2}", metricsTotal.count,
91                             metricsTotal.tag, TimeUtils.duration(metricsTotal.duration))));
45c0d6 92         }
008322 93         add(new BookmarkablePageLink<Void>("metrics", MetricsPage.class,
JM 94                 WicketUtils.newRepositoryParameter(repositoryName)));
8a2e9c 95
JM 96         List<String> repositoryUrls = new ArrayList<String>();
97
2a7306 98         if (GitBlit.getBoolean(Keys.git.enableGitServlet, true)) {
8a2e9c 99             AccessRestrictionType accessRestriction = getRepositoryModel().accessRestriction;
JM 100             switch (accessRestriction) {
101             case NONE:
102                 add(WicketUtils.newClearPixel("accessRestrictionIcon").setVisible(false));
103                 break;
104             case PUSH:
2a7306 105                 add(WicketUtils.newImage("accessRestrictionIcon", "lock_go_16x16.png",
JM 106                         getAccessRestrictions().get(accessRestriction)));
8a2e9c 107                 break;
JM 108             case CLONE:
2a7306 109                 add(WicketUtils.newImage("accessRestrictionIcon", "lock_pull_16x16.png",
JM 110                         getAccessRestrictions().get(accessRestriction)));
8a2e9c 111                 break;
JM 112             case VIEW:
2a7306 113                 add(WicketUtils.newImage("accessRestrictionIcon", "shield_16x16.png",
JM 114                         getAccessRestrictions().get(accessRestriction)));
8a2e9c 115                 break;
JM 116             default:
117                 add(WicketUtils.newClearPixel("accessRestrictionIcon").setVisible(false));
118             }
e21181 119             // add the Gitblit repository url
JM 120             repositoryUrls.add(getRepositoryUrl(getRepositoryModel()));
8a2e9c 121         } else {
f98825 122             add(WicketUtils.newClearPixel("accessRestrictionIcon").setVisible(false));
JM 123         }
8a2e9c 124         repositoryUrls.addAll(GitBlit.self().getOtherCloneUrls(repositoryName));
82df52 125         
e21181 126         String primaryUrl = ArrayUtils.isEmpty(repositoryUrls) ? "" : repositoryUrls.remove(0);
82df52 127         add(new RepositoryUrlPanel("repositoryCloneUrl", primaryUrl));
8a2e9c 128
82df52 129         add(new Label("otherUrls", StringUtils.flattenStrings(repositoryUrls, "<br/>"))
JM 130         .setEscapeModelStrings(false));
5fe7df 131
ef5c58 132         add(new LogPanel("commitsPanel", repositoryName, null, r, numberCommits, 0));
1fa5e8 133         add(new TagsPanel("tagsPanel", repositoryName, r, numberRefs).hideIfEmpty());
JM 134         add(new BranchesPanel("branchesPanel", getRepositoryModel(), r, numberRefs).hideIfEmpty());
155bf7 135
a1ea87 136         if (getRepositoryModel().showReadme) {
JM 137             String htmlText = null;
138             try {
139                 RevCommit head = JGitUtils.getCommit(r, null);
140                 List<String> markdownExtensions = GitBlit.getStrings(Keys.web.markdownExtensions);
141                 List<PathModel> paths = JGitUtils.getFilesInPath(r, null, head);
142                 String readme = null;
143                 for (PathModel path : paths) {
144                     if (!path.isTree()) {
145                         String name = path.name.toLowerCase();
146
147                         if (name.startsWith("readme")) {
148                             if (name.indexOf('.') > -1) {
149                                 String ext = name.substring(name.lastIndexOf('.') + 1);
150                                 if (markdownExtensions.contains(ext)) {
151                                     readme = path.name;
152                                     break;
153                                 }
154                             }
155                         }
156                     }
157                 }
158                 if (!StringUtils.isEmpty(readme)) {
4ab184 159                     String markdownText = JGitUtils.getStringContent(r, head.getTree(), readme);
a1ea87 160                     htmlText = MarkdownUtils.transformMarkdown(markdownText);
JM 161                 }
162             } catch (ParseException p) {
163                 error(p.getMessage());
164             }
165             // Add the html to the page
166             add(new Label("readme", htmlText).setEscapeModelStrings(false).setVisible(
167                     !StringUtils.isEmpty(htmlText)));
168         } else {
169             add(new Label("readme").setVisible(false));
170         }
171
fc8426 172         // Display an activity line graph
ef5c58 173         insertActivityGraph(metrics);
5fe7df 174     }
155bf7 175
cebf45 176     @Override
JM 177     protected String getPageName() {
1e47ab 178         return getString("gb.summary");
cebf45 179     }
fc8426 180
ef5c58 181     private void insertActivityGraph(List<Metric> metrics) {
2a7306 182         if ((metrics != null) && (metrics.size() > 0)
JM 183                 && GitBlit.getBoolean(Keys.web.generateActivityGraph, true)) {
f1720c 184             IChartData data = WicketUtils.getChartData(metrics);
fc8426 185
2a7306 186             ChartProvider provider = new ChartProvider(new Dimension(400, 100), ChartType.LINE,
JM 187                     data);
fc8426 188             ChartAxis dateAxis = new ChartAxis(ChartAxisType.BOTTOM);
2a7306 189             dateAxis.setLabels(new String[] { metrics.get(0).name,
JM 190                     metrics.get(metrics.size() / 2).name, metrics.get(metrics.size() - 1).name });
fc8426 191             provider.addAxis(dateAxis);
JM 192
008322 193             ChartAxis commitAxis = new ChartAxis(ChartAxisType.LEFT);
a1ea87 194             commitAxis.setLabels(new String[] { "",
JM 195                     String.valueOf((int) WicketUtils.maxValue(metrics)) });
fc8426 196             provider.addAxis(commitAxis);
8a2e9c 197             provider.setLineStyles(new LineStyle[] { new LineStyle(2, 4, 0), new LineStyle(0, 4, 1) });
f5d0ad 198             provider.addShapeMarker(new ShapeMarker(MarkerType.CIRCLE, Color.BLUE, 1, -1, 5));
8a2e9c 199
fc8426 200             add(new Chart("commitsChart", provider));
JM 201         } else {
1e8390 202             add(WicketUtils.newBlankImage("commitsChart"));
fc8426 203         }
JM 204     }
5fe7df 205 }