Search in sources :

Example 11 with CSVWriter

use of au.com.bytecode.opencsv.CSVWriter in project Android-IMSI-Catcher-Detector by CellularPrivacy.

the class RealmHelper method prepareOpenCellUploadData.

/**
     * Prepares the CSV file used to upload new data to the OCID server.
     * <p/>
     * OCID CSV upload format:
     * <p/>
     * "cellid"        = CID (in UMTS long format)
     * "measured_at"   = time
     * "rating"        = gpsd_accu
     * "act"           = RAT (TEXT):
     * 1xRTT, CDMA, eHRPD, IS95A, IS95B, EVDO_0, EVDO_A, EVDO_B,
     * UMTS, HSPA+, HSDPA, HSUPA, HSPA, LTE, EDGE, GPRS, GSM
     */
public boolean prepareOpenCellUploadData(Realm realm) {
    boolean result;
    File dir = new File(mExternalFilesDirPath + "OpenCellID/");
    if (!dir.exists()) {
        result = dir.mkdirs();
        if (!result) {
            return false;
        }
    }
    File file = new File(dir, "aimsicd-ocid-data.csv");
    try {
        // Get data not yet submitted:
        RealmResults<Measure> c;
        c = getOCIDSubmitData(realm);
        // Check if we have something to upload:
        if (c.size() > 0) {
            if (!file.exists()) {
                result = file.createNewFile();
                if (!result) {
                    return false;
                }
                // OCID CSV upload format and items
                // mcc,mnc,lac,cellid,lon,lat,signal,measured_at,rating,speed,direction,act,ta,psc,tac,pci,sid,nid,bid
                CSVWriter csvWrite = new CSVWriter(new FileWriter(file));
                // TODO: Add "act"
                csvWrite.writeNext("mcc,mnc,lac,cellid,lon,lat,signal,measured_at,rating");
                int size = c.size();
                log.debug("OCID UPLOAD: row count = " + size);
                for (Measure measure : c) {
                    csvWrite.writeNext(String.valueOf(measure.getBaseStation().getMobileCountryCode()), String.valueOf(measure.getBaseStation().getMobileNetworkCode()), String.valueOf(measure.getBaseStation().getLocationAreaCode()), String.valueOf(measure.getBaseStation().getCellId()), String.valueOf(measure.getGpsLocation().getLongitude()), String.valueOf(measure.getGpsLocation().getLatitude()), String.valueOf(measure.getRxSignal()), String.valueOf(measure.getTime().getTime()), String.valueOf(measure.getGpsLocation().getAccuracy()));
                }
                csvWrite.close();
            }
            return true;
        }
        return false;
    } catch (Exception e) {
        log.error("prepareOpenCellUploadData(): Error creating OpenCellID Upload Data: ", e);
        return false;
    }
}
Also used : FileWriter(java.io.FileWriter) Measure(com.secupwn.aimsicd.data.model.Measure) CSVWriter(au.com.bytecode.opencsv.CSVWriter) File(java.io.File)

Example 12 with CSVWriter

use of au.com.bytecode.opencsv.CSVWriter in project play-cookbook by spinscale.

the class CsvHelper method delete.

public <T extends CsvModel> void delete(T model) {
    if (model._key() == null) {
        return;
    }
    try {
        File out = File.createTempFile(model.getClass().getSimpleName(), null);
        CSVWriter writer = new CSVWriter(new FileWriter(out), separator);
        List<String[]> entries = getEntriesFromFile();
        for (String[] fields : entries) {
            if (!fields[0].equals(model._key().toString())) {
                writer.writeNext(fields);
            }
        }
        writer.close();
        moveDataFile(out);
    } catch (Exception e) {
        Logger.error(e, "Error in csv helper");
    }
}
Also used : FileWriter(java.io.FileWriter) CSVWriter(au.com.bytecode.opencsv.CSVWriter) File(java.io.File) IOException(java.io.IOException)

Example 13 with CSVWriter

use of au.com.bytecode.opencsv.CSVWriter in project play-cookbook by spinscale.

the class CsvHelper method save.

