Search in sources :

Example 1 with AttachmentInfo

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

the class SampleResource method createSamples.

@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.APPLICATION_JSON)
// todo : change end point
@Path("/file")
public Response createSamples(@FormDataParam("file") InputStream fileInputStream, @FormDataParam("file") FormDataContentDisposition header) {
    String userId = requireUserId();
    try {
        SampleCSV sampleCSV = new SampleCSV(userId, fileInputStream);
        String fileName = sampleCSV.generate();
        AttachmentInfo info = new AttachmentInfo();
        info.setFileId(fileName);
        return super.respond(info);
    } catch (Exception e) {
        Logger.error(e);
        throw new WebApplicationException(e);
    }
}
Also used : AttachmentInfo(org.jbei.ice.lib.dto.entry.AttachmentInfo) IOException(java.io.IOException)

Example 2 with AttachmentInfo

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

the class FileResource method post.

/**
 * @return Response with attachment info on uploaded file
 */
@POST
@Path("attachment")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.APPLICATION_JSON)
public Response post(@FormDataParam("file") InputStream fileInputStream, @FormDataParam("file") FormDataContentDisposition contentDispositionHeader) {
    try {
        final String fileName = contentDispositionHeader.getFileName();
        final String fileId = Utils.generateUUID();
        final File attachmentFile = Paths.get(Utils.getConfigValue(ConfigurationKey.DATA_DIRECTORY), Attachments.attachmentDirName, fileId).toFile();
        FileUtils.copyInputStreamToFile(fileInputStream, attachmentFile);
        final AttachmentInfo info = new AttachmentInfo();
        info.setFileId(fileId);
        info.setFilename(fileName);
        return Response.status(Response.Status.OK).entity(info).build();
    } catch (final IOException e) {
        Logger.error(e);
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
    }
}
Also used : AttachmentInfo(org.jbei.ice.lib.dto.entry.AttachmentInfo) IOException(java.io.IOException) File(java.io.File)

Example 3 with AttachmentInfo

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

the class Attachment method toDataTransferObject.

@Override
public AttachmentInfo toDataTransferObject() {
    AttachmentInfo info = new AttachmentInfo();
    info.setFileId(fileId);
    info.setFilename(fileName);
    info.setDescription(description);
    info.setId(id);
    return info;
}
Also used : AttachmentInfo(org.jbei.ice.lib.dto.entry.AttachmentInfo)

Example 4 with AttachmentInfo

use of org.jbei.ice.lib.dto.entry.AttachmentInfo 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 5 with AttachmentInfo

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

the class BulkUploadResource method uploadAttachmentFile.

@POST
@Path("/{id}/attachment")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.APPLICATION_JSON)
public Response uploadAttachmentFile(@PathParam("id") long uploadId, @FormDataParam("file") InputStream fileInputStream, @FormDataParam("entryId") long entryId, @FormDataParam("file") FormDataContentDisposition contentDispositionHeader) {
    String fileName = contentDispositionHeader.getFileName();
    String userId = requireUserId();
    AttachmentInfo attachmentInfo = controller.addAttachment(userId, uploadId, entryId, fileInputStream, fileName);
    if (attachmentInfo == null) {
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
    }
    return Response.status(Response.Status.OK).entity(attachmentInfo).build();
}
Also used : AttachmentInfo(org.jbei.ice.lib.dto.entry.AttachmentInfo)

Aggregations

AttachmentInfo (org.jbei.ice.lib.dto.entry.AttachmentInfo)5 IOException (java.io.IOException)3 CSVParser (com.opencsv.CSVParser)1 File (java.io.File)1 LinkedList (java.util.LinkedList)1 LineIterator (org.apache.commons.io.LineIterator)1 EntryField (org.jbei.ice.lib.dto.entry.EntryField)1 PartData (org.jbei.ice.lib.dto.entry.PartData)1 PartSample (org.jbei.ice.lib.dto.sample.PartSample)1 Entry (org.jbei.ice.storage.model.Entry)1