Search in sources :

Example 1 with CSVEntryMap

use of com.dexels.navajo.adapter.csvmap.CSVEntryMap in project navajo by Dexels.

the class CSVMap method getEntryFieldCount.

public int getEntryFieldCount() {
    int result = -1;
    if (entries != null && entries.length > 0) {
        CSVEntryMap e = entries[0];
        result = e.getEntrySize();
    }
    return result;
}
Also used : CSVEntryMap(com.dexels.navajo.adapter.csvmap.CSVEntryMap)

Example 2 with CSVEntryMap

use of com.dexels.navajo.adapter.csvmap.CSVEntryMap in project navajo by Dexels.

the class CSVMap method store.

@Override
public void store() throws MappableException, UserException {
    if (draftEntries != null) {
        entries = new CSVEntryMap[draftEntries.size()];
        int i = 0;
        for (CSVEntryMap ce : draftEntries) {
            entries[i++] = ce;
        }
    }
    if (update && entries != null) {
        try (FileWriter writer = new FileWriter(fileName)) {
            // Write CSV.
            for (int i = 0; i < entries.length; i++) {
                CSVEntryMap e = entries[i];
                for (int j = 0; j < e.entries.length; j++) {
                    writer.write(e.getEntry(Integer.valueOf(j)));
                    if (j < (e.entries.length - 1))
                        writer.write(separator);
                }
                if (i < (entries.length - 1))
                    writer.write("\n");
            }
            writer.close();
        } catch (IOException ioe) {
            throw new UserException(-1, ioe.getMessage());
        }
    }
}
Also used : CSVEntryMap(com.dexels.navajo.adapter.csvmap.CSVEntryMap) FileWriter(java.io.FileWriter) IOException(java.io.IOException) UserException(com.dexels.navajo.script.api.UserException)

Example 3 with CSVEntryMap

use of com.dexels.navajo.adapter.csvmap.CSVEntryMap in project navajo by Dexels.

the class CSVMap method main.

public static void main(String[] args) throws Exception {
    Mappable csv = new CSVMap();
    ((CSVMap) csv).setSeparator(";");
    ((CSVMap) csv).setIncludeEmpty(true);
    ((CSVMap) csv).setSkipFirstRow(false);
    Binary b = new Binary(new File("C:/Temp/LedenLIJST-vertrouwelijktest.csv"));
    ((CSVMap) csv).setFileContent(b);
    Mappable[] all = ((CSVMap) csv).getEntries();
    for (int i = 0; i < all.length; i++) {
        CSVEntryMap entryMap = ((CSVEntryMap) all[i]);
        logger.info("a = >" + entryMap.getEntry(Integer.valueOf(0)) + "< - >" + entryMap.getEntry(Integer.valueOf(1)) + "< - >" + entryMap.getEntry(Integer.valueOf(2)) + "< - >" + entryMap.getEntry(Integer.valueOf(3)) + "< - >" + entryMap.getEntry(Integer.valueOf(4)) + "< - >" + entryMap.getEntry(Integer.valueOf(5)) + "< - >" + entryMap.getEntry(Integer.valueOf(6)) + "< - >" + entryMap.getEntry(Integer.valueOf(7)) + "< - >" + entryMap.getEntry(Integer.valueOf(8)) + "< - >" + entryMap.getEntry(Integer.valueOf(9)) + "< - >" + entryMap.getEntry(Integer.valueOf(10)) + "< - >" + entryMap.getEntry(Integer.valueOf(11)) + "<");
    }
}
Also used : Mappable(com.dexels.navajo.script.api.Mappable) CSVEntryMap(com.dexels.navajo.adapter.csvmap.CSVEntryMap) Binary(com.dexels.navajo.document.types.Binary) File(java.io.File)

Example 4 with CSVEntryMap

use of com.dexels.navajo.adapter.csvmap.CSVEntryMap in project navajo by Dexels.

the class CSVMap method parseLineDefault.

private void parseLineDefault(String line, List<CSVEntryMap> entryList) {
    StringTokenizer tokens = new StringTokenizer(line, separator);
    CSVEntryMap csvEntry = new CSVEntryMap();
    csvEntry.entries = new String[tokens.countTokens()];
    int index = 0;
    while (tokens.hasMoreTokens()) {
        csvEntry.entries[index++] = tokens.nextToken();
    }
    entryList.add(csvEntry);
}
Also used : StringTokenizer(java.util.StringTokenizer) CSVEntryMap(com.dexels.navajo.adapter.csvmap.CSVEntryMap)

Example 5 with CSVEntryMap

use of com.dexels.navajo.adapter.csvmap.CSVEntryMap in project navajo by Dexels.

the class CSVMap method getEntries.

public CSVEntryMap[] getEntries() throws UserException {
    BufferedReader buffer = null;
    try {
        Reader f = null;
        if (fileContent != null) {
            f = new InputStreamReader(fileContent.getDataAsStream(), StandardCharsets.UTF_8);
        } else {
            f = new FileReader(fileName);
        }
        buffer = new BufferedReader(f);
        String line = "";
        boolean firstLine = true;
        int importCount = 0;
        List<CSVEntryMap> entryList = new ArrayList<>();
        while ((line = buffer.readLine()) != null) {
            if (maximumImportCount != 0 && (importCount >= maximumImportCount)) {
                break;
            }
            if (isSkipFirstRow() && firstLine) {
            // First line will be skipped. Probably contains headers
            } else {
                if (includeEmpty) {
                    parseLineWithEmpty(line, entryList);
                } else {
                    parseLineDefault(line, entryList);
                }
                importCount++;
            }
            firstLine = false;
        }
        entries = new CSVEntryMap[entryList.size()];
        int i = 0;
        for (CSVEntryMap ce : entryList) {
            entries[i++] = ce;
        }
    } catch (java.io.IOException ioe) {
        throw new UserException(-1, ioe.getMessage());
    } finally {
        if (buffer != null) {
            try {
                buffer.close();
            } catch (IOException e) {
            // Too late to apologize!
            }
        }
    }
    return entries;
}
Also used : InputStreamReader(java.io.InputStreamReader) CSVEntryMap(com.dexels.navajo.adapter.csvmap.CSVEntryMap) ArrayList(java.util.ArrayList) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) IOException(java.io.IOException) IOException(java.io.IOException) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) UserException(com.dexels.navajo.script.api.UserException)

Aggregations

CSVEntryMap (com.dexels.navajo.adapter.csvmap.CSVEntryMap)6 UserException (com.dexels.navajo.script.api.UserException)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 Binary (com.dexels.navajo.document.types.Binary)1 Mappable (com.dexels.navajo.script.api.Mappable)1 BufferedReader (java.io.BufferedReader)1 File (java.io.File)1 FileReader (java.io.FileReader)1 FileWriter (java.io.FileWriter)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 StringTokenizer (java.util.StringTokenizer)1