package org.yacos.web.author.validator;

import org.springframework.validation.Errors;
import org.springframework.validation.Validator;
import org.yacos.web.author.form.FormSubmission;

public class ValidateSubmission implements Validator {

	@SuppressWarnings("unchecked")
	public boolean supports(Class arg0) {
		return FormSubmission.class.isAssignableFrom(arg0);
	}

	public void validate(Object arg0, Errors arg1) {

		FormSubmission sub = (FormSubmission)arg0;

		String title = sub.getTitle();
		String theme = sub.getTheme();
		String abs = sub.getAbstractText();

		if (title == null || title.trim().length() == 0) {
			arg1.rejectValue("title", "submissionArticle.title", "The title should not be null");
		}

		if (theme == null || theme.trim().length()==0) {
			arg1.rejectValue("theme", "submissionArticle.theme", "The theme should not be null");
		}

		if (abs == null || abs.trim().length()==0) {
			arg1.rejectValue("abstractText", "submissionArticle.abstractText", "The abstract should not be null");
		}
	}
}
