Search in sources :

Example 11 with GroupController

use of org.jbei.ice.lib.group.GroupController in project ice by JBEI.

the class BulkUploads method create.

/**
 * Creates a new bulk upload. If upload type is not bulk edit, then the status is set to in progress.
 * Default permissions consisting of read permissions for the public groups that the requesting user is
 * a part of are added
 *
 * @param userId identifier for user making request
 * @param info   bulk upload data
 * @return created upload
 */
public BulkUploadInfo create(String userId, BulkUploadInfo info) {
    Account account = accountController.getByEmail(userId);
    BulkUpload upload = new BulkUpload();
    upload.setName(info.getName());
    upload.setAccount(account);
    upload.setCreationTime(new Date());
    upload.setLastUpdateTime(upload.getCreationTime());
    if (info.getStatus() == BulkUploadStatus.BULK_EDIT) {
        // only one instance of bulk edit is allowed to remain
        clearBulkEdits(userId);
        upload.setStatus(BulkUploadStatus.BULK_EDIT);
    } else
        upload.setStatus(BulkUploadStatus.IN_PROGRESS);
    upload.setImportType(info.getType());
    // set default permissions
    GroupController groupController = new GroupController();
    ArrayList<Group> publicGroups = groupController.getAllPublicGroupsForAccount(account);
    for (Group group : publicGroups) {
        Permission permission = new Permission();
        permission.setCanRead(true);
        permission.setUpload(upload);
        permission.setGroup(group);
        permission = DAOFactory.getPermissionDAO().create(permission);
        upload.getPermissions().add(permission);
    }
    upload = dao.create(upload);
    if (info.getEntryList() != null) {
        for (PartData data : info.getEntryList()) {
            Entry entry = entryDAO.get(data.getId());
            // todo if entry is in another bulk upload, then update will fail
            if (entry == null)
                continue;
            upload.getContents().add(entry);
        }
    }
    dao.update(upload);
    return upload.toDataTransferObject();
}
Also used : GroupController(org.jbei.ice.lib.group.GroupController) AccessPermission(org.jbei.ice.lib.dto.access.AccessPermission)

Example 12 with GroupController

use of org.jbei.ice.lib.group.GroupController in project ice by JBEI.

the class EntriesAsCSV method writeList.

/**
 * Iterate through list of entries and extract values
 *
 * @throws IOException on Exception write values to file
 */
private void writeList(EntryFieldLabel... fields) throws IOException {
    // filter entries based on what the user is allowed to see if the user is not an admin
    Account account = this.accountDAO.getByEmail(userId);
    if (account.getType() != AccountType.ADMIN) {
        List<Group> accountGroups = new GroupController().getAllGroups(account);
        entries = permissionDAO.getCanReadEntries(account, accountGroups, entries);
    }
    if (entries == null) {
        Logger.warn("No entries to convert to csv format");
        return;
    }
    // write headers
    Path tmpPath = Paths.get(Utils.getConfigValue(ConfigurationKey.TEMPORARY_DIRECTORY));
    File tmpFile = File.createTempFile("ice-", ".csv", tmpPath.toFile());
    csvPath = tmpFile.toPath();
    FileWriter fileWriter = new FileWriter(tmpFile);
    if (fields == null || fields.length == 0)
        fields = getEntryFields();
    String[] headers = getCSVHeaders(fields);
    Set<Long> sequenceSet = new HashSet<>();
    try (CSVWriter writer = new CSVWriter(fileWriter)) {
        writer.writeNext(headers);
        // write entry fields
        for (long entryId : entries) {
            Entry entry = dao.get(entryId);
            // get contents and write data out
            String[] line = new String[fields.length + 4];
            line[0] = entry.getCreationTime().toString();
            line[1] = entry.getPartNumber();
            // write field values
            int i = 1;
            for (EntryFieldLabel field : fields) {
                line[i + 1] = EntryUtil.entryFieldToValue(entry, field);
                i += 1;
            }
            // write sequence information
            if (this.includeSequences && sequenceDAO.hasSequence(entryId)) {
                line[i + 1] = getSequenceName(entry);
                sequenceSet.add(entryId);
            } else {
                line[i + 1] = "";
            }
            // get parents for entry
            EntryLinks links = new EntryLinks(this.userId, entry.getPartNumber());
            StringBuilder parents = new StringBuilder();
            for (PartData data : links.getParents()) {
                parents.append(data.getPartId()).append(",");
            }
            if (!StringUtils.isEmpty(parents.toString()))
                parents = new StringBuilder(parents.substring(0, parents.length() - 1));
            line[i + 2] = ("\"" + parents + "\"");
            // write line
            writer.writeNext(line);
        }
    }
    if (!sequenceSet.isEmpty())
        writeZip(sequenceSet);
}
Also used : Path(java.nio.file.Path) Account(org.jbei.ice.storage.model.Account) Group(org.jbei.ice.storage.model.Group) GroupController(org.jbei.ice.lib.group.GroupController) CSVWriter(com.opencsv.CSVWriter) EntryFieldLabel(org.jbei.ice.lib.dto.entry.EntryFieldLabel) Entry(org.jbei.ice.storage.model.Entry) ZipEntry(java.util.zip.ZipEntry) PartData(org.jbei.ice.lib.dto.entry.PartData) HashSet(java.util.HashSet)

