Project

General

Profile

Download (2.73 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2009 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/ 
9

    
10
package eu.etaxonomy.cdm.model.validation;
11

    
12
import java.util.Set;
13

    
14
import javax.validation.ConstraintViolation;
15
import javax.validation.Payload;
16

    
17
/**
18
 * A class conveying the severity of a {@link ConstraintViolation}. Severity levels are
19
 * extraneous to the javax.validation framework and can only be conveyed using generic
20
 * {@link Payload} objects. Unfortunately, only the class of those objects is communicated
21
 * back to the client. The class <i>is</i> the message. Concrete instances or {@code enum}
22
 * s (an obvious choice for severity levels) cannot function as {@code Payload} objects.
23
 * The Severity class enables you to program using true Severity instances (one for each
24
 * level), while behind the scenes only the class of those instances is taken into
25
 * account.
26
 * 
27
 * @author ayco holleman
28
 */
29
public abstract class Severity implements Payload {
30

    
31
	public static final Notice NOTICE = new Notice();
32
	public static final Warning WARNING = new Warning();
33
	public static final Error ERROR = new Error();
34

    
35
	//@formatter:off
36
	public static final class Notice extends Severity {};
37
	public static final class Warning extends Severity {};
38
	public static final class Error extends Severity {};
39
	//@formatter:on
40

    
41
	/**
42
	 * Get {@code Severity} object for the specified {@code String} representation. Does the
43
	 * opposite of {@link #toString()}.
44
	 * 
45
	 * @param name
46
	 *            The {@code String} representation of {@code Severity} object you want.
47
	 * 
48
	 * @return The {@code Severity} object
49
	 */
50
	public static Severity forName(String name)
51
	{
52
		if (name.equals(Error.class.getSimpleName())) {
53
			return ERROR;
54
		}
55
		if (name.equals(Warning.class.getSimpleName())) {
56
			return WARNING;
57
		}
58
		return NOTICE;
59
	}
60

    
61

    
62
	/**
63
	 * Get the {@code Severity} of the specified {@code ConstraintViolation}.
64
	 * 
65
	 * @param error
66
	 *            The {@code ConstraintViolation}
67
	 * 
68
	 * @return The {@code Severity}
69
	 */
70
	public static Severity getSeverity(ConstraintViolation<?> error)
71
	{
72
		Set<Class<? extends Payload>> payloads = error.getConstraintDescriptor().getPayload();
73
		for (Class<? extends Payload> payload : payloads) {
74
			if (payload == Error.class) {
75
				return ERROR;
76
			}
77
			if (payload == Warning.class) {
78
				return WARNING;
79
			}
80
			if (payload == Notice.class) {
81
				return NOTICE;
82
			}
83
		}
84
		return null;
85
	}
86

    
87

    
88
	private Severity()
89
	{
90
	}
91

    
92

    
93
	@Override
94
	public String toString()
95
	{
96
		return getClass().getSimpleName();
97
	}
98
}
(4-4/4)