Advertisement
Guest User

Untitled

a guest
Oct 2nd, 2014
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.62 KB | None | 0 0
  1.     @Override
  2.     protected T doRead() throws Exception { //NO SONAR
  3.         logger.info("Start doRead");
  4.         //check if the givem recource exists
  5.         if (!this.resource.exists()) {
  6.             if (this.strict) {
  7.                 throw new IllegalStateException("Input resource must exist (reader is in 'strict' mode): "
  8.                         + this.resource);
  9.             }
  10.             logger.warn("Input resource does not exist '"+this.resource.getDescription()+"'.");
  11.             return null;
  12.         }
  13.         if (isFirstTime) {
  14.             //create the appropriate file pattern deppending on the month number
  15.             //if month < 3 choose current year System's configuration value - 1 (SystemConfiguration.year - 1)
  16.             //else get the current year from the system configuration (SystemConfiguration.year)
  17.             Calendar currentDate = Calendar.getInstance();
  18.             int month = currentDate.get(Calendar.MONTH);
  19.  
  20.             SystemConfiguration sysConf = sysConfServ.findCurrentSystemConfiguration();//get system configuration
  21.             //get create the patern for the current year
  22.             String currentYearPatternStr = Constants.INST_DOCS_FILES_PATTERN.replace(Constants.INST_DOCS_YEAR_PATTERN, String.valueOf(sysConf.getYear()));
  23.  
  24.  
  25.             List<String> patternStrList = new ArrayList<String>();
  26.             patternStrList.add(currentYearPatternStr);
  27.             if (month < 3) { //and if month is < 3 create the pattern for the previous System configuration year
  28.                 String previousYearPatternStr = Constants.INST_DOCS_FILES_PATTERN.replace(Constants.INST_DOCS_YEAR_PATTERN, String.valueOf(sysConf.getYear().intValue() - 1));
  29.                 patternStrList.add(previousYearPatternStr);
  30.             }
  31.  
  32.             List<Pattern> patternList = new ArrayList<Pattern>(); //initialize Patterns
  33.             for (String pattern : patternStrList) {
  34.                 patternList.add(Pattern.compile(pattern));
  35.             }
  36.             Matcher matcher = null; //and Matcher objects
  37.  
  38.  
  39.             //fill the Queue with data
  40.             File[] files = resource.getFile().listFiles();
  41.             if (files != null && files.length != 0) { //if files is not empty
  42.                 for (File file : files) {
  43.                     for (Pattern pattern : patternList) { //then, for each file that its name matches with the pattern
  44.                         matcher = pattern.matcher(file.getName());
  45.                         if (matcher.matches()) {
  46.                             filesQueu.add(file); //append it in file-Queue
  47.                         }
  48.                     }
  49.                 }
  50.             }
  51.             //if file-queue is empty return null
  52.             if (filesQueu.isEmpty()) {
  53.                 isFirstTime = true;
  54.                 return null;
  55.             }
  56.             isFirstTime = false;
  57.         }
  58.  
  59.         //else
  60.         while (!filesQueu.isEmpty()) {
  61.  
  62.             if (!isReading) {
  63.                 File file = filesQueu.peek(); //peek each element from the queue
  64.                 currentFileName = file.getName();
  65.                 this.openExcelFile(new FileSystemResource(file)); //open file
  66.                 this.openSheet(); //get the Sheets
  67.                 final Sheet sheet = this.getSheet(this.currentSheet);
  68.                 int currentFileRowsNumber = sheet.getNumberOfRows(); //get the number of rows
  69.                 currentRow = 0;
  70.             }
  71.  
  72.  
  73.             while (currentRow <= currentFileRowsNumber) {
  74.  
  75.  
  76.  
  77.                 final String[] row = this.readRow(sheet);  //read row
  78.                 //when finish the file reading pop the element
  79.                 if (this.currentRow == currentFileRowsNumber) {
  80.                     filesQueu.poll();
  81.                     this.isReading = false;
  82.                 }
  83.                 if (ObjectUtils.isEmpty(row)) { //if the row is empty, proscceed to the next row
  84.                     currentRow++;
  85.                     continue;
  86.                 }else {
  87.                     try {
  88.                         //else, send the data in  InstDocWriter.write() and create a new document instance in database
  89.                         return this.rowMapper.mapRow(sheet, row, this.currentRow, this.currentFileName);
  90.                     } catch (final Exception e) {
  91.                         throw new ExcelFileParseException("Exception parsing Excel file.", e, this.resource.getDescription(),
  92.                                 sheet.getName(), this.currentRow, row);
  93.                     }
  94.                 }
  95.  
  96.             }
  97.         }
  98.         logger.info("End doRead");
  99.         return null; //when there where not any files left return null
  100.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement