use of org.apache.camel.dataformat.bindy.annotation.DataField in project camel by apache.
the class BindyCsvFactory method bind.
@Override
public void bind(List<String> tokens, Map<String, Object> model, int line) throws Exception {
int pos = 1;
int counterMandatoryFields = 0;
for (String data : tokens) {
// Get DataField from model
DataField dataField = dataFields.get(pos);
ObjectHelper.notNull(dataField, "No position " + pos + " defined for the field: " + data + ", line: " + line);
if (dataField.trim()) {
data = data.trim();
}
if (dataField.required()) {
// Increment counter of mandatory fields
++counterMandatoryFields;
// This is not possible for mandatory fields
if (data.equals("")) {
throw new IllegalArgumentException("The mandatory field defined at the position " + pos + " is empty for the line: " + line);
}
}
// Get Field to be setted
Field field = annotatedFields.get(pos);
field.setAccessible(true);
if (LOG.isDebugEnabled()) {
LOG.debug("Pos: {}, Data: {}, Field type: {}", new Object[] { pos, data, 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 (!data.equals("")) {
try {
value = format.parse(data);
} catch (FormatException ie) {
throw new IllegalArgumentException(ie.getMessage() + ", position: " + pos + ", line: " + line, ie);
} catch (Exception e) {
throw new IllegalArgumentException("Parsing error detected for field defined at the position: " + pos + ", line: " + line, e);
}
} else {
if (!dataField.defaultValue().isEmpty()) {
value = format.parse(dataField.defaultValue());
} else {
value = getDefaultValueForPrimitive(field.getType());
}
}
field.set(modelField, value);
++pos;
}
LOG.debug("Counter mandatory fields: {}", counterMandatoryFields);
if (counterMandatoryFields < numberMandatoryFields) {
throw new IllegalArgumentException("Some mandatory fields are missing, line: " + line);
}
if (pos < totalFields) {
setDefaultValuesForFields(model);
}
}
use of org.apache.camel.dataformat.bindy.annotation.DataField in project camel by apache.
the class BindyFixedLengthFactory method generateFixedLengthPositionMap.
/**
*
* Generate a table containing the data formatted and sorted with their position/offset
* The result is placed in the Map<Integer, List> results
*/
private void generateFixedLengthPositionMap(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);
result = formatString(format, value);
// trim if enabled
if (datafield.trim()) {
result = result.trim();
}
int fieldLength = datafield.length();
if (fieldLength == 0 && (datafield.lengthPos() > 0)) {
List<String> resultVals = results.get(datafield.lengthPos());
fieldLength = Integer.valueOf(resultVals.get(0));
}
if (fieldLength <= 0 && datafield.delimiter().equals("") && datafield.lengthPos() == 0) {
throw new IllegalArgumentException("Either a delimiter value or length for the field: " + field.getName() + " is mandatory.");
}
if (!datafield.delimiter().equals("")) {
result = result + datafield.delimiter();
} else {
// Get length of the field, alignment (LEFT or RIGHT), pad
String align = datafield.align();
char padCharField = datafield.paddingChar();
char padChar;
StringBuilder temp = new StringBuilder();
// Check if we must pad
if (result.length() < fieldLength) {
// No padding defined for the field
if (padCharField == 0) {
// We use the padding defined for the Record
padChar = paddingChar;
} else {
padChar = padCharField;
}
if (align.contains("R")) {
temp.append(generatePaddingChars(padChar, fieldLength, result.length()));
temp.append(result);
} else if (align.contains("L")) {
temp.append(result);
temp.append(generatePaddingChars(padChar, fieldLength, result.length()));
} else {
throw new IllegalArgumentException("Alignment for the field: " + field.getName() + " must be equal to R for RIGHT or L for LEFT");
}
result = temp.toString();
} else if (result.length() > fieldLength) {
// is clipped enabled? if so clip the field
if (datafield.clip()) {
result = result.substring(0, fieldLength);
} else {
throw new IllegalArgumentException("Length for the " + field.getName() + " must not be larger than allowed, was: " + result.length() + ", allowed: " + fieldLength);
}
}
}
if (LOG.isDebugEnabled()) {
LOG.debug("Value to be formatted: {}, position: {}, and its formatted value: {}", new Object[] { value, datafield.pos(), result });
}
} else {
result = "";
}
Integer key;
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);
}
}
}
}
use of org.apache.camel.dataformat.bindy.annotation.DataField 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);
}
}
}
Aggregations