/** * Loads a single instance and wraps it in Optional. */ public Optional<KeyValueEntity> optional() { ValueLoadContext loadContext = createLoadContext(); loadContext.getQuery().setMaxResults(1); List<KeyValueEntity> list = dataManager.loadValues(loadContext); return list.isEmpty() ? Optional.empty() : Optional.of(list.get(0)); }
/** * Loads a single instance. * * @throws IllegalStateException if nothing was loaded */ public KeyValueEntity one() { ValueLoadContext loadContext = createLoadContext(); loadContext.getQuery().setMaxResults(1); List<KeyValueEntity> list = dataManager.loadValues(loadContext); if (!list.isEmpty()) return list.get(0); else throw new IllegalStateException("No results"); }
/** * Loads a single instance and wraps it in Optional. */ public Optional<T> optional() { ValueLoadContext loadContext = createLoadContext(); loadContext.getQuery().setMaxResults(1); List<KeyValueEntity> list = dataManager.loadValues(loadContext); return list.isEmpty() ? Optional.empty() : Optional.ofNullable(castValue(list.get(0).getValue(PROP_NAME))); }
/** * Loads a single instance. * * @throws IllegalStateException if nothing was loaded */ public T one() { ValueLoadContext loadContext = createLoadContext(); loadContext.getQuery().setMaxResults(1); List<KeyValueEntity> list = dataManager.loadValues(loadContext); if (!list.isEmpty()) return castValue(list.get(0).getValue(PROP_NAME)); else throw new IllegalStateException("No results"); }
protected ValueLoadContext createLoadContext() { ValueLoadContext loadContext = ValueLoadContext.create(); if (store != null) loadContext.setStoreName(store); loadContext.setSoftDeletion(softDeletion); ValueLoadContext.Query query = ValueLoadContext.createQuery(queryString); for (Map.Entry<String, Object> entry : parameters.entrySet()) { if (noConversionParams.contains(entry.getKey())) query.setParameter(entry.getKey(), entry.getValue(), false); else query.setParameter(entry.getKey(), entry.getValue()); } loadContext.setQuery(query); loadContext.getQuery().setFirstResult(firstResult); loadContext.getQuery().setMaxResults(maxResults); loadContext.setJoinTransaction(transactional); return loadContext; }
@SuppressWarnings("unchecked") @Override public int getCount() { if (loader == null) { return container.getItems().size(); } if (loader instanceof CollectionLoader) { return (int) dataManager.getCount(((CollectionLoader) loader).createLoadContext()); } else if (loader instanceof KeyValueCollectionLoader) { ValueLoadContext context = ((KeyValueCollectionLoader) loader).createLoadContext(); QueryTransformer transformer = QueryTransformerFactory.createTransformer(context.getQuery().getQueryString()); // TODO it doesn't work for query containing scalars in select transformer.replaceWithCount(); context.getQuery().setQueryString(transformer.getResult()); context.setProperties(Collections.singletonList("cnt")); List<KeyValueEntity> list = dataManager.loadValues(context); Number count = list.get(0).getValue("cnt"); return count == null ? 0 : count.intValue(); } else { log.warn("Unsupported loader type: {}", loader.getClass().getName()); return 0; } }