Projects/Generic CSV Bankstatement Importer/Developers Manual
Contents |
About this document
This document is oriented to Java developers who want to create new CSV bank statement importers for any CSV file format based on the Generic CSV Bank Statement Importer framework.
Introduction
The way to import bank statements lines from a CSV file is either to adapt your CSV file to a defined structure compatible with the Generic CSV Bank Statement Importer or to develop some code to support the concrete CSV format.
The advantage of the latter solution is that the user can directly import the CSV file generated from any software into Openbravo, instead of manually adapting it to the generic CSV format each time he/she wants to import a new file.
The Generic CSV Bank Statement importer module provides a framework that makes it easier to develop the code for supporting any CSV bank statement lines format.
Creating the Java Bean
The first step is to create a Java Bean that represents the file structure we want to import. Each one of the CSV columns must be mandatory represented as a String attribute. Let's see an example that will help us to understand it better:
public class GenericBankStatementLineBean extends GenericCsvImporterBean { public static final String PROPERTY_BPARTNERNAME = "bpartnername"; public static final String PROPERTY_TRANSACTIONDATESTRING = "transactionDateString"; public static final String PROPERTY_CRAMOUNTSTRING = "cramountString"; private String bpartnername; private String cramountString; private BigDecimal cramount; private String transactionDateString; private Date transactionDate; public String getBpartnername() { return bpartnername; } public void setBpartnername(String bpartnername) { this.bpartnername = bpartnername; } public void setCramountString(String cramountString) { this.cramountString = cramountString; setCramount(Utility.stringToBigDecimal(cramountString, getDecimalSeparator())); } public void setTransactionDateString(String transactionDateString) throws ParseException { this.transactionDateString = transactionDateString; setTransactionDate(Utility.stringToDate(transactionDateString, getDateFormat())); } // Rest of getter/setter hidden for simplicity }
- The bean must extend the org.openbravo.bankstatement.importer.generic.csv.bean.GenericCsvImporterBean class
- Each one of the columns in the CSV file is represented by a String attribute into the bean. Example: bpartnername, cramountString and transactionDateString.
- The framework will automatically create a new object for each line inside the CSV file, mapping each CSV field with a String attribute in the class (we will see it later on).
- The CSV fields that represent a date or an amount must be transformed to a Date and BigDecimal object respectively. This is done inside the corresponding setter method:
- For creating the Date object, we can use the Utility.stringToDate() method defined into the Generic CSV Bank Statement Importer framework. Observe that the setter method for the String representation of the date field is executing a setter method call for the correspondent Date attribute:
public void setTransactionDateString(String transactionDateString) throws ParseException { this.transactionDateString = transactionDateString; setTransactionDate(Utility.stringToDate(transactionDateString, getDateFormat())); }
- The same logic is applied for creating the correspondent BigDecimal object, but this time using the Utility.stringToBigDecimal() method:
public void setCramountString(String cramountString) { this.cramountString = cramountString; setCramount(Utility.stringToBigDecimal(cramountString, getDecimalSeparator())); }
- The getDecimalSeparator() and getDateFormat() methods are inherited from the GenericCsvImporterBean class. They are used for automatically getting the decimal separator and date format configured in the client.
Creating the CSV Importer
The last step is to create a class with the logic for importing the CSV file. Let's see an example:
public class GenericCSVImporterImplementation extends GenericCSVImporter<GenericBankStatementLineBean> { @Override public Map<String, String> generateColumnMapping() { Map<String, String> map = new HashMap<String, String>(); map.put("Transaction Date", GenericBankStatementLineBean.PROPERTY_TRANSACTIONDATESTRING); map.put("Business Partner Name", GenericBankStatementLineBean.PROPERTY_BPARTNERNAME); map.put("Amount IN", GenericBankStatementLineBean.PROPERTY_CRAMOUNTSTRING); return map; } @Override public Class<GenericBankStatementLineBean> configureBean() { return GenericBankStatementLineBean.class; } @Override public List<FIN_BankStatementLine> generateFIN_BankStatementLine() { List<FIN_BankStatementLine> bankStatementLines = new ArrayList<FIN_BankStatementLine>(); for (final GenericBankStatementLineBean gbslb : getBankStatementLines()) { bankStatementLines.add(Utility.createFIN_BankStatementLine(gbslb, getTargetBankStatement(), getLineNo() + 10l)); } return bankStatementLines; } }
- The class must extend the GenericCSVImporter class, which forces us to override several methods:
- The generateColumnMapping() method is in charge of defining the mapping between the CSV header columns and the correspondent String attributes of our bean. Example, in the CSV file there is a column called Business Partner Name that is related to the bpartnername attribute of our bean.
- The configureBean() method just return the class that represents our previous bean.
- The generateFIN_BankStatementLine() method is the one that contains the real business logic. Observe how we called the getBankStatementLines() method (defined at the superclass) to get the List of bank statement lines read from the CSV file. These objects belong to your previously defined bean class (GenericBankStatementLineBean in the example), and you can use their getter methods as usual to operate with the data. In the example we just set the Line No attribute.
Finally, the generateFIN_BankStatementLine() method must return the List of FIN_BankStatementLine ready to be saved into the system
This is everything you need to develop your own CSV bank statement lines importer based on the Generic CSV Bank Statement Importer framework.
Finally remember that you can package your code as a module to be easily distributed to your customers. For more information see the Modularity concepts.