View Javadoc

1   package org.kuali.student.r1.common.validator;
2   
3   import java.util.ArrayList;
4   import java.util.HashMap;
5   import java.util.Iterator;
6   import java.util.List;
7   import java.util.Map;
8   import java.util.Stack;
9   
10  import org.kuali.student.r1.common.dictionary.dto.Constraint;
11  import org.kuali.student.r1.common.dictionary.dto.FieldDefinition;
12  import org.kuali.student.r1.common.dictionary.dto.ObjectStructureDefinition;
13  import org.kuali.student.r2.common.dto.ValidationResultInfo;
14  import org.kuali.student.common.util.MessageUtils;
15  
16  /**
17   * This is a sample Validator used for customized validation. Inject this validator into ValidatorFactory.
18   * 
19   * @author Neerav Agrawal
20   *
21   */
22  @Deprecated
23  public class SampCustomValidator implements Validator {
24  
25  	private ObjectStructureDefinition objStructure;
26  
27  	@Override
28  	public List<ValidationResultInfo> validateObject(Object o,
29  			ObjectStructureDefinition objStructure) {
30  		return null;
31  	}
32  
33  	public ObjectStructureDefinition getObjStructure() {
34  		return objStructure;
35  	}
36  
37  	public void setObjStructure(ObjectStructureDefinition objStructure) {
38  		this.objStructure = objStructure;
39  	}
40  
41  	@Override
42  	public List<ValidationResultInfo> validateObject(FieldDefinition field,
43  			Object o, ObjectStructureDefinition objStructure,Stack<String> elementStack) {
44  		 List<ValidationResultInfo> results = new ArrayList<ValidationResultInfo>();
45  		ConstraintDataProvider dataProvider = new BeanConstraintDataProvider();
46  		dataProvider.initialize(o);
47  		String xPath= getElementXpath(elementStack) + field.getName() ;
48  		Object value = dataProvider.getValue(field.getName());
49  		if(value==null){
50  
51              ValidationResultInfo valRes = new ValidationResultInfo(xPath);
52              valRes.setError(MessageUtils.interpolate("Validation Error Sample", toMap(field)));
53              results.add(valRes);
54  		}
55  		
56  		
57  		return results;
58  	}
59  	
60      private String getElementXpath(Stack<String> elementStack) {
61          StringBuilder xPath = new StringBuilder();
62          xPath.append("/");
63          Iterator<String> itr = elementStack.iterator();
64          while (itr.hasNext()) {
65              xPath.append(itr.next() + "/");
66          }
67  
68          return xPath.toString();
69      }
70  
71      private Map<String, Object> toMap(Constraint c) {
72          Map<String, Object> result = new HashMap<String, Object>();
73          result.put("minOccurs", c.getMinOccurs());
74          result.put("maxOccurs", c.getMaxOccurs());
75          result.put("minLength", c.getMinLength());
76          result.put("maxLength", c.getMaxLength());
77          result.put("minValue", c.getExclusiveMin());
78          result.put("maxValue", c.getInclusiveMax());
79          // result.put("dataType", c.getDataType());
80  
81          return result;
82      }
83  }