use of com.haulmont.cuba.core.EntityManager in project cuba by cuba-platform.
the class SchedulingServiceBean method setActive.
@Override
public void setActive(ScheduledTask task, boolean active) {
Transaction tx = persistence.createTransaction();
try {
EntityManager em = persistence.getEntityManager();
ScheduledTask t = em.find(ScheduledTask.class, task.getId());
t.setActive(active);
tx.commit();
} finally {
tx.end();
}
}
use of com.haulmont.cuba.core.EntityManager in project cuba by cuba-platform.
the class SchedulingServiceBean method getAvailableUsers.
@Override
public List<String> getAvailableUsers() {
List<String> result = new ArrayList<>();
Transaction tx = persistence.createTransaction();
try {
EntityManager em = persistence.getEntityManager();
Query query = em.createQuery("select u from sec$User u");
List<User> userList = query.getResultList();
for (User user : userList) {
result.add(user.getLogin());
}
tx.commit();
} finally {
tx.end();
}
return result;
}
use of com.haulmont.cuba.core.EntityManager in project cuba by cuba-platform.
the class Emailer method migrateAttachmentsBatch.
protected int migrateAttachmentsBatch() {
List<SendingAttachment> resultList;
Transaction tx = persistence.createTransaction();
try {
EntityManager em = persistence.getEntityManager();
String qstr = "select a from sys$SendingAttachment a where a.content is not null";
TypedQuery<SendingAttachment> query = em.createQuery(qstr, SendingAttachment.class);
query.setMaxResults(50);
query.setViewName(View.MINIMAL);
resultList = query.getResultList();
tx.commit();
} finally {
tx.end();
}
if (!resultList.isEmpty()) {
emailer.migrateAttachmentsToFileStorage(resultList);
}
return resultList.size();
}
use of com.haulmont.cuba.core.EntityManager in project cuba by cuba-platform.
the class PersistenceSecurityImpl method restoreFilteredData.
@Override
@SuppressWarnings("unchecked")
public void restoreFilteredData(Entity entity) {
MetaClass metaClass = metadata.getClassNN(entity.getClass());
String storeName = metadataTools.getStoreName(metaClass);
EntityManager entityManager = persistence.getEntityManager(storeName);
Multimap<String, Object> filtered = BaseEntityInternalAccess.getFilteredData(entity);
if (filtered == null) {
return;
}
for (Map.Entry<String, Collection<Object>> entry : filtered.asMap().entrySet()) {
MetaProperty property = metaClass.getPropertyNN(entry.getKey());
Collection filteredIds = entry.getValue();
if (property.getRange().isClass() && CollectionUtils.isNotEmpty(filteredIds)) {
Class entityClass = property.getRange().asClass().getJavaClass();
Class propertyClass = property.getJavaType();
if (Collection.class.isAssignableFrom(propertyClass)) {
Collection currentCollection = entity.getValue(property.getName());
if (currentCollection == null) {
throw new RowLevelSecurityException(format("Could not restore an object to currentValue because it is null [%s]. Entity [%s].", property.getName(), metaClass.getName()), metaClass.getName());
}
for (Object entityId : filteredIds) {
Entity reference = entityManager.getReference((Class<Entity>) entityClass, entityId);
// we ignore situations when the currentValue is immutable
currentCollection.add(reference);
}
} else if (Entity.class.isAssignableFrom(propertyClass)) {
Object entityId = filteredIds.iterator().next();
Entity reference = entityManager.getReference((Class<Entity>) entityClass, entityId);
// we ignore the situation when the field is read-only
entity.setValue(property.getName(), reference);
}
}
}
}
use of com.haulmont.cuba.core.EntityManager in project cuba by cuba-platform.
the class RelationsTest method createRole.
public UUID createRole() {
Transaction tx = cont.persistence().createTransaction();
try {
EntityManager em = cont.persistence().getEntityManager();
User user = em.find(User.class, UUID.fromString("60885987-1b61-4247-94c7-dff348347f93"));
Role role = new Role();
role.setName("RelationTest");
em.persist(role);
UserRole userRole = new UserRole();
userRole.setUser(user);
userRole.setRole(role);
em.persist(userRole);
tx.commit();
return role.getId();
} finally {
tx.end();
}
}
Aggregations