Search in sources :

Example 1 with BeanInformation

use of io.crnk.core.engine.information.bean.BeanInformation 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 BeanInformation

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

the class ResourceInformationProviderBase method getResourceFields.

protected List<ResourceField> getResourceFields(Class<?> resourceClass) {
    BeanInformation beanDesc = BeanInformation.get(resourceClass);
    List<String> attributeNames = beanDesc.getAttributeNames();
    List<ResourceField> fields = new ArrayList<>();
    Set<String> relationIdFields = new HashSet<>();
    for (String attributeName : attributeNames) {
        BeanAttributeInformation attributeDesc = beanDesc.getAttribute(attributeName);
        if (!isIgnored(attributeDesc)) {
            InformationBuilder informationBuilder = context.getInformationBuilder();
            InformationBuilder.Field fieldBuilder = informationBuilder.createResourceField();
            buildResourceField(beanDesc, attributeDesc, fieldBuilder);
            fields.add(fieldBuilder.build());
        } else if (attributeDesc.getAnnotation(JsonApiRelationId.class).isPresent()) {
            relationIdFields.add(attributeDesc.getName());
        }
    }
    verifyRelationIdFields(resourceClass, relationIdFields, fields);
    return fields;
}
Also used : BeanInformation(io.crnk.core.engine.information.bean.BeanInformation) InformationBuilder(io.crnk.core.engine.information.InformationBuilder) ResourceField(io.crnk.core.engine.information.resource.ResourceField) ArrayList(java.util.ArrayList) BeanAttributeInformation(io.crnk.core.engine.information.bean.BeanAttributeInformation) HashSet(java.util.HashSet)

Example 3 with BeanInformation

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

the class LinksInformationSerializer method serialize.

@Override
public void serialize(LinksInformation value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
    gen.writeStartObject();
    BeanInformation info = BeanInformation.get(value.getClass());
    for (String attrName : info.getAttributeNames()) {
        BeanAttributeInformation attribute = info.getAttribute(attrName);
        Object linkValue = attribute.getValue(value);
        if (linkValue != null) {
            gen.writeObjectFieldStart(attrName);
            if (linkValue instanceof String) {
                gen.writeStringField(SerializerUtil.HREF, linkValue.toString());
            } else {
                gen.writeObject(linkValue);
            }
            gen.writeEndObject();
        }
    }
    gen.writeEndObject();
}
Also used : BeanInformation(io.crnk.core.engine.information.bean.BeanInformation) BeanAttributeInformation(io.crnk.core.engine.information.bean.BeanAttributeInformation)

Aggregations

BeanAttributeInformation (io.crnk.core.engine.information.bean.BeanAttributeInformation)3 BeanInformation (io.crnk.core.engine.information.bean.BeanInformation)3 InformationBuilder (io.crnk.core.engine.information.InformationBuilder)1 ResourceField (io.crnk.core.engine.information.resource.ResourceField)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1