List of usage examples for org.apache.commons.csv CSVParser CSVParser
public CSVParser(final Reader reader, final CSVFormat format) throws IOException
If you do not read all records from the given reader , you should call #close() on the parser, unless you close the reader .
From source file:canreg.client.gui.components.PreviewFilePanel.java
/** * */// w ww.j a v a 2 s . c o m @Action public void previewAction() { // show the contents of the file BufferedReader br = null; try { changeFile(); // numberOfRecordsTextField.setText(""+(canreg.common.Tools.numberOfLinesInFile(inFile.getAbsolutePath())-1)); FileInputStream fis = new FileInputStream(inFile); br = new BufferedReader(new InputStreamReader(fis, (Charset) charsetsComboBox.getSelectedItem())); CSVFormat csvFormat = CSVFormat.DEFAULT.withFirstRecordAsHeader().withDelimiter(getSeparator()); CSVParser csvParser = new CSVParser(br, csvFormat); int linesToRead = Globals.NUMBER_OF_LINES_IN_IMPORT_PREVIEW; int numberOfLinesRead = 0; Vector<Vector<String>> data = new Vector<Vector<String>>(); String[] headers = csvParser.getHeaderMap().keySet().toArray(new String[0]); for (CSVRecord csvRecord : csvParser) { csvRecord.toMap(); Vector vec = new Vector(); Iterator<String> iterator = csvRecord.iterator(); while (iterator.hasNext()) { vec.add(iterator.next()); } data.add(vec); numberOfLinesRead++; if (numberOfLinesRead >= linesToRead) { break; } } numberOfRecordsShownTextField.setText(numberOfLinesRead + ""); // previewTextArea.setText(headers + "\n" + dataText); // previewTextArea.setCaretPosition(0); previewPanel.setVisible(true); Vector columnNames = new Vector(Arrays.asList(headers)); previewTable.setModel(new DefaultTableModel(data, columnNames)); } catch (FileNotFoundException fileNotFoundException) { JOptionPane.showInternalMessageDialog(CanRegClientApp.getApplication().getMainFrame().getContentPane(), java.util.ResourceBundle.getBundle("canreg/client/gui/dataentry/resources/ImportView") .getString("COULD_NOT_PREVIEW_FILE:") + " \'" + fileNameTextField.getText().trim() + "\'.", java.util.ResourceBundle.getBundle("canreg/client/gui/dataentry/resources/ImportView") .getString("ERROR"), JOptionPane.ERROR_MESSAGE); Logger.getLogger(PreviewFilePanel.class.getName()).log(Level.SEVERE, null, fileNotFoundException); } catch (IOException ex) { Logger.getLogger(PreviewFilePanel.class.getName()).log(Level.SEVERE, null, ex); } finally { try { if (br != null) { br.close(); } } catch (IOException ex) { Logger.getLogger(PreviewFilePanel.class.getName()).log(Level.SEVERE, null, ex); } } }
From source file:com.siemens.sw360.portal.portlets.admin.UserPortlet.java
private List<UserCSV> getUsersFromRequest(PortletRequest request, String fileUploadFormId) throws IOException, TException { final UploadPortletRequest uploadPortletRequest = PortalUtil.getUploadPortletRequest(request); final InputStream stream = uploadPortletRequest.getFileAsStream(fileUploadFormId); Reader reader = new InputStreamReader(stream); CSVFormat format = CommonUtils.sw360CsvFormat; CSVParser parser = new CSVParser(reader, format); List<CSVRecord> records; records = parser.getRecords();/* w ww. j ava2 s.c o m*/ if (records.size() > 0) { records.remove(0); // Remove header } return getUsersFromCSV(records); }
From source file:edu.harvard.mcz.imagecapture.loader.JobVerbatimFieldLoad.java
/** * Attempt to read file with a given CSV format, and if successful, return * the number of rows in the file./* ww w . j ava 2 s. c o m*/ * * @param file to check for csv rows. * @param formatToTry the CSV format to try to read the file with. * @return number of rows in the file. * @throws IOException on a problem reading the header. * @throws FileNotFoundException on not finding the file. */ protected int readRows(File file, CSVFormat formatToTry) throws IOException, FileNotFoundException { int rows = 0; Reader reader = new FileReader(file); CSVParser csvParser = new CSVParser(reader, formatToTry); Iterator<CSVRecord> iterator = csvParser.iterator(); while (iterator.hasNext()) { iterator.next(); rows++; } csvParser.close(); reader.close(); return rows; }
From source file:br.com.hslife.orcamento.service.ImportacaoLancamentoService.java
@Override public void processarArquivoCSVImportado(Arquivo arquivo, Conta conta) throws ApplicationException { try {// ww w.j av a2 s .c o m // Declarao e leitura dos dados do CSV final Reader reader = new InputStreamReader(new ByteArrayInputStream(arquivo.getDados()), "UTF-8"); final CSVParser parser = new CSVParser(reader, CSVFormat.DEFAULT.withHeader()); // Declarao das variveis LancamentoImportado lancamentoImportado = new LancamentoImportado(); for (CSVRecord record : parser) { int quantidade = Integer.parseInt(record.get("QUANTIDADE")); lancamentoImportado.setConta(conta); lancamentoImportado.setData(new SimpleDateFormat("yyyy-MM-dd").parse(record.get("DATA"))); lancamentoImportado.setHistorico(record.get("HISTORICO")); lancamentoImportado.setValor(Double.parseDouble(record.get("VALOR"))); lancamentoImportado.setMoeda(record.get("MOEDA")); lancamentoImportado.setDocumento(record.get("DOCUMENTO")); lancamentoImportado.setObservacao(record.get("OBSERVACAO")); lancamentoImportado.setCategoria(record.get("CATEGORIA")); lancamentoImportado.setFavorecido(record.get("FAVORECIDO")); lancamentoImportado.setMeiopagamento(record.get("MEIOPAGAMENTO")); lancamentoImportado.setHash(lancamentoImportado.getFullLabel()); // Insere o lanamento importado X vezes de acordo com o campo QUANTIDADE for (int i = 1; i <= quantidade; i++) { getRepository().save(lancamentoImportado.clonarLancamento(i)); } } // Fecha os streams parser.close(); reader.close(); } catch (Exception e) { throw new ApplicationException(e); } }
From source file:citation_prediction.CitationCore.java
/** * Get CSV citation history from a file. * //from w ww . ja va2 s . com * @param filename The filename and path containing the citation data. * @param format The format of the file. * @param hasHeader Does the file have a line with headings? * @return A record containing the csv information. */ private static List<CSVRecord> getCSVData(String filename, CSVFormat format, boolean hasHeader) { boolean error = true; List<CSVRecord> list_ourdata = null; try { FileReader ourdata = new FileReader(filename); CSVParser data_parser = new CSVParser(ourdata, format); list_ourdata = data_parser.getRecords(); if (hasHeader) { list_ourdata.remove(0); } //remove header file. Iterator<CSVRecord> list_iterator = list_ourdata.iterator(); for (int rowIndex = 0; rowIndex < list_ourdata.size(); rowIndex++) { CSVRecord record = list_iterator.next(); System.out.println("#" + (rowIndex + 1) + " " + record.toString()); } data_parser.close(); ourdata.close(); error = false; } catch (java.io.FileNotFoundException e) { System.err.println("ERROR: There was an error opening, reading, or parsing the input file."); System.err.println("ERROR:" + filename); error = true; } catch (java.io.IOException e) { System.err.println("ERROR: Could not close the parser or the input file."); error = true; } if (error || list_ourdata == null) { System.exit(1); return null; } else { return list_ourdata; } }
From source file:com.amazonaws.services.dynamodbv2.online.index.integration.tests.ViolationDetectionTest.java
/** * Verifies the contents of the violation detection output file *///from ww w . j a va 2 s . c o m private void checkViolationFileOutput(String detectorOpFile, String tableHashKey, String tableRangeKey, String tableHashKeyType, String tableRangeKeyType, boolean violatedValueExists, List<Map<String, AttributeValue>> hashKeySizeViolations, List<Map<String, AttributeValue>> rangeKeySizeViolations, List<Map<String, AttributeValue>> hashKeyTypeViolations, List<Map<String, AttributeValue>> rangeKeyTypeViolations, int violatedSize, String gsiHashKeyName, String gsiHashKeyType, String gsiRangeKeyName, String gsiRangeKeyType) throws IOException { Map<String, String> hashToRangeHashSizeViolationsMap = new HashMap<String, String>(); Map<String, String> hashToRangeRangeSizeViolationsMap = new HashMap<String, String>(); Map<String, String> hashToRangeHashTypeViolationsMap = new HashMap<String, String>(); Map<String, String> hashToRangeRangeTypeViolationsMap = new HashMap<String, String>(); Map<String, String> hashToGsiHashSizeViolationsMap = new HashMap<String, String>(); Map<String, String> hashToGsiRangeSizeViolationsMap = new HashMap<String, String>(); Map<String, String> hashToGsiHashTypeViolationsMap = new HashMap<String, String>(); Map<String, String> hashToGsiRangeTypeViolationsMap = new HashMap<String, String>(); BufferedReader br = null; CSVParser parser = null; try { br = new BufferedReader(new FileReader(new File(detectorOpFile))); parser = new CSVParser(br, TestUtils.csvFormat); List<CSVRecord> csvRecords = parser.getRecords(); for (CSVRecord csvRecord : csvRecords) { String hashKey = csvRecord.get(ViolationRecord.TABLE_HASH_KEY); String rangeKey = csvRecord.get(ViolationRecord.TABLE_RANGE_KEY); String gsiHashKeyValue = null; if (violatedValueExists) { gsiHashKeyValue = csvRecord.get(ViolationRecord.GSI_HASH_KEY); } String hashKeyViolationType = csvRecord.get(ViolationRecord.GSI_HASH_KEY_VIOLATION_TYPE); String hashKeyViolationDesc = csvRecord.get(ViolationRecord.GSI_HASH_KEY_VIOLATION_DESC); String gsiRangeKeyValue = null; if (violatedValueExists) { gsiRangeKeyValue = csvRecord.get(ViolationRecord.GSI_RANGE_KEY); } String rangeKeyViolationType = csvRecord.get(ViolationRecord.GSI_RANGE_KEY_VIOLATION_TYPE); String rangeKeyViolationDesc = csvRecord.get(ViolationRecord.GSI_RANGE_KEY_VIOLATION_DESC); boolean foundViolation = false; if (hashKeyViolationType.equals("Size Violation")) { foundViolation = true; hashToRangeHashSizeViolationsMap.put(hashKey, rangeKey); if (violatedValueExists) { hashToGsiHashSizeViolationsMap.put(hashKey, gsiHashKeyValue); } Assert.assertTrue("Gsi hash key size violation description is incorrect", hashKeyViolationDesc.equals("Max Bytes Allowed: " + TestUtils.MAX_HASH_KEY_SIZE + " Found: " + violatedSize)); } else if (hashKeyViolationType.equals("Type Violation")) { foundViolation = true; hashToRangeHashTypeViolationsMap.put(hashKey, rangeKey); if (violatedValueExists) { hashToGsiHashTypeViolationsMap.put(hashKey, gsiHashKeyValue); } Assert.assertTrue("Gsi hash key type violation description is incorrect", hashKeyViolationDesc.equals("Expected: " + gsiHashKeyType + " Found: " + TestUtils.returnDifferentAttributeType(gsiHashKeyType))); } else { Assert.assertTrue("Hash key violation description exists even when there is no violation type", hashKeyViolationDesc.equals("")); } if (rangeKeyViolationType.equals("Size Violation")) { foundViolation = true; hashToRangeRangeSizeViolationsMap.put(hashKey, rangeKey); if (violatedValueExists) { hashToGsiRangeSizeViolationsMap.put(hashKey, gsiRangeKeyValue); } Assert.assertTrue("GSI range key size violation description is incorrect", rangeKeyViolationDesc.equals("Max Bytes Allowed: " + TestUtils.MAX_RANGE_KEY_SIZE + " Found: " + violatedSize)); } else if (rangeKeyViolationType.equals("Type Violation")) { foundViolation = true; hashToRangeRangeTypeViolationsMap.put(hashKey, rangeKey); if (violatedValueExists) { hashToGsiRangeTypeViolationsMap.put(hashKey, gsiRangeKeyValue); } Assert.assertTrue("Gsi range key type violation description is incorrect", rangeKeyViolationDesc.equals("Expected: " + gsiRangeKeyType + " Found: " + TestUtils.returnDifferentAttributeType(gsiRangeKeyType))); } else { Assert.assertTrue("Range key violation description exists even when there is no violation type", rangeKeyViolationDesc.equals("")); } Assert.assertTrue("No violation found in a row!", foundViolation); } if (hashKeySizeViolations != null) { for (Map<String, AttributeValue> item : hashKeySizeViolations) { AttributeValue tableHashAttr = item.get(tableHashKey); String expectedTableHashKey = AttributeValueConverter.toBlankString(tableHashAttr); if (hashToRangeHashSizeViolationsMap.containsKey(expectedTableHashKey)) { if (tableRangeKey != null) { AttributeValue tableRangeAttr = item.get(tableRangeKey); String expectedTableRangeKey = AttributeValueConverter.toBlankString(tableRangeAttr); Assert.assertEquals( "Size violated GSI hash key's table's hash key's range key does not match in the output!", expectedTableRangeKey, hashToRangeHashSizeViolationsMap.get(expectedTableHashKey)); } hashToRangeHashSizeViolationsMap.remove(expectedTableHashKey); } else { Assert.fail("Expected size violation on hash key not found!"); } // Check for gsi hash value if (violatedValueExists) { AttributeValue gsiHashAttr = item.get(gsiHashKeyName); String expectedGsiHashValue = AttributeValueConverter .toStringWithAttributeType(gsiHashAttr); Assert.assertEquals("Size violated Gsi hash value mis-match", expectedGsiHashValue, hashToGsiHashSizeViolationsMap.get(expectedTableHashKey)); hashToGsiHashSizeViolationsMap.remove(expectedTableHashKey); } } Assert.assertEquals("Extra entries found for gsi hash key size violations", 0, hashToRangeHashSizeViolationsMap.size()); Assert.assertEquals("Extra entries found for gsi hash key size violation values", 0, hashToGsiHashSizeViolationsMap.size()); } if (rangeKeySizeViolations != null) { for (Map<String, AttributeValue> item : rangeKeySizeViolations) { AttributeValue tableHashAttr = item.get(tableHashKey); String expectedTableHashKey = AttributeValueConverter.toBlankString(tableHashAttr); if (hashToRangeRangeSizeViolationsMap.containsKey(expectedTableHashKey)) { if (tableRangeKey != null) { AttributeValue tableRangeAttr = item.get(tableRangeKey); String expectedTableRangeKey = AttributeValueConverter.toBlankString(tableRangeAttr); Assert.assertEquals( "Size violated GSI range key's table's hash key's range key does not match in the output!", expectedTableRangeKey, hashToRangeRangeSizeViolationsMap.get(expectedTableHashKey)); } hashToRangeRangeSizeViolationsMap.remove(expectedTableHashKey); } else { Assert.fail("Expected size violation on range key not found!"); } // Check for gsi range value if (violatedValueExists) { AttributeValue gsiRangeAttr = item.get(gsiRangeKeyName); String expectedGsiRangeValue = AttributeValueConverter .toStringWithAttributeType(gsiRangeAttr); Assert.assertEquals("Size violated Gsi range value mis-match", expectedGsiRangeValue, hashToGsiRangeSizeViolationsMap.get(expectedTableHashKey)); hashToGsiRangeSizeViolationsMap.remove(expectedTableHashKey); } } Assert.assertEquals("Extra entries found for gsi range key size violations", 0, hashToRangeRangeSizeViolationsMap.size()); Assert.assertEquals("Extra entries found for gsi range key size violation values", 0, hashToGsiRangeSizeViolationsMap.size()); } if (hashKeyTypeViolations != null) { for (Map<String, AttributeValue> item : hashKeyTypeViolations) { AttributeValue tableHashAttr = item.get(tableHashKey); String expectedTableHashKey = AttributeValueConverter.toBlankString(tableHashAttr); if (hashToRangeHashTypeViolationsMap.containsKey(expectedTableHashKey)) { if (tableRangeKey != null) { AttributeValue tableRangeAttr = item.get(tableRangeKey); String exptectedTableRangeKey = AttributeValueConverter.toBlankString(tableRangeAttr); Assert.assertEquals( "Type violated GSI hash key's table's hash key's range key does not match in the output!", exptectedTableRangeKey, hashToRangeHashTypeViolationsMap.get(expectedTableHashKey)); } hashToRangeHashTypeViolationsMap.remove(expectedTableHashKey); } else { Assert.fail("Expected type violation on hash key not found!"); } // Check for gsi hash value if (violatedValueExists) { AttributeValue gsiHashAttr = item.get(gsiHashKeyName); String expectedGsiHashValue = AttributeValueConverter .toStringWithAttributeType(gsiHashAttr); Assert.assertEquals("Type violated Gsi hash value mis-match", expectedGsiHashValue, hashToGsiHashTypeViolationsMap.get(expectedTableHashKey)); hashToGsiHashTypeViolationsMap.remove(expectedTableHashKey); } } Assert.assertEquals("Extra entries found for gsi hash key type violations", 0, hashToRangeHashTypeViolationsMap.size()); Assert.assertEquals("Extra entries found for gsi hash key type violation values", 0, hashToGsiHashTypeViolationsMap.size()); } if (rangeKeyTypeViolations != null) { for (Map<String, AttributeValue> item : rangeKeyTypeViolations) { AttributeValue tableHashAttr = item.get(tableHashKey); String expectedTableHashKey = AttributeValueConverter.toBlankString(tableHashAttr); if (hashToRangeRangeTypeViolationsMap.containsKey(expectedTableHashKey)) { if (tableRangeKey != null) { AttributeValue tableRangeAttr = item.get(tableRangeKey); String exptectedTableRangeKey = AttributeValueConverter.toBlankString(tableRangeAttr); Assert.assertEquals( "Type violated GSI range key's table's hash key's range key does not match in the output!", exptectedTableRangeKey, hashToRangeRangeTypeViolationsMap.get(expectedTableHashKey)); } hashToRangeRangeTypeViolationsMap.remove(expectedTableHashKey); } else { Assert.fail("Expected type violation on range key not found!"); } // Check for gsi range value if (violatedValueExists) { AttributeValue gsiRangeAttr = item.get(gsiRangeKeyName); String expectedGsiRangeValue = AttributeValueConverter .toStringWithAttributeType(gsiRangeAttr); Assert.assertEquals("Type violated Gsi range value mis-match", expectedGsiRangeValue, hashToGsiRangeTypeViolationsMap.get(expectedTableHashKey)); hashToGsiRangeTypeViolationsMap.remove(expectedTableHashKey); } } Assert.assertEquals("Extra entries found for gsi range key type violations", 0, hashToRangeRangeTypeViolationsMap.size()); Assert.assertEquals("Extra entries found for gsi range key type violation values", 0, hashToGsiRangeTypeViolationsMap.size()); } } finally { br.close(); parser.close(); } }
From source file:canreg.client.gui.dataentry.ImportView.java
/** * *//*www . j a va2 s . c om*/ @Action public void previewAction() { // show the contents of the file BufferedReader br = null; try { changeFile(); // numberOfRecordsTextField.setText(""+(canreg.common.Tools.numberOfLinesInFile(inFile.getAbsolutePath())-1)); FileInputStream fis = new FileInputStream(inFile); br = new BufferedReader(new InputStreamReader(fis, (Charset) charsetsComboBox.getSelectedItem())); CSVFormat csvFormat = CSVFormat.DEFAULT.withFirstRecordAsHeader().withDelimiter(getSeparator()); CSVParser csvParser = new CSVParser(br, csvFormat); int linesToRead = Globals.NUMBER_OF_LINES_IN_IMPORT_PREVIEW; int numberOfLinesRead = 0; Vector<Vector<String>> data = new Vector<Vector<String>>(); String[] headers = csvParser.getHeaderMap().keySet().toArray(new String[0]); for (CSVRecord csvRecord : csvParser) { csvRecord.toMap(); Vector vec = new Vector(); Iterator<String> iterator = csvRecord.iterator(); while (iterator.hasNext()) { vec.add(iterator.next()); } data.add(vec); numberOfLinesRead++; if (numberOfLinesRead >= linesToRead) { break; } } numberOfRecordsShownTextField.setText(numberOfLinesRead + ""); // previewTextArea.setText(headers + "\n" + dataText); // previewTextArea.setCaretPosition(0); previewPanel.setVisible(true); Vector columnNames = new Vector(Arrays.asList(headers)); previewTable.setModel(new DefaultTableModel(data, columnNames)); } catch (FileNotFoundException fileNotFoundException) { JOptionPane.showInternalMessageDialog(CanRegClientApp.getApplication().getMainFrame().getContentPane(), java.util.ResourceBundle.getBundle("canreg/client/gui/dataentry/resources/ImportView") .getString("COULD_NOT_PREVIEW_FILE:") + " \'" + fileNameTextField.getText().trim() + "\'.", java.util.ResourceBundle.getBundle("canreg/client/gui/dataentry/resources/ImportView") .getString("ERROR"), JOptionPane.ERROR_MESSAGE); Logger.getLogger(ImportView.class.getName()).log(Level.SEVERE, null, fileNotFoundException); } catch (IOException ex) { JOptionPane.showInternalMessageDialog(CanRegClientApp.getApplication().getMainFrame().getContentPane(), java.util.ResourceBundle.getBundle("canreg/client/gui/dataentry/resources/ImportView") .getString("COULD_NOT_PREVIEW_FILE:") + " \'" + fileNameTextField.getText().trim() + "\'.", java.util.ResourceBundle.getBundle("canreg/client/gui/dataentry/resources/ImportView") .getString("ERROR"), JOptionPane.ERROR_MESSAGE); Logger.getLogger(ImportView.class.getName()).log(Level.SEVERE, null, ex); } finally { try { if (br != null) { br.close(); } } catch (IOException ex) { Logger.getLogger(ImportView.class.getName()).log(Level.SEVERE, null, ex); } } }
From source file:com.github.crab2died.ExcelUtils.java
private <T> List<T> readCSVByMapHandler(InputStream is, Class<T> clazz) throws IOException, Excel4JException { List<T> records = new ArrayList<>(); List<ExcelHeader> headers = Utils.getHeaderList(clazz); if (null == headers || headers.size() <= 0) { throw new Excel4jReadException("[" + clazz + "] must configuration @ExcelFiled"); }/*from ww w . j a va2 s. com*/ String[] csvHeaders = new String[headers.size()]; for (int i = 0; i < headers.size(); i++) { csvHeaders[i] = headers.get(i).getTitle(); } CSVFormat format = CSVFormat.EXCEL.withHeader(csvHeaders).withSkipHeaderRecord(true); try (Reader read = new InputStreamReader(is, StandardCharsets.UTF_8); CSVParser parser = new CSVParser(read, format)) { for (CSVRecord _parser : parser) { T obj; try { obj = clazz.newInstance(); } catch (InstantiationException | IllegalAccessException e) { throw new Excel4jReadException(e); } for (ExcelHeader header : headers) { String value = _parser.get(header.getTitle()); Object objectVal; String filed = header.getFiled(); // ?? if (null != header.getReadConverter() && header.getReadConverter().getClass() != DefaultConvertible.class) { objectVal = header.getReadConverter().execRead(value); } else { // ? objectVal = Utils.str2TargetClass(value, header.getFiledClazz()); } Utils.copyProperty(obj, filed, objectVal); } records.add(obj); } } return records; }
From source file:com.amazonaws.services.dynamodbv2.online.index.integration.tests.ViolationCorrectionTest.java
/** * Validates the output of violation correction. *//*from w w w. j av a2s. co m*/ private void validateCorrectionOutput(String correctionOutputFile, List<List<String>> errorRecords) throws IOException { BufferedReader br = null; CSVParser parser = null; try { br = new BufferedReader(new FileReader(new File(correctionOutputFile))); parser = new CSVParser(br, TestUtils.csvFormat); List<CSVRecord> records = parser.getRecords(); Assert.assertEquals("Error record count does not match", errorRecords.size(), records.size()); for (CSVRecord record : records) { boolean foundError = false; List<String> readRecord = new ArrayList<String>(); for (int i = 0; i < record.size(); i++) { if (record.get(i).equals(record.get(ViolationRecord.GSI_VALUE_UPDATE_ERROR))) { foundError = true; continue; } else { readRecord.add(record.get(i)); } } Assert.assertTrue("Error column not found", foundError); Assert.assertTrue("Unexpected record read from correction output", errorRecords.contains(readRecord)); errorRecords.remove(readRecord); } } finally { br.close(); parser.close(); } }
From source file:com.amazonaws.services.dynamodbv2.online.index.integration.tests.ViolationCorrectionTest.java
/** * Iterates through detection output file: first leave updates blank based on missing updates per key. * Once it has reached the missing update number, it removes the expected gsi values as per the specified 'missingGsiExpectedHashValues'. * Note that once blank number is reached, it also starts adding updates. * It then iterates over the rows again and adds values for Yes/No/Invalid in the delete column. * It returns all error records, if present. If not, it returns all records. *//*from w w w. ja v a 2s. c o m*/ private static List<List<String>> createCorrectionFile(final String detectionFile, final String correctionFile, final String gsiHashKeyName, final String gsiHashKeyType, final String gsiRangeKeyName, final String gsiRangeKeyType, final Map<String, String> tableHashToNewGsiHashValueMap, final Map<String, String> tableHashToNewGsiRangeValueMap, final int missingUpdatesPerKey, final int missingGsiExpectedHashValues, final int invalidValuesForDelete, final int numOfYesForDelete, final int numOfNoForDelete) throws IOException { List<List<String>> errorRecords = null; List<List<String>> allRecords = null; BufferedReader br = null; BufferedWriter bw = null; CSVParser parser = null; CSVPrinter csvPrinter = null; try { br = new BufferedReader(new FileReader(new File(detectionFile))); bw = new BufferedWriter(new FileWriter(new File(correctionFile))); parser = new CSVParser(br, TestUtils.csvFormat); csvPrinter = new CSVPrinter(bw, TestUtils.csvFormat); List<CSVRecord> detectorRecords = parser.getRecords(); int hashMissingUpdates = 0; int rangeMissingUpdates = 0; int missingGsiExpectedHashValuesCurrent = 0; // Print Header Map<String, Integer> header = parser.getHeaderMap(); csvPrinter.printRecord(header.keySet()); allRecords = new ArrayList<List<String>>(); for (CSVRecord csvRecord : detectorRecords) { List<String> newRecord = new ArrayList<String>(); String tableHashKeyRecorded = csvRecord.get(ViolationRecord.TABLE_HASH_KEY); String hashKeyViolationType = null; if (gsiHashKeyName != null) { hashKeyViolationType = csvRecord.get(ViolationRecord.GSI_HASH_KEY_VIOLATION_TYPE); } String rangeKeyViolationType = null; if (gsiRangeKeyName != null) { rangeKeyViolationType = csvRecord.get(ViolationRecord.GSI_RANGE_KEY_VIOLATION_TYPE); } for (int i = 0; i < csvRecord.size(); i++) { newRecord.add(i, csvRecord.get(i)); } String newGsiVal = null; if (hashKeyViolationType != null && (hashKeyViolationType.equals("Size Violation") || hashKeyViolationType.equals("Type Violation"))) { if (hashMissingUpdates < missingUpdatesPerKey) { allRecords.add(newRecord); hashMissingUpdates++; continue; } //Remove expected hash Values if (missingGsiExpectedHashValuesCurrent < missingGsiExpectedHashValues) { newRecord.remove((int) header.get(ViolationRecord.GSI_HASH_KEY)); newRecord.add(header.get(ViolationRecord.GSI_HASH_KEY), ""); missingGsiExpectedHashValuesCurrent++; } newRecord.remove((int) header.get(ViolationRecord.GSI_HASH_KEY_UPDATE_VALUE)); newGsiVal = getNewValue(gsiHashKeyType, 4 /*length*/); newRecord.add(header.get(ViolationRecord.GSI_HASH_KEY_UPDATE_VALUE), newGsiVal); tableHashToNewGsiHashValueMap.put(tableHashKeyRecorded, newGsiVal); } if (rangeKeyViolationType != null && (rangeKeyViolationType.equals("Size Violation") || rangeKeyViolationType.equals("Type Violation"))) { if (rangeMissingUpdates < missingUpdatesPerKey) { allRecords.add(newRecord); rangeMissingUpdates++; continue; } newRecord.remove(header.get(ViolationRecord.GSI_RANGE_KEY_UPDATE_VALUE)); newGsiVal = getNewValue(gsiRangeKeyType, 4 /*length*/); newRecord.add(header.get(ViolationRecord.GSI_RANGE_KEY_UPDATE_VALUE), newGsiVal); tableHashToNewGsiRangeValueMap.put(tableHashKeyRecorded, newGsiVal); } allRecords.add(newRecord); } // Add 'Y' or 'N' for delete column if (numOfNoForDelete > 0 || numOfYesForDelete > 0 || invalidValuesForDelete > 0) { errorRecords = new ArrayList<List<String>>(); int numOfYesAdded = 0; int numOfNoAdded = 0; int numOfInvalids = 0; for (List<String> record : allRecords) { if (numOfInvalids < invalidValuesForDelete) { record.remove(header.get(ViolationRecord.GSI_CORRECTION_DELETE_BLANK)); record.add(header.get(ViolationRecord.GSI_CORRECTION_DELETE_BLANK), "xx"); numOfInvalids++; errorRecords.add(record); continue; } if (numOfYesAdded < numOfYesForDelete) { record.remove(header.get(ViolationRecord.GSI_CORRECTION_DELETE_BLANK)); record.add(header.get(ViolationRecord.GSI_CORRECTION_DELETE_BLANK), "Y"); numOfYesAdded++; continue; } if (numOfNoAdded < numOfNoForDelete) { record.remove(header.get(ViolationRecord.GSI_CORRECTION_DELETE_BLANK)); record.add(header.get(ViolationRecord.GSI_CORRECTION_DELETE_BLANK), "N"); numOfNoAdded++; continue; } } } // Add all records to file csvPrinter.printRecords(allRecords); } finally { br.close(); bw.close(); parser.close(); csvPrinter.close(); } if (errorRecords != null) return errorRecords; else return allRecords; }