View Javadoc

1   package org.paneris.util;
2    
3   public class ValidationProblem {
4   
5       public static final int UNSET = 0;
6       public static final int INCORRECT_STRING_FORMAT = 1;
7       public static final int INCORRECT_LOOKUP_STRING = 2;
8       public static final int INCORRECT_VALUE_TYPE    = 3;
9       public static final int EMPTY_MANDATORY_FIELD   = 4;
10      public static final int DUPLICATED_UNIQUE_FIELD = 5;
11  
12      protected int    problem = UNSET;
13      protected Object incorrectValue;
14      protected String incorrectString;
15      protected String fieldName;
16      // allow a custom error to be set
17      protected String customError = null;
18  
19      public ValidationProblem(int p, String field, Object value) {
20        problem = p;
21        incorrectValue = value;
22        fieldName = field;
23      }
24  
25      public ValidationProblem(int p, String field, String value) {
26        problem = p;
27        incorrectString = value;
28        fieldName = field;
29      }
30  
31      public ValidationProblem(int p, String field) {
32        problem = p;
33        fieldName = field;
34      }
35  
36  // new constructor to allow setting of a custom error message
37      public ValidationProblem(String e) {
38          customError = e;
39      }
40  
41      public int getProblem() {
42        return problem;
43      }
44  
45      public String getFieldName() {
46        return fieldName;
47      }
48  
49      public Object getIncorrectValue() {
50        return incorrectValue;
51      }
52  
53      public String getIncorrectString() {
54        return incorrectString;
55      }
56      
57      public String toString() {
58        switch (problem) {
59            case UNSET:
60                if (customError == null) {
61                  return "No problem has been set";
62                } else {
63                    return customError;
64                }
65            case INCORRECT_STRING_FORMAT:
66              return fieldName + " cannot be set from the value \""+incorrectString+"\"";
67            case INCORRECT_LOOKUP_STRING:
68              return fieldName + " cannot be set: " + 
69                     "There is no record in the looked-up table with value "+incorrectString;
70            case INCORRECT_VALUE_TYPE:
71              return fieldName + " has been set to an Object of incorrect type ("+incorrectValue+")";
72            case EMPTY_MANDATORY_FIELD:
73              return fieldName + " has not been set but is mandatory";
74            case DUPLICATED_UNIQUE_FIELD:
75              return fieldName + " should be unique but the value is already in the database";
76              default:
77                  return "Unspecified problem ";
78        }
79      }
80  }
81