View.java 5.4 KB
Newer Older
K
kohsuke 已提交
1 2 3
package hudson.model;

import hudson.Util;
4 5
import hudson.scm.ChangeLogSet.Entry;
import hudson.util.RunList;
K
kohsuke 已提交
6 7 8 9 10
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;

import javax.servlet.ServletException;
import java.io.IOException;
11 12 13 14 15 16 17
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.GregorianCalendar;
import java.util.HashMap;
K
kohsuke 已提交
18
import java.util.List;
19
import java.util.Map;
K
kohsuke 已提交
20 21

/**
22 23
 * Encapsulates the rendering of the list of {@link TopLevelItem}s
 * that {@link Hudson} owns.
K
kohsuke 已提交
24 25 26
 *
 * @author Kohsuke Kawaguchi
 */
27
public abstract class View extends AbstractModelObject {
K
kohsuke 已提交
28 29

    /**
30
     * Gets all the items in this collection in a read-only view.
K
kohsuke 已提交
31
     */
32
    public abstract Collection<TopLevelItem> getItems();
K
kohsuke 已提交
33 34

    /**
35
     * Checks if the job is in this collection.
K
kohsuke 已提交
36
     */
37
    public abstract boolean contains(TopLevelItem item);
K
kohsuke 已提交
38 39

    /**
40
     * Gets the name of all this collection.
K
kohsuke 已提交
41
     */
42
    public abstract String getViewName();
K
kohsuke 已提交
43

44 45 46 47
    /**
     * Message displayed in the top page. Can be null. Includes HTML.
     */
    public abstract String getDescription();
K
kohsuke 已提交
48 49

    /**
50
     * Returns the path relative to the context root.
K
kohsuke 已提交
51
     */
52
    public abstract String getUrl();
53

54 55 56 57
    public static final class UserInfo implements Comparable<UserInfo> {
        private final User user;
        private Calendar lastChange;
        private Project project;
K
kohsuke 已提交
58

59 60 61 62 63
        UserInfo(User user, Project p, Calendar lastChange) {
            this.user = user;
            this.project = p;
            this.lastChange = lastChange;
        }
K
kohsuke 已提交
64

65 66 67
        public User getUser() {
            return user;
        }
K
kohsuke 已提交
68

69 70 71
        public Calendar getLastChange() {
            return lastChange;
        }
K
kohsuke 已提交
72

73 74 75
        public Project getProject() {
            return project;
        }
K
kohsuke 已提交
76

77 78 79 80 81 82 83
        /**
         * Returns a human-readable string representation of when this user was last active.
         */
        public String getLastChangeTimeString() {
            long duration = new GregorianCalendar().getTimeInMillis()-lastChange.getTimeInMillis();
            return Util.getTimeSpanString(duration);
        }
K
kohsuke 已提交
84

85 86
        public String getTimeSortKey() {
            return Util.XS_DATETIME_FORMATTER.format(lastChange.getTime());
K
kohsuke 已提交
87 88
        }

89 90 91
        public int compareTo(UserInfo that) {
            return that.lastChange.compareTo(this.lastChange);
        }
K
kohsuke 已提交
92 93 94
    }

    /**
95
     * Does this {@link View} has any associated user information recorded?
K
kohsuke 已提交
96
     */
97
    public final boolean hasPeople() {
98
        for (Item item : getItems()) {
99 100 101 102 103 104 105 106 107
            for (Job job : item.getAllJobs()) {
                if (job instanceof Project) {
                    Project p = (Project) job;
                    for (Build build : p.getBuilds()) {
                        for (Entry entry : build.getChangeSet()) {
                            User user = entry.getAuthor();
                            if(user!=null)
                                return true;
                        }
108 109 110
                    }
                }
            }
K
kohsuke 已提交
111
        }
112 113
        return false;
    }
K
kohsuke 已提交
114

115 116 117 118 119
    /**
     * Gets the users that show up in the changelog of this job collection.
     */
    public final List<UserInfo> getPeople() {
        Map<User,UserInfo> users = new HashMap<User,UserInfo>();
120
        for (Item item : getItems()) {
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
            for (Job job : item.getAllJobs()) {
                if (job instanceof Project) {
                    Project p = (Project) job;
                    for (Build build : p.getBuilds()) {
                        for (Entry entry : build.getChangeSet()) {
                            User user = entry.getAuthor();

                            UserInfo info = users.get(user);
                            if(info==null)
                                users.put(user,new UserInfo(user,p,build.getTimestamp()));
                            else
                            if(info.getLastChange().before(build.getTimestamp())) {
                                info.project = p;
                                info.lastChange = build.getTimestamp();
                            }
136 137 138 139 140
                        }
                    }
                }
            }
        }
K
kohsuke 已提交
141

142 143
        List<UserInfo> r = new ArrayList<UserInfo>(users.values());
        Collections.sort(r);
K
kohsuke 已提交
144

145
        return r;
K
kohsuke 已提交
146 147 148
    }

    /**
149
     * Creates a new {@link Item} in this collection.
150 151 152
     *
     * @return
     *      null if fails.
K
kohsuke 已提交
153
     */
154
    public abstract Item doCreateViewItem( StaplerRequest req, StaplerResponse rsp ) throws IOException, ServletException;
155 156

    public void doRssAll( StaplerRequest req, StaplerResponse rsp ) throws IOException, ServletException {
157
        rss(req, rsp, " all builds", new RunList(this));
K
kohsuke 已提交
158 159
    }

160
    public void doRssFailed( StaplerRequest req, StaplerResponse rsp ) throws IOException, ServletException {
161
        rss(req, rsp, " failed builds", new RunList(this).failureOnly());
162
    }
K
kohsuke 已提交
163

164 165 166
    private void rss(StaplerRequest req, StaplerResponse rsp, String suffix, RunList runs) throws IOException, ServletException {
        RSS.forwardToRss(getDisplayName()+ suffix, getUrl(),
            runs.newBuilds(), Run.FEED_ADAPTER, req, rsp );
K
kohsuke 已提交
167
    }
168 169 170 171 172 173

    public static final Comparator<View> SORTER = new Comparator<View>() {
        public int compare(View lhs, View rhs) {
            return lhs.getViewName().compareTo(rhs.getViewName());
        }
    };
K
kohsuke 已提交
174
}