Search in sources :

Example 1 with DataServiceQueryBuilder

use of com.haulmont.cuba.core.app.DataServiceQueryBuilder 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)

Aggregations

EntityManager (com.haulmont.cuba.core.EntityManager)1 Query (com.haulmont.cuba.core.Query)1 Transaction (com.haulmont.cuba.core.Transaction)1 DataServiceQueryBuilder (com.haulmont.cuba.core.app.DataServiceQueryBuilder)1