Example 13 with GroupController

use of org.jbei.ice.lib.group.GroupController in project ice by JBEI.

the class EntryPermissions method getEntryPermissions.

/**
 * Retrieves permissions associated with a part. Requires that the requesting user has write permissions
 * on the specified part
 *
 * @return list of available permissions for the specified part
 * @throws PermissionException if the requesting user does not have write permissions for the part
 */
public List<AccessPermission> getEntryPermissions() {
    // viewing permissions requires write permissions
    authorization.expectWrite(userId, entry);
    ArrayList<AccessPermission> accessPermissions = new ArrayList<>();
    List<Permission> permissions = permissionDAO.getEntryPermissions(entry);
    GroupController groupController = new GroupController();
    Group publicGroup = groupController.createOrRetrievePublicGroup();
    for (Permission permission : permissions) {
        if (permission.getAccount() == null && permission.getGroup() == null)
            continue;
        if (permission.getGroup() != null && permission.getGroup() == publicGroup)
            continue;
        accessPermissions.add(permission.toDataTransferObject());
    }
    return accessPermissions;
}
Also used : Group(org.jbei.ice.storage.model.Group) GroupController(org.jbei.ice.lib.group.GroupController) AccessPermission(org.jbei.ice.lib.dto.access.AccessPermission) ArrayList(java.util.ArrayList) AccessPermission(org.jbei.ice.lib.dto.access.AccessPermission) Permission(org.jbei.ice.storage.model.Permission)

Example 14 with GroupController

use of org.jbei.ice.lib.group.GroupController in project ice by JBEI.

the class EntryCreator method createEntry.

/**
     * Create an entry in the database.
     * <p/>
     * Generates a new Part Number, the record id (UUID), version id, and timestamps.
     * Optionally set the record globally visible or schedule an index rebuild.
     *
     * @param account           account of user creating entry
     * @param entry             entry record being created
     * @param accessPermissions list of permissions to associate with created entry
     * @return entry that was saved in the database.
     */
public Entry createEntry(Account account, Entry entry, ArrayList<AccessPermission> accessPermissions) {
    if (entry.getRecordId() == null) {
        entry.setRecordId(Utils.generateUUID());
        entry.setVersionId(entry.getRecordId());
    }
    entry.setCreationTime(Calendar.getInstance().getTime());
    entry.setModificationTime(entry.getCreationTime());
    if (StringUtils.isEmpty(entry.getOwner()))
        entry.setOwner(account.getFullName());
    if (StringUtils.isEmpty(entry.getOwnerEmail()))
        entry.setOwnerEmail(account.getEmail());
    if (entry.getSelectionMarkers() != null) {
        for (SelectionMarker selectionMarker : entry.getSelectionMarkers()) {
            selectionMarker.setEntry(entry);
        }
    }
    if (entry.getLinks() != null) {
        for (Link link : entry.getLinks()) {
            link.setEntry(entry);
        }
    }
    if (entry.getStatus() == null)
        entry.setStatus("");
    if (entry.getBioSafetyLevel() == null)
        entry.setBioSafetyLevel(0);
    entry = dao.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);
    // add read permission for all public groups
    ArrayList<Group> groups = new GroupController().getAllPublicGroupsForAccount(account);
    for (Group group : groups) {
        addReadPermission(null, group, entry);
    }
    if (accessPermissions != null) {
        for (AccessPermission accessPermission : accessPermissions) {
            if (accessPermission.getArticle() == AccessPermission.Article.ACCOUNT) {
                Account accessAccount = DAOFactory.getAccountDAO().get(accessPermission.getArticleId());
                // add account read permission
                addReadPermission(accessAccount, null, entry);
            } else {
                // add group read permission
                Group group = DAOFactory.getGroupDAO().get(accessPermission.getArticleId());
                addReadPermission(null, group, entry);
            }
        }
    }
    // rebuild blast database
    if (sequenceDAO.hasSequence(entry.getId())) {
        BlastPlus.scheduleBlastIndexRebuildTask(true);
    }
    return entry;
}
Also used : GroupController(org.jbei.ice.lib.group.GroupController) AccessPermission(org.jbei.ice.lib.dto.access.AccessPermission)

