mardi 30 mai 2017

Set attribute of bean from a Generic Method Java

I am facing some difficulties to solve this problem. I have to make changes from a "generic" method for parsing CSV files to POJO lists. It was implemented by somebody else. The clazz parameters represent the possible beans that match the type of CSV file. I have to implement a certain logic for setting the last time a file was persisted in my database. Like this, I can keep track when was the last Parsed and persisted file and launch my batching process from this file for avoiding re-reading the whole folder. Should I use reflection or something like:

//  clazz.getMethod("setDate", initialDateFromFileName);

This is a little piece of the method where I already implemented the logic for retrieving the date I want following some business rules I have to stick with.

    private <T> List<T> performProcessing(String path, List<String> headers, Class<T> clazz, String pattern,
                String account, String fileSeparator){
  LOGGER.info("Start perform CSV Processing for account: " + account + " and pattern :" + pattern);

    List<T> listTargetObject = new ArrayList<>();
    Date lastModifiedDate = (Date) quartzDatabaseRepositoryIn.getLastModifiedDate(account);
    List<String> titleCase = new ArrayList<>();
    File folder = new File(path);
    File[] files = folder.listFiles();

    DateUtils.sortFilesByLastModifiedDateDescending(files);

    for (File file : files) {

        if (file.getName().contains(pattern)) {

            LOGGER.info(MessageFormat.format(FILE_RETRIEVED_LOGGER_MESSAGE, file.getName()));
            Date initialDateFromFileName = DateUtils.getDateFromFileName(file.getName());

            if (initialDateFromFileName.after(lastModifiedDate)) {


                  LOGIC FOR Unmarshalling
       ...
}

Then there is another method who will call the performCSV put it into an ArrayList of Foo Objects and persist into the database.

And let say I have a Bean(class) named: Class.java with different instance variables and one of them is

private Date date;
//With setters and getters

My question is: how could I access each setDate for each clazz parameter I will pass into this method?

Aucun commentaire:

Enregistrer un commentaire