Search in sources :

Example 1 with SampleService

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

PartData (org.jbei.ice.lib.dto.entry.PartData)1 PartSample (org.jbei.ice.lib.dto.sample.PartSample)1 SampleService (org.jbei.ice.lib.entry.sample.SampleService)1