Search in sources :

Example 26 with Query

use of com.haulmont.cuba.core.Query in project cuba by cuba-platform.

the class DbBasedCoordinator method getTasks.

protected synchronized List<ScheduledTask> getTasks() {
    log.trace("Read all active tasks from DB and lock them");
    EntityManager em = persistence.getEntityManager();
    Query query = em.createQuery("select t from sys$ScheduledTask t where t.active = true");
    query.setLockMode(LockModeType.PESSIMISTIC_WRITE);
    return query.getResultList();
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) Query(com.haulmont.cuba.core.Query)

Example 27 with Query

use of com.haulmont.cuba.core.Query in project cuba by cuba-platform.

the class DataServiceQueryBuilder method getQuery.

public Query getQuery(EntityManager em) {
    Query query = em.createQuery(queryString);
    // we have to replace parameter names in macros because for {@link com.haulmont.cuba.core.sys.querymacro.TimeBetweenQueryMacroHandler}
    // we need to replace a parameter with number of days with its value before macros is expanded to JPQL expression
    replaceParamsInMacros(query);
    applyConstraints(query);
    QueryParser parser = QueryTransformerFactory.createParser(queryString);
    Set<String> paramNames = parser.getParamNames();
    for (Map.Entry<String, Object> entry : queryParams.entrySet()) {
        String name = entry.getKey();
        if (paramNames.contains(name)) {
            Object value = entry.getValue();
            boolean convert = noConversionParams == null || Arrays.stream(noConversionParams).noneMatch(s -> s.equals(name));
            if (convert) {
                if (value instanceof Entity) {
                    value = ((Entity) value).getId();
                } else if (value instanceof EnumClass) {
                    value = ((EnumClass) value).getId();
                } else if (value instanceof Collection) {
                    List<Object> list = new ArrayList<>(((Collection) value).size());
                    for (Object item : (Collection) value) {
                        if (item instanceof Entity)
                            list.add(((Entity) item).getId());
                        else if (item instanceof EnumClass)
                            list.add(((EnumClass) item).getId());
                        else
                            list.add(item);
                    }
                    value = list;
                }
            }
            if (value instanceof TemporalValue) {
                TemporalValue temporalValue = (TemporalValue) value;
                query.setParameter(name, temporalValue.date, temporalValue.type);
            } else {
                if (!convert) {
                    query.setParameter(name, value, false);
                } else {
                    query.setParameter(name, value);
                }
            }
        } else
            throw new DevelopmentException("Parameter '" + name + "' is not used in the query");
    }
    return query;
}
Also used : QueryMacroHandler(com.haulmont.cuba.core.sys.QueryMacroHandler) PersistenceSecurity(com.haulmont.cuba.core.PersistenceSecurity) Query(com.haulmont.cuba.core.Query) StringUtils(org.apache.commons.lang.StringUtils) java.util(java.util) Logger(org.slf4j.Logger) EntityManager(com.haulmont.cuba.core.EntityManager) MetaProperty(com.haulmont.chile.core.model.MetaProperty) LoggerFactory(org.slf4j.LoggerFactory) StringHelper(com.haulmont.bali.util.StringHelper) MetaClass(com.haulmont.chile.core.model.MetaClass) Scope(org.springframework.context.annotation.Scope) com.haulmont.cuba.core.global(com.haulmont.cuba.core.global) Inject(javax.inject.Inject) Component(org.springframework.stereotype.Component) EnumClass(com.haulmont.chile.core.datatypes.impl.EnumClass) Entity(com.haulmont.cuba.core.entity.Entity) Entity(com.haulmont.cuba.core.entity.Entity) Query(com.haulmont.cuba.core.Query) EnumClass(com.haulmont.chile.core.datatypes.impl.EnumClass)

Example 28 with Query

use of com.haulmont.cuba.core.Query in project cuba by cuba-platform.

the class StandardCacheLoader method loadData.

