View Javadoc

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