-4

I'm using Bindy to a map csv file data to a given model class. This whole process works fine, except the default value of the boolean field. This field is not part of the csv file, but necessary in the model class for further processes. I have set the default value to true, but this will be "ignored" and the field is currently always set to false if I run the camel route.

This is a snippet of my model class:

@CsvRecord(separator = " ", skipFirstLine = false)
public class MyModel {

  // ... more data fields

  @DataField(pos = 8, defaultValue = "true")
  public boolean approved;

defaultValue expects a string value so I can't set it to Boolean.TRUE. I already tried other values like "TRUE", "yes", "y" and "1" with no luck.

The source of BindyCsvFactory says that the class Format is used to set the default value, but it seems that it can't handle boolean fields and uses the default of boolean instead and this is false.

The locale is currently set to "en_Us" using this command:

BindyCsvDataFormat format = new BindyCsvDataFormat("[model package]");
format.setLocale("en_US");

So the main question is: How do I set the default value of a boolean field to true in a Bindy managed model class?

  • Can you try with a Boolean type to see if you can get that working. If so its likely a little bug in Camel when its a boolean type. – Claus Ibsen Aug 18 '14 at 17:14
  • And btw which version of Camel do you use? – Claus Ibsen Aug 18 '14 at 17:14
  • @ClausIbsen I already tried that and the field was set to `null` (with defaultValue = "true"). The Version is 2.12.2. Also tried 2.13.2 already. –  Aug 18 '14 at 17:44
  • Hm, I got a `java.lang.IllegalArgumentException: Can not find a suitable formatter for the type: java.lang.Boolean`. `org.apache.camel.dataformat.bindy.FormatFactory` seems not to support attributes of type `Boolean`- – Peter Keller Aug 18 '14 at 18:12
  • Why do you have a DataField annotation on the boolean field if its not part of the CSV? What's stopping you just righting public boolean approved = true? – matt helliwell Aug 18 '14 at 20:31
  • @matthelliwell That field is currently not part of the CSV, but later it will be. So setting the default value is much cleaner than setting it manually to `true` :D. But if this boolean thing is really a bug in Camel I'll bypass it by setting it manually, until it's fixed. –  Aug 18 '14 at 20:40
  • 1
    I see thanks. Interstingly the docs (http://camel.apache.org/maven/camel-2.12.0/camel-bindy/apidocs/org/apache/camel/dataformat/bindy/FormatFactory.html) seem to say that the only formatters are for String, Integer and Byte – matt helliwell Aug 18 '14 at 21:19
  • @matthelliwell Nice find. Well, I guess this leaves me no other choiche than setting the value manually like you mentioned before. –  Aug 19 '14 at 10:02
  • If someone wonders about the vote count: this question was targeted by revenge downvoters, like [Erik](http://stackoverflow.com/users/323221), [Nicolas](http://stackoverflow.com/users/1997376) and [BRap](http://stackoverflow.com/users/2504578). –  Jan 14 '17 at 18:19

1 Answers1

3

You can't. If you have a look at the FormatFactory code (http://camel.apache.org/maven/camel-2.11.0/camel-bindy/apidocs/src-html/org/apache/camel/dataformat/bindy/FormatFactory.html) you can see that it doesn't support booleans.

It looks like your choices are either

  • Remove the @DataField annotation and just initialise the boolean with public boolean approved = true. You'll have to add it back in later once the approved field is added to the input file.
  • Read the column as a string or integer and convert to a boolean in your code.
matt helliwell
  • 2,574
  • 16
  • 24
  • 1
    I logged a ticket to get camel-bindy to support this in the future: https://issues.apache.org/jira/browse/CAMEL-7724 – Claus Ibsen Aug 19 '14 at 18:05
  • @ClausIbsen I've tested this change and the defaultValue is still ignored if the field does not appear in the csv file. But if this appears bindy can evaluate the value correctly. Is this the expected behavior? And another interesting thing: I've added another field (a String) to the model class that is not required and has a default value. If I add the boolean to the csv file and leave the new string empty, then the default value of the string is ignored and null will be set to it. If I remove that boolean value, then the default value is used correctly. –  Aug 27 '14 at 07:17