Search in sources :

Example 11 with EntryField

use of org.jbei.ice.lib.dto.entry.EntryField in project ice by JBEI.

the class BulkCSVUpload method processColumnHeaders.

/**
     * @param headerArray list of column header values representing the entry fields
     * @return mapping of col number to the entry type
     * @throws IOException
     */
HashMap<Integer, HeaderValue> processColumnHeaders(String[] headerArray) throws IOException {
    HashMap<Integer, HeaderValue> headers = new HashMap<>();
    for (int i = 0; i < headerArray.length; i += 1) {
        String fieldStr = headerArray[i].trim();
        // account for "*" that indicates a header is required
        if (fieldStr.lastIndexOf("*") != -1)
            fieldStr = fieldStr.substring(0, fieldStr.length() - 1);
        EntryField field = EntryField.fromString(fieldStr);
        if (field != null) {
            // field header maps as is to EntryField which indicates it is not a sub Type
            HeaderValue headerValue = new EntryHeaderValue(false, field);
            headers.put(i, headerValue);
            continue;
        }
        // check if sample field
        SampleField sampleField = SampleField.fromString(fieldStr);
        if (sampleField != null) {
            HeaderValue headerValue = new SampleHeaderValue(sampleField);
            headers.put(i, headerValue);
            continue;
        }
        // check sub Type
        if (subType != null) {
            HeaderValue headerValue = detectSubTypeHeaderValue(subType, fieldStr);
            headers.put(i, headerValue);
            continue;
        }
        // sub data is null, try to detect
        subType = detectSubType(fieldStr);
        if (subType == null) {
            throw new IOException("Unknown field [" + fieldStr + "] for upload [" + addType.getDisplay() + "]");
        }
        HeaderValue headerValue = detectSubTypeHeaderValue(subType, fieldStr);
        headers.put(i, headerValue);
    }
    if (headers.size() != headerArray.length) {
        throw new IOException("Header size and input header array length differ");
    }
    return headers;
}
Also used : HashMap(java.util.HashMap) SampleField(org.jbei.ice.lib.dto.bulkupload.SampleField) IOException(java.io.IOException) EntryField(org.jbei.ice.lib.dto.entry.EntryField)

Example 12 with EntryField

use of org.jbei.ice.lib.dto.entry.EntryField in project ice by JBEI.

the class BulkCSVUpload method detectSubTypeHeaderValue.

HeaderValue detectSubTypeHeaderValue(EntryType subType, String fieldStr) throws IOException {
    int k = fieldStr.indexOf(subType.getDisplay());
    String headerValue = fieldStr.substring(k + subType.getDisplay().length());
    EntryField field = EntryField.fromString(headerValue.trim());
    if (field == null) {
        throw new IOException("Unknown field [" + fieldStr + "] for upload [" + addType.getDisplay() + "]");
    }
    return new EntryHeaderValue(true, field);
}
Also used : IOException(java.io.IOException) EntryField(org.jbei.ice.lib.dto.entry.EntryField)

Example 13 with EntryField

use of org.jbei.ice.lib.dto.entry.EntryField 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<EntryField> 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<Entry> 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) EntryField(org.jbei.ice.lib.dto.entry.EntryField) EntryDAO(org.jbei.ice.storage.hibernate.dao.EntryDAO) ZipEntry(java.util.zip.ZipEntry) ZipOutputStream(java.util.zip.ZipOutputStream) PartData(org.jbei.ice.lib.dto.entry.PartData) HashSet(java.util.HashSet)

Example 14 with EntryField

use of org.jbei.ice.lib.dto.entry.EntryField in project ice by JBEI.

the class RemoteEntriesAsCSV method writeLocalEntries.