Example 15 with GroupController

use of org.jbei.ice.lib.group.GroupController in project ice by JBEI.

the class EntriesAsCSV method writeList.

/**
     * Iterate through list of entries and extract values
     *
     * @param userId identifier of user making request
     * @throws IOException on Exception write values to file
     */
private void writeList(String userId, EntryField... fields) throws IOException {
    // filter entries based on what the user is allowed to see if the user is not an admin
    Account account = this.accountDAO.getByEmail(userId);
    if (account.getType() != AccountType.ADMIN) {
        List<Group> accountGroups = new GroupController().getAllGroups(account);
        entries = permissionDAO.getCanReadEntries(account, accountGroups, entries);
    }
    if (entries == null) {
        Logger.warn("No entries to convert to csv format");
        return;
    }
    // write headers
    Path tmpPath = Paths.get(Utils.getConfigValue(ConfigurationKey.TEMPORARY_DIRECTORY));
    File tmpFile = File.createTempFile("ice-", ".csv", tmpPath.toFile());
    csvPath = tmpFile.toPath();
    FileWriter fileWriter = new FileWriter(tmpFile);
    if (fields == null || fields.length == 0)
        fields = getEntryFields();
    String[] headers = getCSVHeaders(fields);
    Set<Long> sequenceSet = new HashSet<>();
    try (CSVWriter writer = new CSVWriter(fileWriter)) {
        writer.writeNext(headers);
        // write entry fields
        for (long entryId : entries) {
            Entry entry = dao.get(entryId);
            //  get contents and write data out
            String[] line = new String[fields.length + 3];
            line[0] = entry.getCreationTime().toString();
            line[1] = entry.getPartNumber();
            int i = 1;
            for (EntryField field : fields) {
                line[i + 1] = EntryUtil.entryFieldToValue(entry, field);
                i += 1;
            }
            if (this.includeSequences && sequenceDAO.hasSequence(entryId)) {
                line[i + 1] = getSequenceName(entry);
                sequenceSet.add(entryId);
            } else {
                line[i + 1] = "";
            }
            writer.writeNext(line);
        }
    }
    writeZip(userId, sequenceSet);
}
Also used : Path(java.nio.file.Path) Account(org.jbei.ice.storage.model.Account) Group(org.jbei.ice.storage.model.Group) GroupController(org.jbei.ice.lib.group.GroupController) CSVWriter(com.opencsv.CSVWriter) EntryField(org.jbei.ice.lib.dto.entry.EntryField) Entry(org.jbei.ice.storage.model.Entry) ZipEntry(java.util.zip.ZipEntry) HashSet(java.util.HashSet)

Aggregations

GroupController (org.jbei.ice.lib.group.GroupController)25 Group (org.jbei.ice.storage.model.Group)12 AccessPermission (org.jbei.ice.lib.dto.access.AccessPermission)8 PartData (org.jbei.ice.lib.dto.entry.PartData)8 HashSet (java.util.HashSet)6 Entry (org.jbei.ice.storage.model.Entry)6 ArrayList (java.util.ArrayList)5 FolderDetails (org.jbei.ice.lib.dto.folder.FolderDetails)4 Account (org.jbei.ice.storage.model.Account)4 CSVWriter (com.opencsv.CSVWriter)3 Path (java.nio.file.Path)3 AccountController (org.jbei.ice.lib.account.AccountController)3 EntryDAO (org.jbei.ice.storage.hibernate.dao.EntryDAO)3 ZipEntry (java.util.zip.ZipEntry)2 PermissionException (org.jbei.ice.lib.access.PermissionException)2 EntryFieldLabel (org.jbei.ice.lib.dto.entry.EntryFieldLabel)2 RebuildBlastIndexTask (org.jbei.ice.lib.search.blast.RebuildBlastIndexTask)2 ZipOutputStream (java.util.zip.ZipOutputStream)1 PermissionsController (org.jbei.ice.lib.access.PermissionsController)1 ConfigurationSettings (org.jbei.ice.lib.config.ConfigurationSettings)1