Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Override
- protected T doRead() throws Exception { //NO SONAR
- logger.info("Start doRead");
- //check if the givem recource exists
- if (!this.resource.exists()) {
- if (this.strict) {
- throw new IllegalStateException("Input resource must exist (reader is in 'strict' mode): "
- + this.resource);
- }
- logger.warn("Input resource does not exist '"+this.resource.getDescription()+"'.");
- return null;
- }
- if (isFirstTime) {
- //create the appropriate file pattern deppending on the month number
- //if month < 3 choose current year System's configuration value - 1 (SystemConfiguration.year - 1)
- //else get the current year from the system configuration (SystemConfiguration.year)
- Calendar currentDate = Calendar.getInstance();
- int month = currentDate.get(Calendar.MONTH);
- SystemConfiguration sysConf = sysConfServ.findCurrentSystemConfiguration();//get system configuration
- //get create the patern for the current year
- String currentYearPatternStr = Constants.INST_DOCS_FILES_PATTERN.replace(Constants.INST_DOCS_YEAR_PATTERN, String.valueOf(sysConf.getYear()));
- List<String> patternStrList = new ArrayList<String>();
- patternStrList.add(currentYearPatternStr);
- if (month < 3) { //and if month is < 3 create the pattern for the previous System configuration year
- String previousYearPatternStr = Constants.INST_DOCS_FILES_PATTERN.replace(Constants.INST_DOCS_YEAR_PATTERN, String.valueOf(sysConf.getYear().intValue() - 1));
- patternStrList.add(previousYearPatternStr);
- }
- List<Pattern> patternList = new ArrayList<Pattern>(); //initialize Patterns
- for (String pattern : patternStrList) {
- patternList.add(Pattern.compile(pattern));
- }
- Matcher matcher = null; //and Matcher objects
- //fill the Queue with data
- File[] files = resource.getFile().listFiles();
- if (files != null && files.length != 0) { //if files is not empty
- for (File file : files) {
- for (Pattern pattern : patternList) { //then, for each file that its name matches with the pattern
- matcher = pattern.matcher(file.getName());
- if (matcher.matches()) {
- filesQueu.add(file); //append it in file-Queue
- }
- }
- }
- }
- //if file-queue is empty return null
- if (filesQueu.isEmpty()) {
- isFirstTime = true;
- return null;
- }
- isFirstTime = false;
- }
- //else
- while (!filesQueu.isEmpty()) {
- if (!isReading) {
- File file = filesQueu.peek(); //peek each element from the queue
- currentFileName = file.getName();
- this.openExcelFile(new FileSystemResource(file)); //open file
- this.openSheet(); //get the Sheets
- final Sheet sheet = this.getSheet(this.currentSheet);
- int currentFileRowsNumber = sheet.getNumberOfRows(); //get the number of rows
- currentRow = 0;
- }
- while (currentRow <= currentFileRowsNumber) {
- final String[] row = this.readRow(sheet); //read row
- //when finish the file reading pop the element
- if (this.currentRow == currentFileRowsNumber) {
- filesQueu.poll();
- this.isReading = false;
- }
- if (ObjectUtils.isEmpty(row)) { //if the row is empty, proscceed to the next row
- currentRow++;
- continue;
- }else {
- try {
- //else, send the data in InstDocWriter.write() and create a new document instance in database
- return this.rowMapper.mapRow(sheet, row, this.currentRow, this.currentFileName);
- } catch (final Exception e) {
- throw new ExcelFileParseException("Exception parsing Excel file.", e, this.resource.getDescription(),
- sheet.getName(), this.currentRow, row);
- }
- }
- }
- }
- logger.info("End doRead");
- return null; //when there where not any files left return null
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement