Search in sources :

Example 1 with ExportEntry

use of de.opendiabetes.vault.container.csv.ExportEntry in project BachelorPraktikum by lucasbuschlinger.

the class FileExporter method writeToFile.

/**
 * Writes the export data to the file.
 *
 * @param filePath File path where the exported data should be written to.
 * @param data The data to be written.
 * @throws IOException Thrown if something goes wrong when writing the file.
 */
protected void writeToFile(final String filePath, final List<T> data) throws IOException {
    FileChannel channel = fileOutputStream.getChannel();
    byte[] lineFeed = "\n".getBytes(Charset.forName("UTF-8"));
    for (Object entry : data) {
        byte[] messageBytes = ((ExportEntry) entry).toByteEntryLine();
        channel.write(ByteBuffer.wrap(messageBytes));
        channel.write(ByteBuffer.wrap(lineFeed));
    }
    channel.close();
}
Also used : ExportEntry(de.opendiabetes.vault.container.csv.ExportEntry) FileChannel(java.nio.channels.FileChannel)

Example 2 with ExportEntry

use of de.opendiabetes.vault.container.csv.ExportEntry in project BachelorPraktikum by lucasbuschlinger.

the class VaultExporter method prepareData.

/**
 * {@inheritDoc}
 */
@Override
protected List<ExportEntry> prepareData(final List<VaultEntry> data) throws IllegalArgumentException {
    // Status update constants
    final int startPrepareProgress = 33;
    final int prepareDoneProgress = 66;
    if (data == null || data.isEmpty()) {
        LOG.log(Level.SEVERE, "Data cannot be empty");
        throw new IllegalArgumentException("Data cannot be empty");
    }
    List<ExportEntry> returnValues = new ArrayList<>();
    List<VaultEntry> tmpData;
    if (getIsPeriodRestricted()) {
        tmpData = filterPeriodRestriction(data);
    } else {
        tmpData = data;
    }
    // list is ordered by timestamp from database (or should be ordered otherwise)
    Date fromTimestamp = tmpData.get(0).getTimestamp();
    Date toTimestamp = tmpData.get(tmpData.size() - 1).getTimestamp();
    this.notifyStatus(startPrepareProgress, "Preparing data for export");
    if (!tmpData.isEmpty()) {
        int i = 0;
        delayBuffer = new ArrayList<>();
        while (!fromTimestamp.after(toTimestamp)) {
            // start new time slot (1m slots)
            VaultCSVEntry tmpCsvEntry = new VaultCSVEntry();
            tmpCsvEntry.setTimestamp(fromTimestamp);
            // add delayed items
            if (!delayBuffer.isEmpty()) {
                // need to copy the buffer since the loop may create new entries
                VaultEntry[] delayedEntries = delayBuffer.toArray(new VaultEntry[] {});
                delayBuffer.clear();
                for (VaultEntry delayedItem : delayedEntries) {
                    tmpCsvEntry = processVaultEntry(tmpCsvEntry, delayedItem);
                }
            }
            // search and add vault entries for this time slot
            VaultEntry tmpEntry = tmpData.get(i);
            while (fromTimestamp.equals(tmpEntry.getTimestamp())) {
                if (i < tmpData.size() - 1) {
                    i++;
                } else {
                    i--;
                    break;
                }
                tmpCsvEntry = processVaultEntry(tmpCsvEntry, tmpEntry);
                tmpEntry = tmpData.get(i);
            }
            // save entry if not empty
            if (!tmpCsvEntry.isEmpty()) {
                returnValues.add(tmpCsvEntry);
                LOG.log(Level.FINE, "Export entry: {0}", tmpCsvEntry.toCSVString());
            }
            // add 1 minute to timestamp for next time slot
            fromTimestamp = TimestampUtils.addMinutesToTimestamp(fromTimestamp, 1);
        }
    }
    this.notifyStatus(prepareDoneProgress, "Preparation of data successful");
    return returnValues;
}
Also used : ExportEntry(de.opendiabetes.vault.container.csv.ExportEntry) VaultCSVEntry(de.opendiabetes.vault.container.csv.VaultCSVEntry) VaultEntry(de.opendiabetes.vault.container.VaultEntry) ArrayList(java.util.ArrayList) Date(java.util.Date)

Aggregations

ExportEntry (de.opendiabetes.vault.container.csv.ExportEntry)2 VaultEntry (de.opendiabetes.vault.container.VaultEntry)1 VaultCSVEntry (de.opendiabetes.vault.container.csv.VaultCSVEntry)1 FileChannel (java.nio.channels.FileChannel)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1