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

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2010, Sun Microsystems, Inc., Kohsuke Kawaguchi, Stephen Connolly
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 com.thoughtworks.xstream.converters.UnmarshallingContext;
27 import hudson.diagnosis.OldDataMonitor;
28 import hudson.util.XStream2;
29 import jenkins.model.Jenkins;
30 import jenkins.util.NonLocalizable;
31 import org.jvnet.localizer.Localizable;
32 import org.kohsuke.stapler.export.Exported;
33 import org.kohsuke.stapler.export.ExportedBean;
34
35 import java.io.*;
36 import java.util.Collections;
37 import java.util.List;
38 import java.util.Locale;
39
40 /**
41 * Represents health of something (typically project).
42 * A number between 0-100.
43 *
44 * @author connollys
45 * @since 1.115
46 */
47 @ExportedBean(defaultVisibility = 2)
48 // this is always exported as a part of Job and never on its own, so start with 2.
49 public class HealthReport implements Serializable, Comparable<HealthReport> {
50 // These are now 0-20, 21-40, 41-60, 61-80, 81+ but filenames unchanged for compatibility
51 private static final String HEALTH_OVER_80 = "health-80plus.png";
52 private static final String HEALTH_61_TO_80 = "health-60to79.png";
53 private static final String HEALTH_41_TO_60 = "health-40to59.png";
54 private static final String HEALTH_21_TO_40 = "health-20to39.png";
55 private static final String HEALTH_0_TO_20 = "health-00to19.png";
56 private static final String HEALTH_UNKNOWN = "empty.png";
57
58 /**
59 * The percentage health score (from 0 to 100 inclusive).
60 */
61 private int score;
62
63 /**
64 * The path to the icon corresponding to this health score or <code>null</code> to use the default icon
65 * corresponding to the current health score.
66 * <p/>
67 * If the path begins with a '/' then it will be the absolute path, otherwise the image is assumed to be in one of
68 * <code>/images/16x16/</code>, <code>/images/24x24/</code> or <code>/images/32x32/</code> depending on the icon
69 * size selected by the user.
70 */
71 private String iconUrl;
72
73 /**
74 * Recover the health icon's tool-tip when deserializing.
75 *
76 * @deprecated since 2008-10-18. Use {@link #localizibleDescription}
77 */
78 @Deprecated
79 private transient String description;
80
81 /**
82 * The health icon's tool-tip.
83 */
84 private Localizable localizibleDescription;
85
86 /**
87 * Create a new HealthReport.
88 *
89 * @param score The percentage health score (from 0 to 100 inclusive).
90 * @param iconUrl The path to the icon corresponding to this {@link Action}'s health or <code>null</code> to
91 * display the default icon corresponding to the current health score.
92 * <p/>
93 * If the path begins with a '/' then it will be the absolute path, otherwise the image is
94 * assumed to be in one of <code>/images/16x16/</code>, <code>/images/24x24/</code> or
95 * <code>/images/32x32/</code> depending on the icon size selected by the user.
96 * When calculating the url to display for absolute paths, the getIconUrl(String) method
97 * will replace /32x32/ in the path with the appropriate size.
98 * @param description The health icon's tool-tip.
99 * @deprecated since 2008-10-18.
100 * Use {@link #HealthReport(int, String, org.jvnet.localizer.Localizable)}
101 */
102 @Deprecated
103 public HealthReport(int score, String iconUrl, String description) {
104 this(score, iconUrl, new NonLocalizable(description));
105 }
106
107 /**
108 * Create a new HealthReport.
109 *
110 * @param score The percentage health score (from 0 to 100 inclusive).
111 * @param iconUrl The path to the icon corresponding to this {@link Action}'s health or <code>null</code> to
112 * display the default icon corresponding to the current health score.
113 * <p/>
114 * If the path begins with a '/' then it will be the absolute path, otherwise the image is
115 * assumed to be in one of <code>/images/16x16/</code>, <code>/images/24x24/</code> or
116 * <code>/images/32x32/</code> depending on the icon size selected by the user.
117 * When calculating the url to display for absolute paths, the getIconUrl(String) method
118 * will replace /32x32/ in the path with the appropriate size.
119 * @param description The health icon's tool-tip.
120 */
121 public HealthReport(int score, String iconUrl, Localizable description) {
122 this.score = score;
123 if (iconUrl == null) {
124 if (score <= 20) {
125 this.iconUrl = HEALTH_0_TO_20;
126 } else if (score <= 40) {
127 this.iconUrl = HEALTH_21_TO_40;
128 } else if (score <= 60) {
129 this.iconUrl = HEALTH_41_TO_60;
130 } else if (score <= 80) {
131 this.iconUrl = HEALTH_61_TO_80;
132 } else {
133 this.iconUrl = HEALTH_OVER_80;
134 }
135 } else {
136 this.iconUrl = iconUrl;
137 }
138 this.description = null;
139 setLocalizibleDescription(description);
140 }
141
142 /**
143 * Create a new HealthReport.
144 *
145 * @param score The percentage health score (from 0 to 100 inclusive).
146 * @param description The health icon's tool-tip.
147 * @deprecated since 2008-10-18.
148 * Use {@link #HealthReport(int, org.jvnet.localizer.Localizable)}
149 */
150 @Deprecated
151 public HealthReport(int score, String description) {
152 this(score, null, description);
153 }
154
155 /**
156 * Create a new HealthReport.
157 *
158 * @param score The percentage health score (from 0 to 100 inclusive).
159 * @param description The health icon's tool-tip.
160 */
161 public HealthReport(int score, Localizable description) {
162 this(score, null, description);
163 }
164
165 /**
166 * Create a new HealthReport.
167 */
168 public HealthReport() {
169 this(100, HEALTH_UNKNOWN, Messages._HealthReport_EmptyString());
170 }
171
172 /**
173 * Getter for property 'score'.
174 *
175 * @return The percentage health score (from 0 to 100 inclusive).
176 */
177 @Exported
178 public int getScore() {
179 return score;
180 }
181
182 /**
183 * Setter for property 'score'.
184 *
185 * @param score Value to set for property 'score'.
186 */
187 public void setScore(int score) {
188 this.score = score;
189 }
190
191 /**
192 * Getter for property 'iconUrl'.
193 *
194 * @return Value for property 'iconUrl'.
195 */
196 @Exported
197 public String getIconUrl() {
198 return iconUrl;
199 }
200
201 /**
202 * Get's the iconUrl relative to the hudson root url, for the correct size.
203 *
204 * @param size The size, e.g. 32x32, 24x24 or 16x16.
205 * @return The url relative to hudson's root url.
206 */
207 public String getIconUrl(String size) {
208 if (iconUrl == null) {
209 return Jenkins.RESOURCE_PATH + "/images/" + size + "/" + HEALTH_UNKNOWN;
210 }
211 if (iconUrl.startsWith("/")) {
212 return iconUrl.replace("/32x32/", "/" + size + "/");
213 }
214 return Jenkins.RESOURCE_PATH + "/images/" + size + "/" + iconUrl;
215 }
216
217 /**
218 * Setter for property 'iconUrl'.
219 *
220 * @param iconUrl Value to set for property 'iconUrl'.
221 */
222 public void setIconUrl(String iconUrl) {
223 this.iconUrl = iconUrl;
224 }
225
226 /**
227 * Getter for property 'description'.
228 *
229 * @return Value for property 'description'.
230 */
231 @Exported
232 public String getDescription() {
233 return getLocalizableDescription().toString();
234 }
235
236 /**
237 * Setter for property 'description'.
238 *
239 * @param description Value to set for property 'description'.
240 */
241 public void setDescription(String description) {
242 setLocalizibleDescription(new NonLocalizable(description));
243 }
244
245 /**
246 * Getter for property 'localizibleDescription'.
247 *
248 * @return Value for property 'localizibleDescription'.
249 */
250 public Localizable getLocalizableDescription() {
251 return localizibleDescription;
252 }
253
254 /**
255 * Setter for property 'localizibleDescription'.
256 *
257 * @param localizibleDescription Value to set for property 'localizibleDescription'.
258 */
259 public void setLocalizibleDescription(Localizable localizibleDescription) {
260 this.localizibleDescription = localizibleDescription;
261 }
262
263 /**
264 * Getter for property 'aggregatedReports'.
265 *
266 * @return Value for property 'aggregatedReports'.
267 */
268 public List<HealthReport> getAggregatedReports() {
269 return Collections.emptyList();
270 }
271
272 /**
273 * Getter for property 'aggregateReport'.
274 *
275 * @return Value for property 'aggregateReport'.
276 */
277 public boolean isAggregateReport() {
278 return false;
279 }
280
281 /**
282 * {@inheritDoc}
283 */
284 public int compareTo(HealthReport o) {
285 return (this.score < o.score ? -1 : (this.score == o.score ? 0 : 1));
286 }
287
288 /**
289 * Utility method to find the report with the lowest health.
290 */
291 public static HealthReport min(HealthReport a, HealthReport b) {
292 if (a == null && b == null) return null;
293 if (a == null) return b;
294 if (b == null) return a;
295 if (a.compareTo(b) <= 0) return a;
296 return b;
297 }
298
299 /**
300 * Utility method to find the report with the highest health.
301 */
302 public static HealthReport max(HealthReport a, HealthReport b) {
303 if (a == null && b == null) return null;
304 if (a == null) return b;
305 if (b == null) return a;
306 if (a.compareTo(b) >= 0) return a;
307 return b;
308 }
309
310 /**
311 * Fix deserialization of older data.
312 */
313 public static class ConverterImpl extends XStream2.PassthruConverter<HealthReport> {
314 public ConverterImpl(XStream2 xstream) { super(xstream); }
315 @Override protected void callback(HealthReport hr, UnmarshallingContext context) {
316 // If we are being read back in from an older version
317 if (hr.localizibleDescription == null) {
318 hr.localizibleDescription = new NonLocalizable(hr.description == null ? "" : hr.description);
319 OldDataMonitor.report(context, "1.256");
320 }
321 }
322 }
323 }
324