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);
}
}
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);
}
}
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;
}
}
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);
}
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));
}
Aggregations