Search in sources :

Example 1 with CsvValidationException

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);
    }
}
Also used : Path(java.nio.file.Path) Entry(org.jbei.ice.storage.model.Entry) HasEntry(org.jbei.ice.lib.entry.HasEntry) CsvValidationException(com.opencsv.exceptions.CsvValidationException) CSVWriter(com.opencsv.CSVWriter) EntryDAO(org.jbei.ice.storage.hibernate.dao.EntryDAO)

Example 2 with CsvValidationException

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();
    }
}
Also used : ItemsetDbAdapter(org.odk.collect.android.fastexternalitemset.ItemsetDbAdapter) CsvValidationException(com.opencsv.exceptions.CsvValidationException) CSVReader(com.opencsv.CSVReader) SQLException(android.database.SQLException) FileReader(java.io.FileReader) LocalizedApplicationKt.getLocalizedString(org.odk.collect.strings.localization.LocalizedApplicationKt.getLocalizedString) IOException(java.io.IOException)

Aggregations

CsvValidationException (com.opencsv.exceptions.CsvValidationException)2 SQLException (android.database.SQLException)1 CSVReader (com.opencsv.CSVReader)1 CSVWriter (com.opencsv.CSVWriter)1 FileReader (java.io.FileReader)1 IOException (java.io.IOException)1 Path (java.nio.file.Path)1 HasEntry (org.jbei.ice.lib.entry.HasEntry)1 EntryDAO (org.jbei.ice.storage.hibernate.dao.EntryDAO)1 Entry (org.jbei.ice.storage.model.Entry)1 ItemsetDbAdapter (org.odk.collect.android.fastexternalitemset.ItemsetDbAdapter)1 LocalizedApplicationKt.getLocalizedString (org.odk.collect.strings.localization.LocalizedApplicationKt.getLocalizedString)1