use of com.haulmont.cuba.core.EntityManager in project cuba by cuba-platform.
the class DynamicAttributesManager method loadEntityValues.
/**
* Method loads entity values for CategoryAttributeValues of entity type and sets entity values to the corresponding
* property of the {@code CategoryAttributeValue} entity.
*/
@SuppressWarnings("unchecked")
protected void loadEntityValues(List<CategoryAttributeValue> cavsOfEntityType) {
HashMultimap<MetaClass, Object> entitiesIdsToBeLoaded = HashMultimap.create();
HashMultimap<MetaClass, CategoryAttributeValue> cavByType = HashMultimap.create();
cavsOfEntityType.forEach(cav -> {
String className = cav.getCategoryAttribute().getEntityClass();
try {
Class<?> aClass = Class.forName(className);
MetaClass metaClass = metadata.getClass(aClass);
entitiesIdsToBeLoaded.put(metaClass, cav.getObjectEntityValueId());
cavByType.put(metaClass, cav);
} catch (ClassNotFoundException e) {
log.error("Class {} not found", className);
}
});
EntityManager em = persistence.getEntityManager();
for (Map.Entry<MetaClass, Collection<Object>> entry : entitiesIdsToBeLoaded.asMap().entrySet()) {
Map<Object, BaseGenericIdEntity> idToEntityMap = new HashMap<>();
MetaClass metaClass = entry.getKey();
Collection<Object> ids = entry.getValue();
if (!ids.isEmpty()) {
String pkName = referenceToEntitySupport.getPrimaryKeyForLoadingEntity(metaClass);
List<BaseGenericIdEntity> entitiesValues = em.createQuery(format("select e from %s e where e.%s in :ids", metaClass.getName(), pkName)).setParameter("ids", ids).setView(metaClass.getJavaClass(), View.MINIMAL).getResultList();
for (BaseGenericIdEntity entity : entitiesValues) {
idToEntityMap.put(entity.getId(), entity);
}
}
for (CategoryAttributeValue cav : cavByType.get(metaClass)) {
cav.setTransientEntityValue(idToEntityMap.get(cav.getObjectEntityValueId()));
}
}
}
use of com.haulmont.cuba.core.EntityManager in project cuba by cuba-platform.
the class QueryCacheManager method getResultListFromCache.
/**
* Get query results from query cache by specified {@code queryKey}
*/
@SuppressWarnings("unchecked")
public <T> List<T> getResultListFromCache(QueryKey queryKey, List<View> views) {
log.debug("Looking for query in cache: {}", queryKey.printDescription());
List<T> resultList = null;
QueryResult queryResult = queryCache.get(queryKey);
if (queryResult != null) {
MetaClass metaClass = metadata.getClassNN(queryResult.getType());
String storeName = metadata.getTools().getStoreName(metaClass);
EntityManager em = persistence.getEntityManager(storeName);
resultList = new ArrayList<>(queryResult.getResult().size());
if (!metadata.getTools().isCacheable(metaClass)) {
log.warn("Using cacheable query without entity cache for {}", queryResult.getType());
}
for (Object id : queryResult.getResult()) {
resultList.add((T) em.find(metaClass.getJavaClass(), id, views.toArray(new View[views.size()])));
}
} else {
log.debug("Query results are not found in cache: {}", queryKey.printDescription());
}
return resultList;
}
use of com.haulmont.cuba.core.EntityManager in project cuba by cuba-platform.
the class ServerTokenStoreImpl method getRefreshTokenValuesByUserLoginFromDatabase.
protected Set<String> getRefreshTokenValuesByUserLoginFromDatabase(String userLogin) {
try (Transaction tx = persistence.createTransaction()) {
EntityManager em = persistence.getEntityManager();
List<String> result = em.createQuery("select e.tokenValue from sys$RefreshToken e where e.userLogin = :userLogin", String.class).setParameter("userLogin", userLogin).getResultList();
tx.commit();
return new HashSet<>(result);
}
}
use of com.haulmont.cuba.core.EntityManager in project cuba by cuba-platform.
the class ServerTokenStoreImpl method deleteExpiredAccessTokensInDatabase.
protected void deleteExpiredAccessTokensInDatabase() {
try (Transaction tx = persistence.createTransaction()) {
EntityManager em = persistence.getEntityManager();
em.createQuery("delete from sys$AccessToken t where t.expiry < CURRENT_TIMESTAMP").executeUpdate();
tx.commit();
}
}
use of com.haulmont.cuba.core.EntityManager in project cuba by cuba-platform.
the class ServerTokenStoreImpl method removeRefreshTokenFromDatabase.
protected void removeRefreshTokenFromDatabase(String refreshTokenValue) {
try (Transaction tx = persistence.getTransaction()) {
EntityManager em = persistence.getEntityManager();
em.createQuery("delete from sys$RefreshToken t where t.tokenValue = :tokenValue").setParameter("tokenValue", refreshTokenValue).executeUpdate();
tx.commit();
}
}
Aggregations