0

I have below code for class IImporter which is working fine till now. I received many zip files everyday which contains name as 'EQUFULLFILE' and 'NONEQUFULLFILE' in a directory and i am trying to read files from this directory and process it in database table 'EquData' or 'NonEquData' accordingly. Before processing i am just trying to delete all the data from this database table.

But i found issue in my code. Sometimes when i dont receive any files in directory either 'EQUFULLFILE' or 'NONEQUFULLFILE' it just deletes the data from database table.

I just need to modify my code little bit to adapt logic such that when i dont receive any files in directory as 'EQUFULLFILE' then it should not delete any data from database table 'EquData'. When i dont receive any files in directory as'NONEQUFULLFILE' then it should not delete any data from database table 'NonEquData'.

Any suggestion please ?

@Service
public class IImporter {

    private final static Logger log = LoggerFactory.getLogger(IImporter.class);
    private final static String EQU_FILE_TAG = "EQUFULLFILE";
    private final static String NONEQU_FILE_TAG = "NONEQUFULLFILE";


    private boolean isEquity;

    @Autowired
    private IFullreader IFullreader;

    @Autowired
    private ZipWalker zipWalker;

    @Autowired
    private SessionFactory sessionFactory;

    @Transactional
    public void importDir(Path indir) throws IOException {
        log.info("Delete all table DATA");

    //here the logic should be changed and based on file name table should be deleted
        sessionFactory.getCurrentSession().createQuery("delete from EquData").executeUpdate();
        sessionFactory.getCurrentSession().createQuery("delete from NonEquData").executeUpdate();

        log.info("Process directory" + indir.toString());

        Files.walk(indir, 1, FOLLOW_LINKS)
            .filter(Files::isRegularFile)
            .filter(f -> f.toString().endsWith(".zip"))
            .sorted()
            .forEach(f -> zipWalker.processZipFile(f, this::importFile));
    }

    private void importFile(Path path) {
        this.isEquity = path.getFileName().toString().contains(EQU_FILE_TAG);
        if (isEquity) {
        //code for reading data from file EQUFULLFILE
        }       
        else {
        //code for reading data from file NONEQUFULLFILE
        }           
            
        }
    }
}
Symonds
  • 184
  • 1
  • 2
  • 15

1 Answers1

1

You have EQU_FILE_TAG defined twice. I suspect that's a typo.

How about this?

@Service
public class IImporter {

    private final static Logger log = LoggerFactory.getLogger(IImporter.class);
    private final static String EQU_FILE_TAG = "EQUFULLFILE";
    private final static String NONEQU_FILE_TAG = "NONEQUFULLFILE";

    private boolean isEquity;
    private boolean equFileFirstTime = true;
    private boolean nonequFileFirstTime = true;  

    @Autowired
    private IFullreader IFullreader;

    @Autowired
    private ZipWalker zipWalker;

    @Autowired
    private SessionFactory sessionFactory;

    @Transactional
    public void importDir(Path indir) throws IOException {
        log.info("Process directory" + indir.toString());

        Files.walk(indir, 1, FOLLOW_LINKS)
            .filter(Files::isRegularFile)
            .filter(f -> f.toString().endsWith(".zip"))
            .sorted()
            .forEach(f -> zipWalker.processZipFile(f, this::importFile));
    }

    private void importFile(Path path) {
        this.isEquity = path.getFileName().toString().contains(EQU_FILE_TAG);
        if (isEquity) {
            if (equFileFirstTime) {
                log.info("Delete EQUFULLFILE table DATA");
                sessionFactory.getCurrentSession().createQuery("delete from EquData").executeUpdate();
                equFileFirstTime = false;
            }
            //code for reading data from file EQUFULLFILE
        }       
        else {
            if (nonequFileFirstTime) {
                log.info("Delete NONEQUFULLFILE table DATA");
                sessionFactory.getCurrentSession().createQuery("delete from NonEquData").executeUpdate();
                nonequFileFirstTime = false;
            }
            //code for reading data from file NONEQUFULLFILE
        }           
            
    }
     
}
Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111
  • yes sorry it was type for EQU_FILE_TAG ...i will check and let you know but looks like correct logic :) thanks – Symonds Dec 15 '20 at 13:11