Search in sources :

Example 1 with Link

use of org.apache.camel.dataformat.bindy.annotation.Link in project camel by apache.

the class BindyAbstractDataFormat method createLinkedFieldsModel.

protected void createLinkedFieldsModel(Object model, Map<String, Object> row) throws IllegalAccessException {
    for (Field field : model.getClass().getDeclaredFields()) {
        Link linkField = field.getAnnotation(Link.class);
        if (linkField != null) {
            boolean accessible = field.isAccessible();
            field.setAccessible(true);
            if (!row.containsKey(field.getType().getName())) {
                row.put(field.getType().getName(), field.get(model));
            }
            field.setAccessible(accessible);
        }
    }
}
Also used : Field(java.lang.reflect.Field) Link(org.apache.camel.dataformat.bindy.annotation.Link)

Example 2 with Link

use of org.apache.camel.dataformat.bindy.annotation.Link in project camel by apache.

the class BindyCsvFactory method initAnnotatedFields.

@Override
public void initAnnotatedFields() {
    maxpos = 0;
    for (Class<?> cl : models) {
        List<Field> linkFields = new ArrayList<Field>();
        if (LOG.isDebugEnabled()) {
            LOG.debug("Class retrieved: {}", cl.getName());
        }
        for (Field field : cl.getDeclaredFields()) {
            DataField dataField = field.getAnnotation(DataField.class);
            if (dataField != null) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Position defined in the class: {}, position: {}, Field: {}", new Object[] { cl.getName(), dataField.pos(), dataField });
                }
                if (dataField.required()) {
                    ++numberMandatoryFields;
                } else {
                    ++numberOptionalFields;
                }
                int pos = dataField.pos();
                if (annotatedFields.containsKey(pos)) {
                    Field f = annotatedFields.get(pos);
                    LOG.warn("Potentially invalid model: existing @DataField '{}' replaced by '{}'", f.getName(), field.getName());
                }
                dataFields.put(pos, dataField);
                annotatedFields.put(pos, field);
                maxpos = Math.max(maxpos, pos);
            }
            Link linkField = field.getAnnotation(Link.class);
            if (linkField != null) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Class linked: {}, Field: {}", cl.getName(), field);
                }
                linkFields.add(field);
            }
        }
        if (!linkFields.isEmpty()) {
            annotatedLinkFields.put(cl.getName(), linkFields);
        }
        totalFields = numberMandatoryFields + numberOptionalFields;
        if (LOG.isDebugEnabled()) {
            LOG.debug("Number of optional fields: {}", numberOptionalFields);
            LOG.debug("Number of mandatory fields: {}", numberMandatoryFields);
            LOG.debug("Total: {}", totalFields);
        }
    }
    if (annotatedFields.size() < maxpos) {
        LOG.info("Potentially incomplete model: some csv fields may not be mapped to @DataField members");
    }
}
Also used : Field(java.lang.reflect.Field) DataField(org.apache.camel.dataformat.bindy.annotation.DataField) DataField(org.apache.camel.dataformat.bindy.annotation.DataField) ArrayList(java.util.ArrayList) Link(org.apache.camel.dataformat.bindy.annotation.Link)

Example 3 with Link

use of org.apache.camel.dataformat.bindy.annotation.Link in project camel by apache.

the class BindyKeyValuePairFactory method initAnnotatedFields.

@Override
public void initAnnotatedFields() {
    for (Class<?> cl : models) {
        List<Field> linkFields = new ArrayList<Field>();
        for (Field field : cl.getDeclaredFields()) {
            KeyValuePairField keyValuePairField = field.getAnnotation(KeyValuePairField.class);
            if (keyValuePairField != null) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Key declared in the class : {}, key : {}, Field : {}", new Object[] { cl.getName(), keyValuePairField.tag(), keyValuePairField });
                }
                keyValuePairFields.put(keyValuePairField.tag(), keyValuePairField);
                annotatedFields.put(keyValuePairField.tag(), field);
            }
            Link linkField = field.getAnnotation(Link.class);
            if (linkField != null) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Class linked  : {}, Field {}", cl.getName(), field);
                }
                linkFields.add(field);
            }
        }
        if (!linkFields.isEmpty()) {
            annotatedLinkFields.put(cl.getName(), linkFields);
        }
    }
}
Also used : Field(java.lang.reflect.Field) KeyValuePairField(org.apache.camel.dataformat.bindy.annotation.KeyValuePairField) ArrayList(java.util.ArrayList) KeyValuePairField(org.apache.camel.dataformat.bindy.annotation.KeyValuePairField) Link(org.apache.camel.dataformat.bindy.annotation.Link)

Example 4 with Link

use of org.apache.camel.dataformat.bindy.annotation.Link in project camel by apache.

the class BindyAbstractFactory method loadModels.

/**
     * Recursively load model.
     *  
     * @param root
     */
private void loadModels(Class<?> root) {
    models.add(root);
    modelClassNames.add(root.getName());
    for (Field field : root.getDeclaredFields()) {
        Link linkField = field.getAnnotation(Link.class);
        if (linkField != null) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Class linked: {}, Field: {}", field.getType(), field);
            }
            models.add(field.getType());
            modelClassNames.add(field.getType().getName());
            loadModels(field.getType());
        }
    }
}
Also used : Field(java.lang.reflect.Field) Link(org.apache.camel.dataformat.bindy.annotation.Link)

Example 5 with Link

use of org.apache.camel.dataformat.bindy.annotation.Link in project camel by apache.

the class BindyFixedLengthFactory method initAnnotatedFields.

@Override
public void initAnnotatedFields() {
    for (Class<?> cl : models) {
        List<Field> linkFields = new ArrayList<Field>();
        if (LOG.isDebugEnabled()) {
            LOG.debug("Class retrieved: {}", cl.getName());
        }
        for (Field field : cl.getDeclaredFields()) {
            DataField dataField = field.getAnnotation(DataField.class);
            if (dataField != null) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Position defined in the class: {}, position: {}, Field: {}", new Object[] { cl.getName(), dataField.pos(), dataField });
                }
                if (dataField.required()) {
                    ++numberMandatoryFields;
                } else {
                    ++numberOptionalFields;
                }
                dataFields.put(dataField.pos(), dataField);
                annotatedFields.put(dataField.pos(), field);
            }
            Link linkField = field.getAnnotation(Link.class);
            if (linkField != null) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Class linked: {}, Field: {}", cl.getName(), field);
                }
                linkFields.add(field);
            }
        }
        if (!linkFields.isEmpty()) {
            annotatedLinkFields.put(cl.getName(), linkFields);
        }
        totalFields = numberMandatoryFields + numberOptionalFields;
        if (LOG.isDebugEnabled()) {
            LOG.debug("Number of optional fields: {}", numberOptionalFields);
            LOG.debug("Number of mandatory fields: {}", numberMandatoryFields);
            LOG.debug("Total: {}", totalFields);
        }
    }
}
Also used : Field(java.lang.reflect.Field) DataField(org.apache.camel.dataformat.bindy.annotation.DataField) DataField(org.apache.camel.dataformat.bindy.annotation.DataField) ArrayList(java.util.ArrayList) Link(org.apache.camel.dataformat.bindy.annotation.Link)

Aggregations

Field (java.lang.reflect.Field)5 Link (org.apache.camel.dataformat.bindy.annotation.Link)5 ArrayList (java.util.ArrayList)3 DataField (org.apache.camel.dataformat.bindy.annotation.DataField)2 KeyValuePairField (org.apache.camel.dataformat.bindy.annotation.KeyValuePairField)1