1 package hudson.model; 2 3 import hudson.Util; 4 import hudson.scm.ChangeLogSet.Entry; 5 import hudson.util.RunList; 6 import org.kohsuke.stapler.StaplerRequest; 7 import org.kohsuke.stapler.StaplerResponse; 8 9 import javax.servlet.ServletException ; 10 import java.io.IOException ; 11 import java.util.ArrayList ; 12 import java.util.Calendar ; 13 import java.util.Collection ; 14 import java.util.Collections ; 15 import java.util.Comparator ; 16 import java.util.GregorianCalendar ; 17 import java.util.HashMap ; 18 import java.util.List ; 19 import java.util.Map ; 20 21 27 public abstract class View extends AbstractModelObject { 28 29 32 public abstract Collection <TopLevelItem> getItems(); 33 34 37 public abstract boolean contains(TopLevelItem item); 38 39 42 public abstract String getViewName(); 43 44 47 public abstract String getDescription(); 48 49 52 public abstract String getUrl(); 53 54 public static final class UserInfo implements Comparable <UserInfo> { 55 private final User user; 56 private Calendar lastChange; 57 private Project project; 58 59 UserInfo(User user, Project p, Calendar lastChange) { 60 this.user = user; 61 this.project = p; 62 this.lastChange = lastChange; 63 } 64 65 public User getUser() { 66 return user; 67 } 68 69 public Calendar getLastChange() { 70 return lastChange; 71 } 72 73 public Project getProject() { 74 return project; 75 } 76 77 80 public String getLastChangeTimeString() { 81 long duration = new GregorianCalendar ().getTimeInMillis()-lastChange.getTimeInMillis(); 82 return Util.getTimeSpanString(duration); 83 } 84 85 public String getTimeSortKey() { 86 return Util.XS_DATETIME_FORMATTER.format(lastChange.getTime()); 87 } 88 89 public int compareTo(UserInfo that) { 90 long rhs = that.lastChange.getTimeInMillis(); 91 long lhs = this.lastChange.getTimeInMillis(); 92 if(rhs>lhs) return 1; 93 if(rhs<lhs) return -1; 94 return 0; 95 } 96 } 97 98 101 public final boolean hasPeople() { 102 for (Item item : getItems()) { 103 for (Job job : item.getAllJobs()) { 104 if (job instanceof Project) { 105 Project p = (Project) job; 106 for (Build build : p.getBuilds()) { 107 for (Entry entry : build.getChangeSet()) { 108 User user = entry.getAuthor(); 109 if(user!=null) 110 return true; 111 } 112 } 113 } 114 } 115 } 116 return false; 117 } 118 119 122 public final List<UserInfo> getPeople() { 123 Map <User,UserInfo> users = new HashMap <User,UserInfo>(); 124 for (Item item : getItems()) { 125 for (Job job : item.getAllJobs()) { 126 if (job instanceof Project) { 127 Project p = (Project) job; 128 for (Build build : p.getBuilds()) { 129 for (Entry entry : build.getChangeSet()) { 130 User user = entry.getAuthor(); 131 132 UserInfo info = users.get(user); 133 if(info==null) 134 users.put(user,new UserInfo(user,p,build.getTimestamp())); 135 else 136 if(info.getLastChange().before(build.getTimestamp())) { 137 info.project = p; 138 info.lastChange = build.getTimestamp(); 139 } 140 } 141 } 142 } 143 } 144 } 145 146 List<UserInfo> r = new ArrayList <UserInfo>(users.values()); 147 Collections.sort(r); 148 149 return r; 150 } 151 152 158 public abstract Item doCreateItem( StaplerRequest req, StaplerResponse rsp ) throws IOException , ServletException ; 159 160 public void doRssAll( StaplerRequest req, StaplerResponse rsp ) throws IOException , ServletException { 161 rss(req, rsp, " all builds", new RunList(this)); 162 } 163 164 public void doRssFailed( StaplerRequest req, StaplerResponse rsp ) throws IOException , ServletException { 165 rss(req, rsp, " failed builds", new RunList(this).failureOnly()); 166 } 167 168 private void rss(StaplerRequest req, StaplerResponse rsp, String suffix, RunList runs) throws IOException , ServletException { 169 RSS.forwardToRss(getDisplayName()+ suffix, getUrl(), 170 runs.newBuilds(), Run.FEED_ADAPTER, req, rsp ); 171 } 172 173 public static final Comparator <View> SORTER = new Comparator <View>() { 174 public int compare(View lhs, View rhs) { 175 return lhs.getViewName().compareTo(rhs.getViewName()); 176 } 177 }; 178 } 179 | Popular Tags |