Search in sources :

Example 46 with PartData

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

the class FolderContents method addRemoteEntries.

/**
     * @param remoteEntries list of entries that map to entries on other ICE instances. It contains enough information
     *                      for the table view (as a cache)
     * @param folders       list of folders that the remote entries are to be added to
     * @return List of folders that the specified entries were added to
     */
protected List<FolderDetails> addRemoteEntries(String userId, List<PartData> remoteEntries, List<FolderDetails> folders) {
    // nothing adding to destination
    if (remoteEntries == null)
        return new ArrayList<>();
    // nothing to add
    if (remoteEntries.isEmpty())
        return folders;
    EntryDAO entryDAO = DAOFactory.getEntryDAO();
    List<Entry> entryModelList = new ArrayList<>(remoteEntries.size());
    for (PartData partData : remoteEntries) {
        Entry entry = entryDAO.getByRecordId(partData.getRecordId());
        if (entry == null) {
            switch(partData.getType()) {
                case PART:
                default:
                    entry = new Part();
                    break;
                case STRAIN:
                    entry = new Strain();
                    break;
                case PLASMID:
                    entry = new Plasmid();
                    break;
                case ARABIDOPSIS:
                    entry = new ArabidopsisSeed();
                    break;
            }
            entry.setRecordId(partData.getRecordId());
            entry.setVersionId(partData.getRecordId());
            entry.setRecordType(partData.getType().getDisplay());
            entry.setName(partData.getName());
            entry.setShortDescription(partData.getShortDescription());
            entry.setStatus(partData.getStatus());
            entry.setVisibility(Visibility.REMOTE.getValue());
            entry.setPartNumber(partData.getPartId());
            String sequence = partData.isHasSequence() ? "sequence" : "text";
            entry.setLongDescriptionType(sequence);
            entry.setBioSafetyLevel(partData.getBioSafetyLevel());
            entry.setCreationTime(new Date(partData.getCreationTime()));
            entry = entryDAO.create(entry);
        }
        entryModelList.add(entry);
    }
    for (FolderDetails details : folders) {
        Folder folder = folderDAO.get(details.getId());
        if (folder == null) {
            Logger.warn("Could not add entries to folder " + details.getId() + " which doesn't exist");
            continue;
        }
        if (!folderAuthorization.canWrite(userId, folder)) {
            Logger.warn(userId + " lacks write privileges on folder " + folder.getId());
            continue;
        }
        folderDAO.addFolderContents(folder, entryModelList);
        details.setCount(folderDAO.getFolderSize(folder.getId(), null, true));
    }
    return folders;
}
Also used : ArrayList(java.util.ArrayList) FolderDetails(org.jbei.ice.lib.dto.folder.FolderDetails) Date(java.util.Date) PartData(org.jbei.ice.lib.dto.entry.PartData)

Example 47 with PartData

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

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

the class RemoteTransfer method performTransfer.

/**
     * Transfers the sequence file for the part and any parts that are linked to it.
     * If the attached sequence was uploaded as a file or pasted, the system
     * transfers that. If not if attempts to convert the attached sequence to genbank format
     * and transfers that
     *
     * @param partner destination for the sequence transfer
     * @param data    data for part whose sequences are to be transferred
     */
protected void performTransfer(RemotePartner partner, PartData data) {
    String url = partner.getUrl();
    // check main entry for sequence
    if (sequenceDAO.hasSequence(data.getId())) {
        Entry entry = entryDAO.get(data.getId());
        Sequence sequence = sequenceDAO.getByEntry(entry);
        String sequenceString = sequence.getSequenceUser();
        if (StringUtils.isEmpty(sequenceString)) {
            GenbankFormatter genbankFormatter = new GenbankFormatter(entry.getName());
            genbankFormatter.setCircular(true);
            try {
                ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
                genbankFormatter.format(sequence, byteStream);
                sequenceString = byteStream.toString();
            } catch (Exception e) {
                Logger.error(e);
                sequenceString = sequence.getSequence();
            }
        }
        if (!StringUtils.isEmpty(sequenceString))
            remoteContact.transferSequence(url, data.getRecordId(), data.getType(), sequenceString);
    }
    // check child entries
    if (data.getLinkedParts() == null)
        return;
    for (PartData linked : data.getLinkedParts()) {
        performTransfer(partner, linked);
    }
}
Also used : GenbankFormatter(org.jbei.ice.lib.entry.sequence.composers.formatters.GenbankFormatter) Entry(org.jbei.ice.storage.model.Entry) PartData(org.jbei.ice.lib.dto.entry.PartData) Sequence(org.jbei.ice.storage.model.Sequence) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 49 with PartData

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

