use of com.haulmont.cuba.core.EntityManager in project cuba by cuba-platform.
the class QueryResultsManager method savePreviousQueryResults.
@Override
public void savePreviousQueryResults(LoadContext loadContext) {
List<LoadContext.Query> prevQueries = loadContext.getPrevQueries();
if (prevQueries.isEmpty())
return;
LoadContext.Query contextQuery = prevQueries.get(prevQueries.size() - 1);
String entityName = loadContext.getMetaClass();
QueryParser parser = QueryTransformerFactory.createParser(contextQuery.getQueryString());
if (!parser.isEntitySelect(entityName))
return;
int queryKey = loadContext.getQueryKey();
if (resultsAlreadySaved(queryKey, contextQuery))
return;
List idList;
Transaction tx = persistence.createTransaction();
try {
EntityManager em = persistence.getEntityManager();
em.setSoftDeletion(loadContext.isSoftDeletion());
QueryTransformer transformer = QueryTransformerFactory.createTransformer(contextQuery.getQueryString());
transformer.replaceWithSelectId(metadata.getTools().getPrimaryKeyName(metadata.getClassNN(entityName)));
transformer.removeOrderBy();
String queryString = transformer.getResult();
DataServiceQueryBuilder queryBuilder = AppBeans.get(DataServiceQueryBuilder.NAME);
queryBuilder.init(queryString, contextQuery.getParameters(), contextQuery.getNoConversionParams(), null, entityName);
if (prevQueries.size() > 1) {
queryBuilder.restrictByPreviousResults(userSessionSource.getUserSession().getId(), loadContext.getQueryKey());
}
Query query = queryBuilder.getQuery(em);
String logMsg = "Load previous query results: " + DataServiceQueryBuilder.printQuery(query.getQueryString());
log.debug(logMsg);
long start = System.currentTimeMillis();
idList = query.getResultList();
tx.commit();
log.debug("Done in " + (System.currentTimeMillis() - start) + "ms : " + logMsg);
} finally {
tx.end();
}
delete(queryKey);
insert(queryKey, idList);
}
use of com.haulmont.cuba.core.EntityManager in project cuba by cuba-platform.
the class DbBasedCoordinator method getLastFinished.
@Override
public long getLastFinished(ScheduledTask task) {
EntityManager em = persistence.getEntityManager();
Query query = em.createQuery("select max(e.finishTime) from sys$ScheduledExecution e where e.task.id = ?1").setParameter(1, task.getId());
Date date = (Date) query.getFirstResult();
return date == null ? 0 : date.getTime();
}
use of com.haulmont.cuba.core.EntityManager in project cuba by cuba-platform.
the class RunnerBean method registerExecutionFinish.
protected void registerExecutionFinish(ScheduledTask task, ScheduledExecution execution, Object result) {
if ((!BooleanUtils.isTrue(task.getLogFinish()) && !BooleanUtils.isTrue(task.getSingleton()) && task.getSchedulingType() != SchedulingType.FIXED_DELAY) || execution == null)
return;
log.trace("{}: registering execution finish", task);
Transaction tx = persistence.createTransaction();
try {
EntityManager em = persistence.getEntityManager();
execution = em.merge(execution);
execution.setFinishTime(timeSource.currentTimestamp());
if (result != null)
execution.setResult(result.toString());
tx.commit();
} finally {
tx.end();
}
}
use of com.haulmont.cuba.core.EntityManager in project documentation by cuba-platform.
the class OrderServiceBean method calculatePrice.
@Override
public BigDecimal calculatePrice(String orderNumber) {
BigDecimal orderPrice = null;
try (Transaction tx = persistence.createTransaction()) {
EntityManager em = persistence.getEntityManager();
orderPrice = (BigDecimal) em.createQuery("select sum(oi.price) from sales$OrderItem oi where oi.order.number = :orderNumber").setParameter("orderNumber", orderNumber).getSingleResult();
tx.commit();
}
return orderPrice;
}
use of com.haulmont.cuba.core.EntityManager in project cuba by cuba-platform.
the class Emailer method migrateAttachmentsToFileStorage.
@Override
public void migrateAttachmentsToFileStorage(List<SendingAttachment> attachments) {
try (Transaction tx = persistence.createTransaction()) {
EntityManager em = persistence.getEntityManager();
for (SendingAttachment attachment : attachments) {
migrateAttachment(em, attachment);
}
tx.commit();
}
}
Aggregations