2015-06-04 18:52:29 +02:00
|
|
|
package org.gcube.accounting.datamodel.validators;
|
|
|
|
|
|
|
|
import java.io.Serializable;
|
|
|
|
|
|
|
|
import org.gcube.accounting.datamodel.UsageRecord.OperationResult;
|
2015-06-05 11:43:53 +02:00
|
|
|
import org.gcube.accounting.datamodel.annotations.ValidOperationResult;
|
2015-06-05 12:54:26 +02:00
|
|
|
import org.gcube.accounting.exception.InvalidValueException;
|
2015-06-04 18:52:29 +02:00
|
|
|
|
|
|
|
public class ValidOperationResultValidator implements FieldValidator<ValidOperationResult>{
|
|
|
|
|
|
|
|
public Class<ValidOperationResult> annotation() {
|
|
|
|
return ValidOperationResult.class;
|
|
|
|
}
|
|
|
|
|
2015-06-05 11:43:53 +02:00
|
|
|
public boolean isValid(Serializable toValidate) {
|
2015-06-05 12:54:26 +02:00
|
|
|
try {
|
|
|
|
validate(toValidate);
|
|
|
|
} catch (InvalidValueException e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Serializable validate(Serializable toValidate) throws InvalidValueException {
|
2015-06-04 18:52:29 +02:00
|
|
|
if(toValidate instanceof OperationResult){
|
2015-06-05 12:54:26 +02:00
|
|
|
return toValidate;
|
2015-06-04 18:52:29 +02:00
|
|
|
}
|
2015-06-05 12:54:26 +02:00
|
|
|
Integer integerObj = Integer.getInteger((String) toValidate);
|
|
|
|
if(integerObj!=null){
|
|
|
|
return integerObj;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new InvalidValueException(getErrorSuffix());
|
2015-06-04 18:52:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public String getErrorSuffix() {
|
|
|
|
return String.format("not instace of %s", OperationResult.class.getSimpleName());
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|