use of org.apache.camel.dataformat.bindy.annotation.KeyValuePairField in project camel by apache.
the class BindyKeyValuePairFactory method unbind.
/**
*
*/
@Override
public String unbind(Map<String, Object> model) throws Exception {
StringBuilder builder = new StringBuilder();
Map<Integer, KeyValuePairField> keyValuePairFieldsSorted = new TreeMap<Integer, KeyValuePairField>(keyValuePairFields);
Iterator<Integer> it = keyValuePairFieldsSorted.keySet().iterator();
// Map containing the OUT position of the field
// The key is double and is created using the position of the field and
// location of the class in the message (using section)
Map<Integer, String> positions = new TreeMap<Integer, String>();
// Check if separator exists
ObjectHelper.notNull(this.pairSeparator, "The pair separator has not been instantiated or property not defined in the @Message annotation");
char separator = ConverterUtils.getCharDelimiter(this.getPairSeparator());
if (LOG.isDebugEnabled()) {
LOG.debug("Separator converted: '0x{}', from: {}", Integer.toHexString(separator), this.getPairSeparator());
}
while (it.hasNext()) {
KeyValuePairField keyValuePairField = keyValuePairFieldsSorted.get(it.next());
ObjectHelper.notNull(keyValuePairField, "KeyValuePair");
// Retrieve the field
Field field = annotatedFields.get(keyValuePairField.tag());
// Change accessibility to allow to read protected/private fields
field.setAccessible(true);
if (LOG.isDebugEnabled()) {
LOG.debug("Tag: {}, Field type: {}, class: {}", new Object[] { keyValuePairField.tag(), field.getType(), field.getDeclaringClass().getName() });
}
// Retrieve the format, pattern and precision associated to the type
Class<?> type = field.getType();
// Create format
FormattingOptions formattingOptions = ConverterUtils.convert(keyValuePairField, field.getType(), field.getAnnotation(BindyConverter.class), getLocale());
Format<Object> format = (Format<Object>) formatFactory.getFormat(formattingOptions);
// Get object to be formatted
Object obj = model.get(field.getDeclaringClass().getName());
if (obj != null) {
// Get field value
Object keyValue = field.get(obj);
if (this.isMessageOrdered()) {
// Generate a key using the number of the section
// and the position of the field
Integer key1 = sections.get(obj.getClass().getName());
Integer key2 = keyValuePairField.position();
LOG.debug("Key of the section: {}, and the field: {}", key1, key2);
Integer keyGenerated = generateKey(key1, key2);
if (LOG.isDebugEnabled()) {
LOG.debug("Key generated: {}, for section: {}", String.valueOf(keyGenerated), key1);
}
// Add value to the list if not null
if (keyValue != null) {
// Format field value
String valueFormatted;
try {
valueFormatted = format.format(keyValue);
} catch (Exception e) {
throw new IllegalArgumentException("Formatting error detected for the tag: " + keyValuePairField.tag(), e);
}
// Create the key value string
String value = keyValuePairField.tag() + this.getKeyValuePairSeparator() + valueFormatted;
if (LOG.isDebugEnabled()) {
LOG.debug("Value to be formatted: {}, for the tag: {}, and its formatted value: {}", new Object[] { keyValue, keyValuePairField.tag(), valueFormatted });
}
// Add the content to the TreeMap according to the
// position defined
positions.put(keyGenerated, value);
if (LOG.isDebugEnabled()) {
LOG.debug("Positions size: {}", positions.size());
}
}
} else {
// Add value to the list if not null
if (keyValue != null) {
// Format field value
String valueFormatted;
try {
valueFormatted = format.format(keyValue);
} catch (Exception e) {
throw new IllegalArgumentException("Formatting error detected for the tag: " + keyValuePairField.tag(), e);
}
// Create the key value string
String value = keyValuePairField.tag() + this.getKeyValuePairSeparator() + valueFormatted + separator;
// Add content to the stringBuilder
builder.append(value);
if (LOG.isDebugEnabled()) {
LOG.debug("Value added: {}{}{}{}", new Object[] { keyValuePairField.tag(), this.getKeyValuePairSeparator(), valueFormatted, separator });
}
}
}
}
}
// the message according to the order/position
if (this.isMessageOrdered()) {
Iterator<Integer> posit = positions.keySet().iterator();
while (posit.hasNext()) {
String value = positions.get(posit.next());
if (LOG.isDebugEnabled()) {
LOG.debug("Value added at the position ({}) : {}{}", new Object[] { posit, value, separator });
}
builder.append(value + separator);
}
}
return builder.toString();
}
use of org.apache.camel.dataformat.bindy.annotation.KeyValuePairField 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);
}
}
}
use of org.apache.camel.dataformat.bindy.annotation.KeyValuePairField in project camel by apache.
the class BindyKeyValuePairFactory method generateModelFromKeyValueMap.
private void generateModelFromKeyValueMap(Class<?> clazz, Object obj, Map<Integer, List<String>> results, int line, Map<String, List<Object>> lists) throws Exception {
for (Field field : clazz.getDeclaredFields()) {
field.setAccessible(true);
KeyValuePairField keyValuePairField = field.getAnnotation(KeyValuePairField.class);
if (keyValuePairField != null) {
// Key
int key = keyValuePairField.tag();
// Get Value
List<String> values = results.get(key);
String value = null;
// we don't received data
if (values == null) {
/*
* The relation is one to one So we check if we are in a
* target class and if the field is mandatory
*/
if (obj != null) {
// Check mandatory field
if (keyValuePairField.required()) {
throw new IllegalArgumentException("The mandatory key/tag : " + key + " has not been defined !");
}
Object result = getDefaultValueForPrimitive(field.getType());
try {
field.set(obj, result);
} catch (Exception e) {
throw new IllegalArgumentException("Setting of field " + field + " failed for object : " + obj + " and result : " + result);
}
} else {
/*
* The relation is one to many So, we create an object
* with empty fields and we don't check if the fields
* are mandatory
*/
// Get List from Map
List<Object> l = lists.get(clazz.getName());
if (l != null) {
// BigIntegerFormatFactory if object exist
if (!l.isEmpty()) {
obj = l.get(0);
} else {
obj = clazz.newInstance();
}
Object result = getDefaultValueForPrimitive(field.getType());
try {
field.set(obj, result);
} catch (Exception e) {
throw new IllegalArgumentException("Setting of field " + field + " failed for object : " + obj + " and result : " + result);
}
// Add object created to the list
if (!l.isEmpty()) {
l.set(0, obj);
} else {
l.add(0, obj);
}
// and to the Map
lists.put(clazz.getName(), l);
// Reset obj to null
obj = null;
} else {
throw new IllegalArgumentException("The list of values is empty for the following key : " + key + " defined in the class : " + clazz.getName());
}
}
// end of test if obj != null
} else {
// Data have been retrieved from message
if (values.size() >= 1) {
if (obj != null) {
// Relation OneToOne
value = values.get(0);
Object result = null;
if (value != null) {
// Create format object to format the field
FormattingOptions formattingOptions = ConverterUtils.convert(keyValuePairField, field.getType(), field.getAnnotation(BindyConverter.class), getLocale());
Format<?> format = formatFactory.getFormat(formattingOptions);
// format the value of the key received
result = formatField(format, value, key, line);
LOG.debug("Value formated : {}", result);
} else {
result = getDefaultValueForPrimitive(field.getType());
}
try {
field.set(obj, result);
} catch (Exception e) {
throw new IllegalArgumentException("Setting of field " + field + " failed for object : " + obj + " and result : " + result);
}
} else {
// Get List from Map
List<Object> l = lists.get(clazz.getName());
if (l != null) {
// Relation OneToMany
for (int i = 0; i < values.size(); i++) {
// BigIntegerFormatFactory if object exist
if ((!l.isEmpty()) && (l.size() > i)) {
obj = l.get(i);
} else {
obj = clazz.newInstance();
}
value = values.get(i);
// Create format object to format the field
FormattingOptions formattingOptions = ConverterUtils.convert(keyValuePairField, field.getType(), field.getAnnotation(BindyConverter.class), getLocale());
Format<?> format = formatFactory.getFormat(formattingOptions);
// format the value of the key received
Object result = formatField(format, value, key, line);
LOG.debug("Value formated : {}", result);
try {
if (value != null) {
field.set(obj, result);
} else {
field.set(obj, getDefaultValueForPrimitive(field.getType()));
}
} catch (Exception e) {
throw new IllegalArgumentException("Setting of field " + field + " failed for object: " + obj + " and result: " + result);
}
// Add object created to the list
if ((!l.isEmpty()) && (l.size() > i)) {
l.set(i, obj);
} else {
l.add(i, obj);
}
// and to the Map
lists.put(clazz.getName(), l);
// Reset obj to null
obj = null;
}
} else {
throw new IllegalArgumentException("The list of values is empty for the following key: " + key + " defined in the class: " + clazz.getName());
}
}
} else {
// No values found from message
Object result = getDefaultValueForPrimitive(field.getType());
try {
field.set(obj, result);
} catch (Exception e) {
throw new IllegalArgumentException("Setting of field " + field + " failed for object: " + obj + " and result: " + result);
}
}
}
}
OneToMany oneToMany = field.getAnnotation(OneToMany.class);
if (oneToMany != null) {
String targetClass = oneToMany.mappedTo();
if (!targetClass.equals("")) {
// Class cl = Class.forName(targetClass); Does not work in
// OSGI when class is defined in another bundle
Class<?> cl = null;
try {
cl = Thread.currentThread().getContextClassLoader().loadClass(targetClass);
} catch (ClassNotFoundException e) {
cl = getClass().getClassLoader().loadClass(targetClass);
}
if (!lists.containsKey(cl.getName())) {
lists.put(cl.getName(), new ArrayList<Object>());
}
generateModelFromKeyValueMap(cl, null, results, line, lists);
// Add list of objects
field.set(obj, lists.get(cl.getName()));
} else {
throw new IllegalArgumentException("No target class has been defined in @OneToMany annotation");
}
}
}
}
Aggregations