View Javadoc

1   package org.kuali.student.lum.course.service.utils;
2   
3   import java.util.ArrayList;
4   import java.util.List;
5   import java.util.Stack;
6   
7   import org.kuali.student.common.dictionary.dto.FieldDefinition;
8   import org.kuali.student.common.dictionary.dto.ObjectStructureDefinition;
9   import org.kuali.student.common.util.MessageUtils;
10  import org.kuali.student.common.validation.dto.ValidationResultInfo;
11  import org.kuali.student.common.validator.DefaultValidatorImpl;
12  import org.kuali.student.core.atp.dto.AtpInfo;
13  import org.kuali.student.core.atp.service.AtpService;
14  import org.kuali.student.lum.course.dto.CourseInfo;
15  
16  public class ActiveDatesValidator extends DefaultValidatorImpl {
17  	private AtpService atpService;
18  
19  	@Override
20  	public List<ValidationResultInfo> validateObject(Object data,
21  			ObjectStructureDefinition objStructure) {
22  		// Custom validators are required to only override the other
23  		// validateObject method
24  		return null;
25  	}
26  
27  	@Override
28  	public List<ValidationResultInfo> validateObject(FieldDefinition field,
29  			Object o, ObjectStructureDefinition objStructure,
30  			Stack<String> elementStack) {
31  		//Get ATPs and compare dates.  If the end is <= the start, throw a validation error.
32  		List<ValidationResultInfo> results = new ArrayList<ValidationResultInfo>();
33  		if (o instanceof CourseInfo) {
34  			CourseInfo course = (CourseInfo) o;
35  			if (course.getEndTerm() != null && course.getStartTerm() != null) {
36  				try {
37  					AtpInfo startAtp = atpService.getAtp(course.getStartTerm());
38  					AtpInfo endAtp = atpService.getAtp(course.getEndTerm());
39  					if (startAtp.getStartDate()
40  							.compareTo(endAtp.getStartDate()) > 0) {
41  						ValidationResultInfo vr = new ValidationResultInfo();
42  						vr.setElement(field.getName());
43  						vr.setError("validation.endDateAfterStartDate");
44  						results.add(vr);
45  					}
46  				} catch (Exception e) {
47  					throw new RuntimeException("Error validating.", e);
48  				}
49  
50  			}
51  		}
52  		return results;
53  	}
54  
55  	public void setAtpService(AtpService atpService) {
56  		this.atpService = atpService;
57  	}
58  
59  }