Search in sources :

Example 6 with EntryField

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

the class FileBulkUpload method getCSVTemplateBytes.

/**
     * Creates a CSV template for download based on the the type of entries
     *
     * @param addType        entry type that is to be uploaded
     * @param linked         optional type that is linked to this entry. Should be one of {@link EntryType} or null
     * @param linkToExisting true, if <code>addType</code> is to be linked to an existing entry
     * @return byte array of the template or null if the headers for the type cannot be retrieved/is unsupported
     * @throws IllegalArgumentException if the addType is invalid
     */
public static byte[] getCSVTemplateBytes(EntryType addType, EntryType linked, boolean linkToExisting) {
    List<EntryField> headers = BulkCSVUploadHeaders.getHeadersForType(addType);
    if (headers == null)
        throw new IllegalArgumentException("Could not retrieve headers for type " + addType);
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < headers.size(); i++) {
        if (i != 0) {
            sb.append(",");
        }
        sb.append('"');
        EntryField header = headers.get(i);
        sb.append(header.getLabel());
        if (header.isRequired())
            sb.append("*");
        sb.append('"');
    }
    // check linked
    if (linkToExisting) {
        sb.append(",");
        sb.append('"');
        sb.append("Existing Part Number");
        sb.append('"');
    } else {
        if (linked != null) {
            headers = BulkCSVUploadHeaders.getHeadersForType(linked);
            if (headers != null) {
                for (EntryField header : headers) {
                    sb.append(",");
                    sb.append('"');
                    sb.append(linked.getDisplay()).append(" ").append(header.getLabel());
                    if (header.isRequired())
                        sb.append("*");
                    sb.append('"');
                }
            }
        }
    }
    sb.append("\n");
    return sb.toString().getBytes(StandardCharsets.UTF_8);
}
Also used : EntryField(org.jbei.ice.lib.dto.entry.EntryField)

Example 7 with EntryField

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

the class BulkEntryCreator method createOrUpdateEntry.

/**
     * Creates (or updates) entry based on information in parameters
     *
     * @param userId     unique identifier for user making request
     * @param autoUpdate wrapper for information used to create entry
     * @param addType    type of entry being created
     * @return updated wrapper for information used to create entry. Will contain additional information
     * such as the unique identifier for the part, if one was created
     */
public BulkUploadAutoUpdate createOrUpdateEntry(String userId, BulkUploadAutoUpdate autoUpdate, EntryType addType) {
    Account account = accountController.getByEmail(userId);
    BulkUpload draft = null;
    // for bulk edit, drafts will not exist
    if (autoUpdate.getEditMode() != EditMode.BULK_EDIT) {
        draft = createOrRetrieveBulkUpload(account, autoUpdate, addType);
        autoUpdate.setBulkUploadId(draft.getId());
    }
    // for strain with plasmid this is the strain
    Entry entry = entryDAO.get(autoUpdate.getEntryId());
    // for strain with plasmid this is the entry
    Entry otherEntry = null;
    // if entry is null, create entry
    if (entry == null) {
        entry = EntryFactory.buildEntry(autoUpdate.getType());
        if (entry == null)
            return null;
        String name = account.getFullName();
        String email = account.getEmail();
        entry.setOwner(name);
        entry.setOwnerEmail(email);
        entry.setCreator(name);
        entry.setCreatorEmail(email);
        entry = creator.createEntry(account, entry, null);
        autoUpdate.setEntryId(entry.getId());
        if (draft != null) {
            draft.getContents().add(entry);
        }
    }
    // now update the values (for strain with plasmid, some values are for both
    for (Map.Entry<EntryField, String> set : autoUpdate.getKeyValue().entrySet()) {
        String value = set.getValue();
        EntryField field = set.getKey();
        Entry[] ret = InfoToModelFactory.infoToEntryForField(entry, otherEntry, value, field);
        entry = ret[0];
        if (ret.length == 2) {
            otherEntry = ret[1];
        }
    }
    if (draft != null && draft.getStatus() != BulkUploadStatus.PENDING_APPROVAL) {
        if (otherEntry != null && autoUpdate.getEditMode() != EditMode.BULK_EDIT) {
            if (otherEntry.getVisibility() == null || otherEntry.getVisibility() != Visibility.DRAFT.getValue())
                otherEntry.setVisibility(Visibility.DRAFT.getValue());
            entryController.update(userId, otherEntry);
        }
        if ((entry.getVisibility() == null || entry.getVisibility() != Visibility.DRAFT.getValue()) && autoUpdate.getEditMode() != EditMode.BULK_EDIT)
            entry.setVisibility(Visibility.DRAFT.getValue());
    }
    // todo : que?
    //        EntryEditor editor = new EntryEditor();
    //        // set the plasmids and update
    //        if (entry.getRecordType().equalsIgnoreCase(EntryType.STRAIN.toString())
    //                && entry.getLinkedEntries().isEmpty()) {
    //            Strain strain = (Strain) entry;
    //            editor.setStrainPlasmids(account, strain, strain.getPlasmids());
    //        }
    entryController.update(userId, entry);
    // update bulk upload. even if no new entry was created, entries belonging to it was updated
    if (draft != null) {
        draft.setLastUpdateTime(new Date());
        autoUpdate.setLastUpdate(draft.getLastUpdateTime());
        dao.update(draft);
    }
    return autoUpdate;
}
Also used : HashMap(java.util.HashMap) Map(java.util.Map) Date(java.util.Date) EntryField(org.jbei.ice.lib.dto.entry.EntryField)

