final ObjectAdapter parentAdapter = field.getParent();
final ObjectAdapter collection = getAdapter();
if (collection == null) {
// TODO: move logic into Facet
return new Veto("Collection not set up; can't add elements to a non-existant collection");
}
final Consent usableInState = getOneToManyAssociation().isUsable(IsisContext.getAuthenticationSession(), parentAdapter, where);
if (usableInState.isVetoed()) {
return usableInState;
}
final ObjectSpecification specification = sourceAdapter.getSpecification();
final ObjectSpecification elementSpecification = getElementSpecification();
if (!specification.isOfType(elementSpecification)) {
// TODO: move logic into Facet
return new Veto(String.format("Only objects of type %s are allowed in this collection", elementSpecification.getSingularName()));
}
if (parentAdapter.representsPersistent() && sourceAdapter.isTransient()) {
// TODO: move logic into Facet
return new Veto("Can't set field in persistent object with reference to non-persistent object");
}
return getOneToManyAssociation().isValidToAdd(parentAdapter, sourceAdapter);
} else {
return Veto.DEFAULT;
}