Search in sources :

Example 16 with FolderDetails

use of org.jbei.ice.lib.dto.folder.FolderDetails in project ice by JBEI.

the class FolderContents method addEntrySelection.

/**
     * Adds entries in the selection context, to specified folders
     *
     * @param userId        unique identifier for user making request
     * @param entryLocation entry selection context which also contains the folders to
     *                      add the entries obtained from the context to
     * @return list of folders that the entries where added to. They should correspond to the specified
     * folders in the selection context
     */
public List<FolderDetails> addEntrySelection(String userId, EntrySelection entryLocation) {
    Entries retriever = new Entries(userId);
    if (entryLocation.getRemoteEntries() != null && !entryLocation.getRemoteEntries().isEmpty())
        return addRemoteEntries(userId, entryLocation.getRemoteEntries(), entryLocation.getDestination());
    List<Long> entries = retriever.getEntriesFromSelectionContext(entryLocation);
    if (StringUtils.isEmpty(userId)) {
        ArrayList<FolderDetails> destination = entryLocation.getDestination();
        // check that folder is transferred before rejecting
        if (destination == null || destination.isEmpty())
            throw new IllegalArgumentException("Cannot add without valid user id or destination");
        Folder folder = folderDAO.get(destination.get(0).getId());
        if (folder == null)
            throw new IllegalArgumentException("Cannot find folder");
        if (folder.getType() != FolderType.TRANSFERRED)
            throw new PermissionException("Can only add to transferred folder without id");
        FolderDetails details = addEntriesToTransferredFolder(entries, folder);
        List<FolderDetails> result = new ArrayList<>();
        result.add(details);
        return result;
    }
    return addEntriesToFolders(userId, entries, entryLocation.getDestination());
}
Also used : PermissionException(org.jbei.ice.lib.access.PermissionException) ArrayList(java.util.ArrayList) FolderDetails(org.jbei.ice.lib.dto.folder.FolderDetails) Entries(org.jbei.ice.lib.entry.Entries)

Example 17 with FolderDetails

use of org.jbei.ice.lib.dto.folder.FolderDetails in project ice by JBEI.

the class FolderContents method removeFolderContents.

/**
     * Removes the specified contents of a folder, optionally adding them to another folder
     *
     * @param userId    unique identifier for user making request
     * @param folderId  unique identifier for folder whose (specified) entries are being removed
     * @param selection wrapper around the selection context for the contents
     * @param move      true, if the contents are to be added to another (set of) folder(s) (which should be specified in
     *                  <code>selection</code> parameter)
     * @return true, if action completed successfully; false otherwise
     */
public boolean removeFolderContents(String userId, long folderId, EntrySelection selection, boolean move) {
    // remove entries from specified folder
    boolean isAdministrator = accountController.isAdministrator(userId);
    Folder folder = folderDAO.get(folderId);
    if (folder.getType() == FolderType.PUBLIC && !isAdministrator) {
        String errMsg = userId + ": cannot modify folder " + folder.getName();
        throw new PermissionException(errMsg);
    }
    Entries entries = new Entries(userId);
    List<Long> entryIds = entries.getEntriesFromSelectionContext(selection);
    boolean successRemove = folderDAO.removeFolderEntries(folder, entryIds) != null;
    if (!move)
        return successRemove;
    // add to specified folder
    selection.setFolderId(Long.toString(folderId));
    List<FolderDetails> details = addEntrySelection(userId, selection);
    return !details.isEmpty();
}
Also used : PermissionException(org.jbei.ice.lib.access.PermissionException) FolderDetails(org.jbei.ice.lib.dto.folder.FolderDetails) Entries(org.jbei.ice.lib.entry.Entries)

Example 18 with FolderDetails

use of org.jbei.ice.lib.dto.folder.FolderDetails in project ice by JBEI.

the class FolderContents method addEntriesToFolders.

/**
     * Attempts to add the specified list of entries to the specified folder destinations.
     * The user making the request must have read privileges on the entries and write privileges on the destination
     * folders.
     * Any entries that the user is not permitted to read will not be be added and any destination folders that the user
     * does not have write privileges for will not have entries added to it
     *
     * @param userId  unique identifier for user making request
     * @param entries list of entry identifiers to be added. Specified user must have read privileges on any
     *                that are to be added
     * @param folders list of folders that that entries are to be added to
     * @return list of destination folders that were updated successfully
     */
protected List<FolderDetails> addEntriesToFolders(String userId, List<Long> entries, List<FolderDetails> folders) {
    Account account = DAOFactory.getAccountDAO().getByEmail(userId);
    List<Group> accountGroups = new GroupController().getAllGroups(account);
    if (!folderAuthorization.isAdmin(userId))
        entries = DAOFactory.getPermissionDAO().getCanReadEntries(account, accountGroups, entries);
    if (entries == null || entries.isEmpty())
        return new ArrayList<>();
    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 privs on folder " + folder.getId());
            continue;
        }
        // check if the folder is remote or local
        if (folder.getType() == FolderType.REMOTE) {
            Logger.info("Adding entries to remote folder " + folder.getId());
            addToRemoteFolder(account, folder, entries);
        } else {
            List<Entry> entryModelList = DAOFactory.getEntryDAO().getEntriesByIdSet(entries);
            folderDAO.addFolderContents(folder, entryModelList);
            if (folder.isPropagatePermissions()) {
                List<Permission> folderPermissions = permissionDAO.getFolderPermissions(folder);
                addEntryPermission(userId, folderPermissions, entryModelList);
            }
            details.setCount(folderDAO.getFolderSize(folder.getId(), null, true));
        }
    }
    return folders;
}
Also used : GroupController(org.jbei.ice.lib.group.GroupController) AccessPermission(org.jbei.ice.lib.dto.access.AccessPermission) FolderDetails(org.jbei.ice.lib.dto.folder.FolderDetails)

