class OrderFrequencyValidator implements Validator {

	@Override
	public void validate(Object obj, Errors errors) {
		OrderFrequency orderFrequency = (OrderFrequency) obj;
		if (orderFrequency == null) {
			errors.reject("error.general");
		} else {
			ValidationUtils.rejectIfEmpty(errors, "concept", "Concept.noConceptSelected");
			
			Concept concept = orderFrequency.getConcept();
			if (concept != null) {
				if (!ConceptClass.FREQUENCY_UUID.equals(concept.getConceptClass().getUuid())) {
					errors.rejectValue("concept", "OrderFrequency.concept.shouldBeClassFrequency");
				}
				
				OrderFrequency of = Context.getOrderService().getOrderFrequencyByConcept(concept);
				if (of != null && !of.equals(orderFrequency)) {
					errors.rejectValue("concept", "OrderFrequency.concept.shouldNotBeShared");
				}
			}
			ValidateUtil.validateFieldLengths(errors, obj.getClass(), "retireReason");
		}
	}

}