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

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2010, 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.security.ACL;
30 import hudson.security.Permission;
31 import hudson.util.FormValidation;
32 import hudson.views.MyViewsTabBar;
33 import hudson.views.ViewsTabBar;
34
35 import java.io.IOException;
36 import java.text.ParseException;
37 import java.util.Collection;
38 import java.util.Collections;
39 import java.util.List;
40 import java.util.concurrent.CopyOnWriteArrayList;
41
42 import javax.servlet.ServletException;
43
44 import jenkins.model.Jenkins;
45 import net.sf.json.JSONObject;
46
47 import org.acegisecurity.AccessDeniedException;
48 import org.kohsuke.stapler.DataBoundConstructor;
49 import org.kohsuke.stapler.HttpRedirect;
50 import org.kohsuke.stapler.HttpResponse;
51 import org.kohsuke.stapler.QueryParameter;
52 import org.kohsuke.stapler.StaplerFallback;
53 import org.kohsuke.stapler.StaplerRequest;
54 import org.kohsuke.stapler.StaplerResponse;
55
56 /**
57 * A UserProperty that remembers user-private views.
58 *
59 * @author Tom Huybrechts
60 */
61 public class MyViewsProperty extends UserProperty implements ViewGroup, Action, StaplerFallback {
62 private String primaryViewName;
63
64 /**
65 * Always hold at least one view.
66 */
67 private CopyOnWriteArrayList<View> views = new CopyOnWriteArrayList<View>();
68
69 private transient ViewGroupMixIn viewGroupMixIn;
70
71 @DataBoundConstructor
72 public MyViewsProperty(String primaryViewName) {
73 this.primaryViewName = primaryViewName;
74 }
75
76 private MyViewsProperty() {
77 readResolve();
78 }
79
80 public Object readResolve() {
81 if (views == null)
82 // this shouldn't happen, but an error in 1.319 meant the last view could be deleted
83 views = new CopyOnWriteArrayList<View>();
84
85 if (views.isEmpty())
86 // preserve the non-empty invariant
87 views.add(new AllView(Messages.Hudson_ViewName(), this));
88
89 viewGroupMixIn = new ViewGroupMixIn(this) {
90 protected List<View> views() { return views; }
91 protected String primaryView() { return primaryViewName; }
92 protected void primaryView(String name) { primaryViewName=name; }
93 };
94
95 return this;
96 }
97
98 public String getPrimaryViewName() {
99 return primaryViewName;
100 }
101
102 public void setPrimaryViewName(String primaryViewName) {
103 this.primaryViewName = primaryViewName;
104 }
105
106 public User getUser() {
107 return user;
108 }
109
110 ///// ViewGroup methods /////
111 public String getUrl() {
112 return user.getUrl() + "/my-views/";
113 }
114
115 public void save() throws IOException {
116 user.save();
117 }
118
119 public Collection<View> getViews() {
120 return viewGroupMixIn.getViews();
121 }
122
123 public View getView(String name) {
124 return viewGroupMixIn.getView(name);
125 }
126
127 public boolean canDelete(View view) {
128 return viewGroupMixIn.canDelete(view);
129 }
130
131 public void deleteView(View view) throws IOException {
132 viewGroupMixIn.deleteView(view);
133 }
134
135 public void onViewRenamed(View view, String oldName, String newName) {
136 viewGroupMixIn.onViewRenamed(view,oldName,newName);
137 }
138
139 public void addView(View view) throws IOException {
140 viewGroupMixIn.addView(view);
141 }
142
143 public View getPrimaryView() {
144 return viewGroupMixIn.getPrimaryView();
145 }
146
147 public HttpResponse doIndex() {
148 return new HttpRedirect("view/" + getPrimaryView().getViewName() + "/");
149 }
150
151 public synchronized void doCreateView(StaplerRequest req, StaplerResponse rsp)
152 throws IOException, ServletException, ParseException, FormException {
153 checkPermission(View.CREATE);
154 addView(View.create(req, rsp, this));
155 }
156
157 /**
158 * Checks if a private view with the given name exists.
159 * An error is returned if exists==true but the view does not exist.
160 * An error is also returned if exists==false but the view does exist.
161 **/
162 public FormValidation doViewExistsCheck(@QueryParameter String value, @QueryParameter boolean exists) {
163 checkPermission(View.CREATE);
164
165 String view = Util.fixEmpty(value);
166 if (view == null) return FormValidation.ok();
167 if (exists) {
168 return (getView(view)!=null) ?
169 FormValidation.ok() :
170 FormValidation.error(Messages.MyViewsProperty_ViewExistsCheck_NotExist(view));
171 } else {
172 return (getView(view)==null) ?
173 FormValidation.ok() :
174 FormValidation.error(Messages.MyViewsProperty_ViewExistsCheck_AlreadyExists(view));
175 }
176 }
177
178 public ACL getACL() {
179 return user.getACL();
180 }
181
182 public void checkPermission(Permission permission) throws AccessDeniedException {
183 getACL().checkPermission(permission);
184 }
185
186 public boolean hasPermission(Permission permission) {
187 return getACL().hasPermission(permission);
188 }
189
190 ///// Action methods /////
191 public String getDisplayName() {
192 return Messages.MyViewsProperty_DisplayName();
193 }
194
195 public String getIconFileName() {
196 return "user.png";
197 }
198
199 public String getUrlName() {
200 return "my-views";
201 }
202
203 @Extension
204 public static class DescriptorImpl extends UserPropertyDescriptor {
205
206 @Override
207 public String getDisplayName() {
208 return Messages.MyViewsProperty_DisplayName();
209 }
210
211 @Override
212 public UserProperty newInstance(User user) {
213 return new MyViewsProperty();
214 }
215 }
216
217 @Override
218 public UserProperty reconfigure(StaplerRequest req, JSONObject form) throws FormException {
219 req.bindJSON(this, form);
220 return this;
221 }
222
223 public ViewsTabBar getViewsTabBar() {
224 return Jenkins.getInstance().getViewsTabBar();
225 }
226
227 public ItemGroup<? extends TopLevelItem> getItemGroup() {
228 return Jenkins.getInstance();
229 }
230
231 public List<Action> getViewActions() {
232 // Jenkins.getInstance().getViewActions() are tempting but they are in a wrong scope
233 return Collections.emptyList();
234 }
235
236 public Object getStaplerFallback() {
237 return getPrimaryView();
238 }
239
240 public MyViewsTabBar getMyViewsTabBar() {
241 return Jenkins.getInstance().getMyViewsTabBar();
242 }
243
244 @Extension
245 public static class GlobalAction implements RootAction {
246
247 public String getDisplayName() {
248 return Messages.MyViewsProperty_GlobalAction_DisplayName();
249 }
250
251 public String getIconFileName() {
252 // do not show when not logged in
253 if (User.current() == null ) {
254 return null;
255 }
256
257 return "user.png";
258 }
259
260 public String getUrlName() {
261 return "/me/my-views";
262 }
263
264 }
265
266 }
267