the class RemoteTransfer method getPartsForTransfer.

/**
     * Using the list of entry Ids, populates a list of PartData objects that maintains the hierarchical
     * relationships
     *
     * @param entryIds list of ids for entries that are to be transferred
     * @return List of Part Data objects obtained using the list for transfer
     */
public List<PartData> getPartsForTransfer(List<Long> entryIds) {
    // for searching entries to be transferred
    HashSet<Long> forTransfer = new HashSet<>(entryIds);
    HashMap<Long, PartData> toTransfer = new LinkedHashMap<>();
    for (long entryId : entryIds) {
        // already being transferred; skip
        if (toTransfer.containsKey(entryId))
            continue;
        Entry entry = entryDAO.get(entryId);
        if (entry == null)
            continue;
        PartData data = ModelToInfoFactory.getInfo(entry);
        if (data == null) {
            Logger.error("Could not convert entry " + entry.getId() + " to data model");
            continue;
        }
        // check if the linked entries (if any) is in the list of entries to be transferred
        if (data.getLinkedParts() != null && !data.getLinkedParts().isEmpty()) {
            Iterator<PartData> iterator = data.getLinkedParts().iterator();
            while (iterator.hasNext()) {
                PartData linkedData = iterator.next();
                // make sure linked entry is among list to be transferred
                if (!forTransfer.contains(linkedData.getId())) {
                    iterator.remove();
                    continue;
                }
                // check if linked entry has already been transferred
                if (toTransfer.containsKey(linkedData.getId())) {
                    // part of this entry
                    if (toTransfer.remove(linkedData.getId()) == null)
                        Logger.warn("Entry " + linkedData.getId() + " being transferred twice");
                }
            }
        }
        toTransfer.put(data.getId(), data);
    }
    return new LinkedList<>(toTransfer.values());
}
Also used : Entry(org.jbei.ice.storage.model.Entry) PartData(org.jbei.ice.lib.dto.entry.PartData)

Example 50 with PartData

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

the class HibernateSearch method executeSearchNoTerms.

