Search in sources :

Example 81 with Account

use of org.jbei.ice.storage.model.Account in project ice by JBEI.

the class AccountDAO method getByEmail.

/**
     * Retrieve an {@link Account} by the email field.
     *
     * @param email unique email identifier for account
     * @return Account record referenced by email
     */
public Account getByEmail(String email) {
    if (email == null)
        return null;
    try {
        CriteriaQuery<Account> query = getBuilder().createQuery(Account.class);
        Root<Account> from = query.from(Account.class);
        query.where(getBuilder().equal(getBuilder().lower(from.get("email")), email.trim().toLowerCase()));
        return currentSession().createQuery(query).uniqueResult();
    } catch (HibernateException e) {
        Logger.error(e);
        throw new DAOException("Failed to retrieve Account by email: " + email, e);
    }
}
Also used : DAOException(org.jbei.ice.storage.DAOException) Account(org.jbei.ice.storage.model.Account) HibernateException(org.hibernate.HibernateException)

Example 82 with Account

use of org.jbei.ice.storage.model.Account in project ice by JBEI.

the class AccountDAO method getMatchingAccounts.

/**
     * Retrieves accounts whose firstName, lastName, or email fields match the specified
     * token up to the specified limit.
     *
     * @param token filter for the account fields
     * @param limit maximum number of matching accounts to return; 0 to return all
     * @return list of matching accounts
     */
public List<Account> getMatchingAccounts(String token, int limit) {
    try {
        CriteriaQuery<Account> query = getBuilder().createQuery(Account.class);
        Root<Account> from = query.from(Account.class);
        String[] tokens = token.split("\\s+");
        List<Predicate> predicates = new ArrayList<>();
        for (String tok : tokens) {
            tok = tok.toLowerCase();
            predicates.add(getBuilder().or(getBuilder().like(getBuilder().lower(from.get("firstName")), "%" + tok + "%"), getBuilder().like(getBuilder().lower(from.get("lastName")), "%" + tok + "%"), getBuilder().like(getBuilder().lower(from.get("email")), "%" + tok + "%")));
        }
        query.where(predicates.toArray(new Predicate[predicates.size()])).distinct(true);
        return currentSession().createQuery(query).setMaxResults(limit).list();
    } catch (HibernateException e) {
        Logger.error(e);
        throw new DAOException(e);
    }
}
Also used : DAOException(org.jbei.ice.storage.DAOException) Account(org.jbei.ice.storage.model.Account) HibernateException(org.hibernate.HibernateException) ArrayList(java.util.ArrayList) Predicate(javax.persistence.criteria.Predicate)

Example 83 with Account

use of org.jbei.ice.storage.model.Account in project ice by JBEI.

the class FolderContentsTest method testGetContents.

@Test
public void testGetContents() throws Exception {
    FolderContents folderContents = new FolderContents();
    // test with null id
    folderContents.getContents(null, 0, new PageParameters(0, 10, ColumnField.PART_ID, false, null));
    Account account = AccountCreator.createTestAccount("testRetrieveFolderContents", false);
    String userId = account.getEmail();
    FolderDetails folderDetails = new FolderDetails();
    folderDetails.setName("test");
    // create folder
    FolderDetails folder = createPersonalFolder(userId, folderDetails);
    Assert.assertNotNull(folder);
    final short size = 105;
    // create 100 test strains
    HashMap<String, Entry> parts = new HashMap<>();
    List<Long> entryList = new ArrayList<>();
    for (int i = 0; i < size; i += 1) {
        Strain strain = TestEntryCreator.createTestStrain(account);
        Assert.assertNotNull(strain);
        parts.put(strain.getPartNumber(), strain);
        entryList.add(strain.getId());
    }
    Assert.assertEquals(size, parts.size());
    // add to folder
    List<FolderDetails> foldersToAdd = new ArrayList<>();
    foldersToAdd.add(folder);
    foldersToAdd = folderContents.addEntriesToFolders(account.getEmail(), entryList, foldersToAdd);
    Assert.assertNotNull(foldersToAdd);
    // keep track to find duplicates
    HashSet<Long> set = new HashSet<>();
    // retrieve (supported sort types created, status, name, part_id, type)
    FolderDetails details = folderContents.getContents(account.getEmail(), folder.getId(), new PageParameters(0, 15, ColumnField.PART_ID, false, null));
    Assert.assertNotNull(details);
    short pageSize = 15;
    int it = 1;
    while (!details.getEntries().isEmpty()) {
        Assert.assertEquals(pageSize, details.getEntries().size());
        for (PartData partData : details.getEntries()) {
            Assert.assertNotNull(parts.remove(partData.getPartId()));
            Assert.assertFalse(set.contains(partData.getId()));
            set.add(partData.getId());
        }
        // check remaining
        Assert.assertEquals((size - (it * pageSize)), parts.size());
        details = folderContents.getContents(account.getEmail(), folder.getId(), new PageParameters(pageSize * it, pageSize, ColumnField.PART_ID, false, null));
        it += 1;
    }
}
Also used : Account(org.jbei.ice.storage.model.Account) FolderDetails(org.jbei.ice.lib.dto.folder.FolderDetails) PageParameters(org.jbei.ice.lib.dto.common.PageParameters) Strain(org.jbei.ice.storage.model.Strain) Entry(org.jbei.ice.storage.model.Entry) PartData(org.jbei.ice.lib.dto.entry.PartData) Test(org.junit.Test)

