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();
}
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;
}
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;
}
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();
}
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;
}
Aggregations