Search in sources :

Example 1 with Query

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

the class EntityRestoreServiceBean method restoreDetails.

protected void restoreDetails(Entity entity, Date deleteTs, String storeName) {
    EntityManager em = persistence.getEntityManager(storeName);
    MetaClass metaClass = metadata.getClassNN(entity.getClass());
    List<MetaProperty> properties = new ArrayList<>();
    fillProperties(metaClass, properties, OnDelete.class.getName());
    for (MetaProperty property : properties) {
        OnDelete annotation = property.getAnnotatedElement().getAnnotation(OnDelete.class);
        DeletePolicy deletePolicy = annotation.value();
        if (deletePolicy == DeletePolicy.CASCADE) {
            MetaClass detailMetaClass = property.getRange().asClass();
            if (!storeName.equals(metadata.getTools().getStoreName(detailMetaClass))) {
                log.debug("Cannot restore " + property.getRange().asClass() + " because it is from different data store");
                continue;
            }
            if (!SoftDelete.class.isAssignableFrom(detailMetaClass.getJavaClass())) {
                log.debug("Cannot restore " + property.getRange().asClass() + " because it is hard deleted");
                continue;
            }
            MetaProperty inverseProp = property.getInverse();
            if (inverseProp == null) {
                log.debug("Cannot restore " + property.getRange().asClass() + " because it has no inverse property for " + metaClass);
                continue;
            }
            String jpql = "select e from " + detailMetaClass + " e where e." + inverseProp.getName() + ".id = ?1 " + "and e.deleteTs >= ?2 and e.deleteTs <= ?3";
            Query query = em.createQuery(jpql);
            query.setParameter(1, entity.getId());
            query.setParameter(2, DateUtils.addMilliseconds(deleteTs, -100));
            query.setParameter(3, DateUtils.addMilliseconds(deleteTs, 1000));
            // noinspection unchecked
            List<Entity> list = query.getResultList();
            for (Entity detailEntity : list) {
                if (entity instanceof SoftDelete) {
                    restoreEntity(detailEntity, storeName);
                }
            }
        }
    }
    properties = new ArrayList<>();
    fillProperties(metaClass, properties, OnDeleteInverse.class.getName());
    for (MetaProperty property : properties) {
        OnDeleteInverse annotation = property.getAnnotatedElement().getAnnotation(OnDeleteInverse.class);
        DeletePolicy deletePolicy = annotation.value();
        if (deletePolicy == DeletePolicy.CASCADE) {
            MetaClass detailMetaClass = property.getDomain();
            if (!storeName.equals(metadata.getTools().getStoreName(detailMetaClass))) {
                log.debug("Cannot restore " + property.getRange().asClass() + " because it is from different data store");
                continue;
            }
            if (!SoftDelete.class.isAssignableFrom(detailMetaClass.getJavaClass())) {
                log.debug("Cannot restore " + property.getRange().asClass() + " because it is hard deleted");
                continue;
            }
            List<MetaClass> metClassesToRestore = new ArrayList<>();
            metClassesToRestore.add(detailMetaClass);
            metClassesToRestore.addAll(detailMetaClass.getDescendants());
            for (MetaClass metaClassToRestore : metClassesToRestore) {
                if (!metadata.getTools().isPersistent(metaClassToRestore))
                    continue;
                String jpql = "select e from " + metaClassToRestore.getName() + " e where e." + property.getName() + ".id = ?1 and e.deleteTs >= ?2 and e.deleteTs <= ?3";
                Query query = em.createQuery(jpql);
                query.setParameter(1, entity.getId());
                query.setParameter(2, DateUtils.addMilliseconds(deleteTs, -100));
                query.setParameter(3, DateUtils.addMilliseconds(deleteTs, 1000));
                // noinspection unchecked
                List<Entity> list = query.getResultList();
                for (Entity detailEntity : list) {
                    if (entity instanceof SoftDelete) {
                        restoreEntity(detailEntity, storeName);
                    }
                }
            }
        }
    }
}
Also used : Entity(com.haulmont.cuba.core.entity.Entity) SoftDelete(com.haulmont.cuba.core.entity.SoftDelete) Query(com.haulmont.cuba.core.Query) DeletePolicy(com.haulmont.cuba.core.global.DeletePolicy) EntityManager(com.haulmont.cuba.core.EntityManager) MetaClass(com.haulmont.chile.core.model.MetaClass) OnDeleteInverse(com.haulmont.cuba.core.entity.annotation.OnDeleteInverse) MetaProperty(com.haulmont.chile.core.model.MetaProperty) OnDelete(com.haulmont.cuba.core.entity.annotation.OnDelete)

Example 2 with Query

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

the class NumberIdWorker method checkSequenceExists.

protected void checkSequenceExists(String entityName, long startValue, long increment) {
    String seqName = getSequenceName(entityName);
    if (existingSequences.contains(seqName))
        return;
    // Create sequence in separate transaction because it's name is cached and we want to be sure it is created
    // regardless of possible errors in the invoking code
    Transaction tx = persistence.createTransaction(getDataStore(entityName));
    try {
        EntityManager em = persistence.getEntityManager(getDataStore(entityName));
        SequenceSupport sequenceSupport = getSequenceSupport(entityName);
        Query query = em.createNativeQuery(sequenceSupport.sequenceExistsSql(seqName));
        List list = query.getResultList();
        if (list.isEmpty()) {
            query = em.createNativeQuery(sequenceSupport.createSequenceSql(seqName, startValue, increment));
            query.executeUpdate();
        }
        existingSequences.add(seqName);
        tx.commit();
    } finally {
        tx.end();
    }
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) SequenceSupport(com.haulmont.cuba.core.sys.persistence.SequenceSupport) Transaction(com.haulmont.cuba.core.Transaction) Query(com.haulmont.cuba.core.Query) List(java.util.List)

Example 3 with Query

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

the class TestingServiceBean method clearScheduledTasks.

@Override
public void clearScheduledTasks() {
    Transaction tx = persistence.createTransaction();
    try {
        EntityManager em = persistence.getEntityManager();
        Query query = em.createNativeQuery("delete from SYS_SCHEDULED_EXECUTION");
        query.executeUpdate();
        query = em.createNativeQuery("delete from SYS_SCHEDULED_TASK");
        query.executeUpdate();
        tx.commit();
    } finally {
        tx.end();
    }
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) Transaction(com.haulmont.cuba.core.Transaction) Query(com.haulmont.cuba.core.Query)

Example 4 with Query

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

the class TestingServiceBean method executeSelectSql.

@Override
@Transactional(timeout = 2)
public String executeSelectSql(String sql) {
    checkTestMode();
    log.info("started: " + sql);
    EntityManager em = persistence.getEntityManager();
    Query query = em.createNativeQuery(sql);
    query.getResultList();
    log.info("finished: " + sql);
    return "Done";
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) Query(com.haulmont.cuba.core.Query) Transactional(org.springframework.transaction.annotation.Transactional)

Example 5 with Query

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

the class TestingServiceBean method executeUpdateSql.

@Override
@Transactional(timeout = 2)
public String executeUpdateSql(String sql) {
    checkTestMode();
    log.info("started: " + sql);
    EntityManager em = persistence.getEntityManager();
    Query query = em.createNativeQuery(sql);
    query.executeUpdate();
    log.info("finished: " + sql);
    return "Done";
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) Query(com.haulmont.cuba.core.Query) Transactional(org.springframework.transaction.annotation.Transactional)

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