Search in sources :

Example 1 with DataField

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

the class BindyCsvFactory method generateCsvPositionMap.

/**
     * Generate a table containing the data formatted and sorted with their position/offset
     * If the model is Ordered than a key is created combining the annotation @Section and Position of the field
     * If a relation @OneToMany is defined, than we iterate recursively through this function
     * The result is placed in the Map<Integer, List> results
     */
private void generateCsvPositionMap(Class<?> clazz, Object obj, Map<Integer, List<String>> results) throws Exception {
    String result = "";
    for (Field field : clazz.getDeclaredFields()) {
        field.setAccessible(true);
        DataField datafield = field.getAnnotation(DataField.class);
        if (datafield != null) {
            if (obj != null) {
                // Retrieve the format, pattern and precision associated to the type
                Class<?> type = field.getType();
                // Create format
                FormattingOptions formattingOptions = ConverterUtils.convert(datafield, field.getType(), field.getAnnotation(BindyConverter.class), getLocale());
                Format<?> format = formatFactory.getFormat(formattingOptions);
                // Get field value
                Object value = field.get(obj);
                // If the field value is empty, populate it with the default value
                if (ObjectHelper.isNotEmpty(datafield.defaultValue()) && ObjectHelper.isEmpty(value)) {
                    value = datafield.defaultValue();
                }
                result = formatString(format, value);
                if (datafield.trim()) {
                    result = result.trim();
                }
                if (datafield.clip() && result.length() > datafield.length()) {
                    result = result.substring(0, datafield.length());
                }
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Value to be formatted: {}, position: {}, and its formatted value: {}", new Object[] { value, datafield.pos(), result });
                }
            } else {
                result = "";
            }
            Integer key;
            if (isMessageOrdered() && obj != null) {
                // Generate a key using the number of the section
                // and the position of the field
                Integer key1 = sections.get(obj.getClass().getName());
                Integer key2 = datafield.position();
                Integer keyGenerated = generateKey(key1, key2);
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Key generated: {}, for section: {}", String.valueOf(keyGenerated), key1);
                }
                key = keyGenerated;
            } else {
                key = datafield.pos();
            }
            if (!results.containsKey(key)) {
                List<String> list = new LinkedList<String>();
                list.add(result);
                results.put(key, list);
            } else {
                List<String> list = results.get(key);
                list.add(result);
            }
        }
        OneToMany oneToMany = field.getAnnotation(OneToMany.class);
        if (oneToMany != null) {
            // Set global variable
            // Will be used during generation of CSV
            isOneToMany = true;
            List<?> list = (List<?>) field.get(obj);
            if (list != null) {
                Iterator<?> it = list.iterator();
                while (it.hasNext()) {
                    Object target = it.next();
                    generateCsvPositionMap(target.getClass(), target, results);
                }
            } else {
                // Call this function to add empty value
                // in the table
                generateCsvPositionMap(field.getClass(), null, results);
            }
        }
    }
}
Also used : BindyConverter(org.apache.camel.dataformat.bindy.annotation.BindyConverter) OneToMany(org.apache.camel.dataformat.bindy.annotation.OneToMany) LinkedList(java.util.LinkedList) 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) List(java.util.List) LinkedList(java.util.LinkedList)

Example 2 with DataField

use of org.apache.camel.dataformat.bindy.annotation.DataField 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 DataField

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

the class BindyFixedLengthFactory method bind.

