private void closeQuietly(ICsvMapReader mapReader) {
   if (mapReader != null) {
     try {
       mapReader.close();
     } catch (IOException e) {
       LOGGER.error(e.getMessage(), e);
     }
   }
 }
Пример #2
0
  public void executeMode() throws IOException, CoreEngineException {
    // ***** Get the input file
    String mergeCSVPath = config.getProperty(Constants.MERGE_CSV_FILE);
    if ((mergeCSVPath == null) || (mergeCSVPath.isEmpty())) {
      throw new CoreEngineException("No merge CSV file");
    }
    File dataFile = new File(mergeCSVPath);
    if (dataFile.exists() == false) {
      throw new CoreEngineException("Invalid merge CSV file - " + mergeCSVPath);
    }

    // ***** Get the output file
    String modeCSVPath = config.getProperty(Constants.MODE_CSV_FILE);
    if ((modeCSVPath == null) || (modeCSVPath.isEmpty())) {
      throw new CoreEngineException("No mode CSV file path");
    }

    // ***** Calculate mode
    ICsvMapReader csvReader = null;
    ICsvMapWriter csvWriter = null;
    try {
      logger.log(
          Level.INFO, "========== Calulating mode using data file - " + dataFile.getAbsolutePath());

      // ***** Parse the data file
      csvReader = new CsvMapReader(new FileReader(dataFile), CsvPreference.STANDARD_PREFERENCE);
      String readHeaders[] = csvReader.getHeader(true);
      CellProcessor csvReadProcessors[] = getReadCellProcessors();

      // ***** Iterate over each record, process each record & write to CSV
      Map<String, Object> record = null;
      while ((record = csvReader.read(readHeaders, csvReadProcessors)) != null) {
        populateRecordEntries(record);
      }
      logger.log(Level.INFO, recordEntryIndex.toString());
      calculateBedroomModePerYear();
      logger.log(Level.INFO, modeEntryList.toString());

      // ***** Write records once the mode is calculated
      csvWriter = new CsvMapWriter(new FileWriter(modeCSVPath), CsvPreference.STANDARD_PREFERENCE);
      String writeHeaders[] = getHeaders();
      csvWriter.writeHeader(writeHeaders);
      CellProcessor[] csvWriteProcessors = getWriteCellProcessors();

      for (int i = 0; i < modeEntryList.size(); i++) {
        Map<String, Object> modeEntry = (Map<String, Object>) modeEntryList.get(i);
        csvWriter.write(modeEntry, writeHeaders, csvWriteProcessors);
      }

      logger.log(
          Level.INFO,
          "========== Successfully caculated mode using data file - " + dataFile.getAbsolutePath());
    } catch (Exception e) {
      logger.log(
          Level.SEVERE,
          "========== Failed caculating mode using  data file - " + dataFile.getAbsolutePath(),
          e);
      throw new CoreEngineException(e);
    } finally {
      if (csvReader != null) csvReader.close();
      if (csvWriter != null) csvWriter.close();
    }
  }