public boolean supports(Class c) {
return Chapter.class.equals(c);
}
public void validate(Object obj, Errors errors) {
Chapter chapter = (Chapter) obj;
boolean locationErrors = false;
if (chapter == null) {
errors.rejectValue("name", null, null, "Name required");
}
else {
if (chapter.getName() == null || chapter.getName().equals("")) {
errors.rejectValue("name", null, null, "Name required");
}
if (chapter.getType() == null || chapter.getType().equals("")) {
errors.rejectValue("type", null, null, "Type required");
}
/*if ((chapter.getCity() == null || chapter.getCity().equals("")) &&
(chapter.getState() == null || chapter.getState().equals(""))) {
errors.rejectValue("city", null, null, "City and/or state required");
errors.rejectValue("state", null, null, "City and/or state required");
locationErrors = true;
}*/
if (chapter.getCountry() == null || chapter.getCountry().equals("")) {
errors.rejectValue("country", null, null, "Country required");
locationErrors = true;
}
if (chapter.getStatus() == null || chapter.getStatus().equals("")) {
errors.rejectValue("status", null, null, "Status required");
}
if (!locationErrors) {
Point point = mapsService.getCoordinates(chapter.getCity(),
chapter.getState(), chapter.getCountry());
if (point == null) {
errors.rejectValue("city", null, null, "Geocoding failed: given address may be invalid or geocoding service may be offline");
errors.rejectValue("state", null, null, "Geocoding failed: given address may be invalid or geocoding service may be offline");
errors.rejectValue("country", null, null, "Geocoding failed: given address may be invalid or geocoding service may be offline");
} else {
chapter.setLatitude(point.getLatitude());
chapter.setLongitude(point.getLongitude());
}
}
if ((chapter.getWebsite() == null || chapter.getWebsite().equals("")) &&
(chapter.getMailingList() == null ||
chapter.getMailingList().equals(""))) {
errors.rejectValue("website", null, null,
"Website and/or mailing list required");
errors.rejectValue("mailingList", null, null,
"Website and/or mailing list required");
}
if (chapter.getWebsite() != null &&
!chapter.getWebsite().equals("")) {
if (!isValidUrl(chapter.getWebsite())) {
errors.rejectValue("website", null, null, "Invalid website");
}
}
if (chapter.getMailingList() != null &&
!chapter.getMailingList().equals("")) {
if (!isValidUrl(chapter.getMailingList())) {
errors.rejectValue("mailingList", null, null,
"Invalid mailing list");
}
}
for (int i = 0; i < chapter.getAdministrators().size(); i++) {
User u = chapter.getAdministrators().get(i);
if (u.getName() == null || u.getName().equals("")) {
errors.rejectValue("administrators[" + i + "].name", null, null,
"Name required");
}