Showing posts with label csv. Show all posts
Showing posts with label csv. Show all posts

Sunday, October 19, 2014

JSefa tutorial and how to Support for Double, Short Data Type Mapping and Custom String separator

Having a Java Bean object, I wanted to create a CSV file. For that reason, I have used JSEFA which is a Java Simple exchange format API. So JSEFA allows us to convert our Java Bean to CSV file using few lines.

So first, I have created my bean  :


import org.jsefa.csv.annotation.CsvDataType;
import org.jsefa.csv.annotation.CsvField;


@CsvDataType
public class UserInfo {

 @CsvField(pos = 1)
 String userId;
 @CsvField(pos = 2)
 String userPassword;
 @CsvField(pos = 7, converterType = ShortConverter.class)
 Short bloodType;
 @CsvField(pos = 12, converterType = DoubleConverter.class)
 Double altitude;

}

Here if you don't put the Converted, you will get an error of type :

org.jsefa.IOFactoryException: Failed to create an CsvIOFactory
at org.jsefa.csv.CsvIOFactory.createFactory(CsvIOFactory.java:113)
at org.jsefa.csv.CsvIOFactory.createFactory(CsvIOFactory.java:87)
.....
Caused by: org.jsefa.common.mapping.TypeMappingException: Can not create a type mapping for field bloodType of class org.quwic.itms.dal.expression.UserInfo
at org.jsefa.rbf.annotation.RbfTypeMappingFactory.createFieldMappings(RbfTypeMappingFactory.java:181)
at org.jsefa.rbf.annotation.RbfTypeMappingFactory.createComplexTypeMappingIfAbsent(RbfTypeMappingFactory.java:129)

For that reason I have added the convertType = ShortConverter.class. The same thing for the Double type which is not supported by JSEFA.
So the ShortConverter and DoubleConverter classes should be provided. and they need to implements the JSefa Interface SimpleTypeConverter.



import org.jsefa.common.converter.SimpleTypeConverter;

public class DoubleConverter implements SimpleTypeConverter {
 
 private static final DoubleConverter INSTANCE = new DoubleConverter();
 public static DoubleConverter create() {
 return INSTANCE;
 }
 private DoubleConverter() {
 }
 @Override
 public Object fromString(String s) {
 return new Double(s);
 }
 @Override
 public String toString(Object d) {
 return d.toString();
 }

}

Once this is done, you need then to map your beans one by one and create the CSV (or XML file) in your main class:


import java.io.StringWriter;
import org.jsefa.Serializer;
import org.jsefa.csv.CsvIOFactory;
import org.jsefa.csv.config.CsvConfiguration;

public class UsersBeanToCSV {

 public void processUser(List<UserInfo> users){
  CsvConfiguration config = new CsvConfiguration();
  config.setFieldDelimiter(',');
  config.getSimpleTypeConverterProvider().registerConverterType(Double.class, DoubleConverter.class);
  config.getSimpleTypeConverterProvider().registerConverterType(Short.class, ShortConverter.class);
   
  
  Serializer serializer = CsvIOFactory.createFactory(config,MddNotification.class).createSerializer();
  StringWriter writer = new StringWriter();

  serializer.open(writer);
  for(UserInfo user:users){
   serializer.write(user);
  } 
  
  
  serializer.close(true);
 } 
} 

We use here the CsvConfiguration to register the new Converter.
By default, Jsefa uses the ';' to delimit fields, but as  I want to use ',', I declare that using the setFieldDelimiter.

I have just discovered an other issue, so I update my post. In fact, you need to pay attention when the double or Short value may be null (you will have a NullponterException)

 So if the values are optional, you may have some beans with null values.
So you need to modify the DoubleConverter:



import org.jsefa.common.converter.SimpleTypeConverter;

public class DoubleConverter implements SimpleTypeConverter {
 
  private static final DoubleConverter INSTANCE = new DoubleConverter();
  public static DoubleConverter create() {
  return INSTANCE;
  }
  private DoubleConverter() {
  }
  @Override
  public Object fromString(String s) {
   if(s!=null){
    return new Double(s);
   }else{
    return null
   }    
  }
  @Override
  public String toString(Object d) {
   if(d!= null){
    return d.toString();
   }else{
    return null;
   }
  }

}


That's all, hope it helped you :).

Monday, April 14, 2014

Spring Integration: Mqtt integration and Transformation of the mqtt Message from CSV to Java Bean using int:transformer


Working with Spring Integration, the use of Mqtt become very easy. In fact we need only to declare an int-mqtt:message-driven-channel-adapter in the applicationContext or spring-integration-context.xml

The code is :



The integration graph looks then like this :

So in order to convert the message coming from the broker (in CSV format), we need to call the method convert in the created CsvConverter class. In order to configure this using int:transformer, we need to declare a spel-function where we specify the class and called method, then we use the Id inside the spEL expression inside the transformer by putting # before the id of the converter.
so here is the code.

<int:spel-function id="csvConverter" class="org.converter.CsvConverter" method="convert(java.lang.String)" />

<int:transformer id="csvTransformer"
input-channel="mqttMessages"
output-channel="record" expression="#csvConverter(payload)" />

So now the transformer will call the CSVConverter which will convert the mqtt message payload from CSV to Java Bean (here Record).  The conversion is done using CSVeed framework which construct the given bean from the csv message.




Sunday, April 6, 2014

CSV Tutorial : Convert CSV to Java Bean

In my project I needed to convert a CSV file to a list of beans (Person).
I first used JSEFA, but once tested, I have discovered that this framework have problem to convert fields to Double or Float.

I decided then to move to another framework, and finally I found the CSVeed framework which is quit nice and simple.



By default, the separator in CSVeed is ";" so make sure to change it if necessary.

@CsvFile(comment = '%', quote='\'', escape='\\', separator=',')
public Class Person{
...
}

Their web site have a comparison  with other frameworks which do also the conversion. For more details, have a look on their matrix.

Articles les plus consultés