public SearchResults executeSearchNoTerms(String userId, HashMap<String, SearchResult> blastResults, SearchQuery searchQuery) {
    ArrayList<EntryType> entryTypes = searchQuery.getEntryTypes();
    if (entryTypes == null || entryTypes.isEmpty()) {
        entryTypes = new ArrayList<>();
        entryTypes.addAll(Arrays.asList(EntryType.values()));
    }
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    int resultCount;
    FullTextSession fullTextSession = Search.getFullTextSession(session);
    BooleanQuery.Builder builder = new BooleanQuery.Builder();
    QueryBuilder qb = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(Entry.class).get();
    ArrayList<Query> except = new ArrayList<>();
    for (EntryType type : EntryType.values()) {
        if (entryTypes.contains(type))
            continue;
        except.add(qb.keyword().onField("recordType").matching(type.getName()).createQuery());
    }
    // add terms for record types
    Query[] queries = new Query[] {};
    Query recordTypeQuery = qb.all().except(except.toArray(queries)).createQuery();
    builder.add(recordTypeQuery, BooleanClause.Occur.FILTER);
    // visibility
    Query visibilityQuery = qb.keyword().onField("visibility").matching(Visibility.OK.getValue()).createQuery();
    builder.add(visibilityQuery, BooleanClause.Occur.FILTER);
    // bio safety level
    BioSafetyOption option = searchQuery.getBioSafetyOption();
    if (option != null) {
        TermContext bslContext = qb.keyword();
        Query biosafetyQuery = bslContext.onField("bioSafetyLevel").ignoreFieldBridge().matching(option.getIntValue()).createQuery();
        builder.add(biosafetyQuery, BooleanClause.Occur.FILTER);
    }
    // check filter filters
    if (searchQuery.getFieldFilters() != null && !searchQuery.getFieldFilters().isEmpty()) {
        for (FieldFilter fieldFilter : searchQuery.getFieldFilters()) {
            String searchField = SearchFieldFactory.searchFieldForEntryField(fieldFilter.getField());
            if (StringUtils.isEmpty(searchField))
                continue;
            Query filterQuery = qb.keyword().onField(searchField).matching(fieldFilter.getFilter()).createQuery();
            builder.add(filterQuery, BooleanClause.Occur.MUST);
        }
    }
    // check if there is a blast results
    createBlastFilterQuery(fullTextSession, blastResults, builder);
    // wrap Lucene query in a org.hibernate.Query
    FullTextQuery fullTextQuery = fullTextSession.createFullTextQuery(builder.build(), Entry.class);
    // get sorting values
    Sort sort = getSort(searchQuery.getParameters().isSortAscending(), searchQuery.getParameters().getSortField());
    fullTextQuery.setSort(sort);
    // enable security filter if needed
    checkEnableSecurityFilter(userId, fullTextQuery);
    // enable has attachment/sequence/sample (if needed)
    checkEnableHasAttribute(fullTextQuery, searchQuery.getParameters());
    // set paging params
    fullTextQuery.setFirstResult(searchQuery.getParameters().getStart());
    fullTextQuery.setMaxResults(searchQuery.getParameters().getRetrieveCount());
    resultCount = fullTextQuery.getResultSize();
    List result = fullTextQuery.list();
    LinkedList<SearchResult> searchResults = new LinkedList<>();
    for (Object object : result) {
        Entry entry = (Entry) object;
        SearchResult searchResult;
        if (blastResults != null) {
            searchResult = blastResults.get(Long.toString(entry.getId()));
            if (// this should not really happen since we already filter
            searchResult == null)
                continue;
        } else {
            searchResult = new SearchResult();
            searchResult.setScore(1f);
            PartData info = ModelToInfoFactory.createTableViewData(userId, entry, true);
            searchResult.setEntryInfo(info);
        }
        searchResult.setMaxScore(1f);
        searchResults.add(searchResult);
    }
    SearchResults results = new SearchResults();
    results.setResultCount(resultCount);
    results.setResults(searchResults);
    Logger.info(userId + ": obtained " + resultCount + " results for empty query");
    return results;
}
Also used : FullTextSession(org.hibernate.search.FullTextSession) SearchQuery(org.jbei.ice.lib.dto.search.SearchQuery) FullTextQuery(org.hibernate.search.FullTextQuery) QueryBuilder(org.hibernate.search.query.dsl.QueryBuilder) BioSafetyOption(org.jbei.ice.lib.shared.BioSafetyOption) QueryBuilder(org.hibernate.search.query.dsl.QueryBuilder) SearchResults(org.jbei.ice.lib.dto.search.SearchResults) TermContext(org.hibernate.search.query.dsl.TermContext) Entry(org.jbei.ice.storage.model.Entry) SearchResult(org.jbei.ice.lib.dto.search.SearchResult) FieldFilter(org.jbei.ice.lib.dto.search.FieldFilter) EntryType(org.jbei.ice.lib.dto.entry.EntryType) PartData(org.jbei.ice.lib.dto.entry.PartData) FullTextQuery(org.hibernate.search.FullTextQuery) FullTextSession(org.hibernate.search.FullTextSession) Session(org.hibernate.Session)

Aggregations

PartData (org.jbei.ice.lib.dto.entry.PartData)62 Entry (org.jbei.ice.storage.model.Entry)22 Account (org.jbei.ice.storage.model.Account)18 Test (org.junit.Test)13 ArrayList (java.util.ArrayList)11 FolderDetails (org.jbei.ice.lib.dto.folder.FolderDetails)8 Results (org.jbei.ice.lib.dto.common.Results)5 EntryField (org.jbei.ice.lib.dto.entry.EntryField)4 Group (org.jbei.ice.storage.model.Group)4 Strain (org.jbei.ice.storage.model.Strain)4 Date (java.util.Date)3 HashSet (java.util.HashSet)3 PermissionException (org.jbei.ice.lib.access.PermissionException)3 SearchResult (org.jbei.ice.lib.dto.search.SearchResult)3 SearchResults (org.jbei.ice.lib.dto.search.SearchResults)3 EntryCreator (org.jbei.ice.lib.entry.EntryCreator)3 GroupController (org.jbei.ice.lib.group.GroupController)3 EntryDAO (org.jbei.ice.storage.hibernate.dao.EntryDAO)3 Plasmid (org.jbei.ice.storage.model.Plasmid)3 RemotePartner (org.jbei.ice.storage.model.RemotePartner)3