use of javax.persistence.criteria.CriteriaBuilder in project jgnash by ccavanaugh.
the class JpaCommodityDAO method getExchangeNode.
/*
* @see jgnash.engine.CommodityDAOInterface#getExchangeNode(java.lang.String)
*/
@Override
public ExchangeRate getExchangeNode(final String rateId) {
ExchangeRate exchangeRate = null;
try {
Future<ExchangeRate> future = executorService.submit(() -> {
emLock.lock();
try {
ExchangeRate exchangeRate1 = null;
final CriteriaBuilder cb = em.getCriteriaBuilder();
final CriteriaQuery<ExchangeRate> cq = cb.createQuery(ExchangeRate.class);
final Root<ExchangeRate> root = cq.from(ExchangeRate.class);
cq.select(root);
final TypedQuery<ExchangeRate> q = em.createQuery(cq);
for (final ExchangeRate rate : q.getResultList()) {
if (rate.getRateId().equals(rateId)) {
exchangeRate1 = rate;
break;
}
}
return exchangeRate1;
} finally {
emLock.unlock();
}
});
exchangeRate = future.get();
} catch (final InterruptedException | ExecutionException e) {
logger.log(Level.SEVERE, e.getLocalizedMessage(), e);
}
return exchangeRate;
}
use of javax.persistence.criteria.CriteriaBuilder in project jgnash by ccavanaugh.
the class JpaCommodityDAO method getCurrencies.
/*
* @see jgnash.engine.CommodityDAOInterface#getCurrencies()
*/
@Override
@SuppressWarnings("unchecked")
public List<CurrencyNode> getCurrencies() {
List<CurrencyNode> currencyNodeList = Collections.emptyList();
try {
Future<List<CurrencyNode>> future = executorService.submit(() -> {
emLock.lock();
try {
final CriteriaBuilder cb = em.getCriteriaBuilder();
final CriteriaQuery<CurrencyNode> cq = cb.createQuery(CurrencyNode.class);
final Root<CurrencyNode> root = cq.from(CurrencyNode.class);
cq.select(root);
final TypedQuery<CurrencyNode> q = em.createQuery(cq);
return stripMarkedForRemoval(new ArrayList<>(q.getResultList()));
} finally {
emLock.unlock();
}
});
currencyNodeList = future.get();
} catch (final InterruptedException | ExecutionException e) {
logger.log(Level.SEVERE, e.getLocalizedMessage(), e);
}
return currencyNodeList;
}
use of javax.persistence.criteria.CriteriaBuilder in project jgnash by ccavanaugh.
the class JpaConfigDAO method getDefaultConfig.
/*
* @see jgnash.engine.ConfigDAOInterface#getDefaultConfig()
*/
@Override
public synchronized Config getDefaultConfig() {
Config defaultConfig = null;
try {
Future<Config> future = executorService.submit(() -> {
emLock.lock();
try {
Config newConfig;
try {
final CriteriaBuilder cb = em.getCriteriaBuilder();
final CriteriaQuery<Config> cq = cb.createQuery(Config.class);
final Root<Config> root = cq.from(Config.class);
cq.select(root);
final TypedQuery<Config> q = em.createQuery(cq);
newConfig = q.getSingleResult();
} catch (final Exception e) {
newConfig = new Config();
em.getTransaction().begin();
em.persist(newConfig);
em.getTransaction().commit();
logger.info("Generating new default config");
}
return newConfig;
} finally {
emLock.unlock();
}
});
defaultConfig = future.get();
} catch (final InterruptedException | ExecutionException e) {
logger.log(Level.SEVERE, e.getLocalizedMessage(), e);
}
return defaultConfig;
}
use of javax.persistence.criteria.CriteriaBuilder in project jgnash by ccavanaugh.
the class JpaEngineDAO method getStoredObjects.
@Override
public List<StoredObject> getStoredObjects() {
List<StoredObject> list = Collections.emptyList();
try {
final Future<List<StoredObject>> future = executorService.submit(() -> {
emLock.lock();
try {
final CriteriaBuilder cb = em.getCriteriaBuilder();
final CriteriaQuery<StoredObject> cq = cb.createQuery(StoredObject.class);
final Root<StoredObject> root = cq.from(StoredObject.class);
cq.select(root);
final TypedQuery<StoredObject> q = em.createQuery(cq);
return new ArrayList<>(q.getResultList());
} finally {
emLock.unlock();
}
});
list = future.get();
} catch (final InterruptedException | ExecutionException e) {
logSevere(JpaEngineDAO.class, e);
}
return list;
}
use of javax.persistence.criteria.CriteriaBuilder in project che by eclipse.
the class JpaRecipeDao method search.
@Override
@Transactional
public List<RecipeImpl> search(String user, List<String> tags, String type, int skipCount, int maxItems) throws ServerException {
try {
final EntityManager manager = managerProvider.get();
final CriteriaBuilder cb = manager.getCriteriaBuilder();
final CriteriaQuery<RecipeImpl> query = cb.createQuery(RecipeImpl.class);
final Root<RecipeImpl> fromRecipe = query.from(RecipeImpl.class);
final ParameterExpression<String> typeParam = cb.parameter(String.class, "recipeType");
final Predicate checkType = cb.or(cb.isNull(typeParam), cb.equal(fromRecipe.get("type"), typeParam));
final TypedQuery<RecipeImpl> typedQuery;
if (tags != null && !tags.isEmpty()) {
final Join<RecipeImpl, String> tag = fromRecipe.join("tags");
query.select(cb.construct(RecipeImpl.class, tag.getParent())).where(cb.and(checkType, tag.in(tags))).groupBy(fromRecipe.get("id")).having(cb.equal(cb.count(tag), tags.size()));
typedQuery = manager.createQuery(query).setParameter("tags", tags);
} else {
typedQuery = manager.createQuery(query.where(checkType));
}
return typedQuery.setParameter("recipeType", type).setFirstResult(skipCount).setMaxResults(maxItems).getResultList();
} catch (RuntimeException ex) {
throw new ServerException(ex.getLocalizedMessage(), ex);
}
}
Aggregations