public void bind(String record, Map<String, Object> model, int line) throws Exception {
    int pos = 1;
    int counterMandatoryFields = 0;
    DataField dataField;
    String token;
    int offset = 1;
    int length;
    String delimiter;
    Field field;
    // Iterate through the list of positions
    // defined in the @DataField
    // and grab the data from the line
    Collection<DataField> c = dataFields.values();
    Iterator<DataField> itr = c.iterator();
    // this iterator is for a link list that was built using items in order
    while (itr.hasNext()) {
        dataField = itr.next();
        length = dataField.length();
        delimiter = dataField.delimiter();
        if (length == 0 && dataField.lengthPos() != 0) {
            Field lengthField = annotatedFields.get(dataField.lengthPos());
            lengthField.setAccessible(true);
            Object modelObj = model.get(lengthField.getDeclaringClass().getName());
            Object lengthObj = lengthField.get(modelObj);
            length = ((Integer) lengthObj).intValue();
        }
        if (length < 1 && delimiter == null && dataField.lengthPos() == 0) {
            throw new IllegalArgumentException("Either length or delimiter must be specified for the field : " + dataField.toString());
        }
        if (offset - 1 <= -1) {
            throw new IllegalArgumentException("Offset/Position of the field " + dataField.toString() + " cannot be negative");
        }
        // skip ahead if the expected position is greater than the offset
        if (dataField.pos() > offset) {
            LOG.debug("skipping ahead [" + (dataField.pos() - offset) + "] chars.");
            offset = dataField.pos();
        }
        if (length > 0) {
            if (record.length() < offset) {
                token = "";
            } else {
                int endIndex = offset + length - 1;
                if (endIndex > record.length()) {
                    endIndex = record.length();
                }
                token = record.substring(offset - 1, endIndex);
            }
            offset += length;
        } else if (!delimiter.equals("")) {
            String tempToken = record.substring(offset - 1, record.length());
            token = tempToken.substring(0, tempToken.indexOf(delimiter));
            // include the delimiter in the offset calculation
            offset += token.length() + 1;
        } else {
            // defined as a zero-length field
            token = "";
        }
        if (dataField.trim()) {
            token = trim(token, dataField, paddingChar);
        //token = token.trim();
        }
        // Check mandatory field
        if (dataField.required()) {
            // Increment counter of mandatory fields
            ++counterMandatoryFields;
            // This is not possible for mandatory fields
            if (token.equals("")) {
                throw new IllegalArgumentException("The mandatory field defined at the position " + pos + " is empty for the line: " + line);
            }
        }
        // Get Field to be set
        field = annotatedFields.get(dataField.pos());
        field.setAccessible(true);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Pos/Offset: {}, Data: {}, Field type: {}", new Object[] { offset, token, field.getType() });
        }
        // Create format object to format the field
        FormattingOptions formattingOptions = ConverterUtils.convert(dataField, field.getType(), field.getAnnotation(BindyConverter.class), getLocale());
        Format<?> format = formatFactory.getFormat(formattingOptions);
        // field object to be set
        Object modelField = model.get(field.getDeclaringClass().getName());
        // format the data received
        Object value = null;
        if ("".equals(token)) {
            token = dataField.defaultValue();
        }
        if (!"".equals(token)) {
            try {
                value = format.parse(token);
            } catch (FormatException ie) {
                throw new IllegalArgumentException(ie.getMessage() + ", position: " + offset + ", line: " + line, ie);
            } catch (Exception e) {
                throw new IllegalArgumentException("Parsing error detected for field defined at the position/offset: " + offset + ", line: " + line, e);
            }
        } else {
            value = getDefaultValueForPrimitive(field.getType());
        }
        field.set(modelField, value);
        ++pos;
    }
    // check for unmapped non-whitespace data at the end of the line
    if (offset <= record.length() && !(record.substring(offset - 1, record.length())).trim().equals("") && !isIgnoreTrailingChars()) {
        throw new IllegalArgumentException("Unexpected / unmapped characters found at the end of the fixed-length record at line : " + line);
    }
    LOG.debug("Counter mandatory fields: {}", counterMandatoryFields);
    if (pos < totalFields) {
        throw new IllegalArgumentException("Some fields are missing (optional or mandatory), line: " + line);
    }
    if (counterMandatoryFields < numberMandatoryFields) {
        throw new IllegalArgumentException("Some mandatory fields are missing, line: " + line);
    }
}
Also used : BindyConverter(org.apache.camel.dataformat.bindy.annotation.BindyConverter) FormatException(org.apache.camel.dataformat.bindy.format.FormatException) FormatException(org.apache.camel.dataformat.bindy.format.FormatException) Field(java.lang.reflect.Field) DataField(org.apache.camel.dataformat.bindy.annotation.DataField) DataField(org.apache.camel.dataformat.bindy.annotation.DataField)

Example 4 with DataField

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

the class BindyCsvFactory method setDefaultValuesForFields.

/**
     * Set the default values for the non defined fields.
     * @param model the model which has its default fields set.
     * @throws IllegalAccessException if the underlying fields are inaccessible
     * @throws Exception In case the field cannot be parsed
     */
private void setDefaultValuesForFields(final Map<String, Object> model) throws IllegalAccessException, Exception {
    // Set the default values, if defined
    for (int i = 1; i <= dataFields.size(); i++) {
        Field field = annotatedFields.get(i);
        field.setAccessible(true);
        DataField dataField = dataFields.get(i);
        Object modelField = model.get(field.getDeclaringClass().getName());
        if (field.get(modelField) == null && !dataField.defaultValue().isEmpty()) {
            FormattingOptions formattingOptions = ConverterUtils.convert(dataField, field.getType(), field.getAnnotation(BindyConverter.class), getLocale());
            Format<?> format = formatFactory.getFormat(formattingOptions);
            Object value = format.parse(dataField.defaultValue());
            field.set(modelField, value);
        }
    }
}
Also used : Field(java.lang.reflect.Field) DataField(org.apache.camel.dataformat.bindy.annotation.DataField) BindyConverter(org.apache.camel.dataformat.bindy.annotation.BindyConverter) DataField(org.apache.camel.dataformat.bindy.annotation.DataField)

Example 5 with DataField

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

the class BindyCsvFactory method generateHeader.

/**
     * Generate for the first line the headers of the columns
     *
     * @return the headers columns
     */
public String generateHeader() {
    Map<Integer, DataField> dataFieldsSorted = new TreeMap<Integer, DataField>(dataFields);
    Iterator<Integer> it = dataFieldsSorted.keySet().iterator();
    StringBuilder builderHeader = new StringBuilder();
    while (it.hasNext()) {
        DataField dataField = dataFieldsSorted.get(it.next());
        // Retrieve the field
        Field field = annotatedFields.get(dataField.pos());
        // Change accessibility to allow to read protected/private fields
        field.setAccessible(true);
        // Get dataField
        if (!dataField.columnName().equals("")) {
            builderHeader.append(dataField.columnName());
        } else {
            builderHeader.append(field.getName());
        }
        if (it.hasNext()) {
            builderHeader.append(ConverterUtils.getCharDelimiter(separator));
        }
    }
    return builderHeader.toString();
}
Also used : Field(java.lang.reflect.Field) DataField(org.apache.camel.dataformat.bindy.annotation.DataField) DataField(org.apache.camel.dataformat.bindy.annotation.DataField) TreeMap(java.util.TreeMap)

Aggregations

Field (java.lang.reflect.Field)8 DataField (org.apache.camel.dataformat.bindy.annotation.DataField)8 BindyConverter (org.apache.camel.dataformat.bindy.annotation.BindyConverter)5 ArrayList (java.util.ArrayList)3 LinkedList (java.util.LinkedList)2 Link (org.apache.camel.dataformat.bindy.annotation.Link)2 FormatException (org.apache.camel.dataformat.bindy.format.FormatException)2 List (java.util.List)1 TreeMap (java.util.TreeMap)1 OneToMany (org.apache.camel.dataformat.bindy.annotation.OneToMany)1