You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ProjectsPage.java 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. * Copyright 2012 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. */
  16. package com.gitblit.wicket.pages;
  17. import java.io.File;
  18. import java.io.FileInputStream;
  19. import java.io.InputStream;
  20. import java.io.InputStreamReader;
  21. import java.text.MessageFormat;
  22. import java.util.ArrayList;
  23. import java.util.List;
  24. import org.apache.wicket.Component;
  25. import org.apache.wicket.PageParameters;
  26. import org.apache.wicket.markup.html.basic.Label;
  27. import org.apache.wicket.markup.repeater.Item;
  28. import org.apache.wicket.markup.repeater.data.DataView;
  29. import org.apache.wicket.markup.repeater.data.ListDataProvider;
  30. import org.apache.wicket.resource.ContextRelativeResource;
  31. import org.apache.wicket.util.resource.ResourceStreamNotFoundException;
  32. import org.eclipse.jgit.lib.Constants;
  33. import com.gitblit.GitBlit;
  34. import com.gitblit.Keys;
  35. import com.gitblit.models.ProjectModel;
  36. import com.gitblit.models.UserModel;
  37. import com.gitblit.utils.MarkdownUtils;
  38. import com.gitblit.utils.StringUtils;
  39. import com.gitblit.wicket.GitBlitWebSession;
  40. import com.gitblit.wicket.PageRegistration;
  41. import com.gitblit.wicket.PageRegistration.DropDownMenuItem;
  42. import com.gitblit.wicket.PageRegistration.DropDownMenuRegistration;
  43. import com.gitblit.wicket.WicketUtils;
  44. import com.gitblit.wicket.panels.LinkPanel;
  45. public class ProjectsPage extends RootPage {
  46. List<ProjectModel> projectModels = new ArrayList<ProjectModel>();
  47. public ProjectsPage() {
  48. super();
  49. setup(null);
  50. }
  51. public ProjectsPage(PageParameters params) {
  52. super(params);
  53. setup(params);
  54. }
  55. @Override
  56. protected boolean reusePageParameters() {
  57. return true;
  58. }
  59. @Override
  60. protected List<ProjectModel> getProjectModels() {
  61. final UserModel user = GitBlitWebSession.get().getUser();
  62. List<ProjectModel> projects = GitBlit.self().getProjectModels(user, false);
  63. return projects;
  64. }
  65. private void setup(PageParameters params) {
  66. setupPage("", "");
  67. // check to see if we should display a login message
  68. boolean authenticateView = GitBlit.getBoolean(Keys.web.authenticateViewPages, true);
  69. if (authenticateView && !GitBlitWebSession.get().isLoggedIn()) {
  70. String messageSource = GitBlit.getString(Keys.web.loginMessage, "gitblit");
  71. String message = readMarkdown(messageSource, "login.mkd");
  72. Component repositoriesMessage = new Label("projectsMessage", message);
  73. add(repositoriesMessage.setEscapeModelStrings(false));
  74. add(new Label("projectsPanel"));
  75. return;
  76. }
  77. // Load the markdown welcome message
  78. String messageSource = GitBlit.getString(Keys.web.repositoriesMessage, "gitblit");
  79. String message = readMarkdown(messageSource, "welcome.mkd");
  80. Component projectsMessage = new Label("projectsMessage", message).setEscapeModelStrings(
  81. false).setVisible(message.length() > 0);
  82. add(projectsMessage);
  83. List<ProjectModel> projects = getProjects(params);
  84. ListDataProvider<ProjectModel> dp = new ListDataProvider<ProjectModel>(projects);
  85. DataView<ProjectModel> dataView = new DataView<ProjectModel>("project", dp) {
  86. private static final long serialVersionUID = 1L;
  87. int counter;
  88. @Override
  89. protected void onBeforeRender() {
  90. super.onBeforeRender();
  91. counter = 0;
  92. }
  93. public void populateItem(final Item<ProjectModel> item) {
  94. final ProjectModel entry = item.getModelObject();
  95. PageParameters pp = WicketUtils.newProjectParameter(entry.name);
  96. item.add(new LinkPanel("projectTitle", "list", entry.getDisplayName(),
  97. ProjectPage.class, pp));
  98. item.add(new LinkPanel("projectDescription", "list", entry.description,
  99. ProjectPage.class, pp));
  100. item.add(new Label("repositoryCount", entry.repositories.size()
  101. + " "
  102. + (entry.repositories.size() == 1 ? getString("gb.repository")
  103. : getString("gb.repositories"))));
  104. String lastChange;
  105. if (entry.lastChange.getTime() == 0) {
  106. lastChange = "--";
  107. } else {
  108. lastChange = getTimeUtils().timeAgo(entry.lastChange);
  109. }
  110. Label lastChangeLabel = new Label("projectLastChange", lastChange);
  111. item.add(lastChangeLabel);
  112. WicketUtils.setCssClass(lastChangeLabel, getTimeUtils()
  113. .timeAgoCss(entry.lastChange));
  114. WicketUtils.setAlternatingBackground(item, counter);
  115. counter++;
  116. }
  117. };
  118. add(dataView);
  119. // push the panel down if we are hiding the admin controls and the
  120. // welcome message
  121. if (!showAdmin && !projectsMessage.isVisible()) {
  122. WicketUtils.setCssStyle(dataView, "padding-top:5px;");
  123. }
  124. }
  125. @Override
  126. protected void addDropDownMenus(List<PageRegistration> pages) {
  127. PageParameters params = getPageParameters();
  128. pages.add(0, new PageRegistration("gb.projects", ProjectsPage.class, params));
  129. DropDownMenuRegistration menu = new DropDownMenuRegistration("gb.filters",
  130. ProjectsPage.class);
  131. // preserve time filter option on repository choices
  132. menu.menuItems.addAll(getRepositoryFilterItems(params));
  133. // preserve repository filter option on time choices
  134. menu.menuItems.addAll(getTimeFilterItems(params));
  135. if (menu.menuItems.size() > 0) {
  136. // Reset Filter
  137. menu.menuItems.add(new DropDownMenuItem(getString("gb.reset"), null, null));
  138. }
  139. pages.add(menu);
  140. }
  141. private String readMarkdown(String messageSource, String resource) {
  142. String message = "";
  143. if (messageSource.equalsIgnoreCase("gitblit")) {
  144. // Read default message
  145. message = readDefaultMarkdown(resource);
  146. } else {
  147. // Read user-supplied message
  148. if (!StringUtils.isEmpty(messageSource)) {
  149. File file = new File(messageSource);
  150. if (file.exists()) {
  151. try {
  152. FileInputStream fis = new FileInputStream(file);
  153. InputStreamReader reader = new InputStreamReader(fis,
  154. Constants.CHARACTER_ENCODING);
  155. message = MarkdownUtils.transformMarkdown(reader);
  156. reader.close();
  157. } catch (Throwable t) {
  158. message = getString("gb.failedToRead") + " " + file;
  159. warn(message, t);
  160. }
  161. } else {
  162. message = messageSource + " " + getString("gb.isNotValidFile");
  163. }
  164. }
  165. }
  166. return message;
  167. }
  168. private String readDefaultMarkdown(String file) {
  169. String content = readDefaultMarkdown(file, getLanguageCode());
  170. if (StringUtils.isEmpty(content)) {
  171. content = readDefaultMarkdown(file, null);
  172. }
  173. return content;
  174. }
  175. private String readDefaultMarkdown(String file, String lc) {
  176. if (!StringUtils.isEmpty(lc)) {
  177. // convert to file_lc.mkd
  178. file = file.substring(0, file.lastIndexOf('.')) + "_" + lc
  179. + file.substring(file.lastIndexOf('.'));
  180. }
  181. String message;
  182. try {
  183. ContextRelativeResource res = WicketUtils.getResource(file);
  184. InputStream is = res.getResourceStream().getInputStream();
  185. InputStreamReader reader = new InputStreamReader(is, Constants.CHARACTER_ENCODING);
  186. message = MarkdownUtils.transformMarkdown(reader);
  187. reader.close();
  188. } catch (ResourceStreamNotFoundException t) {
  189. if (lc == null) {
  190. // could not find default language resource
  191. message = MessageFormat.format(getString("gb.failedToReadMessage"), file);
  192. error(message, t, false);
  193. } else {
  194. // ignore so we can try default language resource
  195. message = null;
  196. }
  197. } catch (Throwable t) {
  198. message = MessageFormat.format(getString("gb.failedToReadMessage"), file);
  199. error(message, t, false);
  200. }
  201. return message;
  202. }
  203. }