Search in sources :

Example 6 with PartSample

use of org.jbei.ice.lib.dto.sample.PartSample in project ice by JBEI.

the class SampleServiceTest method testRetrieveEntrySamples.

@Test
public void testRetrieveEntrySamples() throws Exception {
    Account account = AccountCreator.createTestAccount("SampleServiceTest.testRetrieveEntrySamples", false);
    String userId = account.getEmail();
    Strain strain = TestEntryCreator.createTestStrain(account);
    long entryId = strain.getId();
    List<PartSample> samplesList = service.retrieveEntrySamples(userId, Long.toString(entryId));
    Assert.assertNotNull(samplesList);
    for (PartSample partSample : samplesList) {
        Assert.assertNotNull(partSample.getId());
        Assert.assertNotNull(partSample.getCreationTime());
        Assert.assertNotNull(partSample.getLabel());
        Assert.assertNotNull(partSample.getLocation());
        Assert.assertNotNull(partSample.getDepositor());
        Assert.assertNotNull(partSample.getId());
        Assert.assertNotNull(partSample.isCanEdit());
        StorageLocation location = partSample.getLocation();
        while (location.getChild() != null) {
            location = location.getChild();
            Assert.assertNotNull(location.getType());
            Assert.assertNotNull(location.getId());
            Assert.assertNotNull(location.getDisplay());
            Assert.assertNotNull(location.getName());
        }
        if (partSample.getComments() != null) {
            for (UserComment comment : partSample.getComments()) {
                Assert.assertNotNull(comment.getId());
                Assert.assertNotNull(comment.getMessage());
            }
        }
    }
}
Also used : Account(org.jbei.ice.storage.model.Account) UserComment(org.jbei.ice.lib.dto.comment.UserComment) PartSample(org.jbei.ice.lib.dto.sample.PartSample) StorageLocation(org.jbei.ice.lib.dto.StorageLocation) Strain(org.jbei.ice.storage.model.Strain) Test(org.junit.Test)

Example 7 with PartSample

use of org.jbei.ice.lib.dto.sample.PartSample in project ice by JBEI.

the class SampleServiceTest method testDelete.

