Search in sources :

Example 16 with CSVWriter

use of com.opencsv.CSVWriter in project CzechIdMng by bcvsolutions.

the class AbstractCsvRenderer method createCSVWriter.

protected CSVWriter createCSVWriter(File file) throws IOException {
    // BOM encoding to csv file
    FileOutputStream os = new FileOutputStream(file);
    os.write(0xef);
    os.write(0xbb);
    os.write(0xbf);
    // 
    return new CSVWriter(new OutputStreamWriter(os, AttachableEntity.DEFAULT_CHARSET));
}
Also used : FileOutputStream(java.io.FileOutputStream) CSVWriter(com.opencsv.CSVWriter) OutputStreamWriter(java.io.OutputStreamWriter)

Example 17 with CSVWriter

use of com.opencsv.CSVWriter in project tutorials by eugenp.

the class FileUtils method initWriter.

private void initWriter() throws Exception {
    if (file == null) {
        file = new File(fileName);
        file.createNewFile();
    }
    if (fileWriter == null)
        fileWriter = new FileWriter(file, true);
    if (CSVWriter == null)
        CSVWriter = new CSVWriter(fileWriter);
}
Also used : FileWriter(java.io.FileWriter) CSVWriter(com.opencsv.CSVWriter) File(java.io.File)

Example 18 with CSVWriter

use of com.opencsv.CSVWriter in project ice by JBEI.

the class RequestRetriever method generateCSVFile.

