Search in sources :

Example 91 with EntityManager

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

Example 92 with EntityManager

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

Example 93 with EntityManager

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

Example 94 with EntityManager

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

Example 95 with EntityManager

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

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