Search in sources :

Example 1 with Preference

use of org.jbei.ice.storage.model.Preference 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 Preference

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

the class PreferencesDAO method createOrUpdatePreference.

public Preference createOrUpdatePreference(Account account, String key, String value) {
    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()));
        Optional<Preference> optional = currentSession().createQuery(query).uniqueResultOptional();
        if (optional.isPresent()) {
            Preference preference = optional.get();
            if (preference.getValue().equalsIgnoreCase(value))
                return preference;
            currentSession().update(preference);
            return preference;
        } else {
            return this.create(new Preference(account, key, value));
        }
    } 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 3 with Preference

use of org.jbei.ice.storage.model.Preference 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 4 with Preference

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

the class PreferencesControllerTest method testCreatePreference.

@Test
public void testCreatePreference() throws Exception {
    Account account = AccountCreator.createTestAccount("testCreatePreference", false);
    Preference preference = controller.createPreference(account, "foo", "bar");
    Assert.assertNotNull(preference);
    preference = controller.createPreference(account, "bar", "foo");
    Assert.assertNotNull(preference);
}
Also used : Account(org.jbei.ice.storage.model.Account) Preference(org.jbei.ice.storage.model.Preference) Test(org.junit.Test)

Example 5 with Preference

use of org.jbei.ice.storage.model.Preference 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)

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