Summary Table
Categories |
Total Count |
PII |
0 |
URL |
0 |
DNS |
0 |
EKL |
0 |
IP |
0 |
PORT |
0 |
VsID |
0 |
CF |
0 |
AI |
0 |
VPD |
0 |
PL |
0 |
Other |
0 |
File Content
package gov.va.oneconsult.seoc.api.validation;
import static org.springframework.util.StringUtils.isEmpty;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.List;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import org.apache.commons.beanutils.BeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.ReflectionUtils;
import gov.va.oneconsult.seoc.api.model.PayableService;
import gov.va.oneconsult.seoc.api.model.Seoc;
public class SeocActivationValidator
implements ConstraintValidator<ActivateCondition, Object>
{
private String trigger;
private String triggerAttribute;
private String[] triggerValues;
private String[] requiredAttributes;
private String requiredChild;
private String childKey;
private String[] requiredChildAttributes;
private String message;
public static final Logger logger = LoggerFactory.getLogger(SeocActivationValidator.class);
@Override
public void initialize(ActivateCondition requiredIfChecked)
{
trigger = requiredIfChecked.trigger();
triggerAttribute = requiredIfChecked.triggerAttribute();
triggerValues = requiredIfChecked.triggerValues();
requiredAttributes = requiredIfChecked.requiredAttributes();
requiredChild = requiredIfChecked.requiredChild();
childKey = requiredIfChecked.childKey();
requiredChildAttributes = requiredIfChecked.requiredChildAttributes();
message = requiredIfChecked.message();
}
@Override
public boolean isValid(Object objectToValidate, ConstraintValidatorContext context)
{
Boolean valid = true;
Boolean finalValid = true;
try
{
Field triggerObjectField = objectToValidate.getClass().getDeclaredField(trigger);
ReflectionUtils.makeAccessible(triggerObjectField);
Object triggerObject = triggerObjectField.get(objectToValidate);
Object conditionValue = BeanUtils.getProperty(triggerObject, triggerAttribute);
if (Arrays.asList(triggerValues).contains(conditionValue))
{
valid = validREV(objectToValidate);
finalValid = finalValid && valid;
if (!valid) {
context.disableDefaultConstraintViolation();
context.buildConstraintViolationWithTemplate("The REV flag cannot be set unless at least one payable service requires pre-certification")
.addPropertyNode("REV")
.addConstraintViolation();
}
for (String propName : requiredAttributes)
{
valid = checkValidity(propName, objectToValidate);
finalValid = finalValid && valid;
if (!valid)
{
context.disableDefaultConstraintViolation();
context.buildConstraintViolationWithTemplate(message)
.addPropertyNode(propName)
.addConstraintViolation();
}
}
if(!requiredChild.isEmpty()) {
Field requiredChildField = objectToValidate.getClass().getDeclaredField(requiredChild);
ReflectionUtils.makeAccessible(requiredChildField);
Object childObject = requiredChildField.get(objectToValidate);
if(childObject!=null && childObject instanceof List<?>)
{
List<?> childObjectAsList = (List<?>) childObject;
if(!childObjectAsList.isEmpty()) {
for(Object s : childObjectAsList) {
Object keyValue = BeanUtils.getProperty(s, childKey);
for(String innerProp : requiredChildAttributes) {
valid = checkValidity(innerProp, s);
finalValid = finalValid && valid;
if (!valid)
{
context.disableDefaultConstraintViolation();
context.buildConstraintViolationWithTemplate(message)
.addPropertyNode("services")
.addPropertyNode(innerProp)
.inIterable().atKey(keyValue)
.addConstraintViolation();
}
}
}
}
}
}
}
} catch (IllegalAccessException e)
{
logger.error("Accessor method is not available for class : {}, exception : {}",
objectToValidate.getClass().getName(), e);
return false;
} catch (NoSuchMethodException e)
{
logger.error("Field or method is not present on class : {}, exception : {}",
objectToValidate.getClass().getName(), e);
return false;
}catch (NoSuchFieldException e)
{
logger.error("Field or method is not present on class : {}, exception : {}",
objectToValidate.getClass().getName(), e);
return false;
}catch (InvocationTargetException e)
{
logger.error("An exception occurred while accessing class : {}, exception : {}",
objectToValidate.getClass().getName(), e);
return false;
}
return finalValid;
}
/**
* Description: Validate the REV flag
* @param parentObject
* @return is REV valid
*/
private boolean validREV(Object parentObject) {
Seoc seoc = (Seoc)parentObject;
Boolean rev = seoc.getRev();
if (rev == null || !rev) {
return true;
}
return rev && seoc.getPrct();
}
/**
* Description: Validate the property on the parentObject
* @param property
* @param parentObject
* @return is property valid
* @throws IllegalAccessException
* @throws InvocationTargetException
* @throws NoSuchMethodException
* @throws NoSuchFieldException
* @throws SecurityException
* @throws IllegalArgumentException
*/
private boolean checkValidity(String property, Object parentObject)
throws IllegalAccessException, InvocationTargetException, NoSuchMethodException,
NoSuchFieldException, SecurityException, IllegalArgumentException
{
boolean valid = true;
try
{
Object propValue = BeanUtils.getProperty(parentObject, property);
Field propField = parentObject.getClass().getDeclaredField(property);
ReflectionUtils.makeAccessible(propField);
Object objectToValidate = propField.get(parentObject);
if (objectToValidate instanceof Integer)
{
valid = objectToValidate != null && (((Integer) objectToValidate).intValue() > 0);
} else
{
if(property.equalsIgnoreCase("billingCodes")) {
PayableService service = (PayableService)parentObject;
String codeRequired = service.getCodeRequired();
if(codeRequired.equalsIgnoreCase("YES")) {
valid = propValue != null && !isEmpty(propValue);
}else {
valid = propValue == null || isEmpty(propValue);
}
}else {
valid = propValue != null && !isEmpty(propValue);
}
}
} catch (IllegalAccessException e)
{
throw e;
} catch (InvocationTargetException e)
{
throw e;
} catch (NoSuchMethodException e)
{
throw e;
} catch (NoSuchFieldException e)
{
throw e;
} catch (SecurityException e)
{
throw e;
} catch (IllegalArgumentException e)
{
throw e;
}
return valid;
}
}