Search in sources :

Example 1 with PreferenceKey

use of org.jbei.ice.lib.dto.user.PreferenceKey in project ice by JBEI.

the class PreferencesDAO method getAccountPreferences.

public List<Preference> getAccountPreferences(Account account, List<PreferenceKey> keys) {
    ArrayList<String> keyString = new ArrayList<>();
    for (PreferenceKey key : keys) keyString.add(key.name());
    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(keyString));
        return currentSession().createQuery(query).list();
    } 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) PreferenceKey(org.jbei.ice.lib.dto.user.PreferenceKey)

Example 2 with PreferenceKey

use of org.jbei.ice.lib.dto.user.PreferenceKey in project ice by JBEI.

the class PreferencesController method retrieveAccountPreferences.

public HashMap<PreferenceKey, String> retrieveAccountPreferences(Account account, ArrayList<PreferenceKey> keys) {
    HashMap<PreferenceKey, String> preferences = new HashMap<>();
    List<Preference> results = dao.getAccountPreferences(account, keys);
    for (Preference preference : results) {
        PreferenceKey key = PreferenceKey.fromString(preference.getKey());
        // bulk uploads do not.
        if (key == null) {
            continue;
        }
        preferences.put(key, preference.getValue());
    }
    return preferences;
}
Also used : HashMap(java.util.HashMap) Preference(org.jbei.ice.storage.model.Preference) PreferenceKey(org.jbei.ice.lib.dto.user.PreferenceKey)

Example 3 with PreferenceKey

use of org.jbei.ice.lib.dto.user.PreferenceKey in project ice by JBEI.

the class PreferencesControllerTest method testRetrieveAccountPreferences.

@Test
public void testRetrieveAccountPreferences() throws Exception {
    Account account = AccountCreator.createTestAccount("testRetrieveAccountPreferences", false);
    ArrayList<PreferenceKey> keys = new ArrayList<>(Arrays.asList(PreferenceKey.values()));
    HashMap<PreferenceKey, String> preferences = controller.retrieveAccountPreferences(account, keys);
    Assert.assertNotNull(preferences);
    Assert.assertEquals(0, preferences.size());
    Preference preference = controller.createPreference(account, PreferenceKey.FUNDING_SOURCE.name(), "JBEI");
    Assert.assertNotNull(preference);
    preferences = controller.retrieveAccountPreferences(account, keys);
    Assert.assertNotNull(preference);
    Assert.assertEquals(1, preferences.size());
    Assert.assertTrue(preferences.containsKey(PreferenceKey.FUNDING_SOURCE));
    Assert.assertTrue(preferences.get(PreferenceKey.FUNDING_SOURCE).equals("JBEI"));
    preference = controller.createPreference(account, PreferenceKey.PRINCIPAL_INVESTIGATOR.name(), "PI");
    Assert.assertNotNull(preference);
    preferences = controller.retrieveAccountPreferences(account, keys);
    Assert.assertNotNull(preference);
    Assert.assertEquals(2, preferences.size());
    Assert.assertTrue(preferences.containsKey(PreferenceKey.FUNDING_SOURCE));
    Assert.assertTrue(preferences.get(PreferenceKey.FUNDING_SOURCE).equals("JBEI"));
    Assert.assertTrue(preferences.containsKey(PreferenceKey.PRINCIPAL_INVESTIGATOR));
    Assert.assertTrue(preferences.get(PreferenceKey.PRINCIPAL_INVESTIGATOR).equals("PI"));
}
Also used : 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) Test(org.junit.Test)

Example 4 with PreferenceKey

use of org.jbei.ice.lib.dto.user.PreferenceKey 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)

Aggregations

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