protected void writeLocalEntries(List<Entry> entries, List<EntryField> fields, CSVWriter writer, ZipOutputStream zos) {
    if (entries == null)
        return;
    SequenceDAO sequenceDAO = DAOFactory.getSequenceDAO();
    Configuration configuration = DAOFactory.getConfigurationDAO().get(ConfigurationKey.URI_PREFIX);
    String thisUrl = configuration == null ? "" : configuration.getValue();
    for (Entry entry : entries) {
        String[] line = new String[fields.size() + 4];
        line[0] = thisUrl;
        line[1] = entry.getCreationTime().toString();
        line[2] = entry.getPartNumber();
        int i = 2;
        for (EntryField field : fields) {
            line[i + 1] = EntryUtil.entryFieldToValue(entry, field);
            i += 1;
        }
        // write sequence to zip file
        long entryId = entry.getId();
        if (sequenceDAO.hasSequence(entryId)) {
            String name = entry.getPartNumber() + ".gb";
            try {
                Sequence sequence = sequenceDAO.getByEntry(entry);
                line[i + 1] = name;
                GenbankFormatter genbankFormatter = new GenbankFormatter(name);
                ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
                genbankFormatter.format(sequence, byteStream);
                ByteArrayWrapper wrapper = new ByteArrayWrapper(byteStream.toByteArray(), name);
                putZipEntry(wrapper, zos);
            } catch (Exception e) {
                line[i + 1] = "";
            }
        } else {
            line[i + 1] = "";
        }
        writer.writeNext(line);
    }
}
Also used : FeaturedDNASequence(org.jbei.ice.lib.dto.FeaturedDNASequence) EntryField(org.jbei.ice.lib.dto.entry.EntryField) GenbankFormatter(org.jbei.ice.lib.entry.sequence.composers.formatters.GenbankFormatter) ZipEntry(java.util.zip.ZipEntry) ByteArrayWrapper(org.jbei.ice.lib.entry.sequence.ByteArrayWrapper) SequenceDAO(org.jbei.ice.storage.hibernate.dao.SequenceDAO)

Example 15 with EntryField

use of org.jbei.ice.lib.dto.entry.EntryField in project ice by JBEI.

the class EntriesAsCSV method writeList.

/**
     * Iterate through list of entries and extract values
     *
     * @param userId identifier of user making request
     * @throws IOException on Exception write values to file
     */
private void writeList(String userId, EntryField... fields) throws IOException {
    // filter entries based on what the user is allowed to see if the user is not an admin
    Account account = this.accountDAO.getByEmail(userId);
    if (account.getType() != AccountType.ADMIN) {
        List<Group> accountGroups = new GroupController().getAllGroups(account);
        entries = permissionDAO.getCanReadEntries(account, accountGroups, entries);
    }
    if (entries == null) {
        Logger.warn("No entries to convert to csv format");
        return;
    }
    // write headers
    Path tmpPath = Paths.get(Utils.getConfigValue(ConfigurationKey.TEMPORARY_DIRECTORY));
    File tmpFile = File.createTempFile("ice-", ".csv", tmpPath.toFile());
    csvPath = tmpFile.toPath();
    FileWriter fileWriter = new FileWriter(tmpFile);
    if (fields == null || fields.length == 0)
        fields = getEntryFields();
    String[] headers = getCSVHeaders(fields);
    Set<Long> sequenceSet = new HashSet<>();
    try (CSVWriter writer = new CSVWriter(fileWriter)) {
        writer.writeNext(headers);
        // write entry fields
        for (long entryId : entries) {
            Entry entry = dao.get(entryId);
            //  get contents and write data out
            String[] line = new String[fields.length + 3];
            line[0] = entry.getCreationTime().toString();
            line[1] = entry.getPartNumber();
            int i = 1;
            for (EntryField field : fields) {
                line[i + 1] = EntryUtil.entryFieldToValue(entry, field);
                i += 1;
            }
            if (this.includeSequences && sequenceDAO.hasSequence(entryId)) {
                line[i + 1] = getSequenceName(entry);
                sequenceSet.add(entryId);
            } else {
                line[i + 1] = "";
            }
            writer.writeNext(line);
        }
    }
    writeZip(userId, sequenceSet);
}
Also used : Path(java.nio.file.Path) Account(org.jbei.ice.storage.model.Account) Group(org.jbei.ice.storage.model.Group) GroupController(org.jbei.ice.lib.group.GroupController) CSVWriter(com.opencsv.CSVWriter) EntryField(org.jbei.ice.lib.dto.entry.EntryField) Entry(org.jbei.ice.storage.model.Entry) ZipEntry(java.util.zip.ZipEntry) HashSet(java.util.HashSet)

Aggregations

EntryField (org.jbei.ice.lib.dto.entry.EntryField)16 IOException (java.io.IOException)5 ZipEntry (java.util.zip.ZipEntry)4 PartData (org.jbei.ice.lib.dto.entry.PartData)4 Date (java.util.Date)3 HashMap (java.util.HashMap)3 HashSet (java.util.HashSet)3 CSVWriter (com.opencsv.CSVWriter)2 Path (java.nio.file.Path)2 FeaturedDNASequence (org.jbei.ice.lib.dto.FeaturedDNASequence)2 ByteArrayWrapper (org.jbei.ice.lib.entry.sequence.ByteArrayWrapper)2 GenbankFormatter (org.jbei.ice.lib.entry.sequence.composers.formatters.GenbankFormatter)2 GroupController (org.jbei.ice.lib.group.GroupController)2 Entry (org.jbei.ice.storage.model.Entry)2 CSVParser (com.opencsv.CSVParser)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1