Example 19 with FolderDetails

use of org.jbei.ice.lib.dto.folder.FolderDetails in project ice by JBEI.

the class FolderContents method getRemoteContents.

/**
     * Retrieves contents of a folder that has been shared remotely with this instance
     *
     * @param userId user folder is shared with
     * @param folder remote shared folder
     * @return wrapper around entries conforming to specified parameters
     * @throws IllegalArgumentException if the folder is not of type <code>REMOTE</code>
     */
protected FolderDetails getRemoteContents(String userId, Folder folder, PageParameters pageParameters) {
    if (folder.getType() != FolderType.REMOTE) {
        String errorMessage = "Folder " + folder.getId() + " is not remote and therefore cannot retrieve contents";
        Logger.error(errorMessage);
        throw new IllegalArgumentException(errorMessage);
    }
    // get remote access
    Account account = DAOFactory.getAccountDAO().getByEmail(userId);
    RemoteAccessModel remoteAccessModel = remoteAccessModelDAO.getByFolder(account, folder);
    if (remoteAccessModel == null) {
        Logger.error("Could not find access model for folder " + folder.getId() + " and user " + userId);
        return null;
    }
    FolderDetails details = folder.toDataTransferObject();
    RemoteClientModel remoteClientModel = remoteAccessModel.getRemoteClientModel();
    AccountTransfer owner = new AccountTransfer();
    owner.setEmail(remoteClientModel.getEmail());
    details.setOwner(owner);
    RemotePartner remotePartner = remoteClientModel.getRemotePartner();
    details.setRemotePartner(remotePartner.toDataTransferObject());
    String token = remoteAccessModel.getToken();
    // todo : currently folder id only
    long remoteFolderId = Long.decode(remoteAccessModel.getIdentifier());
    // retrieve entries from remote partner (ends up in the call below)
    FolderDetails remoteDetails = remoteContact.getRemoteContents(remotePartner.getUrl(), userId, remoteFolderId, token, pageParameters, remotePartner.getApiKey());
    if (remoteDetails == null) {
        Logger.error("Could not retrieve remote shared folder " + remoteFolderId + " from " + remotePartner.getUrl());
        return null;
    }
    details.setCount(remoteDetails.getCount());
    details.setEntries(remoteDetails.getEntries());
    return details;
}
Also used : FolderDetails(org.jbei.ice.lib.dto.folder.FolderDetails) AccountTransfer(org.jbei.ice.lib.account.AccountTransfer)

Example 20 with FolderDetails

use of org.jbei.ice.lib.dto.folder.FolderDetails in project ice by JBEI.

the class FolderContents method addToRemoteFolder.

// transfer entries to remote partner and add to folder
protected void addToRemoteFolder(Account account, Folder folder, List<Long> entries) {
    RemoteAccessModel remoteAccessModel = remoteAccessModelDAO.getByFolder(account, folder);
    if (remoteAccessModel == null) {
        Logger.error("Could not retrieve remote access for folder " + folder.getId());
        return;
    }
    RemotePartner remotePartner = remoteAccessModel.getRemoteClientModel().getRemotePartner();
    RemoteTransfer remoteTransfer = new RemoteTransfer();
    List<PartData> results = remoteTransfer.getPartsForTransfer(entries);
    List<Long> remoteIds = remoteTransfer.transferEntries(remotePartner.getId(), results);
    FolderDetails remoteFolder = new FolderDetails();
    remoteFolder.setId(Long.decode(remoteAccessModel.getIdentifier()));
    // add transferred entries to the folder
    EntrySelection selection = new EntrySelection();
    selection.getEntries().addAll(remoteIds);
    selection.getDestination().add(remoteFolder);
    selection.setSelectionType(EntrySelectionType.FOLDER);
    String token = remoteAccessModel.getToken();
    remoteContact.addTransferredEntriesToFolder(remotePartner.getUrl(), account.getEmail(), selection, remoteFolder.getId(), token, remotePartner.getApiKey());
}
Also used : EntrySelection(org.jbei.ice.lib.entry.EntrySelection) RemoteTransfer(org.jbei.ice.lib.net.RemoteTransfer) PartData(org.jbei.ice.lib.dto.entry.PartData) FolderDetails(org.jbei.ice.lib.dto.folder.FolderDetails)

Aggregations

FolderDetails (org.jbei.ice.lib.dto.folder.FolderDetails)35 Folder (org.jbei.ice.storage.model.Folder)11 PartData (org.jbei.ice.lib.dto.entry.PartData)8 Account (org.jbei.ice.storage.model.Account)8 PermissionException (org.jbei.ice.lib.access.PermissionException)6 AccessPermission (org.jbei.ice.lib.dto.access.AccessPermission)5 Test (org.junit.Test)5 AccountTransfer (org.jbei.ice.lib.account.AccountTransfer)4 GroupController (org.jbei.ice.lib.group.GroupController)4 ArrayList (java.util.ArrayList)3 BulkUploadInfo (org.jbei.ice.lib.bulkupload.BulkUploadInfo)3 EntrySelection (org.jbei.ice.lib.entry.EntrySelection)3 Group (org.jbei.ice.storage.model.Group)3 RemotePartner (org.jbei.ice.storage.model.RemotePartner)3 Date (java.util.Date)2 HashMap (java.util.HashMap)2 TokenHash (org.jbei.ice.lib.account.TokenHash)2 Entries (org.jbei.ice.lib.entry.Entries)2 Entry (org.jbei.ice.storage.model.Entry)2 HashSet (java.util.HashSet)1