use of com.opencsv.exceptions.CsvValidationException in project ice by JBEI.
the class SampleCSV method generate.
public String generate() throws IOException {
try {
String[] header = reader.readNext();
int headerLength = header.length;
EntryDAO dao = DAOFactory.getEntryDAO();
Path path = Paths.get(Utils.getConfigValue(ConfigurationKey.TEMPORARY_DIRECTORY), UUID.randomUUID().toString());
FileWriter fileWriter = new FileWriter(path.toFile());
BufferedWriter writer = new BufferedWriter(fileWriter);
CSVWriter csvWriter = new CSVWriter(writer);
csvWriter.writeNext(new String[] { "Well", "PartID (Strain)" });
for (String[] line : reader) {
if (line.length != headerLength) {
Logger.info("Skipping line");
continue;
}
String alias = line[0];
String name = line[1];
List<Entry> entries = dao.getMatching(name, alias, EntryType.PLASMID);
if (entries.size() != 1) {
Logger.error(entries.size() + " found for " + name + " " + alias);
continue;
}
Entry plasmid = entries.get(0);
List<Entry> parents = dao.getParents(plasmid.getId());
if (parents.size() != 1) {
Logger.error("Found " + parents.size() + " for " + name + " " + alias);
continue;
}
Entry strain = parents.get(0);
String locationLine = line[headerLength - 1];
locationLine = locationLine.substring(locationLine.lastIndexOf('-') + 1);
if (locationLine.length() == 2)
locationLine = locationLine.charAt(0) + "0" + locationLine.charAt(1);
csvWriter.writeNext(new String[] { locationLine, strain.getPartNumber() });
}
csvWriter.close();
return path.getFileName().toString();
} catch (CsvValidationException e) {
throw new IOException(e);
}
}
use of com.opencsv.exceptions.CsvValidationException in project collect by opendatakit.
the class FormLoaderTask method readCSV.
private void readCSV(File csv, String formHash, String pathHash) {
CSVReader reader;
ItemsetDbAdapter ida = new ItemsetDbAdapter();
ida.open();
boolean withinTransaction = false;
try {
reader = new CSVReader(new FileReader(csv));
String[] nextLine;
String[] columnHeaders = null;
int lineNumber = 0;
while ((nextLine = reader.readNext()) != null) {
lineNumber++;
if (lineNumber == 1) {
// first line of csv is column headers
columnHeaders = nextLine;
ida.createTable(formHash, pathHash, columnHeaders, csv.getAbsolutePath());
continue;
}
// System.out.println(nextLine[4] + "etc...");
if (lineNumber == 2) {
// start a transaction for the inserts
withinTransaction = true;
ida.beginTransaction();
}
ida.addRow(pathHash, columnHeaders, nextLine);
}
} catch (IOException | SQLException | CsvValidationException e) {
warningMsg = e.getMessage();
} finally {
if (withinTransaction) {
ida.commit();
}
ida.close();
}
}
Aggregations