Search in sources :

Example 76 with DAOException

use of org.jbei.ice.storage.DAOException in project ice by JBEI.

the class AccountPreferencesDAO method getAccountPreferences.

/**
     * Retrieve the {@link AccountPreferences} of the given {@link Account}.
     *
     * @param account account whose preferences are being retrieved
     * @return retrieved AccountPreferences
     * @throws DAOException
     */
public AccountPreferences getAccountPreferences(Account account) {
    try {
        CriteriaQuery<AccountPreferences> query = getBuilder().createQuery(AccountPreferences.class);
        Root<AccountPreferences> from = query.from(AccountPreferences.class);
        query.where(getBuilder().equal(from.get("account"), account));
        return currentSession().createQuery(query).uniqueResult();
    } catch (HibernateException e) {
        Logger.error(e);
        throw new DAOException("Failed to get AccountPreferences by Account: " + account.getFullName(), e);
    }
}
Also used : DAOException(org.jbei.ice.storage.DAOException) HibernateException(org.hibernate.HibernateException) AccountPreferences(org.jbei.ice.storage.model.AccountPreferences)

Example 77 with DAOException

use of org.jbei.ice.storage.DAOException 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 78 with DAOException

use of org.jbei.ice.storage.DAOException 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 79 with DAOException

use of org.jbei.ice.storage.DAOException in project ice by JBEI.

the class ApiKeyDAO method getByClientId.

/**
     * Retrieves an api key by client id.
     * If one exists it is expected that the client id + the creator id uniquely retrieves it. In other words,
     * the same user cannot have two api keys for the same client
     *
     * @param clientId  client identifier for api key
     * @return container that may or may not contain the found key
     * @throws DAOException if more that one api key is found or there is a problem accessing the database
     */
public Optional<ApiKey> getByClientId(String clientId) {
    try {
        CriteriaQuery<ApiKey> query = getBuilder().createQuery(ApiKey.class);
        Root<ApiKey> from = query.from(ApiKey.class);
        query.where(getBuilder().equal(from.get("clientId"), clientId));
        return currentSession().createQuery(query).uniqueResultOptional();
    } catch (HibernateException he) {
        Logger.error(he);
        throw new DAOException(he);
    }
}
Also used : DAOException(org.jbei.ice.storage.DAOException) ApiKey(org.jbei.ice.storage.model.ApiKey) HibernateException(org.hibernate.HibernateException)

Example 80 with DAOException

use of org.jbei.ice.storage.DAOException in project ice by JBEI.

the class ApiKeyDAO method getAllApiKeys.

public List<ApiKey> getAllApiKeys(String sort, int limit, int start, boolean asc) {
    try {
        CriteriaQuery<ApiKey> query = getBuilder().createQuery(ApiKey.class);
        Root<ApiKey> from = query.from(ApiKey.class);
        query.orderBy(asc ? getBuilder().asc(from.get(sort)) : getBuilder().desc(from.get(sort)));
        return currentSession().createQuery(query).setFirstResult(start).setMaxResults(limit).list();
    } catch (HibernateException he) {
        Logger.error(he);
        throw new DAOException(he);
    }
}
Also used : DAOException(org.jbei.ice.storage.DAOException) ApiKey(org.jbei.ice.storage.model.ApiKey) HibernateException(org.hibernate.HibernateException)

Aggregations

DAOException (org.jbei.ice.storage.DAOException)150 HibernateException (org.hibernate.HibernateException)144 ArrayList (java.util.ArrayList)26 Group (org.jbei.ice.storage.model.Group)14 Account (org.jbei.ice.storage.model.Account)8 Request (org.jbei.ice.storage.model.Request)8 Predicate (javax.persistence.criteria.Predicate)7 SampleRequest (org.jbei.ice.lib.dto.sample.SampleRequest)6 Entry (org.jbei.ice.storage.model.Entry)6 Session (org.hibernate.Session)5 ApiKey (org.jbei.ice.storage.model.ApiKey)5 BulkUpload (org.jbei.ice.storage.model.BulkUpload)5 NativeQuery (org.hibernate.query.NativeQuery)4 Feature (org.jbei.ice.storage.model.Feature)4 Preference (org.jbei.ice.storage.model.Preference)4 Sample (org.jbei.ice.storage.model.Sample)4 Attachment (org.jbei.ice.storage.model.Attachment)3 Audit (org.jbei.ice.storage.model.Audit)3 RemoteClientModel (org.jbei.ice.storage.model.RemoteClientModel)3 ShotgunSequence (org.jbei.ice.storage.model.ShotgunSequence)3