Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse Scout » [How To] use CSV files in Eclipse Scout(Research of : CsvSqlAdapter, CsvSqlSettings, CsvHelper)
[How To] use CSV files in Eclipse Scout [message #1732049] Thu, 12 May 2016 08:37
barust Mising name is currently offline barust Mising nameFriend
Messages: 57
Registered: February 2014
Member
If you want to transfer data to/from CSV (comma-separated values file format) files you can use:
- OpenCSV link here : http://opencsv.sourceforge.net/
- use pure Java code, where Google can help you, for example here :
http://www.mkyong.com/java/how-to-read-and-parse-csv-file-in-java/
http://stackoverflow.com/questions/2885173/how-to-create-a-file-and-write-to-a-file-in-java?rq=1
http://docs.oracle.com/javase/tutorial/essential/io/file.html
http://www.javenue.info/files/csv.zip
In general, there are options to choose and which one to use depends on you. Smile

What about Scout?
If you search in the Scout (Shift+Ctrl+T), you are caught three "wonderful" (IMHO) classes:
- CsvSqlAdapter To execute data receiving and transmitting operations.
- CsvSqlSettings To configure data presentation parameters in a CSV file. Can be used in CsvSqlAdapter.
- CsvHelper As I understand it, it's like an assistant for completing the data based on the default template.
Maybe I am wrong in an explanation these classes, because intelligible documentation I have not found? It would be nice if someone explained function of these classes?

How I use them?
Has been used the following :
- Database table with name FILES and table field with name FILENAME in MySQL
- text file with name testreport1.txt
- the form ExportCashboxForm on #Client#Forms
- service ExportCashboxService on #Server#Services on create() method
- must perform: writing table rows to a TXT/CSV file.
- exportData(CsvSqlSettings) method of CsvSqlAdapter class using sql query result to write to file.

You can also use other methods of a CsvSqlAdapter class to export and import
- exportDataFromTable(File, String, Locale, String, String, String, String, Object, String, List<String>, boolean, List<String>, boolean))
- exportDataWithSql(File, String, Locale, String, String, String, NVPair[], List<String>, boolean, List<String>, boolean)
- exportData(CsvSqlSettings)
- importDataIntoTable(File, String, Locale, int, String, String, String, String, Object, String, List<String>, List<String>, boolean)
- importData(CsvSqlSettings)

public class ExportCashboxService extends AbstractService implements IExportCashboxService {

  @Override
  public ExportCashboxFormData create(ExportCashboxFormData formData) throws ProcessingException {

    ISqlService service = SERVICES.getService(ISqlService.class);
    String sql = "SELECT FILENAME FROM FILES";
    service.select("SELECT FILENAME FROM FILES", formData);

    CsvSqlAdapter exp = new CsvSqlAdapter(service);
    CsvSqlSettings s = new CsvSqlSettings();

    File file = new File("/home/user/reports/testreport1.txt");
    file.setWritable(true);
    
    // for testing 
    //System.out.println(file.getName());

    s.getBindBase();
    s.setColSeparator(',');
    Locale locale = Locale.getDefault();
    s.setContentLocale(locale);
    // There is only one field, you can add other
    List<String> CNames = Arrays.asList("FILENAME");
    s.setCsvColumnNames(CNames);
    // There is only one type, you can add other
    List<String> CTypes = Arrays.asList("VARCHAR");
    s.setCsvColumnTypes(CTypes);
    s.setEncoding("UTF-8");
    s.setFile(file);
    s.setSqlSelect(sql);
    s.setTextDelimiter('"');
    //if you want write to file with columns name and types change below to true.
    s.setWriteColumnNames(false);
    s.setWriteColumnTypes(false);

    exp.exportData(s);

    return formData;
  }


Remark:
During the research I have encountered the following difficulties and I want to direct your attention to them:
- The rights and privileges of a text file must be set properly
- setCsvColumnNames and setCsvColumnTypes methods Must contain the names and types of columns, respectively

Exposed code above is a working, but does not claim to be correct. If you notice I did not use the exception Try Catch where needed, I think you can fix it and add to the topic.
Exposed code above was tested in a Linux environment, so pay attention to the path specification to the files and folders I mean the (\) and (/) characters.
I did not use and did not explain the method CsvHelper. Can anyone explain his appointment?
I would appreciate if anyone had experience of using the exportDataFromTable() or importDataIntoTable() methods, and to put his decision here? Smile
If I made a mistake somewhere please correct me or add to it.

Thanks,
Bakhtiyor.


Resources :
From Wikipedia https://en.wikipedia.org/wiki/Comma-separated_values
From creativyst.com http://www.creativyst.com/Doc/Articles/CSV/CSV01.htm
From mastpoint http://mastpoint.curzonnassau.com/csv-1203/
From rfc-editor.org http://www.rfc-editor.org/rfc/rfc4180.txt
RFC 4180 http://www.digitalpreservation.gov/formats/fdd/fdd000323.shtml
From Oracle Doc http://docs.oracle.com/javase/tutorial/essential/io/index.html

[Updated on: Thu, 12 May 2016 08:43]

Report message to a moderator

Previous Topic:[neon] M7 crash in session creation
Next Topic:[NEON] NPE while using FormBasedAuthentication
Goto Forum:
  


Current Time: Thu Apr 25 22:48:04 GMT 2024

Powered by FUDForum. Page generated in 0.02558 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top