Search in sources :

Example 1 with BeanAttributeInformation

use of io.crnk.core.engine.information.bean.BeanAttributeInformation in project crnk-framework by crnk-project.

the class ActivitiResourceMapper method copyStaticField.

private void copyStaticField(Object resource, String attributeName, Optional<Object> activitiBean, boolean toResource) {
    PreconditionUtil.assertNotNull("cannot process nested holder structures", activitiBean);
    // map fields
    if (activitiBean.isPresent()) {
        BeanInformation activitiBeanInformation = BeanInformation.get(activitiBean.get().getClass());
        if (toResource) {
            Object value = PropertyUtils.getProperty(activitiBean.get(), attributeName);
            PropertyUtils.setProperty(resource, attributeName, mapValue(value));
        } else {
            BeanAttributeInformation attribute = activitiBeanInformation.getAttribute(attributeName);
            if (attribute != null && attribute.getSetter() != null) {
                Object value = PropertyUtils.getProperty(resource, attributeName);
                PropertyUtils.setProperty(activitiBean.get(), attributeName, unmapValue(value));
            }
        }
    }
}
Also used : BeanInformation(io.crnk.core.engine.information.bean.BeanInformation) BeanAttributeInformation(io.crnk.core.engine.information.bean.BeanAttributeInformation)

Example 2 with BeanAttributeInformation

use of io.crnk.core.engine.information.bean.BeanAttributeInformation 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());
            }
        }
    }
}
Also used : ResourceFieldType(io.crnk.core.engine.information.resource.ResourceFieldType) ResourceFieldType(io.crnk.core.engine.information.resource.ResourceFieldType) SerializeType(io.crnk.core.resource.annotations.SerializeType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) JsonApiRelation(io.crnk.core.resource.annotations.JsonApiRelation) BeanAttributeInformation(io.crnk.core.engine.information.bean.BeanAttributeInformation)

Example 3 with BeanAttributeInformation

use of io.crnk.core.engine.information.bean.BeanAttributeInformation in project crnk-framework by crnk-project.

the class JacksonResourceFieldInformationProviderNamingTest method onWrappedBooleanFieldShouldReturnFieldNameBasedOnGetter.

@Test
public void onWrappedBooleanFieldShouldReturnFieldNameBasedOnGetter() throws Exception {
    BeanAttributeInformation attr = beanDesc.getAttribute("accessorField");
    assertThat(sut.getJsonName(attr).isPresent()).isFalse();
}
Also used : BeanAttributeInformation(io.crnk.core.engine.information.bean.BeanAttributeInformation) Test(org.junit.Test)

Example 4 with BeanAttributeInformation

use of io.crnk.core.engine.information.bean.BeanAttributeInformation in project crnk-framework by crnk-project.

the class JacksonResourceFieldInformationProviderNamingTest method onNoSerializationConfigShouldSerializeMethod.

@Test
public void onNoSerializationConfigShouldSerializeMethod() throws Exception {
    sut = new JacksonResourceFieldInformationProvider();
    sut.init(context);
    Method method = TestClass.class.getDeclaredMethod("getAccessorField");
    assertThat(sut.getName(method).isPresent()).isFalse();
    BeanAttributeInformation attr = beanDesc.getAttribute("accessorField");
    assertThat(sut.getJsonName(attr).isPresent()).isFalse();
}
Also used : Method(java.lang.reflect.Method) BeanAttributeInformation(io.crnk.core.engine.information.bean.BeanAttributeInformation) Test(org.junit.Test)

Example 5 with BeanAttributeInformation

use of io.crnk.core.engine.information.bean.BeanAttributeInformation in project crnk-framework by crnk-project.

the class JacksonResourceFieldInformationProviderNamingTest method onFieldWithJsonPropertyShouldReturnCustomName.

@Test
public void onFieldWithJsonPropertyShouldReturnCustomName() throws Exception {
    // GIVEN
    BeanAttributeInformation field = beanDesc.getAttribute("fieldWithJsonProperty");
    // WHEN
    String name = sut.getJsonName(field).get();
    // THEN
    assertThat(name).isEqualTo("customName");
}
Also used : BeanAttributeInformation(io.crnk.core.engine.information.bean.BeanAttributeInformation) Test(org.junit.Test)

Aggregations

BeanAttributeInformation (io.crnk.core.engine.information.bean.BeanAttributeInformation)13 Test (org.junit.Test)9 BeanInformation (io.crnk.core.engine.information.bean.BeanInformation)3 Method (java.lang.reflect.Method)2 InformationBuilder (io.crnk.core.engine.information.InformationBuilder)1 ResourceField (io.crnk.core.engine.information.resource.ResourceField)1 ResourceFieldType (io.crnk.core.engine.information.resource.ResourceFieldType)1 JsonApiRelation (io.crnk.core.resource.annotations.JsonApiRelation)1 SerializeType (io.crnk.core.resource.annotations.SerializeType)1 Field (java.lang.reflect.Field)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1