use of io.crnk.core.resource.annotations.RelationshipRepositoryBehavior in project crnk-framework by crnk-project.
the class DefaultRegistryEntryBuilder method setupImplicitRelationshipRepository.
private ResponseRelationshipEntry setupImplicitRelationshipRepository(ResourceField relationshipField) {
RelationshipRepositoryBehavior behavior = relationshipField.getRelationshipRepositoryBehavior();
if (behavior == RelationshipRepositoryBehavior.DEFAULT) {
if (relationshipField.hasIdField() || relationshipField.getLookupIncludeAutomatically() == LookupIncludeBehavior.NONE) {
behavior = RelationshipRepositoryBehavior.FORWARD_OWNER;
} else {
behavior = RelationshipRepositoryBehavior.CUSTOM;
}
}
if (behavior == RelationshipRepositoryBehavior.IMPLICIT_FROM_OWNER) {
behavior = RelationshipRepositoryBehavior.FORWARD_OWNER;
}
if (behavior == RelationshipRepositoryBehavior.IMPLICIT_GET_OPPOSITE_MODIFY_OWNER) {
behavior = RelationshipRepositoryBehavior.FORWARD_GET_OPPOSITE_SET_OWNER;
}
if (behavior != RelationshipRepositoryBehavior.CUSTOM) {
ResourceInformation sourceInformation = relationshipField.getParentResourceInformation();
ResourceFieldAccess fieldAccess = relationshipField.getAccess();
RepositoryMethodAccess access = new RepositoryMethodAccess(fieldAccess.isPostable(), fieldAccess.isPatchable(), fieldAccess.isReadable(), fieldAccess.isPatchable());
RelationshipMatcher matcher = new RelationshipMatcher().rule().field(relationshipField).add();
RelationshipRepositoryInformationImpl implicitRepoInformation = new RelationshipRepositoryInformationImpl(matcher, access);
ForwardingRelationshipRepository repository;
if (behavior == RelationshipRepositoryBehavior.FORWARD_OWNER) {
repository = new ForwardingRelationshipRepository(sourceInformation.getResourceType(), matcher, ForwardingDirection.OWNER, ForwardingDirection.OWNER);
} else if (behavior == RelationshipRepositoryBehavior.FORWARD_GET_OPPOSITE_SET_OWNER) {
repository = new ForwardingRelationshipRepository(sourceInformation.getResourceType(), matcher, ForwardingDirection.OPPOSITE, ForwardingDirection.OWNER);
} else {
PreconditionUtil.assertEquals("unknown behavior", RelationshipRepositoryBehavior.FORWARD_OPPOSITE, behavior);
repository = new ForwardingRelationshipRepository(sourceInformation.getResourceType(), matcher, ForwardingDirection.OPPOSITE, ForwardingDirection.OPPOSITE);
}
repository.setResourceRegistry(moduleRegistry.getResourceRegistry());
return setupRelationship(relationshipField, implicitRepoInformation, repository);
} else {
return null;
}
}
Aggregations