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