class CohortValidator implements Validator {

	@Override
	public void validate(Object obj, Errors errors) {
		if (obj == null || !(obj instanceof Cohort)) {
			throw new IllegalArgumentException("The parameter obj should not be null and must be of type"
					+ Cohort.class);
		}



		Cohort cohort = (Cohort) obj;
		if (!cohort.getVoided()) {
			Collection<CohortMembership> members = cohort.getMemberships();
			if (!CollectionUtils.isEmpty(members)) {
				for (CohortMembership member : members) {
					Patient p = Context.getPatientService().getPatient(member.getPatientId());
					int dateCompare = OpenmrsUtil.compareWithNullAsLatest(member.getStartDate(), member.getEndDate());
					if (p != null && p.getVoided() && !member.getVoided()) {
						String message = "Patient " + p.getPatientId()
								+ " is voided, cannot add voided members to a cohort";
						errors.rejectValue("memberships", "Cohort.patientAndMemberShouldBeVoided", message);
					}
					if (dateCompare == 1) {
						String message = "Start date is null or end date is before start date";
						errors.rejectValue("memberships", "Cohort.startDateShouldNotBeNullOrBeforeEndDate", message);
					}
				}
			}
		}
	}

}