@Test
public void testDelete() throws Exception {
    Account account = AccountCreator.createTestAccount("testDelete", false);
    String userId = account.getEmail();
    // ----- CASE #1 -----
    Strain strain1 = TestEntryCreator.createTestStrain(account);
    PartSample partSample1 = new PartSample();
    partSample1.setLabel("testForShelfScheme1");
    // well
    StorageLocation well = new StorageLocation();
    well.setDisplay("well");
    well.setType(SampleType.WELL);
    // box
    StorageLocation box = new StorageLocation();
    box.setDisplay("box");
    box.setType(SampleType.BOX_INDEXED);
    box.setChild(well);
    // shelf
    StorageLocation shelf = new StorageLocation();
    shelf.setDisplay("shelf");
    shelf.setType(SampleType.SHELF);
    shelf.setChild(box);
    partSample1.setLocation(shelf);
    partSample1 = service.createSample(userId, strain1.getRecordId(), partSample1, null);
    Assert.assertNotNull(partSample1);
    // fetch
    List<PartSample> samples1 = service.retrieveEntrySamples(userId, Long.toString(strain1.getId()));
    Assert.assertEquals(1, samples1.size());
    StorageLocation location1 = samples1.get(0).getLocation();
    // delete
    Assert.assertTrue(service.delete(userId, strain1.getId(), samples1.get(0).getId()));
    Assert.assertNull(DAOFactory.getStorageDAO().get(location1.getId()));
    // ----- CASE 2 -----
    Strain strain2 = TestEntryCreator.createTestStrain(account);
    PartSample partSample2 = new PartSample();
    partSample2.setLabel("testForPlateScheme2");
    // tube2
    StorageLocation tube2 = new StorageLocation();
    tube2.setDisplay("tube2");
    tube2.setType(SampleType.TUBE);
    // well
    StorageLocation well2 = new StorageLocation();
    well2.setDisplay("well2");
    well2.setType(SampleType.WELL);
    well2.setChild(tube2);
    // plate2
    StorageLocation plate2 = new StorageLocation();
    plate2.setDisplay("plate2");
    plate2.setType(SampleType.PLATE96);
    plate2.setChild(well2);
    partSample2.setLocation(plate2);
    partSample2 = service.createSample(userId, Long.toString(strain2.getId()), partSample2, null);
    Assert.assertNotNull(partSample2);
    // ----- CASE 3 -----
    PartSample partSample3 = new PartSample();
    partSample3.setLabel("testForPlateScheme3");
    // tube2
    StorageLocation tube3 = new StorageLocation();
    tube3.setDisplay("tube3");
    tube3.setType(SampleType.TUBE);
    // well
    StorageLocation well3 = new StorageLocation();
    well3.setDisplay("well3");
    well3.setType(SampleType.WELL);
    well3.setChild(tube3);
    // plate2
    StorageLocation plate3 = new StorageLocation();
    plate3.setDisplay("plate3");
    plate3.setType(SampleType.PLATE96);
    plate3.setChild(well3);
    partSample3.setLocation(plate3);
    partSample3 = service.createSample(userId, Long.toString(strain2.getId()), partSample3, null);
    Assert.assertNotNull(partSample3);
    // fetch
    List<PartSample> samples3 = service.retrieveEntrySamples(userId, Long.toString(strain2.getId()));
    Assert.assertEquals(2, samples3.size());
    StorageLocation location3 = samples3.get(1).getLocation();
    // delete #2 and #3 consequently
    Assert.assertTrue(service.delete(userId, strain2.getId(), samples3.get(1).getId()));
    Assert.assertNull(DAOFactory.getStorageDAO().get(location3.getId()));
    List<PartSample> samples2 = service.retrieveEntrySamples(userId, Long.toString(strain2.getId()));
    Assert.assertEquals(1, samples2.size());
    StorageLocation location2 = samples2.get(0).getLocation();
    Assert.assertTrue(service.delete(userId, strain2.getId(), samples2.get(0).getId()));
    Assert.assertNull(DAOFactory.getStorageDAO().get(location2.getId()));
    List<PartSample> samplesEmpty = service.retrieveEntrySamples(userId, Long.toString(strain2.getId()));
    Assert.assertEquals(0, samplesEmpty.size());
}
Also used : Account(org.jbei.ice.storage.model.Account) PartSample(org.jbei.ice.lib.dto.sample.PartSample) StorageLocation(org.jbei.ice.lib.dto.StorageLocation) Strain(org.jbei.ice.storage.model.Strain) Test(org.junit.Test)

Example 8 with PartSample

use of org.jbei.ice.lib.dto.sample.PartSample in project ice by JBEI.

the class EntryController method createEntryComment.

public UserComment createEntryComment(String userId, long partId, UserComment newComment) {
    Entry entry = dao.get(partId);
    if (entry == null)
        return null;
    authorization.canRead(userId, entry);
    Account account = accountController.getByEmail(userId);
    Comment comment = new Comment();
    comment.setAccount(account);
    comment.setEntry(entry);
    comment.setBody(newComment.getMessage());
    comment.setCreationTime(new Date());
    comment = commentDAO.create(comment);
    if (newComment.getSamples() != null) {
        SampleDAO sampleDAO = DAOFactory.getSampleDAO();
        for (PartSample partSample : newComment.getSamples()) {
            Sample sample = sampleDAO.get(partSample.getId());
            if (sample == null)
                continue;
            comment.getSamples().add(sample);
            sample.getComments().add(comment);
        }
    }
    comment = commentDAO.update(comment);
    return comment.toDataTransferObject();
}
Also used : UserComment(org.jbei.ice.lib.dto.comment.UserComment) PartSample(org.jbei.ice.lib.dto.sample.PartSample) PartSample(org.jbei.ice.lib.dto.sample.PartSample)

Example 9 with PartSample

use of org.jbei.ice.lib.dto.sample.PartSample 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 PartSample

use of org.jbei.ice.lib.dto.sample.PartSample in project ice by JBEI.

the class BulkEntryCreator method createEntries.

