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