public synchronized <T extends CsvModel> T save(T model) {
    try {
        File out = File.createTempFile(model.getClass().getSimpleName(), null);
        CSVWriter writer = new CSVWriter(new FileWriter(out), separator);
        if (dataFile.exists()) {
            List<String[]> myEntries = getEntriesFromFile();
            for (String[] fields : myEntries) {
                // update it
                if (fields[0].equals(model._key())) {
                    String[] arrayObject = createArrayFromObject(model, model._key().toString());
                    if (arrayObject != null) {
                        writer.writeNext(arrayObject);
                    }
                } else {
                    writer.writeNext(fields);
                }
            }
        }
        // insert it if it was not updated...
        if (model._key() == null) {
            Long id = getNewId();
            String[] arrayObject = createArrayFromObject(model, id.toString());
            writer.writeNext(arrayObject);
            model.id = id;
        }
        writer.close();
        moveDataFile(out);
    } catch (Exception e) {
        Logger.error(e, "Error in csv helper");
    }
    return model;
}
Also used : FileWriter(java.io.FileWriter) AtomicLong(java.util.concurrent.atomic.AtomicLong) CSVWriter(au.com.bytecode.opencsv.CSVWriter) File(java.io.File) IOException(java.io.IOException)

Example 14 with CSVWriter

use of au.com.bytecode.opencsv.CSVWriter in project dbeaver by serge-rider.

the class PostgreUtils method generateObjectString.

public static String generateObjectString(Object[] values) throws DBCException {
    String[] line = new String[values.length];
    for (int i = 0; i < values.length; i++) {
        final Object value = values[i];
        line[i] = value == null ? "NULL" : value.toString();
    }
    StringWriter out = new StringWriter();
    final CSVWriter writer = new CSVWriter(out);
    writer.writeNext(line);
    try {
        writer.flush();
    } catch (IOException e) {
        log.warn(e);
    }
    return "(" + out.toString().trim() + ")";
}
Also used : StringWriter(java.io.StringWriter) CSVWriter(au.com.bytecode.opencsv.CSVWriter) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBSTypedObject(org.jkiss.dbeaver.model.struct.DBSTypedObject) IOException(java.io.IOException)

Example 15 with CSVWriter

use of au.com.bytecode.opencsv.CSVWriter in project ORCID-Source by ORCID.

the class OrgManagerImpl method writeAmbiguousOrgs.

@Override
public void writeAmbiguousOrgs(Writer writer) {
    @SuppressWarnings("resource") CSVWriter csvWriter = new CSVWriter(writer);
    csvWriter.writeNext(AMBIGUOUS_ORGS_HEADER);
    int firstResult = 0;
    List<AmbiguousOrgEntity> chunk = null;
    do {
        chunk = getAmbiguousOrgs(firstResult, CHUNK_SIZE);
        for (AmbiguousOrgEntity orgEntity : chunk) {
            String[] line = new String[] { String.valueOf(orgEntity.getId()), orgEntity.getSourceOrcid(), orgEntity.getName(), orgEntity.getCity(), orgEntity.getRegion(), orgEntity.getCountry().value(), String.valueOf(orgEntity.getUsedCount()) };
            csvWriter.writeNext(line);
        }
        firstResult += chunk.size();
    } while (!chunk.isEmpty());
}
Also used : CSVWriter(au.com.bytecode.opencsv.CSVWriter) AmbiguousOrgEntity(org.orcid.persistence.jpa.entities.AmbiguousOrgEntity)

Aggregations

CSVWriter (au.com.bytecode.opencsv.CSVWriter)23 IOException (java.io.IOException)9 FileWriter (java.io.FileWriter)7 PrintWriter (java.io.PrintWriter)5 ArrayList (java.util.ArrayList)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 OutputStreamWriter (java.io.OutputStreamWriter)4 File (java.io.File)3 StringWriter (java.io.StringWriter)2 List (java.util.List)2 Lists (com.google.common.collect.Lists)1 Measure (com.secupwn.aimsicd.data.model.Measure)1 DataflowException (edu.uci.ics.texera.api.exception.DataflowException)1 Tuple (edu.uci.ics.texera.api.tuple.Tuple)1 UncheckedIOException (java.io.UncheckedIOException)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 Collectors (java.util.stream.Collectors)1 TableModel (javax.swing.table.TableModel)1 ObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector)1 StructObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector)1