public ByteArrayOutputStream generateCSVFile(String userId, ArrayList<Long> ids) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    OutputStreamWriter streamWriter = new OutputStreamWriter(out);
    try (CSVWriter writer = new CSVWriter(streamWriter)) {
        SampleService sampleService = new SampleService();
        Set<Long> idSet = new HashSet<>(ids);
        for (long id : idSet) {
            Request request = dao.get(id);
            if (request == null)
                continue;
            String[] line = new String[3];
            Entry entry = request.getEntry();
            line[0] = entry.getName();
            List<PartSample> samples = sampleService.retrieveEntrySamples(userId, Long.toString(request.getEntry().getId()));
            if (samples.isEmpty()) {
                Logger.info("No samples found for " + line[0]);
                continue;
            }
            String plate = null;
            String well = null;
            for (PartSample sample : samples) {
                StorageLocation location = sample.getLocation();
                if (location == null)
                    continue;
                if (location.getType() == SampleType.GENERIC) {
                    plate = "generic";
                    well = "";
                    break;
                } else if (location.getType() == SampleType.PLATE96) {
                    if (sample.getLabel().contains("backup"))
                        continue;
                    plate = location.getDisplay().replaceFirst("^0+(?!$)", "");
                    StorageLocation child = location.getChild();
                    while (child != null) {
                        if (child.getType() == SampleType.WELL) {
                            well = child.getDisplay();
                            break;
                        }
                        child = child.getChild();
                    }
                    if (!StringUtils.isEmpty(well) && !StringUtils.isEmpty(plate))
                        break;
                }
            }
            if (plate == null || well == null)
                continue;
            String email = request.getAccount().getEmail();
            int index = email.indexOf('@');
            char typeChar = request.getType() == SampleRequestType.LIQUID_CULTURE ? 'L' : 'A';
            line[1] = typeChar + " " + plate + " " + well + " " + email.substring(0, index);
            line[1] = line[1].trim().replaceAll(" +", " ");
            line[2] = request.getPlateDescription().trim().replaceAll(" +", " ");
            if (request.getGrowthTemperature() != null)
                line[2] += " " + request.getGrowthTemperature();
            writer.writeNext(line);
        }
    }
    return out;
}
Also used : Request(org.jbei.ice.storage.model.Request) CSVWriter(com.opencsv.CSVWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Entry(org.jbei.ice.storage.model.Entry) OutputStreamWriter(java.io.OutputStreamWriter) StorageLocation(org.jbei.ice.lib.dto.StorageLocation)

Example 19 with CSVWriter

use of com.opencsv.CSVWriter in project ice by JBEI.

the class RemoteEntriesAsCSV method writeList.

private boolean writeList(List<RemotePartner> partners) throws IOException {
    Path tmpPath = Paths.get(Utils.getConfigValue(ConfigurationKey.TEMPORARY_DIRECTORY));
    File tmpFile = File.createTempFile("remote-ice-", ".csv", tmpPath.toFile());
    csvPath = tmpFile.toPath();
    FileWriter fileWriter = new FileWriter(tmpFile);
    List<EntryFieldLabel> fields = getEntryFields();
    String[] headers = getCSVHeaders(fields);
    // csv file headers
    File tmpZip = File.createTempFile("zip-", ".zip", tmpPath.toFile());
    FileOutputStream fos = new FileOutputStream(tmpZip);
    try (CSVWriter writer = new CSVWriter(fileWriter);
        ZipOutputStream zos = new ZipOutputStream(fos)) {
        writer.writeNext(headers);
        // go through partners
        for (RemotePartner partner : partners) {
            try {
                Logger.info("Retrieving from " + partner.getUrl());
                PartnerEntries partnerEntries = remoteEntries.getPublicEntries(partner.getId(), 0, Integer.MAX_VALUE, null, true);
                Results<PartData> webEntries = partnerEntries.getEntries();
                if (webEntries == null || webEntries.getData() == null) {
                    Logger.error("Could not retrieve entries for " + partner.getUrl());
                    continue;
                }
                Logger.info("Obtained " + webEntries.getResultCount() + " from " + partner.getUrl());
                // go through entries for each partner and write to the zip file
                writeDataEntries(partner, webEntries.getData(), fields, writer, zos);
            } catch (Exception e) {
                Logger.warn("Exception retrieving entries " + e.getMessage());
            }
        }
        // write local entries
        if (this.includeLocal) {
            Logger.info("Retrieving local public entries");
            Group publicGroup = new GroupController().createOrRetrievePublicGroup();
            Set<Group> groups = new HashSet<>();
            groups.add(publicGroup);
            EntryDAO entryDAO = DAOFactory.getEntryDAO();
            List<Long> results = entryDAO.retrieveVisibleEntries(null, groups, ColumnField.CREATED, true, 0, Integer.MAX_VALUE, null);
            writeLocalEntries(results, fields, writer, zos);
        }
        // write the csv file to the zip
        writeZip(tmpZip, zos);
    }
    return true;
}
Also used : Path(java.nio.file.Path) GroupController(org.jbei.ice.lib.group.GroupController) PartnerEntries(org.jbei.ice.lib.dto.web.PartnerEntries) CSVWriter(com.opencsv.CSVWriter) EntryFieldLabel(org.jbei.ice.lib.dto.entry.EntryFieldLabel) EntryDAO(org.jbei.ice.storage.hibernate.dao.EntryDAO) ZipOutputStream(java.util.zip.ZipOutputStream) PartData(org.jbei.ice.lib.dto.entry.PartData) HashSet(java.util.HashSet)

Example 20 with CSVWriter

use of com.opencsv.CSVWriter in project NightSkyGuide by MTBehnke.

the class ObservationsFragment method exportObsCSV.

// export observations to CSV file
public void exportObsCSV() {
    // first check permissions and if not enabled exit
    String state = Environment.getExternalStorageState();
    if (!Environment.MEDIA_MOUNTED.equals(state)) {
        Toast.makeText(getActivity(), "External storage unavailable", Toast.LENGTH_LONG).show();
        return;
    }
    String exportDir = android.os.Environment.getExternalStorageDirectory().getAbsolutePath();
    String fileName = "ObservationLog.csv";
    String filePath = exportDir + File.separator + fileName;
    File existCheck = new File(filePath);
    try {
        if (existCheck.exists()) {
            int version = 0;
            do {
                version++;
                fileName = "ObservationLog (" + version + ").csv";
                filePath = exportDir + File.separator + fileName;
                existCheck = new File(filePath);
            } while (existCheck.exists());
        }
        FileOutputStream file = new FileOutputStream(filePath);
        file.write(0xef);
        file.write(0xbb);
        file.write(0xbf);
        CSVWriter csvWrite = new CSVWriter(new OutputStreamWriter(file));
        String[] arrHeaderStr = { getString(R.string.hint_objectID), getString(R.string.hint_date), getString(R.string.hint_location), getString(R.string.hint_seeing), getString(R.string.hint_transparency), getString(R.string.hint_telescope), getString(R.string.hint_eyepiece), getString(R.string.hint_power), getString(R.string.hint_filter), getString(R.string.hint_notes) };
        csvWrite.writeNext(arrHeaderStr);
        data.moveToFirst();
        while (!data.isAfterLast()) {
            String[] arrStr = { data.getString(objectIdCol), data.getString(dateCol), data.getString(locationCol), data.getString(seeingCol), data.getString(transparencyCol), data.getString(telescopeCol), data.getString(eyepieceCol), data.getString(powerCol), data.getString(filterCol), data.getString(notesCol) };
            csvWrite.writeNext(arrStr);
            data.moveToNext();
        }
        csvWrite.close();
        Toast.makeText(getActivity(), "File saved to:\n" + filePath, Toast.LENGTH_LONG).show();
    } catch (Exception exception) {
        Toast.makeText(getActivity(), "Error writing file", Toast.LENGTH_LONG).show();
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) CSVWriter(com.opencsv.CSVWriter) OutputStreamWriter(java.io.OutputStreamWriter) File(java.io.File)

Aggregations

CSVWriter (com.opencsv.CSVWriter)21 OutputStreamWriter (java.io.OutputStreamWriter)7 FileOutputStream (java.io.FileOutputStream)5 FileWriter (java.io.FileWriter)5 File (java.io.File)4 Path (java.nio.file.Path)4 ArrayList (java.util.ArrayList)4 Entry (org.jbei.ice.storage.model.Entry)4 IOException (java.io.IOException)3 Writer (java.io.Writer)3 HashSet (java.util.HashSet)3 SPRTMethod (kr.ac.kaist.se.simulator.method.SPRTMethod)3 GroupController (org.jbei.ice.lib.group.GroupController)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 StringWriter (java.io.StringWriter)2 SimpleDateFormat (java.text.SimpleDateFormat)2 ZipEntry (java.util.zip.ZipEntry)2 BaseChecker (kr.ac.kaist.se.mc.BaseChecker)2 Simulator (kr.ac.kaist.se.simulator.Simulator)2 EntryFieldLabel (org.jbei.ice.lib.dto.entry.EntryFieldLabel)2