Example 84 with Account

use of org.jbei.ice.storage.model.Account in project ice by JBEI.

the class FolderContentsTest method testAddEntrySelection.

@Test
public void testAddEntrySelection() throws Exception {
    // create account
    Account account = AccountCreator.createTestAccount("FolderContentsTest.testAddEntrySelection", true);
    String userId = account.getEmail();
    Account user = AccountCreator.createTestAccount("FolderContentsTest.testAddEntrySelection2", false);
    // create folder
    FolderDetails folderDetails = new FolderDetails();
    folderDetails.setName("testAdd");
    folderDetails.setOwner(user.toDataTransferObject());
    FolderController controller = new FolderController();
    folderDetails = controller.createPersonalFolder(userId, folderDetails);
    Assert.assertNotNull(folderDetails);
    // check folder ownership
    Assert.assertEquals(1, controller.getUserFolders(user.getEmail()).size());
    // entry selection context for adding to folder
    EntrySelection selection = new EntrySelection();
    selection.setSelectionType(EntrySelectionType.FOLDER);
    selection.getDestination().add(folderDetails);
    // create entries
    long id = TestEntryCreator.createTestPart(userId);
    selection.getEntries().add(id);
    id = TestEntryCreator.createTestPart(userId);
    selection.getEntries().add(id);
    // add to folder
    FolderContents folderContents = new FolderContents();
    List<FolderDetails> folders = folderContents.addEntrySelection(userId, selection);
    Assert.assertNotNull(folders);
}
Also used : Account(org.jbei.ice.storage.model.Account) EntrySelection(org.jbei.ice.lib.entry.EntrySelection) FolderDetails(org.jbei.ice.lib.dto.folder.FolderDetails) Test(org.junit.Test)

Example 85 with Account

use of org.jbei.ice.storage.model.Account in project ice by JBEI.

the class FolderPermissionsTest method testCreateFolderPermission.

@Test
public void testCreateFolderPermission() throws Exception {
    Account account = AccountCreator.createTestAccount("FolderPermissionsTest.testCreateFolderPermission", false);
    String userId = account.getEmail();
    Folder folder = new Folder();
    folder.setOwnerEmail(userId);
    folder.setType(FolderType.PRIVATE);
    folder.setDescription("test folder");
    folder.setName("test");
    folder = DAOFactory.getFolderDAO().create(folder);
    Assert.assertNotNull(folder);
    FolderPermissions folderPermissions = new FolderPermissions(userId, folder.getId());
    AccessPermission accessPermission = new AccessPermission();
    // create a new account
    Account account2 = AccountCreator.createTestAccount("FolderPermissionsTest.testCreateFolderPermission2", false);
    // give read permission to folder for account
    accessPermission.setArticle(AccessPermission.Article.ACCOUNT);
    accessPermission.setType(AccessPermission.Type.READ_FOLDER);
    accessPermission.setArticleId(account2.getId());
    accessPermission.setTypeId(folder.getId());
    Assert.assertNotNull(folderPermissions.createPermission(accessPermission));
}
Also used : Account(org.jbei.ice.storage.model.Account) AccessPermission(org.jbei.ice.lib.dto.access.AccessPermission) Folder(org.jbei.ice.storage.model.Folder) Test(org.junit.Test)

Aggregations

Account (org.jbei.ice.storage.model.Account)153 Test (org.junit.Test)71 Group (org.jbei.ice.storage.model.Group)24 Entry (org.jbei.ice.storage.model.Entry)21 Strain (org.jbei.ice.storage.model.Strain)20 PartData (org.jbei.ice.lib.dto.entry.PartData)18 Folder (org.jbei.ice.storage.model.Folder)18 ArrayList (java.util.ArrayList)16 UserGroup (org.jbei.ice.lib.dto.group.UserGroup)16 PermissionException (org.jbei.ice.lib.access.PermissionException)11 EntryCreator (org.jbei.ice.lib.entry.EntryCreator)10 Plasmid (org.jbei.ice.storage.model.Plasmid)10 AccountTransfer (org.jbei.ice.lib.account.AccountTransfer)8 AccessPermission (org.jbei.ice.lib.dto.access.AccessPermission)8 FolderDetails (org.jbei.ice.lib.dto.folder.FolderDetails)8 DAOException (org.jbei.ice.storage.DAOException)8 RemotePartner (org.jbei.ice.storage.model.RemotePartner)8 HibernateException (org.hibernate.HibernateException)7 HashSet (java.util.HashSet)6 Part (org.jbei.ice.storage.model.Part)6