Search in sources :

Example 6 with Preference

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

the class PreferencesDAO method getPreference.

public Preference getPreference(Account account, String key) {
    try {
        CriteriaQuery<Preference> query = getBuilder().createQuery(Preference.class);
        Root<Preference> from = query.from(Preference.class);
        query.where(getBuilder().equal(from.get("account"), account), getBuilder().equal(from.get("key"), key.toUpperCase()));
        return currentSession().createQuery(query).uniqueResult();
    } catch (HibernateException he) {
        Logger.error(he);
        throw new DAOException(he);
    }
}
Also used : DAOException(org.jbei.ice.storage.DAOException) Preference(org.jbei.ice.storage.model.Preference) HibernateException(org.hibernate.HibernateException)

Example 7 with Preference

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

the class PreferencesDAO method retrievePreferenceValues.

public HashMap<String, String> retrievePreferenceValues(Account account, HashSet<String> keys) {
    try {
        CriteriaQuery<Preference> query = getBuilder().createQuery(Preference.class);
        Root<Preference> from = query.from(Preference.class);
        query.where(getBuilder().equal(from.get("account"), account), from.get("key").in(keys));
        List<Preference> result = currentSession().createQuery(query).list();
        Iterator iterator = result.iterator();
        HashMap<String, String> results = new HashMap<>();
        while (iterator.hasNext()) {
            Preference preference = (Preference) iterator.next();
            if (keys.contains(preference.getKey().toUpperCase())) {
                results.put(preference.getKey().toUpperCase().trim(), preference.getValue().trim());
            }
        }
        return results;
    } catch (HibernateException he) {
        Logger.error(he);
        throw new DAOException(he);
    }
}
Also used : DAOException(org.jbei.ice.storage.DAOException) Preference(org.jbei.ice.storage.model.Preference) HibernateException(org.hibernate.HibernateException)

Example 8 with Preference

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

the class PreferencesController method getUserPreferences.

public UserPreferences getUserPreferences(String requester, long userId) {
    AccountDAO accountDAO = DAOFactory.getAccountDAO();
    Account account = accountDAO.getByEmail(requester);
    Account requestedAccount = accountDAO.get(userId);
    if (account == null || requestedAccount == null)
        return null;
    if (account.getType() != AccountType.ADMIN && !requester.equalsIgnoreCase(requestedAccount.getEmail()))
        return null;
    ArrayList<PreferenceKey> keys = new ArrayList<>();
    keys.add(PreferenceKey.PRINCIPAL_INVESTIGATOR);
    keys.add(PreferenceKey.FUNDING_SOURCE);
    List<Preference> preferences = dao.getAccountPreferences(requestedAccount, keys);
    if (preferences == null)
        return null;
    UserPreferences userPreferences = new UserPreferences();
    userPreferences.setUserId(requestedAccount.getEmail());
    for (Preference preference : preferences) {
        userPreferences.getPreferences().add(preference.toDataTransferObject());
    }
    return userPreferences;
}
Also used : UserPreferences(org.jbei.ice.lib.dto.user.UserPreferences) Account(org.jbei.ice.storage.model.Account) Preference(org.jbei.ice.storage.model.Preference) ArrayList(java.util.ArrayList) PreferenceKey(org.jbei.ice.lib.dto.user.PreferenceKey) AccountDAO(org.jbei.ice.storage.hibernate.dao.AccountDAO)

Example 9 with Preference

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

the class PreferencesController method updatePreference.

public PreferenceInfo updatePreference(String requesterEmail, long userId, String key, String value) {
    if (value == null)
        return null;
    // todo : check permissions
    Account account = DAOFactory.getAccountDAO().get(userId);
    Preference preference = dao.createOrUpdatePreference(account, key, value);
    return preference.toDataTransferObject();
}
Also used : Account(org.jbei.ice.storage.model.Account) Preference(org.jbei.ice.storage.model.Preference)

Example 10 with Preference

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

the class PreferencesController method getPreferenceValue.

public String getPreferenceValue(String userId, String preferenceKey) {
    Account account = accountDAO.getByEmail(userId);
    if (account == null)
        return null;
    Preference preference = dao.getPreference(account, preferenceKey);
    if (preference == null)
        return null;
    return preference.getValue();
}
Also used : Account(org.jbei.ice.storage.model.Account) Preference(org.jbei.ice.storage.model.Preference)

Aggregations

Preference (org.jbei.ice.storage.model.Preference)10 Account (org.jbei.ice.storage.model.Account)5 HibernateException (org.hibernate.HibernateException)4 PreferenceKey (org.jbei.ice.lib.dto.user.PreferenceKey)4 DAOException (org.jbei.ice.storage.DAOException)4 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 HashMap (java.util.HashMap)1 UserPreferences (org.jbei.ice.lib.dto.user.UserPreferences)1 AccountDAO (org.jbei.ice.storage.hibernate.dao.AccountDAO)1