File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\hudson\model\ProxyView.java.html

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 */
24 package hudson.model;
25
26 import hudson.Extension;
27 import hudson.Util;
28 import hudson.model.Descriptor.FormException;
29 import hudson.util.FormValidation;
30 import java.io.IOException;
31 import java.util.Collection;
32 import javax.servlet.ServletException;
33
34 import jenkins.model.Jenkins;
35 import org.kohsuke.stapler.DataBoundConstructor;
36 import org.kohsuke.stapler.QueryParameter;
37 import org.kohsuke.stapler.Stapler;
38 import org.kohsuke.stapler.StaplerFallback;
39 import org.kohsuke.stapler.StaplerRequest;
40 import org.kohsuke.stapler.StaplerResponse;
41
42 /**
43 * A view that delegates to another.
44 *
45 * TODO: this does not respond to renaming or deleting the proxied view.
46 *
47 * @author Tom Huybrechts
48 *
49 */
50 public class ProxyView extends View implements StaplerFallback {
51
52 private String proxiedViewName;
53
54 @DataBoundConstructor
55 public ProxyView(String name) {
56 super(name);
57
58 if (Jenkins.getInstance().getView(name) != null) {
59 // if this is a valid global view name, let's assume the
60 // user wants to show it
61 proxiedViewName = name;
62 }
63 }
64
65 public View getProxiedView() {
66 if (proxiedViewName == null) {
67 // just so we avoid errors just after creation
68 return Jenkins.getInstance().getPrimaryView();
69 } else {
70 return Jenkins.getInstance().getView(proxiedViewName);
71 }
72 }
73
74 public String getProxiedViewName() {
75 return proxiedViewName;
76 }
77
78 public void setProxiedViewName(String proxiedViewName) {
79 this.proxiedViewName = proxiedViewName;
80 }
81
82 @Override
83 public Collection<TopLevelItem> getItems() {
84 return getProxiedView().getItems();
85 }
86
87 @Override
88 public boolean contains(TopLevelItem item) {
89 return getProxiedView().contains(item);
90 }
91
92 @Override
93 public void onJobRenamed(Item item, String oldName, String newName) {
94 if (oldName.equals(proxiedViewName)) {
95 proxiedViewName = newName;
96 }
97 }
98
99 @Override
100 protected void submit(StaplerRequest req) throws IOException, ServletException, FormException {
101 String proxiedViewName = req.getSubmittedForm().getString("proxiedViewName");
102 if (Jenkins.getInstance().getView(proxiedViewName) == null) {
103 throw new FormException("Not an existing global view", "proxiedViewName");
104 }
105 this.proxiedViewName = proxiedViewName;
106 }
107
108 @Override
109 public Item doCreateItem(StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException {
110 return getProxiedView().doCreateItem(req, rsp);
111 }
112
113 /**
114 * Fails if a global view with the given name does not exist.
115 */
116 public FormValidation doViewExistsCheck(@QueryParameter String value) {
117 checkPermission(View.CREATE);
118
119 String view = Util.fixEmpty(value);
120 if(view==null) return FormValidation.ok();
121
122 if(Jenkins.getInstance().getView(view)!=null)
123 return FormValidation.ok();
124 else
125 return FormValidation.error(Messages.ProxyView_NoSuchViewExists(value));
126 }
127
128 @Extension
129 public static class DescriptorImpl extends ViewDescriptor {
130
131 @Override
132 public String getDisplayName() {
133 return Messages.ProxyView_DisplayName();
134 }
135
136 @Override
137 public boolean isInstantiable() {
138 // doesn't make sense to add a ProxyView to the global views
139 return !(Stapler.getCurrentRequest().findAncestorObject(ViewGroup.class) instanceof Jenkins);
140 }
141
142 }
143
144 public Object getStaplerFallback() {
145 return getProxiedView();
146 }
147
148 }
149