diff --git a/core/src/main/java/hudson/model/AbstractItem.java b/core/src/main/java/hudson/model/AbstractItem.java index 162fef555b..1f238e1d8d 100644 --- a/core/src/main/java/hudson/model/AbstractItem.java +++ b/core/src/main/java/hudson/model/AbstractItem.java @@ -1,8 +1,8 @@ /* * The MIT License * - * Copyright (c) 2004-2010, Sun Microsystems, Inc., Kohsuke Kawaguchi, - * Daniel Dyer, Tom Huybrechts + * Copyright (c) 2004-2011, Sun Microsystems, Inc., Kohsuke Kawaguchi, + * Daniel Dyer, Tom Huybrechts, Yahoo!, Inc. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -34,6 +34,7 @@ import hudson.cli.declarative.CLIMethod; import hudson.cli.declarative.CLIResolver; import hudson.model.listeners.ItemListener; import hudson.model.listeners.SaveableListener; +import hudson.search.SearchIndexBuilder; import hudson.security.AccessControlled; import hudson.security.Permission; import hudson.security.ACL; @@ -91,6 +92,8 @@ public abstract class AbstractItem extends Actionable implements Item, HttpDelet protected volatile String description; private transient ItemGroup parent; + + protected String displayName; protected AbstractItem(ItemGroup parent, String name) { this.parent = parent; @@ -116,9 +119,18 @@ public abstract class AbstractItem extends Actionable implements Item, HttpDelet @Exported public String getDisplayName() { + if(null!=displayName) { + return displayName; + } + // if the displayName is not set, then return the name as we use to do return getName(); } - + + public void setDisplayName(String displayName) throws IOException { + this.displayName = displayName; + save(); + } + public File getRootDir() { return parent.getRootDirFor(this); } @@ -548,6 +560,18 @@ public abstract class AbstractItem extends Actionable implements Item, HttpDelet out.abort(); // don't leave anything behind } } + + + /* (non-Javadoc) + * @see hudson.model.AbstractModelObject#getSearchName() + */ + @Override + public String getSearchName() { + // the search name of abstract items should be the name and not display name. + // this will make suggestions use the names and not the display name + // so that the links will 302 directly to the thing the user was finding + return getName(); + } public String toString() { return super.toString()+'['+getFullName()+']'; diff --git a/core/src/main/java/hudson/model/Job.java b/core/src/main/java/hudson/model/Job.java index b0d8d10975..c61281124e 100644 --- a/core/src/main/java/hudson/model/Job.java +++ b/core/src/main/java/hudson/model/Job.java @@ -941,6 +941,21 @@ public abstract class Job, RunT extends Run, RunT extends Run, ExtensionPoint, Saveable { + + private final static Logger logger = Logger.getLogger(View.class.getName()); + /** * Container of this view. Set right after the construction * and never change thereafter. @@ -672,14 +678,34 @@ public abstract class View extends AbstractModelObject implements AccessControll } } - + void addDisplayNamesToSearchIndex(SearchIndexBuilder sib, Collection items) { + for(TopLevelItem item : items) { + + if(logger.isLoggable(Level.FINE)) { + logger.fine((String.format("Adding url=%s,displayName=%s", + item.getSearchUrl(), item.getDisplayName()))); + } + sib.add(item.getSearchUrl(), item.getDisplayName()); + } + } + @Override public SearchIndexBuilder makeSearchIndex() { - return super.makeSearchIndex() - .add(new CollectionSearchIndex() {// for jobs in the view + SearchIndexBuilder sib = super.makeSearchIndex(); + sib.add(new CollectionSearchIndex() {// for jobs in the view protected TopLevelItem get(String key) { return getItem(key); } - protected Collection all() { return getItems(); } + protected Collection all() { return getItems(); } + @Override + protected String getName(TopLevelItem o) { + // return the name instead of the display for suggestion searching + return o.getName(); + } }); + + // add the display name for each item in the search index + addDisplayNamesToSearchIndex(sib, getItems()); + + return sib; } /** diff --git a/core/src/main/java/hudson/search/Search.java b/core/src/main/java/hudson/search/Search.java index 55737b8942..05287c3a2d 100644 --- a/core/src/main/java/hudson/search/Search.java +++ b/core/src/main/java/hudson/search/Search.java @@ -1,7 +1,8 @@ /* * The MIT License * - * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi + * Copyright (c) 2004-2011, Sun Microsystems, Inc., Kohsuke Kawaguchi, + * Yahoo!, Inc. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -25,6 +26,7 @@ package hudson.search; import static javax.servlet.http.HttpServletResponse.SC_NOT_FOUND; +import hudson.Util; import hudson.util.EditDistance; import java.io.IOException; import java.util.AbstractList; @@ -33,6 +35,9 @@ import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.Set; +import java.util.logging.Level; +import java.util.logging.Logger; + import javax.servlet.ServletException; import org.kohsuke.stapler.Ancestor; import org.kohsuke.stapler.QueryParameter; @@ -49,12 +54,17 @@ import org.kohsuke.stapler.export.Flavor; * @author Kohsuke Kawaguchi */ public class Search { + private final static Logger logger = Logger.getLogger(Search.class.getName()); + public void doIndex(StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException { List l = req.getAncestors(); for( int i=l.size()-1; i>=0; i-- ) { Ancestor a = l.get(i); if (a.getObject() instanceof SearchableModelObject) { SearchableModelObject smo = (SearchableModelObject) a.getObject(); + if(logger.isLoggable(Level.FINE)){ + logger.fine(String.format("smo.displayName=%s, searchName=%s",smo.getDisplayName(), smo.getSearchName())); + } SearchIndex index = smo.getSearchIndex(); String query = req.getParameter("q"); @@ -171,12 +181,49 @@ public class Search { } /** - * Performs a search and returns the match, or null if no match was found. + * When there are mutiple suggested items, this method can narrow down the resultset + * to the SuggestedItem that has a url that contains the query. This is useful is one + * job has a display name that matches another job's project name. + * @param r A list of Suggested items. It is assumed that there is at least one + * SuggestedItem in r. + * @param query A query string + * @return Returns the SuggestedItem which has a search url that contains the query. + * If no SuggestedItems have a search url which contains the query, then the first + * SuggestedItem in the List is returned. + */ + static SuggestedItem findClosestSuggestedItem(List r, String query) { + for(SuggestedItem curItem : r) { + if(logger.isLoggable(Level.FINE)) { + logger.fine(String.format("item's searchUrl:%s;query=%s", curItem.item.getSearchUrl(), query)); + } + if(curItem.item.getSearchUrl().contains(Util.rawEncode(query))) { + return curItem; + } + } + + // couldn't find an item with the query in the url so just + // return the first one + return r.get(0); + } + + /** + * Performs a search and returns the match, or null if no match was found + * or more than one match was found */ public static SuggestedItem find(SearchIndex index, String query) { List r = find(Mode.FIND, index, query); - if(r.isEmpty()) return null; - else return r.get(0); + if(r.isEmpty()){ + return null; + } + else if(1==r.size()){ + return r.get(0); + } + else { + // we have more than one suggested item, so return the item who's url + // contains the query as this is probably the job's name + return findClosestSuggestedItem(r, query); + } + } public static List suggest(SearchIndex index, final String tokenList) { @@ -244,6 +291,18 @@ public class Search { } }; } + + + public String toString() { + StringBuilder s = new StringBuilder("TokenList{"); + for(String token : tokens) { + s.append(token); + s.append(","); + } + s.append('}'); + + return s.toString(); + } } private static List find(Mode m, SearchIndex index, String tokenList) { @@ -256,14 +315,19 @@ public class Search { List items = new ArrayList(); // items found in 1 step - + if(logger.isLoggable(Level.FINE)) { + logger.fine("tokens="+tokens.toString()); + } + // first token int w=1; // width of token for (String token : tokens.subSequence(0)) { items.clear(); m.find(index,token,items); - for (SearchItem si : items) + for (SearchItem si : items) { paths[w].add(new SuggestedItem(si)); + logger.info("found search item:"+si.getSearchName()); + } w++; } @@ -285,4 +349,5 @@ public class Search { return paths[tokens.length()]; } + } diff --git a/core/src/main/java/jenkins/model/Jenkins.java b/core/src/main/java/jenkins/model/Jenkins.java index b5611e9a23..9adcd0f6c8 100644 --- a/core/src/main/java/jenkins/model/Jenkins.java +++ b/core/src/main/java/jenkins/model/Jenkins.java @@ -1,9 +1,10 @@ /* * The MIT License * - * Copyright (c) 2004-2010, Sun Microsystems, Inc., Kohsuke Kawaguchi, + * Copyright (c) 2004-2011, Sun Microsystems, Inc., Kohsuke Kawaguchi, * Erik Ramfelt, Koichi Fujikawa, Red Hat, Inc., Seiji Sogabe, - * Stephen Connolly, Tom Huybrechts, Yahoo! Inc., Alan Harder, CloudBees, Inc. + * Stephen Connolly, Tom Huybrechts, Yahoo! Inc., Alan Harder, CloudBees, Inc., + * Yahoo!, Inc. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -3396,6 +3397,85 @@ public class Jenkins extends AbstractCIBase implements ModifiableItemGroup itemCollection = items.values(); + + // if there are a lot of projects, we'll have to store their + // display names in a HashSet or something for a quick check + for(TopLevelItem item : itemCollection) { + if(item.getName().equals(currentJobName)) { + // we won't compare the candidate displayName against the current + // item. This is to prevent an validation warning if the user + // sets the displayName to what the existing display name is + continue; + } + else if(displayName.equals(item.getDisplayName())) { + return false; + } + } + + return true; + } + + /** + * True if there is no item in Jenkins that has this name + * @param name The name to test + * @param currentJobName The name of the job that the user is configuring + * @return + */ + boolean isNameUnique(String name, String currentJobName) { + Item item = getItem(name); + + if(null==item) { + // the candidate name didn't return any items so the name is unique + return true; + } + else if(item.getName().equals(currentJobName)) { + // the candidate name returned an item, but the item is the item + // that the user is configuring so this is ok + return true; + } + else { + // the candidate name returned an item, so it is not unique + return false; + } + } + + /** + * Checks to see if the candidate displayName collides with any + * existing display names or project names + * @param displayName The display name to test + * @param jobName The name of the job the user is configuring + * @return + */ + public FormValidation doCheckDisplayName(@QueryParameter String displayName, + @QueryParameter String jobName) { + displayName = displayName.trim(); + + if(LOGGER.isLoggable(Level.FINE)) { + LOGGER.log(Level.FINE, "Current job name is " + jobName); + } + + if(!isNameUnique(displayName, jobName)) { + return FormValidation.warning(Messages.Jenkins_CheckDisplayName_NameNotUniqueWarning(displayName)); + } + else if(!isDisplayNameUnique(displayName, jobName)){ + return FormValidation.warning(Messages.Jenkins_CheckDisplayName_DisplayNameNotUniqueWarning(displayName)); + } + else { + return FormValidation.ok(); + } + } + public static class MasterComputer extends Computer { protected MasterComputer() { super(Jenkins.getInstance()); diff --git a/core/src/main/resources/hudson/matrix/MatrixProject/configure-entries.jelly b/core/src/main/resources/hudson/matrix/MatrixProject/configure-entries.jelly index 0a13bae520..a337b52441 100644 --- a/core/src/main/resources/hudson/matrix/MatrixProject/configure-entries.jelly +++ b/core/src/main/resources/hudson/matrix/MatrixProject/configure-entries.jelly @@ -1,7 +1,8 @@