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;
}
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);
}
}
}
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;
}
}
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();
}
}
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 =====================");
}
Aggregations