Search in sources :

Example 1 with VaultEntry

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

the class FileExporter method filterPeriodRestriction.

/**
 * This filters the data if the options indicate a period restriction on the data.
 *
 * @param data The data to filter.
 * @return The filtered data.
 */
protected List<VaultEntry> filterPeriodRestriction(final List<VaultEntry> data) {
    List<VaultEntry> tempData = new ArrayList<>();
    Date begin = getExportPeriodFrom();
    Date end = getExportPeriodTo();
    for (VaultEntry entry : data) {
        Date timestamp = entry.getTimestamp();
        if (timestamp.before(begin) || timestamp.after(end)) {
            continue;
        }
        tempData.add(entry);
    }
    return tempData;
}
Also used : VaultEntry(de.opendiabetes.vault.container.VaultEntry) ArrayList(java.util.ArrayList) Date(java.util.Date)

Example 2 with VaultEntry

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

the class AbstractFileImporter method importData.

/**
 * Imports the data from a specified file path.
 *
 * @param filePath File path from which the data should be imported to
 * @return List of VaultEntry consisting of the imported data.
 * @throws Exception Thrown if there was an error reading the file
 */
public List<VaultEntry> importData(final String filePath) throws Exception {
    if (filePath == null) {
        LOG.log(Level.SEVERE, "File path cannot be empty.");
        throw new IllegalArgumentException("File path cannot be empty.");
    }
    File file = new File(filePath);
    if (!file.exists()) {
        LOG.log(Level.SEVERE, "File at given path does not exist.");
        throw new FileNotFoundException("File at given path does not exist.");
    }
    preprocessingIfNeeded(filePath);
    this.notifyStatus(0, "Preprocessing done.");
    FileInputStream inputStream = null;
    List<VaultEntry> result = null;
    try {
        inputStream = new FileInputStream(filePath);
        result = processImport(inputStream, filePath);
    } finally {
        if (inputStream != null) {
            inputStream.close();
        }
    }
    return result;
}
Also used : VaultEntry(de.opendiabetes.vault.container.VaultEntry) FileNotFoundException(java.io.FileNotFoundException) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 3 with VaultEntry

use of de.opendiabetes.vault.container.VaultEntry 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)

Example 4 with VaultEntry

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

the class CSVImporter method processImport.

/**
 * {@inheritDoc}
 */
public List<VaultEntry> processImport(final InputStream fileInputStream, final String filenameForLogging) throws Exception {
    List<VaultEntry> importedData = new ArrayList<>();
    final int maxProgress = 100;
    // This list is used as a placeholder for future extensions
    List<String[]> metaEntries = new ArrayList<>();
    this.notifyStatus(0, "Reading Header");
    CsvReader creader = null;
    if (getDelimiter() == AUTO_DELIMITER) {
        // try to detect the delimiter by trial and error
        LOG.log(Level.INFO, "using automatic delimiter detection");
        char[] delimiterList = { ',', ';', '\t' };
        for (char delimiter : delimiterList) {
            creader = getValidatedCreader(delimiter, filenameForLogging, metaEntries);
            if (null != creader) {
                setDelimiter(delimiter);
                break;
            }
        }
    } else {
        // use the delimiter that was set
        creader = getValidatedCreader(getDelimiter(), filenameForLogging, metaEntries);
    }
    if (creader == null) {
        // header could not be validated
        LOG.log(Level.SEVERE, "No valid header found in File: " + filenameForLogging);
        throw new Exception("No valid header found in File: " + filenameForLogging);
    }
    // read entries
    while (creader.readRecord()) {
        /*here the method template is used to process all records */
        List<VaultEntry> entryList = parseEntry(creader);
        if (entryList != null && !entryList.isEmpty()) {
            importedData.addAll(entryList);
        }
    }
    this.notifyStatus(maxProgress, "Done importing all entries");
    return importedData;
}
Also used : CsvReader(com.csvreader.CsvReader) VaultEntry(de.opendiabetes.vault.container.VaultEntry) ArrayList(java.util.ArrayList) IOException(java.io.IOException)

Example 5 with VaultEntry

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

the class VaultDAO method queryVaultEntriesBetween.

// /**
// * This is used to retrieve an entry from the database by its identifier.
// *
// * @param id The ID of the {@link VaultEntry} to be retrieved.
// * @return The {@link VaultEntry} with respective ID or null.
// */
// public VaultEntry queryVaultEntryById(final long id) {
// List<VaultEntry> returnValues;
// try {
// PreparedQuery<VaultEntry> query
// = vaultDao.queryBuilder().orderBy("timestamp", true)
// .limit(1L)
// .where()
// .eq(VaultEntry.ID_FIELD_NAME, id)
// .prepare();
// returnValues = vaultDao.query(query);
// } catch (SQLException exception) {
// LOG.log(Level.SEVERE, "Error while db query", exception);
// return null;
// }
// return returnValues.get(0);
// }
/**
 * This is used to retrieve all {@link VaultEntry}s from the database which lie in the specified period of time, no matter their type.
 *
 * @param from The start of the time period to query entries from.
 * @param to The end of the time period to query entries from.
 * @return List of all {@link VaultEntry}s which lie in the specified time period.
 */
public List<VaultEntry> queryVaultEntriesBetween(final Date from, final Date to) {
    List<VaultEntry> returnValues = new ArrayList<>();
    try {
        Date fromTimestamp = TimestampUtils.createCleanTimestamp(from);
        Date toTimestamp = TimestampUtils.createCleanTimestamp(to);
        PreparedQuery<VaultEntry> query = vaultDao.queryBuilder().orderBy("timestamp", true).where().between(VaultEntry.TIMESTAMP_FIELD_NAME, fromTimestamp, toTimestamp).prepare();
        returnValues = vaultDao.query(query);
    } catch (SQLException exception) {
        LOG.log(Level.SEVERE, "Error while db query", exception);
    }
    return returnValues;
}
Also used : SQLException(java.sql.SQLException) VaultEntry(de.opendiabetes.vault.container.VaultEntry) ArrayList(java.util.ArrayList) Date(java.util.Date)

Aggregations

VaultEntry (de.opendiabetes.vault.container.VaultEntry)8 ArrayList (java.util.ArrayList)6 Date (java.util.Date)5 SQLException (java.sql.SQLException)3 CsvReader (com.csvreader.CsvReader)1 ExportEntry (de.opendiabetes.vault.container.csv.ExportEntry)1 VaultCSVEntry (de.opendiabetes.vault.container.csv.VaultCSVEntry)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1