public boolean createEntries(String userId, long draftId, List<PartWithSample> data, HashMap<String, InputStream> files) {
    BulkUpload draft = dao.get(draftId);
    if (draft == null)
        return false;
    // check permissions
    authorization.expectWrite(userId, draft);
    SampleService sampleService = new SampleService();
    EntryAuthorization entryAuthorization = new EntryAuthorization();
    for (PartWithSample partWithSample : data) {
        if (partWithSample == null)
            continue;
        PartData partData = partWithSample.getPartData();
        if (partData == null)
            continue;
        Entry entry = InfoToModelFactory.infoToEntry(partData);
        if (entry == null)
            continue;
        entry.setVisibility(Visibility.DRAFT.getValue());
        Account account = accountController.getByEmail(userId);
        entry.setOwner(account.getFullName());
        entry.setOwnerEmail(account.getEmail());
        // check if there is any linked parts. create if so (expect a max of 1)
        if (partData.getLinkedParts() != null && partData.getLinkedParts().size() > 0) {
            // create linked
            PartData linked = partData.getLinkedParts().get(0);
            // for existing the link already....exists so just verify
            if (linked.getId() == 0) {
                Entry linkedEntry = InfoToModelFactory.infoToEntry(linked);
                if (linkedEntry != null) {
                    linkedEntry.setVisibility(Visibility.DRAFT.getValue());
                    linkedEntry.setOwner(account.getFullName());
                    linkedEntry.setOwnerEmail(account.getEmail());
                    linkedEntry = entryDAO.create(linkedEntry);
                    linked.setId(linkedEntry.getId());
                    linked.setModificationTime(linkedEntry.getModificationTime().getTime());
                    addWritePermission(account, linkedEntry);
                    // check for attachments and sequences for linked entry
                    saveFiles(linked, linkedEntry, files);
                    entry.getLinkedEntries().add(linkedEntry);
                }
            }
            entry = entryDAO.create(entry);
            // attempt to get linked entry and add
            if (linked.getId() != 0) {
                Entry linkedEntry = entryDAO.get(linked.getId());
                if (linkedEntry != null && entryAuthorization.canWrite(userId, entry)) {
                    EntryLinks links = new EntryLinks(userId, Long.toString(entry.getId()));
                    links.addLink(linked, LinkType.CHILD);
                }
            }
        } else {
            entry = entryDAO.create(entry);
        }
        // check for pi
        String piEmail = entry.getPrincipalInvestigatorEmail();
        if (StringUtils.isNotEmpty(piEmail)) {
            Account pi = DAOFactory.getAccountDAO().getByEmail(piEmail);
            if (pi != null) {
                // add write permission for the PI
                addWritePermission(pi, entry);
            }
        }
        // add write permissions for owner
        addWritePermission(account, entry);
        draft.getContents().add(entry);
        dao.update(draft);
        // save files
        saveFiles(partData, entry, files);
        // save sample, if available
        PartSample partSample = partWithSample.getPartSample();
        if (partSample == null)
            continue;
        sampleService.createSample(userId, Long.toString(entry.getId()), partSample, null);
    }
    return true;
}
Also used : SampleService(org.jbei.ice.lib.entry.sample.SampleService) PartData(org.jbei.ice.lib.dto.entry.PartData) PartSample(org.jbei.ice.lib.dto.sample.PartSample)

Aggregations

PartSample (org.jbei.ice.lib.dto.sample.PartSample)12 StorageLocation (org.jbei.ice.lib.dto.StorageLocation)6 Account (org.jbei.ice.storage.model.Account)4 Strain (org.jbei.ice.storage.model.Strain)4 Test (org.junit.Test)4 UserComment (org.jbei.ice.lib.dto.comment.UserComment)3 HasEntry (org.jbei.ice.lib.entry.HasEntry)3 ArrayList (java.util.ArrayList)2 PartData (org.jbei.ice.lib.dto.entry.PartData)2 CSVParser (com.opencsv.CSVParser)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1 LineIterator (org.apache.commons.io.LineIterator)1 AttachmentInfo (org.jbei.ice.lib.dto.entry.AttachmentInfo)1 EntryField (org.jbei.ice.lib.dto.entry.EntryField)1 SampleService (org.jbei.ice.lib.entry.sample.SampleService)1 Entry (org.jbei.ice.storage.model.Entry)1 Storage (org.jbei.ice.storage.model.Storage)1