コード例 #1
0
  @SuppressWarnings("unchecked")
  @Override
  public List<T> parse(InputStream inputStream, Class<T> clazz) {

    // Preconditions
    SeedException.createNew(RendererErrorCode.INCORRECT_PARAM)
        .put(PARAM, clazz)
        .throwsIf(!DataRepresentation.class.equals(clazz));
    SeedException.createNew(RendererErrorCode.INCORRECT_PARAM)
        .put(PARAM, inputStream)
        .throwsIfNull(inputStream);

    ICsvMapReader mapReader = null;
    List<T> dataRepresentations = new ArrayList<T>();
    try {
      mapReader =
          new CsvMapReader(
              new InputStreamReader(inputStream, template.getCharsetName()),
              template.getPreferences());

      // Read file header and use it to determine the number of column
      String[] headers = mapReader.getHeader(true);
      if (headers != null && headers.length > 0) {
        final CellProcessor[] processors = new CellProcessor[headers.length];
        for (int i = 0; i < processors.length; i++) {
          processors[i] = new Optional();
        }

        // Read the input stream
        Map<String, Object> beanMap;
        while ((beanMap = mapReader.read(headers, processors)) != null) {
          // Get a bean map and transform it in an data representation
          DataRepresentation dataRepresentation = buildDataRepresentation(beanMap);
          dataRepresentations.add((T) dataRepresentation);
        }
      }

    } catch (IOException e) {
      LOGGER.error(e.getMessage(), e);
    } finally {
      closeQuietly(mapReader);
    }
    return dataRepresentations;
  }
コード例 #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();
    }
  }