PageRenderTime 42ms CodeModel.GetById 15ms app.highlight 20ms RepoModel.GetById 1ms app.codeStats 1ms

/hudson-core/src/main/java/hudson/model/ProxyView.java

http://github.com/hudson/hudson
Java | 146 lines | 86 code | 22 blank | 38 comment | 10 complexity | 59163838f4d28d2062c7d5d5f2cb30f4 MD5 | raw file
  1/*
  2 * The MIT License
  3 * 
  4 * Copyright (c) 2004-2009, Sun Microsystems, Inc., Tom Huybrechts
  5 * 
  6 * Permission is hereby granted, free of charge, to any person obtaining a copy
  7 * of this software and associated documentation files (the "Software"), to deal
  8 * in the Software without restriction, including without limitation the rights
  9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 10 * copies of the Software, and to permit persons to whom the Software is
 11 * furnished to do so, subject to the following conditions:
 12 * 
 13 * The above copyright notice and this permission notice shall be included in
 14 * all copies or substantial portions of the Software.
 15 * 
 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 22 * THE SOFTWARE.
 23 */
 24package hudson.model;
 25
 26import hudson.Extension;
 27import hudson.Util;
 28import hudson.model.Descriptor.FormException;
 29import hudson.util.FormValidation;
 30import java.io.IOException;
 31import java.util.Collection;
 32import javax.servlet.ServletException;
 33import org.kohsuke.stapler.DataBoundConstructor;
 34import org.kohsuke.stapler.QueryParameter;
 35import org.kohsuke.stapler.Stapler;
 36import org.kohsuke.stapler.StaplerFallback;
 37import org.kohsuke.stapler.StaplerRequest;
 38import org.kohsuke.stapler.StaplerResponse;
 39
 40/**
 41 * A view that delegates to another.
 42 * 
 43 * TODO: this does not respond to renaming or deleting the proxied view.
 44 * 
 45 * @author Tom Huybrechts
 46 *
 47 */
 48public class ProxyView extends View implements StaplerFallback {
 49
 50    private String proxiedViewName;
 51
 52    @DataBoundConstructor
 53    public ProxyView(String name) {
 54        super(name);
 55
 56        if (Hudson.getInstance().getView(name) != null) {
 57            // if this is a valid global view name, let's assume the
 58            // user wants to show it
 59            proxiedViewName = name;
 60        }
 61    }
 62
 63    public View getProxiedView() {
 64        if (proxiedViewName == null) {
 65            // just so we avoid errors just after creation
 66            return Hudson.getInstance().getPrimaryView();
 67        } else {
 68            return Hudson.getInstance().getView(proxiedViewName);
 69        }
 70    }
 71
 72    public String getProxiedViewName() {
 73        return proxiedViewName;
 74    }
 75
 76    public void setProxiedViewName(String proxiedViewName) {
 77        this.proxiedViewName = proxiedViewName;
 78    }
 79
 80    @Override
 81    public Collection<TopLevelItem> getItems() {
 82        return getProxiedView().getItems();
 83    }
 84
 85    @Override
 86    public boolean contains(TopLevelItem item) {
 87        return getProxiedView().contains(item);
 88    }
 89
 90    @Override
 91    public void onJobRenamed(Item item, String oldName, String newName) {
 92        if (oldName.equals(proxiedViewName)) {
 93            proxiedViewName = newName;
 94        }
 95    }
 96
 97    @Override
 98    protected void submit(StaplerRequest req) throws IOException, ServletException, FormException {
 99        String proxiedViewName = req.getSubmittedForm().getString("proxiedViewName");
100        if (Hudson.getInstance().getView(proxiedViewName) == null) {
101            throw new FormException("Not an existing global view", "proxiedViewName");
102        }
103        this.proxiedViewName = proxiedViewName;
104    }
105
106    @Override
107    public Item doCreateItem(StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException {
108        return getProxiedView().doCreateItem(req, rsp);
109    }
110
111    /**
112     * Fails if a global view with the given name does not exist.
113     */
114    public FormValidation doViewExistsCheck(@QueryParameter String value) {
115        checkPermission(View.CREATE);
116
117        String view = Util.fixEmpty(value);
118        if(view==null) return FormValidation.ok();
119
120        if(Hudson.getInstance().getView(view)!=null)
121            return FormValidation.ok();
122        else
123            return FormValidation.error(Messages.ProxyView_NoSuchViewExists(value));
124    }
125
126    @Extension
127    public static class DescriptorImpl extends ViewDescriptor {
128
129        @Override
130        public String getDisplayName() {
131            return Messages.ProxyView_DisplayName();
132        }
133        
134        @Override
135        public boolean isInstantiable() {
136        	// doesn't make sense to add a ProxyView to the global views
137        	return !(Stapler.getCurrentRequest().findAncestorObject(ViewGroup.class) instanceof Hudson);
138        }
139
140    }
141
142    public Object getStaplerFallback() {
143        return getProxiedView();
144    }
145
146}