KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > hudson > model > View


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 JavaDoc;
10 import java.io.IOException JavaDoc;
11 import java.util.ArrayList JavaDoc;
12 import java.util.Calendar JavaDoc;
13 import java.util.Collection JavaDoc;
14 import java.util.Collections JavaDoc;
15 import java.util.Comparator JavaDoc;
16 import java.util.GregorianCalendar JavaDoc;
17 import java.util.HashMap JavaDoc;
18 import java.util.List JavaDoc;
19 import java.util.Map JavaDoc;
20
21 /**
22  * Encapsulates the rendering of the list of {@link TopLevelItem}s
23  * that {@link Hudson} owns.
24  *
25  * @author Kohsuke Kawaguchi
26  */

27 public abstract class View extends AbstractModelObject {
28
29     /**
30      * Gets all the items in this collection in a read-only view.
31      */

32     public abstract Collection JavaDoc<TopLevelItem> getItems();
33
34     /**
35      * Checks if the job is in this collection.
36      */

37     public abstract boolean contains(TopLevelItem item);
38
39     /**
40      * Gets the name of all this collection.
41      */

42     public abstract String JavaDoc getViewName();
43
44     /**
45      * Message displayed in the top page. Can be null. Includes HTML.
46      */

47     public abstract String JavaDoc getDescription();
48
49     /**
50      * Returns the path relative to the context root.
51      */

52     public abstract String JavaDoc getUrl();
53
54     public static final class UserInfo implements Comparable JavaDoc<UserInfo> {
55         private final User user;
56         private Calendar JavaDoc lastChange;
57         private Project project;
58
59         UserInfo(User user, Project p, Calendar JavaDoc 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 JavaDoc getLastChange() {
70             return lastChange;
71         }
72
73         public Project getProject() {
74             return project;
75         }
76
77         /**
78          * Returns a human-readable string representation of when this user was last active.
79          */

80         public String JavaDoc getLastChangeTimeString() {
81             long duration = new GregorianCalendar JavaDoc().getTimeInMillis()-lastChange.getTimeInMillis();
82             return Util.getTimeSpanString(duration);
83         }
84
85         public String JavaDoc 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     /**
99      * Does this {@link View} has any associated user information recorded?
100      */

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     /**
120      * Gets the users that show up in the changelog of this job collection.
121      */

122     public final List<UserInfo> getPeople() {
123         Map JavaDoc<User,UserInfo> users = new HashMap JavaDoc<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 JavaDoc<UserInfo>(users.values());
147         Collections.sort(r);
148
149         return r;
150     }
151
152     /**
153      * Creates a new {@link Item} in this collection.
154      *
155      * @return
156      * null if fails.
157      */

158     public abstract Item doCreateItem( StaplerRequest req, StaplerResponse rsp ) throws IOException JavaDoc, ServletException JavaDoc;
159
160     public void doRssAll( StaplerRequest req, StaplerResponse rsp ) throws IOException JavaDoc, ServletException JavaDoc {
161         rss(req, rsp, " all builds", new RunList(this));
162     }
163
164     public void doRssFailed( StaplerRequest req, StaplerResponse rsp ) throws IOException JavaDoc, ServletException JavaDoc {
165         rss(req, rsp, " failed builds", new RunList(this).failureOnly());
166     }
167
168     private void rss(StaplerRequest req, StaplerResponse rsp, String JavaDoc suffix, RunList runs) throws IOException JavaDoc, ServletException JavaDoc {
169         RSS.forwardToRss(getDisplayName()+ suffix, getUrl(),
170             runs.newBuilds(), Run.FEED_ADAPTER, req, rsp );
171     }
172
173     public static final Comparator JavaDoc<View> SORTER = new Comparator JavaDoc<View>() {
174         public int compare(View lhs, View rhs) {
175             return lhs.getViewName().compareTo(rhs.getViewName());
176         }
177     };
178 }
179
Popular Tags