Search in sources :

Example 1 with VaultCSVEntry

use of de.opendiabetes.vault.container.csv.VaultCSVEntry 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

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