Created
February 11, 2014 09:59
-
-
Save nyilmaz/8932100 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package magnet.support.common.validation.validator; | |
import magnet.support.common.validation.annotation.IsBirthdateValid; | |
import org.apache.commons.lang3.reflect.FieldUtils; | |
import org.joda.time.DateTime; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import javax.validation.ConstraintValidator; | |
import javax.validation.ConstraintValidatorContext; | |
/** | |
* @author nyilmaz | |
*/ | |
public class BirthdateValidator implements ConstraintValidator<IsBirthdateValid, Object> { | |
private static final Logger logger = LoggerFactory.getLogger(BirthdateValidator.class); | |
String birthdayFieldName; | |
String birthmonthFieldName; | |
String birthyearFieldName; | |
Boolean required; | |
@Override | |
public void initialize(IsBirthdateValid constraintAnnotation) { | |
birthdayFieldName = constraintAnnotation.dayField(); | |
birthmonthFieldName = constraintAnnotation.monthField(); | |
birthyearFieldName = constraintAnnotation.yearField(); | |
required = constraintAnnotation.required(); | |
} | |
@Override | |
public boolean isValid(Object value, ConstraintValidatorContext context) { | |
try { | |
Object dayObj = FieldUtils.readField(value, birthdayFieldName, true); | |
Object monthObj = FieldUtils.readField(value, birthmonthFieldName, true); | |
Object yearObj = FieldUtils.readField(value, birthyearFieldName, true); | |
Conditions conditions = new Conditions(dayObj, monthObj, yearObj); | |
if(required) { | |
return conditions.notAnyNull().OrDate(); | |
} else { | |
return conditions.allNull().orNotAnyNull().OrDate(); | |
} | |
// joda verilenler yanlissa IllegalArgumentException firlatiyor | |
} catch(IllegalArgumentException e) { | |
return false; | |
} catch(IllegalAccessException e) { | |
return false; | |
} catch(ClassCastException e) { | |
logger.error("Cannot cast given field class. Birthdate related fields must be Integer!", e); | |
return false; | |
} | |
return true; | |
} | |
static class Conditions { | |
private Object dayObj; | |
private Object monthObj; | |
private Object yearObj; | |
private Boolean result; | |
Conditions(Object dayObj, Object monthObj, Object yearObj) { | |
this.dayObj = dayObj; | |
this.monthObj = monthObj; | |
this.yearObj = yearObj; | |
} | |
public Conditions notAnyNull() { | |
result = dayObj == null || monthObj == null || yearObj == null; | |
return this; | |
} | |
public Conditions orNotAnyNull() { | |
result |= dayObj == null || monthObj == null || yearObj == null; | |
return this; | |
} | |
public Conditions allNull() { | |
result = dayObj == null && monthObj == null && yearObj == null; | |
return this; | |
} | |
public Boolean OrDate() { | |
try { | |
new DateTime((Integer)yearObj, (Integer)monthObj, (Integer)dayObj, 0, 0); | |
} catch(IllegalArgumentException e) { | |
return result; | |
} | |
return true; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment