use of io.crnk.core.resource.annotations.JsonApiRelation in project crnk-framework by crnk-project.
the class ResourceInformationProviderBase method buildResourceField.
protected void buildResourceField(BeanInformation beanDesc, BeanAttributeInformation attributeDesc, InformationBuilder.Field fieldBuilder) {
fieldBuilder.underlyingName(attributeDesc.getName());
fieldBuilder.jsonName(getJsonName(attributeDesc));
ResourceFieldType fieldType = getFieldType(attributeDesc);
fieldBuilder.fieldType(fieldType);
fieldBuilder.access(getAccess(attributeDesc, fieldType));
fieldBuilder.serializeType(getSerializeType(attributeDesc, fieldType));
fieldBuilder.lookupIncludeBehavior(getLookupIncludeBehavior(attributeDesc));
fieldBuilder.relationshipRepositoryBehavior(getRelationshipRepositoryBehavior(attributeDesc));
Type genericType;
if (useFieldType(attributeDesc)) {
fieldBuilder.type(attributeDesc.getField().getType());
genericType = attributeDesc.getField().getGenericType();
} else {
fieldBuilder.type(attributeDesc.getGetter().getReturnType());
genericType = attributeDesc.getGetter().getGenericReturnType();
}
fieldBuilder.genericType(genericType);
if (fieldType == ResourceFieldType.RELATIONSHIP) {
fieldBuilder.oppositeResourceType(getResourceType(genericType, context));
fieldBuilder.oppositeName(getOppositeName(attributeDesc));
Optional<JsonApiRelation> relationAnnotation = attributeDesc.getAnnotation(JsonApiRelation.class);
if (relationAnnotation.isPresent()) {
boolean multiValued = Collection.class.isAssignableFrom(attributeDesc.getImplementationClass());
String suffix = multiValued ? "Ids" : "Id";
String idFieldName;
if (relationAnnotation.get().idField().length() > 0) {
idFieldName = relationAnnotation.get().idField();
} else {
idFieldName = attributeDesc.getName() + suffix;
}
BeanAttributeInformation idAttribute = beanDesc.getAttribute(idFieldName);
if (idAttribute == null && multiValued && attributeDesc.getName().endsWith("s")) {
// also try to correlate by removing ending s
String idFieldNameTemp = attributeDesc.getName().substring(0, attributeDesc.getName().length() - 1) + suffix;
BeanAttributeInformation idAttributeTemp = beanDesc.getAttribute(idFieldNameTemp);
// make sure there are no custom get-named methods
if (idAttributeTemp != null && attributeDesc.getGetter() != null && idAttributeTemp.getGetter().getReturnType().equals(attributeDesc.getGetter().getReturnType())) {
idFieldName = idFieldNameTemp;
idAttribute = idAttributeTemp;
}
}
if (idAttribute != null) {
fieldBuilder.idName(idFieldName);
fieldBuilder.idType(idAttribute.getImplementationClass());
}
}
}
}
Aggregations