Guest User

Untitled

a guest
Dec 16th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.62 KB | None | 0 0
  1. @Target({ElementType.TYPE, ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER})
  2. @Retention(RetentionPolicy.RUNTIME)
  3. @Documented
  4. @Constraint(validatedBy = { FieldValidator.class })
  5. public @interface NotNullOn {
  6.  
  7. String message() default "values Missing";
  8.  
  9. Class<?>[] groups() default {};
  10.  
  11.  
  12. Class<? extends Payload>[] payload() default {};
  13.  
  14.  
  15. }
  16.  
  17.  
  18. public class FieldValidator implements ConstraintValidator<NotNullOn, Employee> {
  19.  
  20. @Override
  21. public void initialize(NotNullOn constraintAnnotation) {
  22. //default method.
  23. }
  24.  
  25. @Override
  26. public boolean isValid(Employee value, ConstraintValidatorContext context) {
  27. if (value == null){
  28. return false;
  29. }
  30.  
  31. if (value.getLastName() == null) {
  32. context.disableDefaultConstraintViolation();
  33. context.buildConstraintViolationWithTemplate("lastName is required")
  34. .addPropertyNode("lastName").addConstraintViolation();
  35. return false;
  36. }
  37.  
  38. if (value.getLastName() != null){
  39. context.disableDefaultConstraintViolation();
  40. context.buildConstraintViolationWithTemplate("lastName is required for company address")
  41. .addPropertyNode("lastName").addConstraintViolation();
  42. return false;
  43. }
  44. return true;
  45.  
  46. }
  47.  
  48. }
  49.  
  50. public class Department {
  51.  
  52. private Long deptId;
  53.  
  54. private String deptName;
  55.  
  56. private List<Employee> employees;
  57.  
  58.  
  59. public Long getDeptId() {
  60. return deptId;
  61. }
  62.  
  63.  
  64. public void setDeptId(Long deptId) {
  65. this.deptId = deptId;
  66. }
  67.  
  68.  
  69. public String getDeptName() {
  70. return deptName;
  71. }
  72.  
  73.  
  74. public void setDeptName(String deptName) {
  75. this.deptName = deptName;
  76. }
  77.  
  78.  
  79. public List<Employee> getEmployees() {
  80. return employees;
  81. }
  82.  
  83. public void setEmployees(List<Employee> employees) {
  84. this.employees = employees;
  85. }
  86.  
  87.  
  88. }
  89.  
  90. @NotNullOn
  91. public class Employee {
  92.  
  93. private String name;
  94.  
  95. private String lastName;
  96.  
  97. private Long phNum;
  98.  
  99. private Long empId;
  100.  
  101.  
  102. public String getName() {
  103. return name;
  104. }
  105.  
  106.  
  107. public void setName(String name) {
  108. this.name = name;
  109. }
  110.  
  111.  
  112. public String getLastName() {
  113. return lastName;
  114. }
  115.  
  116.  
  117. public void setLastName(String lastName) {
  118. this.lastName = lastName;
  119. }
  120.  
  121.  
  122. public Long getPhNum() {
  123. return phNum;
  124. }
  125.  
  126.  
  127. public void setPhNum(Long phNum) {
  128. this.phNum = phNum;
  129. }
  130.  
  131.  
  132. public Long getEmpId() {
  133. return empId;
  134. }
  135.  
  136.  
  137. public void setEmpId(Long empId) {
  138. this.empId = empId;
  139. }
  140.  
  141. }
Add Comment
Please, Sign In to add comment