Search in sources :

Example 36 with Query

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

the class DeletePolicyProcessor method getCollection.

protected Collection<Entity> getCollection(MetaProperty property) {
    MetaProperty inverseProperty = property.getInverse();
    if (inverseProperty == null) {
        log.warn("Inverse property not found for property {}", property);
        Collection<Entity> value = entity.getValue(property.getName());
        return value == null ? Collections.EMPTY_LIST : value;
    }
    String invPropName = inverseProperty.getName();
    String qlStr = "select e from " + property.getRange().asClass().getName() + " e where e." + invPropName + "." + primaryKeyName + " = ?1";
    Query query = entityManager.createQuery(qlStr);
    query.setParameter(1, entity.getId());
    List<Entity> list = query.getResultList();
    // If the property is not loaded, it means it was not modified and further check is not needed
    if (!PersistenceHelper.isLoaded(entity, property.getName())) {
        return list;
    }
    // Check whether the collection items still belong to the master entity, because they could be changed in the
    // current transaction that did not affect the database yet
    List<Entity> result = new ArrayList<>(list.size());
    for (Entity item : list) {
        Entity master = item.getValue(invPropName);
        if (entity.equals(master))
            result.add(item);
    }
    return result;
}
Also used : Entity(com.haulmont.cuba.core.entity.Entity) Query(com.haulmont.cuba.core.Query) MetaProperty(com.haulmont.chile.core.model.MetaProperty)

Example 37 with Query

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

the class DeletePolicyProcessor method unlink.

protected void unlink(String entityName, MetaProperty property) {
    if (metadata.getTools().isOwningSide(property)) {
        String template = property.getRange().getCardinality().isMany() ? "select e from %s e join e.%s c where c." + primaryKeyName + " = ?1" : "select e from %s e where e.%s." + primaryKeyName + " = ?1";
        String qstr = String.format(template, entityName, property.getName());
        Query query = entityManager.createQuery(qstr);
        query.setParameter(1, entity.getId());
        List<Entity> list = query.getResultList();
        for (Entity e : list) {
            if (property.getRange().getCardinality().isMany()) {
                Collection collection = e.getValue(property.getName());
                if (collection != null) {
                    collection.removeIf(o -> entity.equals(o));
                }
            } else {
                setReferenceNull(e, property);
            }
        }
    } else {
        MetaProperty inverseProp = property.getInverse();
        if (inverseProp != null && inverseProp.getDomain().equals(metaClass)) {
            setReferenceNull(entity, inverseProp);
        }
    }
}
Also used : Entity(com.haulmont.cuba.core.entity.Entity) Query(com.haulmont.cuba.core.Query) MetaProperty(com.haulmont.chile.core.model.MetaProperty)

Example 38 with Query

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

the class AbstractAuthenticationProvider method loadUser.

@Nullable
protected User loadUser(String login) throws LoginException {
    if (login == null) {
        throw new IllegalArgumentException("Login is null");
    }
    EntityManager em = persistence.getEntityManager();
    String queryStr = "select u from sec$User u where u.loginLowerCase = ?1 and (u.active = true or u.active is null)";
    Query q = em.createQuery(queryStr);
    q.setParameter(1, login.toLowerCase());
    List list = q.getResultList();
    if (list.isEmpty()) {
        log.debug("Unable to find user: {}", login);
        return null;
    } else {
        // noinspection UnnecessaryLocalVariable
        User user = (User) list.get(0);
        return user;
    }
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) User(com.haulmont.cuba.security.entity.User) Query(com.haulmont.cuba.core.Query) List(java.util.List) Nullable(javax.annotation.Nullable)

Example 39 with Query

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

the class PermissionTest method tearDown.

@After
public void tearDown() throws Exception {
    Transaction tx = cont.persistence().createTransaction();
    try {
        EntityManager em = cont.persistence().getEntityManager();
        Query q;
        q = em.createNativeQuery("delete from SEC_USER_ROLE where ID = ? or ID = ?");
        q.setParameter(1, userRole1Id.toString());
        q.setParameter(2, userRole2Id.toString());
        q.executeUpdate();
        q = em.createNativeQuery("delete from SEC_USER where ID = ?");
        q.setParameter(1, userId.toString());
        q.executeUpdate();
        q = em.createNativeQuery("delete from SEC_GROUP where ID = ?");
        q.setParameter(1, groupId.toString());
        q.executeUpdate();
        q = em.createNativeQuery("delete from SEC_PERMISSION where ID = ? or ID = ?");
        q.setParameter(1, permission1Id.toString());
        q.setParameter(2, permission2Id.toString());
        q.executeUpdate();
        q = em.createNativeQuery("delete from SEC_ROLE where ID = ? or ID = ?");
        q.setParameter(1, role1Id.toString());
        q.setParameter(2, role2Id.toString());
        q.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) After(org.junit.After)

Example 40 with Query

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

the class SoftDeleteTest method testQueryWithoutConditions.

@Test
public void testQueryWithoutConditions() {
    System.out.println("===================== BEGIN testQueryWithoutConditions =====================");
    Transaction tx = cont.persistence().createTransaction();
    try {
        EntityManager em = cont.persistence().getEntityManager();
        Query query = em.createQuery("select r from sec$Role r");
        List<Role> list = query.getResultList();
        for (Role role : list) {
            if (role.getId().equals(role2Id)) {
                fail();
            }
        }
        tx.commit();
    } finally {
        tx.end();
    }
    System.out.println("===================== END testQueryWithoutConditions =====================");
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) Transaction(com.haulmont.cuba.core.Transaction) Query(com.haulmont.cuba.core.Query)

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