@Override
@SuppressWarnings("unchecked")
public CacheSet loadData(ObjectsCache cache) throws CacheException {
    CacheSet cacheSet;
    if (StringUtils.isEmpty(dbQuery)) {
        try {
            dbQuery = resources.getResourceAsString(queryPath);
        } catch (Exception e) {
            log.error("Broken or missing query file for cache: " + cache.getName());
            throw new CacheException(e);
        }
    }
    MetaClass metaClass = metadata.getSession().getClass(metaClassName);
    View view = metadata.getViewRepository().getView(metaClass, viewName);
    Transaction tx = persistence.createTransaction();
    try {
        EntityManager em = persistence.getEntityManager();
        Query query = em.createQuery(dbQuery);
        query.setView(view);
        query.setMaxResults(getMaxQueryResults());
        List<Object> resultList = query.getResultList();
        cacheSet = new CacheSet(resultList);
        tx.commit();
    } catch (Exception e) {
        throw new CacheException(e);
    } finally {
        tx.end();
    }
    return cacheSet;
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) MetaClass(com.haulmont.chile.core.model.MetaClass) Transaction(com.haulmont.cuba.core.Transaction) Query(com.haulmont.cuba.core.Query)

Example 29 with Query

use of com.haulmont.cuba.core.Query in project cuba by cuba-platform.

the class Scheduling method removeExecutionHistory.

@Authenticated
@Override
public String removeExecutionHistory(String age, String maxPeriod) {
    List<UUID> list;
    Transaction tx = persistence.createTransaction();
    try {
        EntityManager em = persistence.getEntityManager();
        String jpql = "select e.id from sys$ScheduledExecution e where e.startTime < ?1";
        if (maxPeriod != null) {
            jpql += " and e.task.period <= ?2";
        }
        jpql += " order by e.startTime";
        Query query = em.createQuery(jpql);
        Date startDate = DateUtils.addHours(timeSource.currentTimestamp(), -Integer.parseInt(age));
        query.setParameter(1, startDate);
        if (maxPeriod != null) {
            query.setParameter(2, Integer.parseInt(maxPeriod) * 3600);
        }
        list = query.getResultList();
        tx.commit();
    } finally {
        tx.end();
    }
    for (int i = 0; i < list.size(); i += 100) {
        final List<UUID> subList = list.subList(i, Math.min(i + 100, list.size()));
        persistence.createTransaction().execute(new Transaction.Runnable() {

            @Override
            public void run(EntityManager em) {
                Query query = em.createQuery("delete from sys$ScheduledExecution e where e.id in ?1");
                query.setParameter(1, subList);
                query.executeUpdate();
            }
        });
    }
    return "Deleted " + list.size();
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) Transaction(com.haulmont.cuba.core.Transaction) Query(com.haulmont.cuba.core.Query) UUID(java.util.UUID) Date(java.util.Date) Authenticated(com.haulmont.cuba.security.app.Authenticated)

Example 30 with Query

use of com.haulmont.cuba.core.Query in project cuba by cuba-platform.

the class EntityManagerImpl method createQuery.

@Override
public Query createQuery(String qlStr) {
    Query query = createQueryInstance(false, null);
    query.setQueryString(qlStr);
    return query;
}
Also used : Query(com.haulmont.cuba.core.Query) TypedQuery(com.haulmont.cuba.core.TypedQuery)

Aggregations

Query (com.haulmont.cuba.core.Query)42 EntityManager (com.haulmont.cuba.core.EntityManager)27 Transaction (com.haulmont.cuba.core.Transaction)25 TypedQuery (com.haulmont.cuba.core.TypedQuery)12 Entity (com.haulmont.cuba.core.entity.Entity)7 List (java.util.List)6 MetaProperty (com.haulmont.chile.core.model.MetaProperty)5 MetaClass (com.haulmont.chile.core.model.MetaClass)3 Date (java.util.Date)3 TransactionParams (com.haulmont.cuba.core.TransactionParams)2 com.haulmont.cuba.core.global (com.haulmont.cuba.core.global)2 QueryImpl (com.haulmont.cuba.core.sys.QueryImpl)2 User (com.haulmont.cuba.security.entity.User)2 After (org.junit.After)2 Transactional (org.springframework.transaction.annotation.Transactional)2 Logger (ch.qos.logback.classic.Logger)1 LoggerContext (ch.qos.logback.classic.LoggerContext)1 StringHelper (com.haulmont.bali.util.StringHelper)1 EnumClass (com.haulmont.chile.core.datatypes.impl.EnumClass)1 PersistenceSecurity (com.haulmont.cuba.core.PersistenceSecurity)1