Example 8 with EntryField

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

the class BulkCSVUpload method processUpload.

/**
     * Processes the csv upload
     *
     * @return wrapper around id of created bulk upload or error message
     */
public ProcessedBulkUpload processUpload() {
    ProcessedBulkUpload processedBulkUpload = new ProcessedBulkUpload();
    try (FileInputStream inputStream = new FileInputStream(csvFilePath.toFile())) {
        try {
            List<PartWithSample> updates = getBulkUploadDataFromFile(inputStream);
            // create actual entries
            BulkEntryCreator creator = new BulkEntryCreator();
            long uploadId = creator.createBulkUpload(userId, addType);
            // create entries
            if (!creator.createEntries(userId, uploadId, updates, null)) {
                String errorMsg = "Error creating entries for upload";
                throw new IOException(errorMsg);
            //todo: delete upload id
            }
            processedBulkUpload.setUploadId(uploadId);
        } catch (IOException e) {
            // validation exception; convert entries to headers
            processedBulkUpload.setSuccess(false);
            processedBulkUpload.setUserMessage("Validation failed");
            for (EntryField field : invalidFields) {
                processedBulkUpload.getHeaders().add(new EntryHeaderValue(false, field));
            }
            Logger.error(e);
            return processedBulkUpload;
        }
    } catch (IOException e) {
        // general server error
        processedBulkUpload.setSuccess(false);
        processedBulkUpload.setUserMessage("Server error processing upload.");
        Logger.error(e);
    }
    return processedBulkUpload;
}
Also used : IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) EntryField(org.jbei.ice.lib.dto.entry.EntryField)

Example 9 with EntryField

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

the class BulkCSVUpload method getBulkUploadDataFromFile.

