Search in sources :

Example 21 with EntityManager

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()));
        }
    }
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) MetaClass(com.haulmont.chile.core.model.MetaClass)

Example 22 with EntityManager

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;
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) MetaClass(com.haulmont.chile.core.model.MetaClass) MetadataObject(com.haulmont.chile.core.model.MetadataObject) View(com.haulmont.cuba.core.global.View)

Example 23 with EntityManager

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);
    }
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) Transaction(com.haulmont.cuba.core.Transaction)

Example 24 with EntityManager

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();
    }
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) Transaction(com.haulmont.cuba.core.Transaction)

Example 25 with EntityManager

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();
    }
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) Transaction(com.haulmont.cuba.core.Transaction)

Aggregations

EntityManager (com.haulmont.cuba.core.EntityManager)167 Transaction (com.haulmont.cuba.core.Transaction)140 Query (com.haulmont.cuba.core.Query)27 User (com.haulmont.cuba.security.entity.User)27 Test (org.junit.Test)25 View (com.haulmont.cuba.core.global.View)22 MetaClass (com.haulmont.chile.core.model.MetaClass)14 Group (com.haulmont.cuba.security.entity.Group)12 Before (org.junit.Before)11 Entity (com.haulmont.cuba.core.entity.Entity)10 SendingMessage (com.haulmont.cuba.core.entity.SendingMessage)8 UUID (java.util.UUID)7 Nullable (javax.annotation.Nullable)7 TypedQuery (com.haulmont.cuba.core.TypedQuery)6 List (java.util.List)6 MetaProperty (com.haulmont.chile.core.model.MetaProperty)5 Role (com.haulmont.cuba.security.entity.Role)5 UserRole (com.haulmont.cuba.security.entity.UserRole)5 SoftDeleteOneToOneA (com.haulmont.cuba.testmodel.softdelete_one_to_one.SoftDeleteOneToOneA)5 FileDescriptor (com.haulmont.cuba.core.entity.FileDescriptor)4