public boolean supports(Class c) {
return Event.class.equals(c);
}
public void validate(Object obj, Errors errors) {
Event event = (Event) obj;
boolean locationErrors = false;
if (event == null) {
errors.rejectValue("name", null, null, "Name required");
}
else {
if (event.getName() == null || event.getName().equals("")) {
errors.rejectValue("name", null, null, "Name required");
}
if (event.getTimeZone() == null || event.getTimeZone().equals("")) {
errors.rejectValue("timeZone", null, null, "Time zone required");
}
if (event.getStartDate() == null) {
errors.rejectValue("startDate", null, null, "Start date required");
} else if (event.getEndDate() == null) {
errors.rejectValue("endDate", null, null, "End date required");
} else if (event.getEndDate().before(event.getStartDate())) {
errors.rejectValue("endDate", null, null,
"End time must occur after start time");
}
if (event.getStreetAddress() == null ||
event.getStreetAddress().equals("")) {
errors.rejectValue("streetAddress", null, null, "Address required");
locationErrors = true;
}
if (event.getCity() == null || event.getCity().equals("")) {
errors.rejectValue("city", null, null, "City required");
locationErrors = true;
}
if (event.getCountry() == null || event.getCountry().equals("")) {
errors.rejectValue("country", null, null, "Country required");
locationErrors = true;
}
if (event.getUrl() != null && !event.getUrl().equals("") &&
!isValidUrl(event.getUrl())) {
errors.rejectValue("url", null, null, "Invalid URL");
}
if (event.getDescription() == null ||
event.getDescription().equals("")) {
errors.rejectValue("description", null, null, "Description required");
}
if (!locationErrors) {
Point point = mapsService.getCoordinates(event.getStreetAddress(),
event.getCity(), event.getState(), event.getCountry());
if (point == null) {
errors.rejectValue("streetAddress", null, null, "Geocoding failed: given address may be invalid or geocoding service may be offline");
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 {
event.setLatitude(point.getLatitude());
event.setLongitude(point.getLongitude());
}
}
}
}