// NOTE: this also validates the part data (with the exception of the actual files)
List<PartWithSample> getBulkUploadDataFromFile(InputStream inputStream) throws IOException {
    List<PartWithSample> partDataList = new LinkedList<>();
    // initialize parser to null; when not-null in the loop below, then the header has been parsed
    CSVParser parser = null;
    HashMap<Integer, HeaderValue> headers = null;
    // parse CSV file
    try {
        LineIterator it = IOUtils.lineIterator(inputStream, "UTF-8");
        int index = 0;
        while (it.hasNext()) {
            String line = it.nextLine().trim();
            // check if first time parsing (first line)
            if (parser == null) {
                // to indicate the type of parser to use (tab or comma separated)
                if (line.contains("\t") && !line.contains(","))
                    parser = new CSVParser('\t');
                else
                    parser = new CSVParser();
                // get column headers
                String[] fieldStrArray = parser.parseLine(line);
                headers = processColumnHeaders(fieldStrArray);
                continue;
            }
            // skip any empty lines (holes) in the csv file
            if (StringUtils.isBlank(line) || line.replaceAll(",", "").trim().isEmpty())
                continue;
            // at this point we must have headers since that should be the first item in the file
            if (headers == null)
                throw new IOException("Could not parse file headers");
            // parser != null; process line contents with available headers
            String[] valuesArray = parser.parseLine(line);
            PartData partData = new PartData(addType);
            PartSample partSample = null;
            if (subType != null) {
                partData.getLinkedParts().add(new PartData(subType));
            }
            // for each column
            for (int i = 0; i < valuesArray.length; i += 1) {
                HeaderValue headerForColumn = headers.get(i);
                // process sample information
                if (headerForColumn.isSampleField()) {
                    // todo : move to another method
                    if (partSample == null)
                        partSample = new PartSample();
                    setPartSampleData(((SampleHeaderValue) headerForColumn).getSampleField(), partSample, valuesArray[i]);
                } else {
                    EntryHeaderValue entryHeaderValue = (EntryHeaderValue) headerForColumn;
                    EntryField field = entryHeaderValue.getEntryField();
                    PartData data;
                    String value = valuesArray[i];
                    boolean isSubType = entryHeaderValue.isSubType();
                    if (isSubType)
                        data = partData.getLinkedParts().get(0);
                    else
                        data = partData;
                    // get the data for the field
                    switch(field) {
                        case ATT_FILENAME:
                            ArrayList<AttachmentInfo> attachments = data.getAttachments();
                            if (attachments == null) {
                                attachments = new ArrayList<>();
                                data.setAttachments(attachments);
                            }
                            attachments.clear();
                            attachments.add(new AttachmentInfo(value));
                            break;
                        case SEQ_FILENAME:
                            data.setSequenceFileName(value);
                            break;
                        case SEQ_TRACE_FILES:
                            // todo
                            break;
                        case EXISTING_PART_NUMBER:
                            Entry entry = DAOFactory.getEntryDAO().getByPartNumber(value);
                            if (entry == null)
                                throw new IOException("Could not locate part number \"" + value + "\" for linking");
                            PartData toLink = entry.toDataTransferObject();
                            data.getLinkedParts().add(toLink);
                            break;
                        default:
                            partData = EntryUtil.setPartDataFromField(partData, value, field, isSubType);
                    }
                }
            }
            // validate
            List<EntryField> fields = EntryUtil.validates(partData);
            if (!fields.isEmpty()) {
                invalidFields.clear();
                invalidFields.addAll(fields);
                return null;
            }
            partData.setIndex(index);
            PartWithSample partWithSample = new PartWithSample(partSample, partData);
            partDataList.add(partWithSample);
            index += 1;
        }
    } finally {
        IOUtils.closeQuietly(inputStream);
    }
    return partDataList;
}
Also used : AttachmentInfo(org.jbei.ice.lib.dto.entry.AttachmentInfo) IOException(java.io.IOException) LineIterator(org.apache.commons.io.LineIterator) LinkedList(java.util.LinkedList) EntryField(org.jbei.ice.lib.dto.entry.EntryField) Entry(org.jbei.ice.storage.model.Entry) CSVParser(com.opencsv.CSVParser) PartData(org.jbei.ice.lib.dto.entry.PartData) PartSample(org.jbei.ice.lib.dto.sample.PartSample)

Example 10 with EntryField

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

the class BulkEntryCreator method submitBulkImportDraft.

/**
     * Submits a bulk import that has been saved. This action is restricted to the owner of the
     * draft or to administrators.
     */
protected ProcessedBulkUpload submitBulkImportDraft(String userId, BulkUpload draft, ProcessedBulkUpload processedBulkUpload) throws PermissionException {
    // validate entries
    BulkUploadValidation validation = new BulkUploadValidation(draft);
    if (!validation.isValid()) {
        processedBulkUpload.setSuccess(false);
        for (EntryField entryField : validation.getFailedFields()) {
            processedBulkUpload.getHeaders().add(new EntryHeaderValue(false, entryField));
        }
        processedBulkUpload.setUserMessage("Cannot submit your bulk upload due to a validation failure");
        return processedBulkUpload;
    }
    draft.setStatus(BulkUploadStatus.PENDING_APPROVAL);
    draft.setLastUpdateTime(new Date());
    draft.setName(userId);
    BulkUpload bulkUpload = dao.update(draft);
    if (bulkUpload != null) {
        // convert entries to pending
        dao.setEntryStatus(bulkUpload, Visibility.PENDING);
        String email = Utils.getConfigValue(ConfigurationKey.BULK_UPLOAD_APPROVER_EMAIL);
        if (email != null && !email.isEmpty()) {
            String subject = Utils.getConfigValue(ConfigurationKey.PROJECT_NAME) + " Bulk Upload Notification";
            String body = "A bulk upload has been submitted and is pending verification.\n\n";
            body += "Please login to the registry at:\n\n";
            body += Utils.getConfigValue(ConfigurationKey.URI_PREFIX);
            body += "\n\nand use the \"Pending Approval\" menu item to approve it\n\nThanks.";
            EmailFactory.getEmail().send(email, subject, body);
        }
        return processedBulkUpload;
    }
    return null;
}
Also used : Date(java.util.Date) EntryField(org.jbei.ice.lib.dto.entry.EntryField)

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